Skip to content

Commit d4aea02

Browse files
committed
provide suggestion for incorrect deprecated syntax
1 parent 748d354 commit d4aea02

File tree

5 files changed

+45
-18
lines changed

5 files changed

+45
-18
lines changed

src/libsyntax/attr/builtin.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Parsing and validation of builtin attributes
1212
13-
use ast::{self, Attribute, MetaItem, Name, NestedMetaItemKind};
13+
use ast::{self, Attribute, LitKind, MetaItem, Name, NestedMetaItemKind};
1414
use errors::{Applicability, Handler};
1515
use feature_gate::{Features, GatedCfg};
1616
use parse::ParseSess;
@@ -598,12 +598,10 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
598598
let diagnostic = &sess.span_diagnostic;
599599

600600
'outer: for attr in attrs_iter {
601-
if attr.path != "deprecated" {
601+
if !attr.check_name("deprecated") {
602602
continue
603603
}
604604

605-
mark_used(attr);
606-
607605
if depr.is_some() {
608606
span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes");
609607
break
@@ -670,6 +668,28 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
670668
}
671669

672670
Some(Deprecation {since: since, note: note})
671+
} else if let Some(lit) = attr
672+
.meta()
673+
.as_ref()
674+
.and_then(|meta| meta.name_value_literal())
675+
{
676+
let mut err = sess
677+
.span_diagnostic
678+
.struct_span_err(attr.span, "expected meta item sequence");
679+
680+
if let LitKind::Str(s, _) = lit.node {
681+
let sp = attr.path.span.to(lit.span);
682+
err.span_suggestion_with_applicability(
683+
sp,
684+
"use the `note` key",
685+
format!("deprecated(note = \"{}\")", s),
686+
Applicability::MachineApplicable,
687+
);
688+
}
689+
690+
err.emit();
691+
692+
continue 'outer;
673693
} else {
674694
Some(Deprecation{since: None, note: None})
675695
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ fn multiple1() { } //~ ERROR multiple deprecated attributes
3434
#[deprecated(since = "a", since = "b", note = "c")] //~ ERROR multiple 'since' items
3535
fn f1() { }
3636

37+
#[deprecated = "reason"]
38+
fn foo() { } //~^ ERROR expected meta item sequence
39+
//~| HELP use the `note` key
40+
//~| SUGGESTION deprecated(note = "reason")
41+
3742
fn main() { }

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ error[E0538]: multiple 'since' items
4040
LL | #[deprecated(since = "a", since = "b", note = "c")] //~ ERROR multiple 'since' items
4141
| ^^^^^^^^^^^
4242

43-
error: aborting due to 7 previous errors
43+
error: expected meta item sequence
44+
--> $DIR/deprecation-sanity.rs:37:1
45+
|
46+
LL | #[deprecated = "reason"]
47+
| ^^---------------------^
48+
| |
49+
| help: use the `note` key: `deprecated(note = "reason")`
50+
51+
error: aborting due to 8 previous errors
4452

4553
Some errors occurred: E0538, E0541, E0550, E0551.
4654
For more information about an error, try `rustc --explain E0538`.

src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -630,17 +630,17 @@ mod link_section {
630630

631631
struct StructForDeprecated;
632632

633-
#[deprecated = "1500"]
633+
#[deprecated]
634634
mod deprecated {
635-
mod inner { #![deprecated="1500"] }
635+
mod inner { #![deprecated] }
636636

637-
#[deprecated = "1500"] fn f() { }
637+
#[deprecated] fn f() { }
638638

639-
#[deprecated = "1500"] struct S1;
639+
#[deprecated] struct S1;
640640

641-
#[deprecated = "1500"] type T = super::StructForDeprecated;
641+
#[deprecated] type T = super::StructForDeprecated;
642642

643-
#[deprecated = "1500"] impl super::StructForDeprecated { }
643+
#[deprecated] impl super::StructForDeprecated { }
644644
}
645645

646646
#[must_use = "1400"]

src/test/ui/feature-gate/issue-43106-gating-of-deprecated.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@
1818
// compile-pass
1919
// skip-codegen
2020
#![allow(dead_code)]
21-
#![deprecated = "1100"]
22-
23-
// Since we expect for the mix of attributes used here to compile
24-
// successfully, and we are just testing for the expected warnings of
25-
// various (mis)uses of attributes, we use the `rustc_error` attribute
26-
// on the `fn main()`.
27-
21+
#![deprecated]
2822

2923
fn main() {
3024
println!("Hello World");

0 commit comments

Comments
 (0)