Skip to content

Commit a95e6bb

Browse files
committed
require type defaults to be after const generic parameters
as if this is currently possible. HA!
1 parent 9e92106 commit a95e6bb

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src/librustc_ast_passes/ast_validation.rs

Lines changed: 19 additions & 6 deletions
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 listing is currently not possible",
1136+
);
1137+
}
1138+
err.emit();
1139+
break;
1140+
}
11281141
}
11291142
}
11301143
}
Lines changed: 8 additions & 0 deletions
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() {}
Lines changed: 19 additions & 0 deletions
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 listing is currently not possible
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)