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 55f8d3f

Browse files
committedMar 5, 2025
Pretty-print #[deprecated] attribute in HIR.
1 parent f9e0239 commit 55f8d3f

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed
 

‎compiler/rustc_hir_pretty/src/lib.rs

+74
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,80 @@ 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+
}
120194
hir::Attribute::Parsed(pa) => {
121195
self.word("#[attr=\"");
122196
pa.print_attribute(self);

‎tests/ui/unpretty/deprecated-attr.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ compile-flags: -Zunpretty=hir
2+
//@ check-pass
3+
4+
#[deprecated]
5+
pub struct PlainDeprecated;
6+
7+
#[deprecated = "here's why this is deprecated"]
8+
pub struct DirectNote;
9+
10+
#[deprecated(note = "here's why this is deprecated")]
11+
pub struct ExplicitNote;
12+
13+
#[deprecated(since = "1.2.3", note = "here's why this is deprecated")]
14+
pub struct SinceAndNote;
15+
16+
#[deprecated(note = "here's why this is deprecated", since = "1.2.3")]
17+
pub struct FlippedOrder;
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[prelude_import]
2+
use ::std::prelude::rust_2015::*;
3+
#[macro_use]
4+
extern crate std;
5+
//@ compile-flags: -Zunpretty=hir
6+
//@ check-pass
7+
8+
#[deprecated]
9+
struct PlainDeprecated;
10+
11+
#[deprecated = "here's why this is deprecated"]
12+
struct DirectNote;
13+
14+
#[deprecated = "here's why this is deprecated"]
15+
struct ExplicitNote;
16+
17+
#[deprecated(since = "1.2.3", note = "here's why this is deprecated"]
18+
struct SinceAndNote;
19+
20+
#[deprecated(since = "1.2.3", note = "here's why this is deprecated"]
21+
struct FlippedOrder;

‎tests/ui/unpretty/diagnostic-attr.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ compile-flags: -Zunpretty=hir
2+
//@ check-pass
3+
4+
#[diagnostic::on_unimplemented(
5+
message = "My Message for `ImportantTrait<{A}>` implemented for `{Self}`",
6+
label = "My Label",
7+
note = "Note 1",
8+
note = "Note 2"
9+
)]
10+
pub trait ImportantTrait<A> {}
11+
12+
#[diagnostic::do_not_recommend]
13+
impl<T> ImportantTrait<T> for T where T: Clone {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#[prelude_import]
2+
use ::std::prelude::rust_2015::*;
3+
#[macro_use]
4+
extern crate std;
5+
//@ compile-flags: -Zunpretty=hir
6+
//@ check-pass
7+
8+
#[diagnostic::on_unimplemented(message =
9+
"My Message for `ImportantTrait<{A}>` implemented for `{Self}`", label =
10+
"My Label", note = "Note 1", note = "Note 2")]
11+
trait ImportantTrait<A> { }
12+
13+
#[diagnostic::do_not_recommend]
14+
impl <T> ImportantTrait<T> for T where T: Clone
15+
{#![diagnostic::do_not_recommend]
16+
}

0 commit comments

Comments
 (0)
Failed to load comments.