From f208f207d647336f453b1374e2b2f96fc0548f76 Mon Sep 17 00:00:00 2001 From: Ellen Date: Sat, 29 May 2021 03:54:32 +0100 Subject: [PATCH 1/4] Make lifetime ordering error pretty print const param defaults --- compiler/rustc_ast_passes/src/ast_validation.rs | 9 ++++++--- .../defaults/intermixed-lifetime.min.stderr | 4 ++-- .../defaults/param-order-err-pretty-prints-default.rs | 5 +++++ .../param-order-err-pretty-prints-default.stderr | 8 ++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.rs create mode 100644 src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.stderr diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index ba2da7694978d..30aa51a121a80 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -938,8 +938,11 @@ fn validate_generic_param_order( } GenericParamKind::Type { default: None } => (), GenericParamKind::Lifetime => (), - // FIXME(const_generics_defaults) - GenericParamKind::Const { ty: _, kw_span: _, default: _ } => (), + GenericParamKind::Const { ty: _, kw_span: _, default: Some(default) } => { + ordered_params += " = "; + ordered_params += &pprust::expr_to_string(&*default.value); + } + GenericParamKind::Const { ty: _, kw_span: _, default: None } => (), } first = false; } @@ -959,7 +962,7 @@ fn validate_generic_param_order( span, &format!( "reorder the parameters: lifetimes, {}", - if sess.features_untracked().const_generics { + if sess.features_untracked().unordered_const_ty_params() { "then consts and types" } else { "then types, then consts" diff --git a/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr b/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr index 985e7b655ece9..29d835e36c6eb 100644 --- a/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr +++ b/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr @@ -2,13 +2,13 @@ error: lifetime parameters must be declared prior to const parameters --> $DIR/intermixed-lifetime.rs:7:28 | LL | struct Foo(&'a (), T); - | -----------------^^---------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>` + | -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>` error: lifetime parameters must be declared prior to type parameters --> $DIR/intermixed-lifetime.rs:10:37 | LL | struct Bar(&'a (), T); - | --------------------------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>` + | --------------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>` error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.rs b/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.rs new file mode 100644 index 0000000000000..933eacb312dbf --- /dev/null +++ b/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.rs @@ -0,0 +1,5 @@ +#![feature(const_generics_defaults)] +struct Foo(&'a u32); +//~^ Error lifetime parameters must be declared prior to const parameters + +fn main() {} diff --git a/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.stderr b/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.stderr new file mode 100644 index 0000000000000..f50653fe9a19f --- /dev/null +++ b/src/test/ui/const-generics/defaults/param-order-err-pretty-prints-default.stderr @@ -0,0 +1,8 @@ +error: lifetime parameters must be declared prior to const parameters + --> $DIR/param-order-err-pretty-prints-default.rs:2:33 + | +LL | struct Foo(&'a u32); + | ----------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const M: usize = 10>` + +error: aborting due to previous error + From d75742b1eb42f0f5c1be42fc68a88c1444b4fb57 Mon Sep 17 00:00:00 2001 From: Ellen Date: Sat, 29 May 2021 05:37:45 +0100 Subject: [PATCH 2/4] Fix missing note on type mismatch error diagnostics --- compiler/rustc_middle/src/ty/diagnostics.rs | 2 +- src/test/ui/const-generics/defaults/mismatch.full.stderr | 8 +++++++- src/test/ui/const-generics/defaults/mismatch.min.stderr | 8 +++++++- src/test/ui/const-generics/defaults/mismatch.rs | 2 -- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 982c8a354b4ab..866df2958a02a 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -54,7 +54,7 @@ impl<'tcx> TyS<'tcx> { /// ADTs with no type arguments. pub fn is_simple_text(&self) -> bool { match self.kind() { - Adt(_, substs) => substs.types().next().is_none(), + Adt(_, substs) => substs.types().next().is_none() && substs.consts().next().is_none(), Ref(_, ty, _) => ty.is_simple_text(), _ => self.is_simple_ty(), } diff --git a/src/test/ui/const-generics/defaults/mismatch.full.stderr b/src/test/ui/const-generics/defaults/mismatch.full.stderr index be4f364d8ee62..741137afe4588 100644 --- a/src/test/ui/const-generics/defaults/mismatch.full.stderr +++ b/src/test/ui/const-generics/defaults/mismatch.full.stderr @@ -5,6 +5,9 @@ LL | let e: Example::<13> = (); | ------------- ^^ expected struct `Example`, found `()` | | | expected due to this + | + = note: expected struct `Example` + found unit type `()` error[E0308]: mismatched types --> $DIR/mismatch.rs:14:34 @@ -40,12 +43,15 @@ LL | let e: Example3::<7> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:22:28 + --> $DIR/mismatch.rs:20:28 | LL | let e: Example4::<7> = (); | ------------- ^^ expected struct `Example4`, found `()` | | | expected due to this + | + = note: expected struct `Example4<7_usize>` + found unit type `()` error: aborting due to 5 previous errors diff --git a/src/test/ui/const-generics/defaults/mismatch.min.stderr b/src/test/ui/const-generics/defaults/mismatch.min.stderr index be4f364d8ee62..741137afe4588 100644 --- a/src/test/ui/const-generics/defaults/mismatch.min.stderr +++ b/src/test/ui/const-generics/defaults/mismatch.min.stderr @@ -5,6 +5,9 @@ LL | let e: Example::<13> = (); | ------------- ^^ expected struct `Example`, found `()` | | | expected due to this + | + = note: expected struct `Example` + found unit type `()` error[E0308]: mismatched types --> $DIR/mismatch.rs:14:34 @@ -40,12 +43,15 @@ LL | let e: Example3::<7> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:22:28 + --> $DIR/mismatch.rs:20:28 | LL | let e: Example4::<7> = (); | ------------- ^^ expected struct `Example4`, found `()` | | | expected due to this + | + = note: expected struct `Example4<7_usize>` + found unit type `()` error: aborting due to 5 previous errors diff --git a/src/test/ui/const-generics/defaults/mismatch.rs b/src/test/ui/const-generics/defaults/mismatch.rs index 68a640c0a08b3..cfc8594f17cfa 100644 --- a/src/test/ui/const-generics/defaults/mismatch.rs +++ b/src/test/ui/const-generics/defaults/mismatch.rs @@ -17,8 +17,6 @@ fn main() { //~^ Error: mismatched types let e: Example3::<7> = (); //~^ Error: mismatched types - // FIXME(const_generics_defaults): There should be a note for the error below, but it is - // missing. let e: Example4::<7> = (); //~^ Error: mismatched types } From 21078c140fcb83c4232a8e7c6ad6707c3f10cdcb Mon Sep 17 00:00:00 2001 From: Ellen Date: Mon, 7 Jun 2021 10:09:47 +0100 Subject: [PATCH 3/4] note :sparkles: uwuuu --- src/test/ui/const-generics/defaults/mismatch.full.stderr | 8 ++++---- src/test/ui/const-generics/defaults/mismatch.min.stderr | 8 ++++---- src/test/ui/const-generics/defaults/mismatch.rs | 5 +++++ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/test/ui/const-generics/defaults/mismatch.full.stderr b/src/test/ui/const-generics/defaults/mismatch.full.stderr index 741137afe4588..4aa8401ab2216 100644 --- a/src/test/ui/const-generics/defaults/mismatch.full.stderr +++ b/src/test/ui/const-generics/defaults/mismatch.full.stderr @@ -10,7 +10,7 @@ LL | let e: Example::<13> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:14:34 + --> $DIR/mismatch.rs:15:34 | LL | let e: Example2:: = (); | ------------------- ^^ expected struct `Example2`, found `()` @@ -21,7 +21,7 @@ LL | let e: Example2:: = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:16:34 + --> $DIR/mismatch.rs:18:34 | LL | let e: Example3::<13, u32> = (); | ------------------- ^^ expected struct `Example3`, found `()` @@ -32,7 +32,7 @@ LL | let e: Example3::<13, u32> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:18:28 + --> $DIR/mismatch.rs:21:28 | LL | let e: Example3::<7> = (); | ------------- ^^ expected struct `Example3`, found `()` @@ -43,7 +43,7 @@ LL | let e: Example3::<7> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:20:28 + --> $DIR/mismatch.rs:24:28 | LL | let e: Example4::<7> = (); | ------------- ^^ expected struct `Example4`, found `()` diff --git a/src/test/ui/const-generics/defaults/mismatch.min.stderr b/src/test/ui/const-generics/defaults/mismatch.min.stderr index 741137afe4588..4aa8401ab2216 100644 --- a/src/test/ui/const-generics/defaults/mismatch.min.stderr +++ b/src/test/ui/const-generics/defaults/mismatch.min.stderr @@ -10,7 +10,7 @@ LL | let e: Example::<13> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:14:34 + --> $DIR/mismatch.rs:15:34 | LL | let e: Example2:: = (); | ------------------- ^^ expected struct `Example2`, found `()` @@ -21,7 +21,7 @@ LL | let e: Example2:: = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:16:34 + --> $DIR/mismatch.rs:18:34 | LL | let e: Example3::<13, u32> = (); | ------------------- ^^ expected struct `Example3`, found `()` @@ -32,7 +32,7 @@ LL | let e: Example3::<13, u32> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:18:28 + --> $DIR/mismatch.rs:21:28 | LL | let e: Example3::<7> = (); | ------------- ^^ expected struct `Example3`, found `()` @@ -43,7 +43,7 @@ LL | let e: Example3::<7> = (); found unit type `()` error[E0308]: mismatched types - --> $DIR/mismatch.rs:20:28 + --> $DIR/mismatch.rs:24:28 | LL | let e: Example4::<7> = (); | ------------- ^^ expected struct `Example4`, found `()` diff --git a/src/test/ui/const-generics/defaults/mismatch.rs b/src/test/ui/const-generics/defaults/mismatch.rs index cfc8594f17cfa..9d9a8793aaac0 100644 --- a/src/test/ui/const-generics/defaults/mismatch.rs +++ b/src/test/ui/const-generics/defaults/mismatch.rs @@ -11,12 +11,17 @@ pub struct Example4; fn main() { let e: Example::<13> = (); //~^ Error: mismatched types + //~| expected struct `Example` let e: Example2:: = (); //~^ Error: mismatched types + //~| expected struct `Example2` let e: Example3::<13, u32> = (); //~^ Error: mismatched types + //~| expected struct `Example3` let e: Example3::<7> = (); //~^ Error: mismatched types + //~| expected struct `Example3<7_usize>` let e: Example4::<7> = (); //~^ Error: mismatched types + //~| expected struct `Example4<7_usize>` } From 47fe696d8f247d91c67068c25bb91767713aeb60 Mon Sep 17 00:00:00 2001 From: Ellen Date: Tue, 8 Jun 2021 09:07:52 +0100 Subject: [PATCH 4/4] use non_erasable_generics --- compiler/rustc_middle/src/ty/diagnostics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 866df2958a02a..bfb4c0cb538de 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -54,7 +54,7 @@ impl<'tcx> TyS<'tcx> { /// ADTs with no type arguments. pub fn is_simple_text(&self) -> bool { match self.kind() { - Adt(_, substs) => substs.types().next().is_none() && substs.consts().next().is_none(), + Adt(_, substs) => substs.non_erasable_generics().next().is_none(), Ref(_, ty, _) => ty.is_simple_text(), _ => self.is_simple_ty(), }