Skip to content

Commit 8a6b4d5

Browse files
committed
Mark all erroneous generic params as invariant
1 parent 238545e commit 8a6b4d5

14 files changed

+122
-70
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -1802,8 +1802,8 @@ fn receiver_is_implemented<'tcx>(
18021802
pub fn check_variances_for_type_defn<'tcx>(
18031803
tcx: TyCtxt<'tcx>,
18041804
item: LocalDefId,
1805-
variances: &[ty::Variance],
1806-
) -> Result<(), ErrorGuaranteed> {
1805+
variances: &'tcx [ty::Variance],
1806+
) -> Result<&'tcx [ty::Variance], ErrorGuaranteed> {
18071807
let identity_args = ty::GenericArgs::identity_for_item(tcx, item);
18081808

18091809
match tcx.def_kind(item) {
@@ -1819,7 +1819,7 @@ pub fn check_variances_for_type_defn<'tcx>(
18191819
);
18201820
tcx.type_of(item).skip_binder().error_reported()?;
18211821
}
1822-
_ => return Ok(()),
1822+
_ => return Ok(variances),
18231823
}
18241824

18251825
let ty_predicates = tcx.predicates_of(item);
@@ -1856,7 +1856,7 @@ pub fn check_variances_for_type_defn<'tcx>(
18561856

18571857
let ty_generics = tcx.generics_of(item);
18581858

1859-
let mut res = Ok(());
1859+
let mut variances = variances;
18601860

18611861
for (index, _) in variances.iter().enumerate() {
18621862
let parameter = Parameter(index as u32);
@@ -1880,18 +1880,20 @@ pub fn check_variances_for_type_defn<'tcx>(
18801880
hir_param.span,
18811881
"hir generics and ty generics in different order",
18821882
);
1883-
continue;
1884-
}
1885-
1886-
match hir_param.name {
1887-
hir::ParamName::Error => {}
1888-
_ => {
1889-
let has_explicit_bounds = explicitly_bounded_params.contains(&parameter);
1890-
res = Err(report_bivariance(tcx, hir_param, has_explicit_bounds, item));
1883+
} else {
1884+
match hir_param.name {
1885+
hir::ParamName::Error => {}
1886+
_ => {
1887+
let has_explicit_bounds = explicitly_bounded_params.contains(&parameter);
1888+
report_bivariance(tcx, hir_param, has_explicit_bounds, item);
1889+
}
18911890
}
1892-
}
1891+
};
1892+
let mut v = variances.to_vec();
1893+
v[index] = ty::Invariant;
1894+
variances = tcx.arena.alloc_slice(&v);
18931895
}
1894-
res
1896+
Ok(variances)
18951897
}
18961898

18971899
fn report_bivariance(

compiler/rustc_hir_analysis/src/variance/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] {
7272
}
7373
};
7474

75-
let _ = crate::check::wfcheck::check_variances_for_type_defn(tcx, item_def_id, variances);
76-
variances
75+
match crate::check::wfcheck::check_variances_for_type_defn(tcx, item_def_id, variances) {
76+
Ok(variances) => variances,
77+
Err(_) => tcx.arena.alloc_from_iter(variances.iter().map(|_| ty::Invariant)),
78+
}
7779
}
7880

7981
#[instrument(level = "trace", skip(tcx), ret)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct Fail<T>;
2+
//~^ ERROR: type parameter `T` is never used
3+
4+
impl Fail<i32> {
5+
const C: () = ();
6+
}
7+
8+
fn main() {
9+
Fail::<()>::C
10+
//~^ ERROR: no associated item named `C` found for struct `Fail<()>`
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0392]: type parameter `T` is never used
2+
--> $DIR/wrong-projection-self-ty-on-invalid-type.rs:1:13
3+
|
4+
LL | struct Fail<T>;
5+
| ^ unused type parameter
6+
|
7+
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
8+
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
9+
10+
error[E0599]: no associated item named `C` found for struct `Fail<()>` in the current scope
11+
--> $DIR/wrong-projection-self-ty-on-invalid-type.rs:9:17
12+
|
13+
LL | struct Fail<T>;
14+
| -------------- associated item `C` not found for this struct
15+
...
16+
LL | Fail::<()>::C
17+
| ^ associated item not found in `Fail<()>`
18+
|
19+
= note: the associated item was found for
20+
- `Fail<i32>`
21+
22+
error: aborting due to 2 previous errors
23+
24+
Some errors have detailed explanations: E0392, E0599.
25+
For more information about an error, try `rustc --explain E0392`.

