Skip to content

Commit 4e68da2

Browse files
authored
Rollup merge of rust-lang#58166 - euclio:deprecation-shorthand, r=petrochenkov
allow shorthand syntax for deprecation reason Fixes rust-lang#48271. Created based on discussion in rust-lang#56896.
2 parents f6da289 + 113b7f7 commit 4e68da2

File tree

8 files changed

+117
-67
lines changed

8 files changed

+117
-67
lines changed

src/libsyntax/attr/builtin.rs

+65-60
Original file line numberDiff line numberDiff line change
@@ -596,81 +596,86 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
596596
let diagnostic = &sess.span_diagnostic;
597597

598598
'outer: for attr in attrs_iter {
599-
if attr.path != "deprecated" {
600-
continue
599+
if !attr.check_name("deprecated") {
600+
continue;
601601
}
602602

603-
mark_used(attr);
604-
605603
if depr.is_some() {
606604
span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes");
607605
break
608606
}
609607

610-
depr = if let Some(metas) = attr.meta_item_list() {
611-
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
612-
if item.is_some() {
613-
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()));
614-
return false
615-
}
616-
if let Some(v) = meta.value_str() {
617-
*item = Some(v);
618-
true
619-
} else {
620-
if let Some(lit) = meta.name_value_literal() {
621-
handle_errors(
622-
sess,
623-
lit.span,
624-
AttrError::UnsupportedLiteral(
625-
"literal in `deprecated` \
626-
value must be a string",
627-
lit.node.is_bytestr()
628-
),
629-
);
630-
} else {
631-
span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
608+
let meta = attr.meta().unwrap();
609+
depr = match &meta.node {
610+
MetaItemKind::Word => Some(Deprecation { since: None, note: None }),
611+
MetaItemKind::NameValue(..) => {
612+
meta.value_str().map(|note| {
613+
Deprecation { since: None, note: Some(note) }
614+
})
615+
}
616+
MetaItemKind::List(list) => {
617+
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
618+
if item.is_some() {
619+
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()));
620+
return false
632621
}
622+
if let Some(v) = meta.value_str() {
623+
*item = Some(v);
624+
true
625+
} else {
626+
if let Some(lit) = meta.name_value_literal() {
627+
handle_errors(
628+
sess,
629+
lit.span,
630+
AttrError::UnsupportedLiteral(
631+
"literal in `deprecated` \
632+
value must be a string",
633+
lit.node.is_bytestr()
634+
),
635+
);
636+
} else {
637+
span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
638+
}
633639

634-
false
635-
}
636-
};
640+
false
641+
}
642+
};
637643

