Skip to content

Commit 942b7ce

Browse files
committed
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
1 parent 1fc3c4c commit 942b7ce

9 files changed

+45
-35
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

-14
Original file line numberDiff line numberDiff line change
@@ -1166,20 +1166,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11661166
}
11671167
}
11681168
}
1169-
if !self.session.features_untracked().const_generics_defaults {
1170-
if let GenericParamKind::Const { default: Some(ref default), .. } = param.kind {
1171-
let mut err = self.err_handler().struct_span_err(
1172-
default.value.span,
1173-
"default values for const generic parameters are unstable",
1174-
);
1175-
err.help(
1176-
"add `#![feature(const_generics_defaults)]` \
1177-
to the crate attributes to enable",
1178-
);
1179-
err.emit();
1180-
break;
1181-
}
1182-
}
11831169
}
11841170

11851171
validate_generic_param_order(

compiler/rustc_ast_passes/src/feature_gate.rs

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

compiler/rustc_parse/src/parser/generics.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::{
55
self as ast, Attribute, GenericBounds, GenericParam, GenericParamKind, WhereClause,
66
};
77
use rustc_errors::PResult;
8-
use rustc_span::symbol::kw;
8+
use rustc_span::symbol::{kw, sym};
99

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

59-
// Parse optional const generics default value.
60-
let default = if self.eat(&token::Eq) { Some(self.parse_const_arg()?) } else { None };
59+
// Parse optional const generics default value, taking care of feature gating the spans
60+
// with the unstable syntax mechanism.
61+
let default = if self.eat(&token::Eq) {
62+
// The gated span goes from the `=` to the end of the const argument that follows (and
63+
// which could be a block expression).
64+
let start = self.prev_token.span;
65+
let const_arg = self.parse_const_arg()?;
66+
let span = start.to(const_arg.value.span);
67+
self.sess.gated_spans.gate(sym::const_generics_defaults, span);
68+
Some(const_arg)
69+
} else {
70+
None
71+
};
6172

6273
Ok(GenericParam {
6374
ident,
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn foo<const SIZE: usize = 5>() {}
2-
//~^ ERROR default values for const generic parameters are unstable
2+
//~^ ERROR default values for const generic parameters are experimental
33

44
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
error: default values for const generic parameters are unstable
2-
--> $DIR/default_function_param.rs:1:28
1+
error[E0658]: default values for const generic parameters are experimental
2+
--> $DIR/default_function_param.rs:1:26
33
|
44
LL | fn foo<const SIZE: usize = 5>() {}
5-
| ^
5+
| ^^^
66
|
7+
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
78
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
89

910
error: aborting due to previous error
1011

12+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
trait Foo<const KIND: bool = true> {}
2-
//~^ ERROR default values for const generic parameters are unstable
2+
//~^ ERROR default values for const generic parameters are experimental
33

44
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
error: default values for const generic parameters are unstable
2-
--> $DIR/default_trait_param.rs:1:30
1+
error[E0658]: default values for const generic parameters are experimental
2+
--> $DIR/default_trait_param.rs:1:28
33
|
44
LL | trait Foo<const KIND: bool = true> {}
5-
| ^^^^
5+
| ^^^^^^
66
|
7+
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
78
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
89

910
error: aborting due to previous error
1011

12+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
#[cfg(FALSE)]
12
struct A<const N: usize = 3>;
2-
//~^ ERROR default values for const generic parameters are unstable
3+
//~^ ERROR default values for const generic parameters are experimental
34

4-
fn foo<const N: u8 = 6>() {}
5-
//~^ ERROR default values for const generic parameters are unstable
5+
#[cfg(FALSE)]
6+
fn foo<const B: bool = false>() {}
7+
//~^ ERROR default values for const generic parameters are experimental
68

79
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
error: default values for const generic parameters are unstable
2-
--> $DIR/feature-gate-const_generics_defaults.rs:1:27
1+
error[E0658]: default values for const generic parameters are experimental
2+
--> $DIR/feature-gate-const_generics_defaults.rs:2:25
33
|
44
LL | struct A<const N: usize = 3>;
5-
| ^
5+
| ^^^
66
|
7+
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
78
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
89

9-
error: default values for const generic parameters are unstable
10-
--> $DIR/feature-gate-const_generics_defaults.rs:4:22
10+
error[E0658]: default values for const generic parameters are experimental
11+
--> $DIR/feature-gate-const_generics_defaults.rs:6:22
1112
|
12-
LL | fn foo<const N: u8 = 6>() {}
13-
| ^
13+
LL | fn foo<const B: bool = false>() {}
14+
| ^^^^^^^
1415
|
16+
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
1517
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
1618

1719
error: aborting due to 2 previous errors
1820

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

0 commit comments

Comments
 (0)