From 25714685fdc28e5a8cb0d1e97c2aea3400cd7b56 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Wed, 18 Mar 2020 23:41:02 +0100 Subject: [PATCH 1/4] `const_generics`: abort on mising feature --- src/librustc_ast_passes/feature_gate.rs | 29 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/librustc_ast_passes/feature_gate.rs b/src/librustc_ast_passes/feature_gate.rs index 364c86bd99b4e..df9d5c4ca7260 100644 --- a/src/librustc_ast_passes/feature_gate.rs +++ b/src/librustc_ast_passes/feature_gate.rs @@ -2,7 +2,7 @@ use rustc_ast::ast::{self, AssocTyConstraint, AssocTyConstraintKind, NodeId}; use rustc_ast::ast::{GenericParam, GenericParamKind, PatKind, RangeEnd, VariantData}; use rustc_ast::attr; use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor}; -use rustc_errors::{struct_span_err, Handler}; +use rustc_errors::{struct_span_err, FatalError, Handler}; use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP}; use rustc_feature::{Features, GateIssue, UnstableFeatures}; use rustc_session::parse::{feature_err, feature_err_issue, ParseSess}; @@ -13,19 +13,22 @@ use rustc_span::Span; use log::debug; macro_rules! gate_feature_fn { - ($cx: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr) => {{ + ($cx: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr $(, $fatal: expr $(,)?)?) => {{ let (cx, has_feature, span, name, explain) = (&*$cx, $has_feature, $span, $name, $explain); let has_feature: bool = has_feature(&$cx.features); debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature); if !has_feature && !span.allows_unstable($name) { feature_err_issue(cx.parse_sess, name, span, GateIssue::Language, explain).emit(); + $( + $fatal.raise(); + )? } }}; } macro_rules! gate_feature_post { - ($cx: expr, $feature: ident, $span: expr, $explain: expr) => { - gate_feature_fn!($cx, |x: &Features| x.$feature, $span, sym::$feature, $explain) + ($cx: expr, $feature: ident, $span: expr, $explain: expr $(, $fatal: expr $(,)?)?) => { + gate_feature_fn!($cx, |x: &Features| x.$feature, $span, sym::$feature, $explain $(, $fatal)?) }; } @@ -517,12 +520,18 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn visit_generic_param(&mut self, param: &'a GenericParam) { match param.kind { - GenericParamKind::Const { .. } => gate_feature_post!( - &self, - const_generics, - param.ident.span, - "const generics are unstable" - ), + GenericParamKind::Const { .. } => { + // FIXME(const_generics): we currently stop compilation prematurely in case a + // generic param is found in locations where the `const_generics` + // feature is required. + gate_feature_post!( + &self, + const_generics, + param.ident.span, + "const generics are unstable", + FatalError, + ); + } _ => {} } visit::walk_generic_param(self, param) From ba1d63c023168f6bc7257f346c4dc6626cb26581 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 19 Mar 2020 00:09:24 +0100 Subject: [PATCH 2/4] update tests --- ...aram-type-depends-on-type-param-ungated.rs | 5 +++- ...-type-depends-on-type-param-ungated.stderr | 18 ++++++------- .../ui/const-generics/issues/issue-69913.rs | 5 ++++ .../const-generics/issues/issue-69913.stderr | 12 +++++++++ .../feature-gate-const_generics-fn.rs | 3 +++ .../feature-gate-const_generics-fn.stderr | 12 +++++++++ .../feature-gate-const_generics-ptr.rs | 9 ++++--- .../feature-gate-const_generics-ptr.stderr | 26 ++++++------------- ... => feature-gate-const_generics-struct.rs} | 2 -- .../feature-gate-const_generics-struct.stderr | 12 +++++++++ .../feature-gate-const_generics.stderr | 21 --------------- 11 files changed, 69 insertions(+), 56 deletions(-) create mode 100644 src/test/ui/const-generics/issues/issue-69913.rs create mode 100644 src/test/ui/const-generics/issues/issue-69913.stderr create mode 100644 src/test/ui/feature-gates/feature-gate-const_generics-fn.rs create mode 100644 src/test/ui/feature-gates/feature-gate-const_generics-fn.stderr rename src/test/ui/feature-gates/{feature-gate-const_generics.rs => feature-gate-const_generics-struct.rs} (58%) create mode 100644 src/test/ui/feature-gates/feature-gate-const_generics-struct.stderr delete mode 100644 src/test/ui/feature-gates/feature-gate-const_generics.stderr diff --git a/src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.rs b/src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.rs index 78bd549ba791a..b61cebc7483ac 100644 --- a/src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.rs +++ b/src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.rs @@ -1,6 +1,9 @@ +#![feature(const_generics)] +//~^ WARNING the feature `const_generics` is incomplete and may cause the compiler to crash + use std::marker::PhantomData; -struct B(PhantomData<[T; N]>); //~ ERROR const generics are unstable +struct B(PhantomData<[T; N]>); //~^ ERROR the types of const generic parameters must derive `PartialEq` and `Eq` fn main() {} diff --git a/src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.stderr b/src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.stderr index 14ade3f33fd9b..345f8e60bfe6f 100644 --- a/src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.stderr +++ b/src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.stderr @@ -1,19 +1,17 @@ -error[E0658]: const generics are unstable - --> $DIR/const-param-type-depends-on-type-param-ungated.rs:3:19 +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/const-param-type-depends-on-type-param-ungated.rs:1:12 | -LL | struct B(PhantomData<[T; N]>); - | ^ +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ | - = note: see issue #44580 for more information - = help: add `#![feature(const_generics)]` to the crate attributes to enable + = note: `#[warn(incomplete_features)]` on by default error[E0741]: the types of const generic parameters must derive `PartialEq` and `Eq` - --> $DIR/const-param-type-depends-on-type-param-ungated.rs:3:22 + --> $DIR/const-param-type-depends-on-type-param-ungated.rs:6:22 | LL | struct B(PhantomData<[T; N]>); | ^ `T` doesn't derive both `PartialEq` and `Eq` -error: aborting due to 2 previous errors +error: aborting due to previous error -Some errors have detailed explanations: E0658, E0741. -For more information about an error, try `rustc --explain E0658`. +For more information about this error, try `rustc --explain E0741`. diff --git a/src/test/ui/const-generics/issues/issue-69913.rs b/src/test/ui/const-generics/issues/issue-69913.rs new file mode 100644 index 0000000000000..00ea8d9eed9d1 --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-69913.rs @@ -0,0 +1,5 @@ +fn foo(bar: [usize; A + B]) { + //~^ ERROR const generics are unstable +} + +fn main() { } diff --git a/src/test/ui/const-generics/issues/issue-69913.stderr b/src/test/ui/const-generics/issues/issue-69913.stderr new file mode 100644 index 0000000000000..057bce23f3fab --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-69913.stderr @@ -0,0 +1,12 @@ +error[E0658]: const generics are unstable + --> $DIR/issue-69913.rs:1:14 + | +LL | fn foo(bar: [usize; A + B]) { + | ^ + | + = note: see issue #44580 for more information + = help: add `#![feature(const_generics)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/feature-gates/feature-gate-const_generics-fn.rs b/src/test/ui/feature-gates/feature-gate-const_generics-fn.rs new file mode 100644 index 0000000000000..c5faa451e0de1 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-const_generics-fn.rs @@ -0,0 +1,3 @@ +fn foo() {} //~ ERROR const generics are unstable + +fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-const_generics-fn.stderr b/src/test/ui/feature-gates/feature-gate-const_generics-fn.stderr new file mode 100644 index 0000000000000..45e5051092d0c --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-const_generics-fn.stderr @@ -0,0 +1,12 @@ +error[E0658]: const generics are unstable + --> $DIR/feature-gate-const_generics-fn.rs:1:14 + | +LL | fn foo() {} + | ^ + | + = note: see issue #44580 for more information + = help: add `#![feature(const_generics)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/feature-gates/feature-gate-const_generics-ptr.rs b/src/test/ui/feature-gates/feature-gate-const_generics-ptr.rs index 1ab11ce3b4423..e5a3cc9ddbec5 100644 --- a/src/test/ui/feature-gates/feature-gate-const_generics-ptr.rs +++ b/src/test/ui/feature-gates/feature-gate-const_generics-ptr.rs @@ -1,9 +1,10 @@ +#![feature(const_generics)] +//~^ WARNING the feature `const_generics` is incomplete and may cause the compiler to crash + struct ConstFn; -//~^ ERROR const generics are unstable -//~^^ ERROR using function pointers as const generic parameters is unstable +//~^ ERROR using function pointers as const generic parameters is unstable struct ConstPtr; -//~^ ERROR const generics are unstable -//~^^ ERROR using raw pointers as const generic parameters is unstable +//~^ ERROR using raw pointers as const generic parameters is unstable fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-const_generics-ptr.stderr b/src/test/ui/feature-gates/feature-gate-const_generics-ptr.stderr index dc7ef55e7ab99..29aae1e6fc2e1 100644 --- a/src/test/ui/feature-gates/feature-gate-const_generics-ptr.stderr +++ b/src/test/ui/feature-gates/feature-gate-const_generics-ptr.stderr @@ -1,23 +1,13 @@ -error[E0658]: const generics are unstable - --> $DIR/feature-gate-const_generics-ptr.rs:1:22 +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/feature-gate-const_generics-ptr.rs:1:12 | -LL | struct ConstFn; - | ^ - | - = note: see issue #44580 for more information - = help: add `#![feature(const_generics)]` to the crate attributes to enable - -error[E0658]: const generics are unstable - --> $DIR/feature-gate-const_generics-ptr.rs:5:23 - | -LL | struct ConstPtr; - | ^ +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ | - = note: see issue #44580 for more information - = help: add `#![feature(const_generics)]` to the crate attributes to enable + = note: `#[warn(incomplete_features)]` on by default error[E0658]: using function pointers as const generic parameters is unstable - --> $DIR/feature-gate-const_generics-ptr.rs:1:25 + --> $DIR/feature-gate-const_generics-ptr.rs:4:25 | LL | struct ConstFn; | ^^^^ @@ -26,7 +16,7 @@ LL | struct ConstFn; = help: add `#![feature(const_compare_raw_pointers)]` to the crate attributes to enable error[E0658]: using raw pointers as const generic parameters is unstable - --> $DIR/feature-gate-const_generics-ptr.rs:5:26 + --> $DIR/feature-gate-const_generics-ptr.rs:7:26 | LL | struct ConstPtr; | ^^^^^^^^^^ @@ -34,6 +24,6 @@ LL | struct ConstPtr; = note: see issue #53020 for more information = help: add `#![feature(const_compare_raw_pointers)]` to the crate attributes to enable -error: aborting due to 4 previous errors +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/feature-gates/feature-gate-const_generics.rs b/src/test/ui/feature-gates/feature-gate-const_generics-struct.rs similarity index 58% rename from src/test/ui/feature-gates/feature-gate-const_generics.rs rename to src/test/ui/feature-gates/feature-gate-const_generics-struct.rs index fe1ded1c4bbc4..ac2363dfdb684 100644 --- a/src/test/ui/feature-gates/feature-gate-const_generics.rs +++ b/src/test/ui/feature-gates/feature-gate-const_generics-struct.rs @@ -1,5 +1,3 @@ -fn foo() {} //~ ERROR const generics are unstable - struct Foo([(); X]); //~ ERROR const generics are unstable fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-const_generics-struct.stderr b/src/test/ui/feature-gates/feature-gate-const_generics-struct.stderr new file mode 100644 index 0000000000000..764d5da85df36 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-const_generics-struct.stderr @@ -0,0 +1,12 @@ +error[E0658]: const generics are unstable + --> $DIR/feature-gate-const_generics-struct.rs:1:18 + | +LL | struct Foo([(); X]); + | ^ + | + = note: see issue #44580 for more information + = help: add `#![feature(const_generics)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/feature-gates/feature-gate-const_generics.stderr b/src/test/ui/feature-gates/feature-gate-const_generics.stderr deleted file mode 100644 index 02aa1f5a4d843..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-const_generics.stderr +++ /dev/null @@ -1,21 +0,0 @@ -error[E0658]: const generics are unstable - --> $DIR/feature-gate-const_generics.rs:1:14 - | -LL | fn foo() {} - | ^ - | - = note: see issue #44580 for more information - = help: add `#![feature(const_generics)]` to the crate attributes to enable - -error[E0658]: const generics are unstable - --> $DIR/feature-gate-const_generics.rs:3:18 - | -LL | struct Foo([(); X]); - | ^ - | - = note: see issue #44580 for more information - = help: add `#![feature(const_generics)]` to the crate attributes to enable - -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0658`. From 0c9028606b19a98bd6ba30c836af1574d3825205 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 19 Mar 2020 10:51:53 +0100 Subject: [PATCH 3/4] added `gate-test-const_generics` --- src/test/ui/feature-gates/feature-gate-const_generics-fn.rs | 2 ++ src/test/ui/feature-gates/feature-gate-const_generics-struct.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/test/ui/feature-gates/feature-gate-const_generics-fn.rs b/src/test/ui/feature-gates/feature-gate-const_generics-fn.rs index c5faa451e0de1..e5b1548d54b7b 100644 --- a/src/test/ui/feature-gates/feature-gate-const_generics-fn.rs +++ b/src/test/ui/feature-gates/feature-gate-const_generics-fn.rs @@ -1,3 +1,5 @@ +// gate-test-const_generics + fn foo() {} //~ ERROR const generics are unstable fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-const_generics-struct.rs b/src/test/ui/feature-gates/feature-gate-const_generics-struct.rs index ac2363dfdb684..8641b8a4aa63a 100644 --- a/src/test/ui/feature-gates/feature-gate-const_generics-struct.rs +++ b/src/test/ui/feature-gates/feature-gate-const_generics-struct.rs @@ -1,3 +1,5 @@ +// gate-test-const_generics + struct Foo([(); X]); //~ ERROR const generics are unstable fn main() {} From ec4bfbf87fc2a9ff857e57c2253f72003e5f7a8e Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 19 Mar 2020 12:50:17 +0100 Subject: [PATCH 4/4] bless you --- src/test/ui/feature-gates/feature-gate-const_generics-fn.stderr | 2 +- .../ui/feature-gates/feature-gate-const_generics-struct.stderr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/ui/feature-gates/feature-gate-const_generics-fn.stderr b/src/test/ui/feature-gates/feature-gate-const_generics-fn.stderr index 45e5051092d0c..414490142ecd5 100644 --- a/src/test/ui/feature-gates/feature-gate-const_generics-fn.stderr +++ b/src/test/ui/feature-gates/feature-gate-const_generics-fn.stderr @@ -1,5 +1,5 @@ error[E0658]: const generics are unstable - --> $DIR/feature-gate-const_generics-fn.rs:1:14 + --> $DIR/feature-gate-const_generics-fn.rs:3:14 | LL | fn foo() {} | ^ diff --git a/src/test/ui/feature-gates/feature-gate-const_generics-struct.stderr b/src/test/ui/feature-gates/feature-gate-const_generics-struct.stderr index 764d5da85df36..66332213a2833 100644 --- a/src/test/ui/feature-gates/feature-gate-const_generics-struct.stderr +++ b/src/test/ui/feature-gates/feature-gate-const_generics-struct.stderr @@ -1,5 +1,5 @@ error[E0658]: const generics are unstable - --> $DIR/feature-gate-const_generics-struct.rs:1:18 + --> $DIR/feature-gate-const_generics-struct.rs:3:18 | LL | struct Foo([(); X]); | ^