From 8a6b4d524890aad20d6a0e2c64780cda20743024 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 7 Jun 2024 16:01:33 +0000 Subject: [PATCH] Mark all erroneous generic params as invariant --- .../rustc_hir_analysis/src/check/wfcheck.rs | 30 +++++++------ .../rustc_hir_analysis/src/variance/mod.rs | 6 ++- ...rong-projection-self-ty-on-invalid-type.rs | 11 +++++ ...-projection-self-ty-on-invalid-type.stderr | 25 +++++++++++ .../dont-collect-stmts-from-parent-body.rs | 1 - ...dont-collect-stmts-from-parent-body.stderr | 16 +------ tests/ui/traits/trait-selection-ice-84727.rs | 1 + .../traits/trait-selection-ice-84727.stderr | 34 +++++++++++--- tests/ui/variance/variance-regions-direct.rs | 2 +- .../variance/variance-regions-direct.stderr | 2 +- .../ui/variance/variance-regions-indirect.rs | 8 ++-- .../variance/variance-regions-indirect.stderr | 44 +++++++++---------- tests/ui/variance/variance-trait-bounds.rs | 6 +-- .../ui/variance/variance-trait-bounds.stderr | 6 +-- 14 files changed, 122 insertions(+), 70 deletions(-) create mode 100644 tests/ui/associated-consts/wrong-projection-self-ty-on-invalid-type.rs create mode 100644 tests/ui/associated-consts/wrong-projection-self-ty-on-invalid-type.stderr diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 0f07e21545437..87d689614fbef 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -1802,8 +1802,8 @@ fn receiver_is_implemented<'tcx>( pub fn check_variances_for_type_defn<'tcx>( tcx: TyCtxt<'tcx>, item: LocalDefId, - variances: &[ty::Variance], -) -> Result<(), ErrorGuaranteed> { + variances: &'tcx [ty::Variance], +) -> Result<&'tcx [ty::Variance], ErrorGuaranteed> { let identity_args = ty::GenericArgs::identity_for_item(tcx, item); match tcx.def_kind(item) { @@ -1819,7 +1819,7 @@ pub fn check_variances_for_type_defn<'tcx>( ); tcx.type_of(item).skip_binder().error_reported()?; } - _ => return Ok(()), + _ => return Ok(variances), } let ty_predicates = tcx.predicates_of(item); @@ -1856,7 +1856,7 @@ pub fn check_variances_for_type_defn<'tcx>( let ty_generics = tcx.generics_of(item); - let mut res = Ok(()); + let mut variances = variances; for (index, _) in variances.iter().enumerate() { let parameter = Parameter(index as u32); @@ -1880,18 +1880,20 @@ pub fn check_variances_for_type_defn<'tcx>( hir_param.span, "hir generics and ty generics in different order", ); - continue; - } - - match hir_param.name { - hir::ParamName::Error => {} - _ => { - let has_explicit_bounds = explicitly_bounded_params.contains(¶meter); - res = Err(report_bivariance(tcx, hir_param, has_explicit_bounds, item)); + } else { + match hir_param.name { + hir::ParamName::Error => {} + _ => { + let has_explicit_bounds = explicitly_bounded_params.contains(¶meter); + report_bivariance(tcx, hir_param, has_explicit_bounds, item); + } } - } + }; + let mut v = variances.to_vec(); + v[index] = ty::Invariant; + variances = tcx.arena.alloc_slice(&v); } - res + Ok(variances) } fn report_bivariance( diff --git a/compiler/rustc_hir_analysis/src/variance/mod.rs b/compiler/rustc_hir_analysis/src/variance/mod.rs index 043fea52d1e3e..7de7f353b70ec 100644 --- a/compiler/rustc_hir_analysis/src/variance/mod.rs +++ b/compiler/rustc_hir_analysis/src/variance/mod.rs @@ -72,8 +72,10 @@ fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] { } }; - let _ = crate::check::wfcheck::check_variances_for_type_defn(tcx, item_def_id, variances); - variances + match crate::check::wfcheck::check_variances_for_type_defn(tcx, item_def_id, variances) { + Ok(variances) => variances, + Err(_) => tcx.arena.alloc_from_iter(variances.iter().map(|_| ty::Invariant)), + } } #[instrument(level = "trace", skip(tcx), ret)] diff --git a/tests/ui/associated-consts/wrong-projection-self-ty-on-invalid-type.rs b/tests/ui/associated-consts/wrong-projection-self-ty-on-invalid-type.rs new file mode 100644 index 0000000000000..5a1eeaa0a85ed --- /dev/null +++ b/tests/ui/associated-consts/wrong-projection-self-ty-on-invalid-type.rs @@ -0,0 +1,11 @@ +struct Fail; +//~^ ERROR: type parameter `T` is never used + +impl Fail { + const C: () = (); +} + +fn main() { + Fail::<()>::C + //~^ ERROR: no associated item named `C` found for struct `Fail<()>` +} diff --git a/tests/ui/associated-consts/wrong-projection-self-ty-on-invalid-type.stderr b/tests/ui/associated-consts/wrong-projection-self-ty-on-invalid-type.stderr new file mode 100644 index 0000000000000..cd97efeafffaa --- /dev/null +++ b/tests/ui/associated-consts/wrong-projection-self-ty-on-invalid-type.stderr @@ -0,0 +1,25 @@ +error[E0392]: type parameter `T` is never used + --> $DIR/wrong-projection-self-ty-on-invalid-type.rs:1:13 + | +LL | struct Fail; + | ^ unused type parameter + | + = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` + = help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead + +error[E0599]: no associated item named `C` found for struct `Fail<()>` in the current scope + --> $DIR/wrong-projection-self-ty-on-invalid-type.rs:9:17 + | +LL | struct Fail; + | -------------- associated item `C` not found for this struct +... +LL | Fail::<()>::C + | ^ associated item not found in `Fail<()>` + | + = note: the associated item was found for + - `Fail` + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0392, E0599. +For more information about an error, try `rustc --explain E0392`. diff --git a/tests/ui/inference/dont-collect-stmts-from-parent-body.rs b/tests/ui/inference/dont-collect-stmts-from-parent-body.rs index 635fe74b8679d..752c595bb9974 100644 --- a/tests/ui/inference/dont-collect-stmts-from-parent-body.rs +++ b/tests/ui/inference/dont-collect-stmts-from-parent-body.rs @@ -8,7 +8,6 @@ fn main() { impl Type { fn new() -> Type { Type - //~^ ERROR type annotations needed } } }; diff --git a/tests/ui/inference/dont-collect-stmts-from-parent-body.stderr b/tests/ui/inference/dont-collect-stmts-from-parent-body.stderr index f82527273fbee..543b834332ca3 100644 --- a/tests/ui/inference/dont-collect-stmts-from-parent-body.stderr +++ b/tests/ui/inference/dont-collect-stmts-from-parent-body.stderr @@ -7,18 +7,6 @@ LL | struct Type; = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData` = help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead -error[E0282]: type annotations needed - --> $DIR/dont-collect-stmts-from-parent-body.rs:10:17 - | -LL | Type - | ^^^^ cannot infer type of the type parameter `T` declared on the struct `Type` - | -help: consider specifying the generic argument - | -LL | Type:: - | +++++ - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error -Some errors have detailed explanations: E0282, E0392. -For more information about an error, try `rustc --explain E0282`. +For more information about this error, try `rustc --explain E0392`. diff --git a/tests/ui/traits/trait-selection-ice-84727.rs b/tests/ui/traits/trait-selection-ice-84727.rs index 08a282892a59c..b7482753be4d5 100644 --- a/tests/ui/traits/trait-selection-ice-84727.rs +++ b/tests/ui/traits/trait-selection-ice-84727.rs @@ -20,6 +20,7 @@ where { fn over(self) -> Cell { //~^ ERROR mismatched types + //~| ERROR `over` has an incompatible type for trait self.over(); } } diff --git a/tests/ui/traits/trait-selection-ice-84727.stderr b/tests/ui/traits/trait-selection-ice-84727.stderr index d4bc4163897c4..83f3b7ad95472 100644 --- a/tests/ui/traits/trait-selection-ice-84727.stderr +++ b/tests/ui/traits/trait-selection-ice-84727.stderr @@ -17,7 +17,7 @@ LL | Self: Over, Cell>, | ^^^^^ not found in this scope error[E0412]: cannot find type `NewBg` in this scope - --> $DIR/trait-selection-ice-84727.rs:32:27 + --> $DIR/trait-selection-ice-84727.rs:33:27 | LL | fn over(self) -> Cell { | ^^^^^ not found in this scope @@ -27,6 +27,30 @@ help: you might be missing a type parameter LL | impl<'b, TopFg, TopBg, BottomFg, BottomBg, NewBg> Over<&Cell, ()> | +++++++ +error[E0053]: method `over` has an incompatible type for trait + --> $DIR/trait-selection-ice-84727.rs:21:22 + | +LL | impl + | ----- ----- expected type parameter + | | + | found type parameter +... +LL | fn over(self) -> Cell { + | ^^^^^^^^^^^ + | | + | expected type parameter `NewBg`, found type parameter `NewFg` + | help: change the output type to match the trait: `Cell` + | +note: type in trait + --> $DIR/trait-selection-ice-84727.rs:12:22 + | +LL | fn over(self) -> Output; + | ^^^^^^ + = note: expected signature `fn(Cell<_, _>) -> Cell<_, NewBg>` + found signature `fn(Cell<_, _>) -> Cell<_, NewFg>` + = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound + = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters + error[E0308]: mismatched types --> $DIR/trait-selection-ice-84727.rs:21:22 | @@ -34,14 +58,14 @@ LL | fn over(self) -> Cell { | ---- ^^^^^^^^^^^ expected `Cell`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression -LL | +... LL | self.over(); | - help: remove this semicolon to return this value | = note: expected struct `Cell` found unit type `()` -error: aborting due to 5 previous errors +error: aborting due to 6 previous errors -Some errors have detailed explanations: E0308, E0412. -For more information about an error, try `rustc --explain E0308`. +Some errors have detailed explanations: E0053, E0308, E0412. +For more information about an error, try `rustc --explain E0053`. diff --git a/tests/ui/variance/variance-regions-direct.rs b/tests/ui/variance/variance-regions-direct.rs index f1763c403f1a1..53167ecba94b7 100644 --- a/tests/ui/variance/variance-regions-direct.rs +++ b/tests/ui/variance/variance-regions-direct.rs @@ -49,7 +49,7 @@ struct Test6<'a, 'b:'a> { //~ ERROR [+, o] // No uses at all is bivariant: #[rustc_variance] -struct Test7<'a> { //~ ERROR [*] +struct Test7<'a> { //~ ERROR [o] //~^ ERROR: `'a` is never used x: isize } diff --git a/tests/ui/variance/variance-regions-direct.stderr b/tests/ui/variance/variance-regions-direct.stderr index edfc888f65667..8d7655ea113a9 100644 --- a/tests/ui/variance/variance-regions-direct.stderr +++ b/tests/ui/variance/variance-regions-direct.stderr @@ -36,7 +36,7 @@ error: [+, o] LL | struct Test6<'a, 'b:'a> { | ^^^^^^^^^^^^^^^^^^^^^^^ -error: [*] +error: [o] --> $DIR/variance-regions-direct.rs:52:1 | LL | struct Test7<'a> { diff --git a/tests/ui/variance/variance-regions-indirect.rs b/tests/ui/variance/variance-regions-indirect.rs index 31e25641d8c3a..04ae83c2503f2 100644 --- a/tests/ui/variance/variance-regions-indirect.rs +++ b/tests/ui/variance/variance-regions-indirect.rs @@ -5,7 +5,7 @@ #![feature(rustc_attrs)] #[rustc_variance] -enum Base<'a, 'b, 'c:'b, 'd> { //~ ERROR [-, +, o, *] +enum Base<'a, 'b, 'c: 'b, 'd> {//~ ERROR [-, +, o, o] //~^ ERROR: `'d` is never used Test8A(extern "Rust" fn(&'a isize)), Test8B(&'b [isize]), @@ -13,19 +13,19 @@ enum Base<'a, 'b, 'c:'b, 'd> { //~ ERROR [-, +, o, *] } #[rustc_variance] -struct Derived1<'w, 'x:'y, 'y, 'z> { //~ ERROR [*, o, +, -] +struct Derived1<'w, 'x: 'y, 'y, 'z> { //~ ERROR [o, o, +, -] //~^ ERROR: `'w` is never used f: Base<'z, 'y, 'x, 'w> } #[rustc_variance] // Combine - and + to yield o -struct Derived2<'a, 'b:'a, 'c> { //~ ERROR [o, o, *] +struct Derived2<'a, 'b: 'a, 'c> { //~ ERROR [o, o, o] //~^ ERROR: `'c` is never used f: Base<'a, 'a, 'b, 'c> } #[rustc_variance] // Combine + and o to yield o (just pay attention to 'a here) -struct Derived3<'a:'b, 'b, 'c> { //~ ERROR [o, +, *] +struct Derived3<'a: 'b, 'b, 'c> {//~ ERROR [o, +, o] //~^ ERROR: `'c` is never used f: Base<'a, 'b, 'a, 'c> } diff --git a/tests/ui/variance/variance-regions-indirect.stderr b/tests/ui/variance/variance-regions-indirect.stderr index 901ec0c6a762b..c626e6ca34c44 100644 --- a/tests/ui/variance/variance-regions-indirect.stderr +++ b/tests/ui/variance/variance-regions-indirect.stderr @@ -1,58 +1,58 @@ error[E0392]: lifetime parameter `'d` is never used - --> $DIR/variance-regions-indirect.rs:8:26 + --> $DIR/variance-regions-indirect.rs:8:27 | -LL | enum Base<'a, 'b, 'c:'b, 'd> { - | ^^ unused lifetime parameter +LL | enum Base<'a, 'b, 'c: 'b, 'd> { + | ^^ unused lifetime parameter | = help: consider removing `'d`, referring to it in a field, or using a marker such as `PhantomData` error[E0392]: lifetime parameter `'w` is never used --> $DIR/variance-regions-indirect.rs:16:17 | -LL | struct Derived1<'w, 'x:'y, 'y, 'z> { +LL | struct Derived1<'w, 'x: 'y, 'y, 'z> { | ^^ unused lifetime parameter | = help: consider removing `'w`, referring to it in a field, or using a marker such as `PhantomData` error[E0392]: lifetime parameter `'c` is never used - --> $DIR/variance-regions-indirect.rs:22:28 + --> $DIR/variance-regions-indirect.rs:22:29 | -LL | struct Derived2<'a, 'b:'a, 'c> { - | ^^ unused lifetime parameter +LL | struct Derived2<'a, 'b: 'a, 'c> { + | ^^ unused lifetime parameter | = help: consider removing `'c`, referring to it in a field, or using a marker such as `PhantomData` error[E0392]: lifetime parameter `'c` is never used - --> $DIR/variance-regions-indirect.rs:28:28 + --> $DIR/variance-regions-indirect.rs:28:29 | -LL | struct Derived3<'a:'b, 'b, 'c> { - | ^^ unused lifetime parameter +LL | struct Derived3<'a: 'b, 'b, 'c> { + | ^^ unused lifetime parameter | = help: consider removing `'c`, referring to it in a field, or using a marker such as `PhantomData` -error: [-, +, o, *] +error: [-, +, o, o] --> $DIR/variance-regions-indirect.rs:8:1 | -LL | enum Base<'a, 'b, 'c:'b, 'd> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | enum Base<'a, 'b, 'c: 'b, 'd> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: [*, o, +, -] +error: [o, o, +, -] --> $DIR/variance-regions-indirect.rs:16:1 | -LL | struct Derived1<'w, 'x:'y, 'y, 'z> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | struct Derived1<'w, 'x: 'y, 'y, 'z> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: [o, o, *] +error: [o, o, o] --> $DIR/variance-regions-indirect.rs:22:1 | -LL | struct Derived2<'a, 'b:'a, 'c> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | struct Derived2<'a, 'b: 'a, 'c> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: [o, +, *] +error: [o, +, o] --> $DIR/variance-regions-indirect.rs:28:1 | -LL | struct Derived3<'a:'b, 'b, 'c> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | struct Derived3<'a: 'b, 'b, 'c> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [-, +, o] --> $DIR/variance-regions-indirect.rs:34:1 diff --git a/tests/ui/variance/variance-trait-bounds.rs b/tests/ui/variance/variance-trait-bounds.rs index 25a01b160ddd1..665820325d652 100644 --- a/tests/ui/variance/variance-trait-bounds.rs +++ b/tests/ui/variance/variance-trait-bounds.rs @@ -18,19 +18,19 @@ struct TestStruct> { //~ ERROR [+, +] } #[rustc_variance] -enum TestEnum> { //~ ERROR [*, +] +enum TestEnum> { //~ ERROR [o, +] //~^ ERROR: `U` is never used Foo(T) } #[rustc_variance] -struct TestContraStruct> { //~ ERROR [*, +] +struct TestContraStruct> { //~ ERROR [o, +] //~^ ERROR: `U` is never used t: T } #[rustc_variance] -struct TestBox+Setter> { //~ ERROR [*, +] +struct TestBox+Setter> { //~ ERROR [o, +] //~^ ERROR: `U` is never used t: T } diff --git a/tests/ui/variance/variance-trait-bounds.stderr b/tests/ui/variance/variance-trait-bounds.stderr index 95ed18c1ad2bf..028a32e22f625 100644 --- a/tests/ui/variance/variance-trait-bounds.stderr +++ b/tests/ui/variance/variance-trait-bounds.stderr @@ -31,19 +31,19 @@ error: [+, +] LL | struct TestStruct> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: [*, +] +error: [o, +] --> $DIR/variance-trait-bounds.rs:21:1 | LL | enum TestEnum> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: [*, +] +error: [o, +] --> $DIR/variance-trait-bounds.rs:27:1 | LL | struct TestContraStruct> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: [*, +] +error: [o, +] --> $DIR/variance-trait-bounds.rs:33:1 | LL | struct TestBox+Setter> {