From 7632ade65bde6160c46f31495532f5beadcaa3d8 Mon Sep 17 00:00:00 2001 From: Andy Russell Date: Wed, 22 Jan 2020 10:33:46 -0500 Subject: [PATCH] clarify "incorrect issue" error --- src/libsyntax/attr/builtin.rs | 55 ++++++++++++------- .../unstable-attribute-allow-issue-0.rs | 4 +- .../unstable-attribute-allow-issue-0.stderr | 8 ++- .../stability-attribute-sanity-2.rs | 2 +- .../stability-attribute-sanity-2.stderr | 8 ++- 5 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/libsyntax/attr/builtin.rs b/src/libsyntax/attr/builtin.rs index 6cfe4f2de1e..1da005d70d4 100644 --- a/src/libsyntax/attr/builtin.rs +++ b/src/libsyntax/attr/builtin.rs @@ -371,6 +371,7 @@ macro_rules! get_meta { let mut feature = None; let mut reason = None; let mut issue = None; + let mut issue_num = None; let mut is_soft = false; for meta in metas { if let Some(mi) = meta.meta_item() { @@ -389,6 +390,37 @@ macro_rules! get_meta { if !get(mi, &mut issue) { continue 'outer; } + + // These unwraps are safe because `get` ensures the meta item + // is a name/value pair string literal. + issue_num = match &*issue.unwrap().as_str() { + "none" => None, + issue => { + match issue.parse() { + Ok(num) => { + // FIXME(rossmacarthur): disallow 0 + // Disallowing this requires updates to + // some submodules + NonZeroU32::new(num) + } + Err(err) => { + struct_span_err!( + diagnostic, + mi.span, + E0545, + "`issue` must be a numeric string \ + or \"none\"", + ) + .span_label( + mi.name_value_literal().unwrap().span, + &err.to_string(), + ) + .emit(); + continue 'outer; + } + } + } + }; } sym::soft => { if !mi.is_word() { @@ -420,27 +452,8 @@ macro_rules! get_meta { } match (feature, reason, issue) { - (Some(feature), reason, Some(issue)) => { - let issue = match &*issue.as_str() { - "none" => None, - issue => { - if let Ok(num) = issue.parse() { - // FIXME(rossmacarthur): disallow 0 - // Disallowing this requires updates to some submodules - NonZeroU32::new(num) - } else { - struct_span_err!( - diagnostic, - attr.span, - E0545, - "incorrect 'issue'" - ) - .emit(); - continue; - } - } - }; - let level = Unstable { reason, issue, is_soft }; + (Some(feature), reason, Some(_)) => { + let level = Unstable { reason, issue: issue_num, is_soft }; if sym::unstable == meta_name { stab = Some(Stability { level, feature, rustc_depr: None }); } else { diff --git a/src/test/ui/feature-gate/unstable-attribute-allow-issue-0.rs b/src/test/ui/feature-gate/unstable-attribute-allow-issue-0.rs index c8ad0d13a14..7a2bf468f89 100644 --- a/src/test/ui/feature-gate/unstable-attribute-allow-issue-0.rs +++ b/src/test/ui/feature-gate/unstable-attribute-allow-issue-0.rs @@ -9,5 +9,5 @@ fn unstable_issue_0() {} #[unstable(feature = "unstable_test_feature", issue = "none")] fn unstable_issue_none() {} -#[unstable(feature = "unstable_test_feature", issue = "something")] //~ ERROR incorrect 'issue' -fn unstable_issue_not_allowed() {} +#[unstable(feature = "unstable_test_feature", issue = "something")] +fn unstable_issue_not_allowed() {} //~^ ERROR `issue` must be a numeric string or "none" diff --git a/src/test/ui/feature-gate/unstable-attribute-allow-issue-0.stderr b/src/test/ui/feature-gate/unstable-attribute-allow-issue-0.stderr index 10bd6f373d9..21ff12185ec 100644 --- a/src/test/ui/feature-gate/unstable-attribute-allow-issue-0.stderr +++ b/src/test/ui/feature-gate/unstable-attribute-allow-issue-0.stderr @@ -1,8 +1,10 @@ -error[E0545]: incorrect 'issue' - --> $DIR/unstable-attribute-allow-issue-0.rs:12:1 +error[E0545]: `issue` must be a numeric string or "none" + --> $DIR/unstable-attribute-allow-issue-0.rs:12:47 | LL | #[unstable(feature = "unstable_test_feature", issue = "something")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^----------- + | | + | invalid digit found in string error: aborting due to previous error diff --git a/src/test/ui/stability-attribute/stability-attribute-sanity-2.rs b/src/test/ui/stability-attribute/stability-attribute-sanity-2.rs index 5a67ca0b0c8..e74147ce900 100644 --- a/src/test/ui/stability-attribute/stability-attribute-sanity-2.rs +++ b/src/test/ui/stability-attribute/stability-attribute-sanity-2.rs @@ -10,7 +10,7 @@ fn f1() { } #[stable(feature = "a", sinse = "1.0.0")] //~ ERROR unknown meta item 'sinse' fn f2() { } -#[unstable(feature = "a", issue = "no")] //~ ERROR incorrect 'issue' +#[unstable(feature = "a", issue = "no")] //~ ERROR `issue` must be a numeric string or "none" fn f3() { } fn main() { } diff --git a/src/test/ui/stability-attribute/stability-attribute-sanity-2.stderr b/src/test/ui/stability-attribute/stability-attribute-sanity-2.stderr index d683d089530..541b94afe0f 100644 --- a/src/test/ui/stability-attribute/stability-attribute-sanity-2.stderr +++ b/src/test/ui/stability-attribute/stability-attribute-sanity-2.stderr @@ -10,11 +10,13 @@ error[E0541]: unknown meta item 'sinse' LL | #[stable(feature = "a", sinse = "1.0.0")] | ^^^^^^^^^^^^^^^ expected one of `since`, `note` -error[E0545]: incorrect 'issue' - --> $DIR/stability-attribute-sanity-2.rs:13:1 +error[E0545]: `issue` must be a numeric string or "none" + --> $DIR/stability-attribute-sanity-2.rs:13:27 | LL | #[unstable(feature = "a", issue = "no")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^---- + | | + | invalid digit found in string error: aborting due to 3 previous errors