Skip to content

Commit

Permalink
make const_generics_defaults use the unstable syntax mechanism
Browse files Browse the repository at this point in the history
This is important to not accidentally stabilize the parsing of the syntax while it still is experimental and not formally accepted
  • Loading branch information
lqd committed Jan 1, 2021
1 parent 1fc3c4c commit 942b7ce
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 35 deletions.
14 changes: 0 additions & 14 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
extended_key_value_attributes,
"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 {
// Errors for `destructuring_assignment` can get quite noisy, especially where `_` is
// involved, so we only emit errors where there are no other parsing errors.
Expand Down
17 changes: 14 additions & 3 deletions compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_ast::{
self as ast, Attribute, GenericBounds, GenericParam, GenericParamKind, WhereClause,
};
use rustc_errors::PResult;
use rustc_span::symbol::kw;
use rustc_span::symbol::{kw, sym};

impl<'a> Parser<'a> {
/// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
Expand Down Expand Up @@ -56,8 +56,19 @@ impl<'a> Parser<'a> {
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;

// Parse optional const generics default value.
let default = if self.eat(&token::Eq) { Some(self.parse_const_arg()?) } else { None };
// Parse optional const generics default value, taking care of feature gating the spans
// 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 {
ident,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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() {}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
error: default values for const generic parameters are unstable
--> $DIR/default_function_param.rs:1:28
error[E0658]: default values for const generic parameters are experimental
--> $DIR/default_function_param.rs:1:26
|
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

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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() {}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
error: default values for const generic parameters are unstable
--> $DIR/default_trait_param.rs:1:30
error[E0658]: default values for const generic parameters are experimental
--> $DIR/default_trait_param.rs:1:28
|
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

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#[cfg(FALSE)]
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>() {}
//~^ ERROR default values for const generic parameters are unstable
#[cfg(FALSE)]
fn foo<const B: bool = false>() {}
//~^ ERROR default values for const generic parameters are experimental

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
error: default values for const generic parameters are unstable
--> $DIR/feature-gate-const_generics_defaults.rs:1:27
error[E0658]: default values for const generic parameters are experimental
--> $DIR/feature-gate-const_generics_defaults.rs:2:25
|
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

error: default values for const generic parameters are unstable
--> $DIR/feature-gate-const_generics_defaults.rs:4:22
error[E0658]: default values for const generic parameters are experimental
--> $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

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.

0 comments on commit 942b7ce

Please sign in to comment.