From bf269335d07b47e548277a02e4cda3c8519a9eec Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 6 Feb 2020 16:19:39 +0900 Subject: [PATCH] Forbid using `0` as issue number --- src/librustc_attr/builtin.rs | 39 +++++++++++-------- .../unstable-attribute-allow-issue-0.rs | 4 +- .../unstable-attribute-allow-issue-0.stderr | 12 +++++- .../stability-attribute-sanity-2.rs | 3 +- .../stability-attribute-sanity-2.stderr | 2 +- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/librustc_attr/builtin.rs b/src/librustc_attr/builtin.rs index be7c164395b..ab03297fffe 100644 --- a/src/librustc_attr/builtin.rs +++ b/src/librustc_attr/builtin.rs @@ -396,26 +396,31 @@ where issue_num = match &*issue.unwrap().as_str() { "none" => None, issue => { + let emit_diag = |msg: &str| { + struct_span_err!( + diagnostic, + mi.span, + E0545, + "`issue` must be a non-zero numeric string \ + or \"none\"", + ) + .span_label( + mi.name_value_literal().unwrap().span, + msg, + ) + .emit(); + }; match issue.parse() { - Ok(num) => { - // FIXME(rossmacarthur): disallow 0 - // Disallowing this requires updates to - // some submodules - NonZeroU32::new(num) + Ok(num) if num == 0 => { + emit_diag( + "`issue` must not be \"0\", \ + use \"none\" instead", + ); + continue 'outer; } + Ok(num) => 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(); + emit_diag(&err.to_string()); continue 'outer; } } 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 7a2bf468f89..bffe43262e0 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 @@ -4,10 +4,10 @@ #![stable(feature = "stable_test_feature", since = "1.0.0")] #[unstable(feature = "unstable_test_feature", issue = "0")] -fn unstable_issue_0() {} +fn unstable_issue_0() {} //~^ ERROR `issue` must be a non-zero numeric string or "none" #[unstable(feature = "unstable_test_feature", issue = "none")] fn unstable_issue_none() {} #[unstable(feature = "unstable_test_feature", issue = "something")] -fn unstable_issue_not_allowed() {} //~^ ERROR `issue` must be a numeric string or "none" +fn unstable_issue_not_allowed() {} //~^ ERROR `issue` must be a non-zero 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 21ff12185ec..7bbaf92fc68 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,4 +1,12 @@ -error[E0545]: `issue` must be a numeric string or "none" +error[E0545]: `issue` must be a non-zero numeric string or "none" + --> $DIR/unstable-attribute-allow-issue-0.rs:6:47 + | +LL | #[unstable(feature = "unstable_test_feature", issue = "0")] + | ^^^^^^^^--- + | | + | `issue` must not be "0", use "none" instead + +error[E0545]: `issue` must be a non-zero numeric string or "none" --> $DIR/unstable-attribute-allow-issue-0.rs:12:47 | LL | #[unstable(feature = "unstable_test_feature", issue = "something")] @@ -6,5 +14,5 @@ LL | #[unstable(feature = "unstable_test_feature", issue = "something")] | | | invalid digit found in string -error: aborting due to previous error +error: aborting due to 2 previous errors 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 e74147ce900..de3ea4eaca9 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,8 @@ fn f1() { } #[stable(feature = "a", sinse = "1.0.0")] //~ ERROR unknown meta item 'sinse' fn f2() { } -#[unstable(feature = "a", issue = "no")] //~ ERROR `issue` must be a numeric string or "none" +#[unstable(feature = "a", issue = "no")] +//~^ ERROR `issue` must be a non-zero 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 541b94afe0f..3b826191899 100644 --- a/src/test/ui/stability-attribute/stability-attribute-sanity-2.stderr +++ b/src/test/ui/stability-attribute/stability-attribute-sanity-2.stderr @@ -10,7 +10,7 @@ error[E0541]: unknown meta item 'sinse' LL | #[stable(feature = "a", sinse = "1.0.0")] | ^^^^^^^^^^^^^^^ expected one of `since`, `note` -error[E0545]: `issue` must be a numeric string or "none" +error[E0545]: `issue` must be a non-zero numeric string or "none" --> $DIR/stability-attribute-sanity-2.rs:13:27 | LL | #[unstable(feature = "a", issue = "no")]