Skip to content

Commit

Permalink
provide suggestion for incorrect deprecated syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
euclio committed Dec 16, 2018
1 parent 748d354 commit d4aea02
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
28 changes: 24 additions & 4 deletions src/libsyntax/attr/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//! Parsing and validation of builtin attributes

use ast::{self, Attribute, MetaItem, Name, NestedMetaItemKind};
use ast::{self, Attribute, LitKind, MetaItem, Name, NestedMetaItemKind};
use errors::{Applicability, Handler};
use feature_gate::{Features, GatedCfg};
use parse::ParseSess;
Expand Down Expand Up @@ -598,12 +598,10 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
let diagnostic = &sess.span_diagnostic;

'outer: for attr in attrs_iter {
if attr.path != "deprecated" {
if !attr.check_name("deprecated") {
continue
}

mark_used(attr);

if depr.is_some() {
span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes");
break
Expand Down Expand Up @@ -670,6 +668,28 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
}

Some(Deprecation {since: since, note: note})
} else if let Some(lit) = attr
.meta()
.as_ref()
.and_then(|meta| meta.name_value_literal())
{
let mut err = sess
.span_diagnostic
.struct_span_err(attr.span, "expected meta item sequence");

if let LitKind::Str(s, _) = lit.node {
let sp = attr.path.span.to(lit.span);
err.span_suggestion_with_applicability(
sp,
"use the `note` key",
format!("deprecated(note = \"{}\")", s),
Applicability::MachineApplicable,
);
}

err.emit();

continue 'outer;
} else {
Some(Deprecation{since: None, note: None})
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/deprecation/deprecation-sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ fn multiple1() { } //~ ERROR multiple deprecated attributes
#[deprecated(since = "a", since = "b", note = "c")] //~ ERROR multiple 'since' items
fn f1() { }

#[deprecated = "reason"]
fn foo() { } //~^ ERROR expected meta item sequence
//~| HELP use the `note` key
//~| SUGGESTION deprecated(note = "reason")

fn main() { }
10 changes: 9 additions & 1 deletion src/test/ui/deprecation/deprecation-sanity.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ error[E0538]: multiple 'since' items
LL | #[deprecated(since = "a", since = "b", note = "c")] //~ ERROR multiple 'since' items
| ^^^^^^^^^^^

error: aborting due to 7 previous errors
error: expected meta item sequence
--> $DIR/deprecation-sanity.rs:37:1
|
LL | #[deprecated = "reason"]
| ^^---------------------^
| |
| help: use the `note` key: `deprecated(note = "reason")`

error: aborting due to 8 previous errors

Some errors occurred: E0538, E0541, E0550, E0551.
For more information about an error, try `rustc --explain E0538`.
12 changes: 6 additions & 6 deletions src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,17 +630,17 @@ mod link_section {

struct StructForDeprecated;

#[deprecated = "1500"]
#[deprecated]
mod deprecated {
mod inner { #![deprecated="1500"] }
mod inner { #![deprecated] }

#[deprecated = "1500"] fn f() { }
#[deprecated] fn f() { }

#[deprecated = "1500"] struct S1;
#[deprecated] struct S1;

#[deprecated = "1500"] type T = super::StructForDeprecated;
#[deprecated] type T = super::StructForDeprecated;

#[deprecated = "1500"] impl super::StructForDeprecated { }
#[deprecated] impl super::StructForDeprecated { }
}

#[must_use = "1400"]
Expand Down
8 changes: 1 addition & 7 deletions src/test/ui/feature-gate/issue-43106-gating-of-deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
// compile-pass
// skip-codegen
#![allow(dead_code)]
#![deprecated = "1100"]

// Since we expect for the mix of attributes used here to compile
// successfully, and we are just testing for the expected warnings of
// various (mis)uses of attributes, we use the `rustc_error` attribute
// on the `fn main()`.

#![deprecated]

fn main() {
println!("Hello World");
Expand Down

0 comments on commit d4aea02

Please sign in to comment.