Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop compilation on missing const_generics feature. #70122

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/librustc_ast_passes/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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)?)
};
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T, const N: T>(PhantomData<[T; N]>); //~ ERROR const generics are unstable
struct B<T, const N: T>(PhantomData<[T; N]>);
//~^ ERROR the types of const generic parameters must derive `PartialEq` and `Eq`

fn main() {}
Original file line number Diff line number Diff line change
@@ -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<T, const N: T>(PhantomData<[T; N]>);
| ^
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: see issue #44580 </~https://github.com/rust-lang/rust/issues/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<T, const N: T>(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`.
5 changes: 5 additions & 0 deletions src/test/ui/const-generics/issues/issue-69913.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {
//~^ ERROR const generics are unstable
}

fn main() { }
12 changes: 12 additions & 0 deletions src/test/ui/const-generics/issues/issue-69913.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0658]: const generics are unstable
--> $DIR/issue-69913.rs:1:14
|
LL | fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {
| ^
|
= note: see issue #44580 </~https://github.com/rust-lang/rust/issues/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`.
5 changes: 5 additions & 0 deletions src/test/ui/feature-gates/feature-gate-const_generics-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// gate-test-const_generics

fn foo<const X: ()>() {} //~ ERROR const generics are unstable

fn main() {}
12 changes: 12 additions & 0 deletions src/test/ui/feature-gates/feature-gate-const_generics-fn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0658]: const generics are unstable
--> $DIR/feature-gate-const_generics-fn.rs:3:14
|
LL | fn foo<const X: ()>() {}
| ^
|
= note: see issue #44580 </~https://github.com/rust-lang/rust/issues/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`.
9 changes: 5 additions & 4 deletions src/test/ui/feature-gates/feature-gate-const_generics-ptr.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![feature(const_generics)]
//~^ WARNING the feature `const_generics` is incomplete and may cause the compiler to crash

struct ConstFn<const F: fn()>;
//~^ 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<const P: *const u32>;
//~^ 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() {}
26 changes: 8 additions & 18 deletions src/test/ui/feature-gates/feature-gate-const_generics-ptr.stderr
Original file line number Diff line number Diff line change
@@ -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<const F: fn()>;
| ^
|
= note: see issue #44580 </~https://github.com/rust-lang/rust/issues/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<const P: *const u32>;
| ^
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: see issue #44580 </~https://github.com/rust-lang/rust/issues/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<const F: fn()>;
| ^^^^
Expand All @@ -26,14 +16,14 @@ LL | struct ConstFn<const F: fn()>;
= 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<const P: *const u32>;
| ^^^^^^^^^^
|
= note: see issue #53020 </~https://github.com/rust-lang/rust/issues/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`.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn foo<const X: ()>() {} //~ ERROR const generics are unstable
// gate-test-const_generics

struct Foo<const X: usize>([(); X]); //~ ERROR const generics are unstable

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0658]: const generics are unstable
--> $DIR/feature-gate-const_generics-struct.rs:3:18
|
LL | struct Foo<const X: usize>([(); X]);
| ^
|
= note: see issue #44580 </~https://github.com/rust-lang/rust/issues/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`.
21 changes: 0 additions & 21 deletions src/test/ui/feature-gates/feature-gate-const_generics.stderr

This file was deleted.