tests/ui/inference/dont-collect-stmts-from-parent-body.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ fn main() {
88
impl<T> Type<T> {
99
fn new() -> Type<T> {
1010
Type
11-
//~^ ERROR type annotations needed
1211
}
1312
}
1413
};

tests/ui/inference/dont-collect-stmts-from-parent-body.stderr

+2-14
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ LL | struct Type<T>;
77
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
88
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
99

10-
error[E0282]: type annotations needed
11-
--> $DIR/dont-collect-stmts-from-parent-body.rs:10:17
12-
|
13-
LL | Type
14-
| ^^^^ cannot infer type of the type parameter `T` declared on the struct `Type`
15-
|
16-
help: consider specifying the generic argument
17-
|
18-
LL | Type::<T>
19-
| +++++
20-
21-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
2211

23-
Some errors have detailed explanations: E0282, E0392.
24-
For more information about an error, try `rustc --explain E0282`.
12+
For more information about this error, try `rustc --explain E0392`.

tests/ui/traits/trait-selection-ice-84727.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ where
2020
{
2121
fn over(self) -> Cell<NewFg> {
2222
//~^ ERROR mismatched types
23+
//~| ERROR `over` has an incompatible type for trait
2324
self.over();
2425
}
2526
}

tests/ui/traits/trait-selection-ice-84727.stderr

+29-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | Self: Over<Color<BottomBg>, Cell<NewFg>>,
1717
| ^^^^^ not found in this scope
1818

