Skip to content

Commit e9d4134

Browse files
authored
Rollup merge of #74654 - lcnr:default-no-more, r=varkor
require type defaults to be after const generic parameters From current discussions it seems like the goal here is for type and const parameters to be unordered and allow things like `struct Foo<const N: usize, T = u32>(T)` and `struct Foo<T, const N: usize = 7>` this way. Note: This means that using `min_const_generics` it will not be possible for an adt to have both type defaults and const parameters. closes #70471 r? @varkor @eddyb
2 parents 9f2ef3f + 2f56596 commit e9d4134

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src/librustc_ast_passes/ast_validation.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -1118,13 +1118,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11181118
fn visit_generics(&mut self, generics: &'a Generics) {
11191119
let mut prev_ty_default = None;
11201120
for param in &generics.params {
1121-
if let GenericParamKind::Type { ref default, .. } = param.kind {
1122-
if default.is_some() {
1121+
match param.kind {
1122+
GenericParamKind::Lifetime => (),
1123+
GenericParamKind::Type { default: Some(_), .. } => {
11231124
prev_ty_default = Some(param.ident.span);
1124-
} else if let Some(span) = prev_ty_default {
1125-
self.err_handler()
1126-
.span_err(span, "type parameters with a default must be trailing");
1127-
break;
1125+
}
1126+
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
1127+
if let Some(span) = prev_ty_default {
1128+
let mut err = self.err_handler().struct_span_err(
1129+
span,
1130+
"type parameters with a default must be trailing",
1131+
);
1132+
if matches!(param.kind, GenericParamKind::Const { .. }) {
1133+
err.note(
1134+
"using type defaults and const parameters \
1135+
in the same parameter list is currently not permitted",
1136+
);
1137+
}
1138+
err.emit();
1139+
break;
1140+
}
11281141
}
11291142
}
11301143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(const_generics)] //~ WARN the feature `const_generics` is incomplete
2+
3+
struct A<T = u32, const N: usize> {
4+
//~^ ERROR type parameters with a default must be trailing
5+
arg: T,
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: type parameters with a default must be trailing
2+
--> $DIR/wrong-order.rs:3:10
3+
|
4+
LL | struct A<T = u32, const N: usize> {
5+
| ^
6+
|
7+
= note: using type defaults and const parameters in the same parameter list is currently not permitted
8+
9+
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
10+
--> $DIR/wrong-order.rs:1:12
11+
|
12+
LL | #![feature(const_generics)]
13+
| ^^^^^^^^^^^^^^
14+
|
15+
= note: `#[warn(incomplete_features)]` on by default
16+
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
17+
18+
error: aborting due to previous error; 1 warning emitted
19+

0 commit comments

Comments
 (0)