638-
let mut since = None;
639-
let mut note = None;
640-
for meta in metas {
641-
match &meta.node {
642-
NestedMetaItemKind::MetaItem(mi) => {
643-
match &*mi.name().as_str() {
644-
"since" => if !get(mi, &mut since) { continue 'outer },
645-
"note" => if !get(mi, &mut note) { continue 'outer },
646-
_ => {
647-
handle_errors(
648-
sess,
649-
meta.span,
650-
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
651-
);
652-
continue 'outer
644+
let mut since = None;
645+
let mut note = None;
646+
for meta in list {
647+
match &meta.node {
648+
NestedMetaItemKind::MetaItem(mi) => {
649+
match &*mi.name().as_str() {
650+
"since" => if !get(mi, &mut since) { continue 'outer },
651+
"note" => if !get(mi, &mut note) { continue 'outer },
652+
_ => {
653+
handle_errors(
654+
sess,
655+
meta.span,
656+
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
657+
);
658+
continue 'outer
659+
}
653660
}
654661
}
655-
}
656-
NestedMetaItemKind::Literal(lit) => {
657-
handle_errors(
658-
sess,
659-
lit.span,
660-
AttrError::UnsupportedLiteral(
661-
"item in `deprecated` must be a key/value pair",
662-
false,
663-
),
664-
);
665-
continue 'outer
662+
NestedMetaItemKind::Literal(lit) => {
663+
handle_errors(
664+
sess,
665+
lit.span,
666+
AttrError::UnsupportedLiteral(
667+
"item in `deprecated` must be a key/value pair",
668+
false,
669+
),
670+
);
671+
continue 'outer
672+
}
666673
}
667674
}
668-
}
669675

670-
Some(Deprecation {since: since, note: note})
671-
} else {
672-
Some(Deprecation{since: None, note: None})
673-
}
676+
Some(Deprecation { since, note })
677+
}
678+
};
674679
}
675680

676681
depr

src/libsyntax/attr/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ fn name_from_path(path: &Path) -> Name {
165165
}
166166

167167
impl Attribute {
168+
/// Returns `true` if the attribute's path matches the argument. If it matches, then the
169+
/// attribute is marked as used.
170+
///
171+
/// To check the attribute name without marking it used, use the `path` field directly.
168172
pub fn check_name(&self, name: &str) -> bool {
169173
let matches = self.path == name;
170174
if matches {

src/libsyntax/feature_gate.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1185,9 +1185,15 @@ pub const BUILTIN_ATTRIBUTES: &[(&str, AttributeType, AttributeTemplate, Attribu
11851185
("stable", Whitelisted, template!(List: r#"feature = "name", since = "version""#), Ungated),
11861186
("unstable", Whitelisted, template!(List: r#"feature = "name", reason = "...", issue = "N""#),
11871187
Ungated),
1188-
("deprecated", Normal, template!(Word, List: r#"/*opt*/ since = "version",
1189-
/*opt*/ note = "reason"#,
1190-
NameValueStr: "reason"), Ungated),
1188+
("deprecated",
1189+
Normal,
1190+
template!(
1191+
Word,
1192+
List: r#"/*opt*/ since = "version", /*opt*/ note = "reason"#,
1193+
NameValueStr: "reason"
1194+
),
1195+
Ungated
1196+
),
11911197

11921198
("rustc_paren_sugar", Normal, template!(Word), Gated(Stability::Unstable,
11931199
"unboxed_closures",

src/test/rustdoc/deprecated.rs

+5
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ pub struct V;
2828
// 'Deprecated$'
2929
#[deprecated]
3030
pub struct W;
31+
32+
// @matches deprecated/struct.X.html '//*[@class="stab deprecated"]' \
33+
// 'Deprecated: shorthand reason$'
34+
#[deprecated = "shorthand reason"]
35+
pub struct X;

src/test/ui/deprecation/deprecation-sanity.rs

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ mod bogus_attribute_types_1 {
1515

1616
#[deprecated(since(b), note = "a")] //~ ERROR incorrect meta item
1717
fn f6() { }
18+
19+
#[deprecated(note = b"test")] //~ ERROR literal in `deprecated` value must be a string
20+
fn f7() { }
21+
22+
#[deprecated("test")] //~ ERROR item in `deprecated` must be a key/value pair
23+
fn f8() { }
1824
}
1925

2026
#[deprecated(since = "a", note = "b")]

src/test/ui/deprecation/deprecation-sanity.stderr

+16-4
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,31 @@ error[E0551]: incorrect meta item
2828
LL | #[deprecated(since(b), note = "a")] //~ ERROR incorrect meta item
2929
| ^^^^^^^^
3030

31+
error[E0565]: literal in `deprecated` value must be a string
32+
--> $DIR/deprecation-sanity.rs:19:25
33+
|
34+
LL | #[deprecated(note = b"test")] //~ ERROR literal in `deprecated` value must be a string
35+
| ^^^^^^^ help: consider removing the prefix: `"test"`
36+
37+
error[E0565]: item in `deprecated` must be a key/value pair
38+
--> $DIR/deprecation-sanity.rs:22:18
39+
|
40+
LL | #[deprecated("test")] //~ ERROR item in `deprecated` must be a key/value pair
41+
| ^^^^^^
42+
3143
error[E0550]: multiple deprecated attributes
32-
--> $DIR/deprecation-sanity.rs:22:1
44+
--> $DIR/deprecation-sanity.rs:28:1
3345
|
3446
LL | fn multiple1() { } //~ ERROR multiple deprecated attributes
3547
| ^^^^^^^^^^^^^^^^^^
3648

3749
error[E0538]: multiple 'since' items
38-
--> $DIR/deprecation-sanity.rs:24:27
50+
--> $DIR/deprecation-sanity.rs:30:27
3951
|
4052
LL | #[deprecated(since = "a", since = "b", note = "c")] //~ ERROR multiple 'since' items
4153
| ^^^^^^^^^^^
4254

43-
error: aborting due to 7 previous errors
55+
error: aborting due to 9 previous errors
4456

45-
Some errors occurred: E0538, E0541, E0550, E0551.
57+
Some errors occurred: E0538, E0541, E0550, E0551, E0565.
4658
For more information about an error, try `rustc --explain E0538`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[deprecated = b"test"] //~ ERROR attribute must be of the form
2+
fn foo() {}
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: attribute must be of the form `#[deprecated]` or `#[deprecated(/*opt*/ since = "version", /*opt*/ note = "reason)]` or `#[deprecated = "reason"]`
2+
--> $DIR/invalid-literal.rs:1:1
3+
|
4+
LL | #[deprecated = b"test"] //~ ERROR attribute must be of the form
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)