1919
error[E0412]: cannot find type `NewBg` in this scope
20-
--> $DIR/trait-selection-ice-84727.rs:32:27
20+
--> $DIR/trait-selection-ice-84727.rs:33:27
2121
|
2222
LL | fn over(self) -> Cell<NewBg> {
2323
| ^^^^^ not found in this scope
@@ -27,21 +27,45 @@ help: you might be missing a type parameter
2727
LL | impl<'b, TopFg, TopBg, BottomFg, BottomBg, NewBg> Over<&Cell<BottomFg, BottomBg>, ()>
2828
| +++++++
2929

30+
error[E0053]: method `over` has an incompatible type for trait
31+
--> $DIR/trait-selection-ice-84727.rs:21:22
32+
|
33+
LL | impl<TopFg, TopBg, BottomFg, BottomBg, NewFg, NewBg>
34+
| ----- ----- expected type parameter
35+
| |
36+
| found type parameter
37+
...
38+
LL | fn over(self) -> Cell<NewFg> {
39+
| ^^^^^^^^^^^
40+
| |
41+
| expected type parameter `NewBg`, found type parameter `NewFg`
42+
| help: change the output type to match the trait: `Cell<NewFg, NewBg>`
43+
|
44+
note: type in trait
45+
--> $DIR/trait-selection-ice-84727.rs:12:22
46+
|
47+
LL | fn over(self) -> Output;
48+
| ^^^^^^
49+
= note: expected signature `fn(Cell<_, _>) -> Cell<_, NewBg>`
50+
found signature `fn(Cell<_, _>) -> Cell<_, NewFg>`
51+
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
52+
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
53+
3054
error[E0308]: mismatched types
3155
--> $DIR/trait-selection-ice-84727.rs:21:22
3256
|
3357
LL | fn over(self) -> Cell<NewFg> {
3458
| ---- ^^^^^^^^^^^ expected `Cell<NewFg>`, found `()`
3559
| |
3660
| implicitly returns `()` as its body has no tail or `return` expression
37-
LL |
61+
...
3862
LL | self.over();
3963
| - help: remove this semicolon to return this value
4064
|
4165
= note: expected struct `Cell<NewFg>`
4266
found unit type `()`
4367

44-
error: aborting due to 5 previous errors
68+
error: aborting due to 6 previous errors
4569

46-
Some errors have detailed explanations: E0308, E0412.
47-
For more information about an error, try `rustc --explain E0308`.
70+
Some errors have detailed explanations: E0053, E0308, E0412.
71+
For more information about an error, try `rustc --explain E0053`.

tests/ui/variance/variance-regions-direct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct Test6<'a, 'b:'a> { //~ ERROR [+, o]
4949
// No uses at all is bivariant:
5050

5151
#[rustc_variance]
52-
struct Test7<'a> { //~ ERROR [*]
52+
struct Test7<'a> { //~ ERROR [o]
5353
//~^ ERROR: `'a` is never used
5454
x: isize
5555
}

tests/ui/variance/variance-regions-direct.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ error: [+, o]
3636
LL | struct Test6<'a, 'b:'a> {
3737
| ^^^^^^^^^^^^^^^^^^^^^^^
3838

39-
error: [*]
39+
error: [o]
4040
--> $DIR/variance-regions-direct.rs:52:1
4141
|
4242
LL | struct Test7<'a> {

tests/ui/variance/variance-regions-indirect.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@
55
#![feature(rustc_attrs)]
66

77
#[rustc_variance]
8-
enum Base<'a, 'b, 'c:'b, 'd> { //~ ERROR [-, +, o, *]
8+
enum Base<'a, 'b, 'c: 'b, 'd> {//~ ERROR [-, +, o, o]
99
//~^ ERROR: `'d` is never used
1010
Test8A(extern "Rust" fn(&'a isize)),
1111
Test8B(&'b [isize]),
1212
Test8C(&'b mut &'c str),
1313
}
1414

1515
#[rustc_variance]
16-
struct Derived1<'w, 'x:'y, 'y, 'z> { //~ ERROR [*, o, +, -]
16+
struct Derived1<'w, 'x: 'y, 'y, 'z> { //~ ERROR [o, o, +, -]
1717
//~^ ERROR: `'w` is never used
1818
f: Base<'z, 'y, 'x, 'w>
1919
}
2020

2121
#[rustc_variance] // Combine - and + to yield o
22-
struct Derived2<'a, 'b:'a, 'c> { //~ ERROR [o, o, *]
22+
struct Derived2<'a, 'b: 'a, 'c> { //~ ERROR [o, o, o]
2323
//~^ ERROR: `'c` is never used
2424
f: Base<'a, 'a, 'b, 'c>
2525
}
2626

2727
#[rustc_variance] // Combine + and o to yield o (just pay attention to 'a here)
28-
struct Derived3<'a:'b, 'b, 'c> { //~ ERROR [o, +, *]
28+
struct Derived3<'a: 'b, 'b, 'c> {//~ ERROR [o, +, o]
2929
//~^ ERROR: `'c` is never used
3030
f: Base<'a, 'b, 'a, 'c>
3131
}

tests/ui/variance/variance-regions-indirect.stderr

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
error[E0392]: lifetime parameter `'d` is never used
2-
--> $DIR/variance-regions-indirect.rs:8:26
2+
--> $DIR/variance-regions-indirect.rs:8:27
33
|
4-
LL | enum Base<'a, 'b, 'c:'b, 'd> {
5-
| ^^ unused lifetime parameter
4+
LL | enum Base<'a, 'b, 'c: 'b, 'd> {
5+
| ^^ unused lifetime parameter
66
|
77
= help: consider removing `'d`, referring to it in a field, or using a marker such as `PhantomData`
88

99
error[E0392]: lifetime parameter `'w` is never used
1010
--> $DIR/variance-regions-indirect.rs:16:17
1111
|
12-
LL | struct Derived1<'w, 'x:'y, 'y, 'z> {
12+
LL | struct Derived1<'w, 'x: 'y, 'y, 'z> {
1313
| ^^ unused lifetime parameter
1414
|
1515
= help: consider removing `'w`, referring to it in a field, or using a marker such as `PhantomData`
1616

1717
error[E0392]: lifetime parameter `'c` is never used
18-
--> $DIR/variance-regions-indirect.rs:22:28
18+
--> $DIR/variance-regions-indirect.rs:22:29
1919
|
20-
LL | struct Derived2<'a, 'b:'a, 'c> {
21-
| ^^ unused lifetime parameter
20+
LL | struct Derived2<'a, 'b: 'a, 'c> {
21+
| ^^ unused lifetime parameter
2222
|
2323
= help: consider removing `'c`, referring to it in a field, or using a marker such as `PhantomData`
2424

2525
error[E0392]: lifetime parameter `'c` is never used
26-
--> $DIR/variance-regions-indirect.rs:28:28
26+
--> $DIR/variance-regions-indirect.rs:28:29
2727
|
28-
LL | struct Derived3<'a:'b, 'b, 'c> {
29-
| ^^ unused lifetime parameter
28+
LL | struct Derived3<'a: 'b, 'b, 'c> {
29+
| ^^ unused lifetime parameter
3030
|
3131
= help: consider removing `'c`, referring to it in a field, or using a marker such as `PhantomData`
3232

33-
error: [-, +, o, *]
33+
error: [-, +, o, o]
3434
--> $DIR/variance-regions-indirect.rs:8:1
3535
|
36-
LL | enum Base<'a, 'b, 'c:'b, 'd> {
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
LL | enum Base<'a, 'b, 'c: 'b, 'd> {
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

39-
error: [*, o, +, -]
39+
error: [o, o, +, -]
4040
--> $DIR/variance-regions-indirect.rs:16:1
4141
|
42-
LL | struct Derived1<'w, 'x:'y, 'y, 'z> {
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
LL | struct Derived1<'w, 'x: 'y, 'y, 'z> {
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

45-
error: [o, o, *]
45+
error: [o, o, o]
4646
--> $DIR/variance-regions-indirect.rs:22:1
4747
|
48-
LL | struct Derived2<'a, 'b:'a, 'c> {
49-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
LL | struct Derived2<'a, 'b: 'a, 'c> {
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050

51-
error: [o, +, *]
51+
error: [o, +, o]
5252
--> $DIR/variance-regions-indirect.rs:28:1
5353
|
54-
LL | struct Derived3<'a:'b, 'b, 'c> {
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54+
LL | struct Derived3<'a: 'b, 'b, 'c> {
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5656

5757
error: [-, +, o]
5858
--> $DIR/variance-regions-indirect.rs:34:1

tests/ui/variance/variance-trait-bounds.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ struct TestStruct<U,T:Setter<U>> { //~ ERROR [+, +]
1818
}
1919

2020
#[rustc_variance]
21-
enum TestEnum<U,T:Setter<U>> { //~ ERROR [*, +]
21+
enum TestEnum<U,T:Setter<U>> { //~ ERROR [o, +]
2222
//~^ ERROR: `U` is never used
2323
Foo(T)
2424
}
2525

2626
#[rustc_variance]
27-
struct TestContraStruct<U,T:Setter<U>> { //~ ERROR [*, +]
27+
struct TestContraStruct<U,T:Setter<U>> { //~ ERROR [o, +]
2828
//~^ ERROR: `U` is never used
2929
t: T
3030
}
3131

3232
#[rustc_variance]
33-
struct TestBox<U,T:Getter<U>+Setter<U>> { //~ ERROR [*, +]
33+
struct TestBox<U,T:Getter<U>+Setter<U>> { //~ ERROR [o, +]
3434
//~^ ERROR: `U` is never used
3535
t: T
3636
}

tests/ui/variance/variance-trait-bounds.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ error: [+, +]
3131
LL | struct TestStruct<U,T:Setter<U>> {
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3333

34-
error: [*, +]
34+
error: [o, +]
3535
--> $DIR/variance-trait-bounds.rs:21:1
3636
|
3737
LL | enum TestEnum<U,T:Setter<U>> {
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3939

40-
error: [*, +]
40+
error: [o, +]
4141
--> $DIR/variance-trait-bounds.rs:27:1
4242
|
4343
LL | struct TestContraStruct<U,T:Setter<U>> {
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4545

46-
error: [*, +]
46+
error: [o, +]
4747
--> $DIR/variance-trait-bounds.rs:33:1
4848
|
4949
LL | struct TestBox<U,T:Getter<U>+Setter<U>> {

0 commit comments

Comments
 (0)