make const_generics_defaults use the unstable syntax mechanism

This is important to not accidentally stabilize the parsing of the syntax while it still is experimental and not formally accepted
This commit is contained in:
Rémy Rakic 2020-12-31 18:32:06 +01:00
parent 1fc3c4c16d
commit 942b7ce2c1
9 changed files with 45 additions and 35 deletions

View file

@ -1166,20 +1166,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
} }
} }
} }
if !self.session.features_untracked().const_generics_defaults {
if let GenericParamKind::Const { default: Some(ref default), .. } = param.kind {
let mut err = self.err_handler().struct_span_err(
default.value.span,
"default values for const generic parameters are unstable",
);
err.help(
"add `#![feature(const_generics_defaults)]` \
to the crate attributes to enable",
);
err.emit();
break;
}
}
} }
validate_generic_param_order( validate_generic_param_order(

View file

@ -619,6 +619,10 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
extended_key_value_attributes, extended_key_value_attributes,
"arbitrary expressions in key-value attributes are unstable" "arbitrary expressions in key-value attributes are unstable"
); );
gate_all!(
const_generics_defaults,
"default values for const generic parameters are experimental"
);
if sess.parse_sess.span_diagnostic.err_count() == 0 { if sess.parse_sess.span_diagnostic.err_count() == 0 {
// Errors for `destructuring_assignment` can get quite noisy, especially where `_` is // Errors for `destructuring_assignment` can get quite noisy, especially where `_` is
// involved, so we only emit errors where there are no other parsing errors. // involved, so we only emit errors where there are no other parsing errors.

View file

@ -5,7 +5,7 @@ use rustc_ast::{
self as ast, Attribute, GenericBounds, GenericParam, GenericParamKind, WhereClause, self as ast, Attribute, GenericBounds, GenericParam, GenericParamKind, WhereClause,
}; };
use rustc_errors::PResult; use rustc_errors::PResult;
use rustc_span::symbol::kw; use rustc_span::symbol::{kw, sym};
impl<'a> Parser<'a> { impl<'a> Parser<'a> {
/// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`. /// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
@ -56,8 +56,19 @@ impl<'a> Parser<'a> {
self.expect(&token::Colon)?; self.expect(&token::Colon)?;
let ty = self.parse_ty()?; let ty = self.parse_ty()?;
// Parse optional const generics default value. // Parse optional const generics default value, taking care of feature gating the spans
let default = if self.eat(&token::Eq) { Some(self.parse_const_arg()?) } else { None }; // with the unstable syntax mechanism.
let default = if self.eat(&token::Eq) {
// The gated span goes from the `=` to the end of the const argument that follows (and
// which could be a block expression).
let start = self.prev_token.span;
let const_arg = self.parse_const_arg()?;
let span = start.to(const_arg.value.span);
self.sess.gated_spans.gate(sym::const_generics_defaults, span);
Some(const_arg)
} else {
None
};
Ok(GenericParam { Ok(GenericParam {
ident, ident,

View file

@ -1,4 +1,4 @@
fn foo<const SIZE: usize = 5>() {} fn foo<const SIZE: usize = 5>() {}
//~^ ERROR default values for const generic parameters are unstable //~^ ERROR default values for const generic parameters are experimental
fn main() {} fn main() {}

View file

@ -1,10 +1,12 @@
error: default values for const generic parameters are unstable error[E0658]: default values for const generic parameters are experimental
--> $DIR/default_function_param.rs:1:28 --> $DIR/default_function_param.rs:1:26
| |
LL | fn foo<const SIZE: usize = 5>() {} LL | fn foo<const SIZE: usize = 5>() {}
| ^ | ^^^
| |
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable = help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
error: aborting due to previous error error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -1,4 +1,4 @@
trait Foo<const KIND: bool = true> {} trait Foo<const KIND: bool = true> {}
//~^ ERROR default values for const generic parameters are unstable //~^ ERROR default values for const generic parameters are experimental
fn main() {} fn main() {}

View file

@ -1,10 +1,12 @@
error: default values for const generic parameters are unstable error[E0658]: default values for const generic parameters are experimental
--> $DIR/default_trait_param.rs:1:30 --> $DIR/default_trait_param.rs:1:28
| |
LL | trait Foo<const KIND: bool = true> {} LL | trait Foo<const KIND: bool = true> {}
| ^^^^ | ^^^^^^
| |
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable = help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
error: aborting due to previous error error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -1,7 +1,9 @@
#[cfg(FALSE)]
struct A<const N: usize = 3>; struct A<const N: usize = 3>;
//~^ ERROR default values for const generic parameters are unstable //~^ ERROR default values for const generic parameters are experimental
fn foo<const N: u8 = 6>() {} #[cfg(FALSE)]
//~^ ERROR default values for const generic parameters are unstable fn foo<const B: bool = false>() {}
//~^ ERROR default values for const generic parameters are experimental
fn main() {} fn main() {}

View file

@ -1,18 +1,21 @@
error: default values for const generic parameters are unstable error[E0658]: default values for const generic parameters are experimental
--> $DIR/feature-gate-const_generics_defaults.rs:1:27 --> $DIR/feature-gate-const_generics_defaults.rs:2:25
| |
LL | struct A<const N: usize = 3>; LL | struct A<const N: usize = 3>;
| ^ | ^^^
| |
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable = help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
error: default values for const generic parameters are unstable error[E0658]: default values for const generic parameters are experimental
--> $DIR/feature-gate-const_generics_defaults.rs:4:22 --> $DIR/feature-gate-const_generics_defaults.rs:6:22
| |
LL | fn foo<const N: u8 = 6>() {} LL | fn foo<const B: bool = false>() {}
| ^ | ^^^^^^^
| |
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable = help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
error: aborting due to 2 previous errors error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0658`.