Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3896004

Browse files
committedMar 4, 2025
Pretty-print #[deprecated] and #[diagnostic::(..)] attributes.
1 parent f9e0239 commit 3896004

File tree

1 file changed

+86
-0
lines changed
  • compiler/rustc_hir_pretty/src

1 file changed

+86
-0
lines changed
 

‎compiler/rustc_hir_pretty/src/lib.rs

+86
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,92 @@ impl<'a> State<'a> {
117117
));
118118
self.hardbreak()
119119
}
120+
hir::Attribute::Parsed(AttributeKind::Deprecation { deprecation, .. }) => {
121+
self.word("#[deprecated");
122+
123+
// There are three possible forms here:
124+
// 1. a form with explicit components like
125+
// `#[deprecated(since = "1.2.3", note = "some note", suggestion = "something")]`
126+
// where each component may be present or absent.
127+
// 2. `#[deprecated = "message"]`
128+
// 3. `#[deprecated]`
129+
//
130+
// Let's figure out which we need.
131+
// If there's a `since` or `suggestion` value, we're definitely in form 1.
132+
if matches!(
133+
deprecation.since,
134+
rustc_attr_parsing::DeprecatedSince::RustcVersion(..)
135+
| rustc_attr_parsing::DeprecatedSince::Future
136+
| rustc_attr_parsing::DeprecatedSince::NonStandard(..)
137+
) || deprecation.suggestion.is_some()
138+
{
139+
self.word("(");
140+
let mut use_comma = false;
141+
142+
match &deprecation.since {
143+
rustc_attr_parsing::DeprecatedSince::RustcVersion(rustc_version) => {
144+
self.word("since = \"");
145+
self.word(format!(
146+
"{}.{}.{}",
147+
rustc_version.major, rustc_version.minor, rustc_version.patch
148+
));
149+
self.word("\"");
150+
use_comma = true;
151+
}
152+
rustc_attr_parsing::DeprecatedSince::Future => {
153+
self.word("since = \"future\"");
154+
use_comma = true;
155+
}
156+
rustc_attr_parsing::DeprecatedSince::NonStandard(symbol) => {
157+
self.word("since = \"");
158+
self.word(symbol.to_ident_string());
159+
self.word("\"");
160+
use_comma = true;
161+
}
162+
_ => {}
163+
}
164+
165+
if let Some(note) = &deprecation.note {
166+
if use_comma {
167+
self.word(", ");
168+
}
169+
self.word("note = \"");
170+
self.word(note.to_ident_string());
171+
self.word("\"");
172+
use_comma = true;
173+
}
174+
175+
if let Some(suggestion) = &deprecation.suggestion {
176+
if use_comma {
177+
self.word(", ");
178+
}
179+
self.word("suggestion = \"");
180+
self.word(suggestion.to_ident_string());
181+
self.word("\"");
182+
}
183+
} else if let Some(note) = &deprecation.note {
184+
// We're in form 2: `#[deprecated = "message"]`.
185+
self.word(" = \"");
186+
self.word(note.to_ident_string());
187+
self.word("\"");
188+
} else {
189+
// We're in form 3: `#[deprecated]`. Nothing to do here.
190+
}
191+
192+
self.word("]");
193+
}
194+
hir::Attribute::Parsed(AttributeKind::Diagnostic(diagnostic_attribute)) => {
195+
// Unfortunately it appears that the contents of the diagnostic attribute
196+
// are not present here, so we cannot currently include them in the printed output.
197+
match diagnostic_attribute {
198+
rustc_attr_parsing::DiagnosticAttribute::DoNotRecommend => {
199+
self.word("#[diagnostic::do_not_recommend]");
200+
}
201+
rustc_attr_parsing::DiagnosticAttribute::OnUnimplemented => {
202+
self.word("#[diagnostic::on_unimplemented]");
203+
}
204+
}
205+
}
120206
hir::Attribute::Parsed(pa) => {
121207
self.word("#[attr=\"");
122208
pa.print_attribute(self);

0 commit comments

Comments
 (0)
Failed to load comments.