diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2020-07-23 00:42:18 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-23 00:42:18 -0700 |
| commit | e9d41344673d5f22f3ea82b9f30595017e317e5b (patch) | |
| tree | 618083433907394f4f0f29a1ddd78c5b50728fb3 | |
| parent | 9f2ef3f62d445a5871745ea02297644ca249a7b1 (diff) | |
| parent | 2f565967b056335659ff1dfc2108c746f4ac8af3 (diff) | |
| download | rust-e9d41344673d5f22f3ea82b9f30595017e317e5b.tar.gz rust-e9d41344673d5f22f3ea82b9f30595017e317e5b.zip | |
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
| -rw-r--r-- | src/librustc_ast_passes/ast_validation.rs | 25 | ||||
| -rw-r--r-- | src/test/ui/const-generics/defaults/wrong-order.rs | 8 | ||||
| -rw-r--r-- | src/test/ui/const-generics/defaults/wrong-order.stderr | 19 |
3 files changed, 46 insertions, 6 deletions
diff --git a/src/librustc_ast_passes/ast_validation.rs b/src/librustc_ast_passes/ast_validation.rs index 975881d9a0a..daf3e23d6a1 100644 --- a/src/librustc_ast_passes/ast_validation.rs +++ b/src/librustc_ast_passes/ast_validation.rs @@ -1118,13 +1118,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> { fn visit_generics(&mut self, generics: &'a Generics) { let mut prev_ty_default = None; for param in &generics.params { - if let GenericParamKind::Type { ref default, .. } = param.kind { - if default.is_some() { + match param.kind { + GenericParamKind::Lifetime => (), + GenericParamKind::Type { default: Some(_), .. } => { prev_ty_default = Some(param.ident.span); - } else if let Some(span) = prev_ty_default { - self.err_handler() - .span_err(span, "type parameters with a default must be trailing"); - break; + } + GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => { + if let Some(span) = prev_ty_default { + let mut err = self.err_handler().struct_span_err( + span, + "type parameters with a default must be trailing", + ); + if matches!(param.kind, GenericParamKind::Const { .. }) { + err.note( + "using type defaults and const parameters \ + in the same parameter list is currently not permitted", + ); + } + err.emit(); + break; + } } } } diff --git a/src/test/ui/const-generics/defaults/wrong-order.rs b/src/test/ui/const-generics/defaults/wrong-order.rs new file mode 100644 index 00000000000..7f17c6358b7 --- /dev/null +++ b/src/test/ui/const-generics/defaults/wrong-order.rs @@ -0,0 +1,8 @@ +#![feature(const_generics)] //~ WARN the feature `const_generics` is incomplete + +struct A<T = u32, const N: usize> { + //~^ ERROR type parameters with a default must be trailing + arg: T, +} + +fn main() {} diff --git a/src/test/ui/const-generics/defaults/wrong-order.stderr b/src/test/ui/const-generics/defaults/wrong-order.stderr new file mode 100644 index 00000000000..283f6656121 --- /dev/null +++ b/src/test/ui/const-generics/defaults/wrong-order.stderr @@ -0,0 +1,19 @@ +error: type parameters with a default must be trailing + --> $DIR/wrong-order.rs:3:10 + | +LL | struct A<T = u32, const N: usize> { + | ^ + | + = note: using type defaults and const parameters in the same parameter list is currently not permitted + +warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/wrong-order.rs:1:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + = note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information + +error: aborting due to previous error; 1 warning emitted + |
