Skip to content

Commit ee8fa4e

Browse files
committed
Check variances in the non-hir wfchecker
1 parent d27c057 commit ee8fa4e

12 files changed

+105
-78
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use {rustc_attr_data_structures as attrs, rustc_hir as hir};
3636

3737
use super::compare_impl_item::check_type_bounds;
3838
use super::*;
39+
use crate::check::wfcheck::check_variances_for_type_defn;
3940

4041
fn add_abi_diag_help<T: EmissionGuarantee>(abi: ExternAbi, diag: &mut Diag<'_, T>) {
4142
if let ExternAbi::Cdecl { unwind } = abi {
@@ -762,6 +763,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
762763
DefKind::Const => {}
763764
DefKind::Enum => {
764765
check_enum(tcx, def_id);
766+
check_variances_for_type_defn(tcx, def_id);
765767
}
766768
DefKind::Fn => {
767769
if let Some(i) = tcx.intrinsic(def_id) {
@@ -802,9 +804,11 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
802804
}
803805
DefKind::Struct => {
804806
check_struct(tcx, def_id);
807+
check_variances_for_type_defn(tcx, def_id);
805808
}
806809
DefKind::Union => {
807810
check_union(tcx, def_id);
811+
check_variances_for_type_defn(tcx, def_id);
808812
}
809813
DefKind::OpaqueTy => {
810814
check_opaque_precise_captures(tcx, def_id);
@@ -832,6 +836,9 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
832836
}
833837
DefKind::TyAlias => {
834838
check_type_alias_type_params_are_used(tcx, def_id);
839+
if tcx.type_alias_is_lazy(def_id) {
840+
check_variances_for_type_defn(tcx, def_id);
841+
}
835842
}
836843
DefKind::ForeignMod => {
837844
let it = tcx.hir_expect_item(def_id);

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -292,25 +292,13 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
292292
}
293293
hir::ItemKind::Fn { ident, sig, .. } => check_item_fn(tcx, def_id, ident, sig.decl),
294294
hir::ItemKind::Const(_, _, ty, _) => check_const_item(tcx, def_id, ty.span, item.span),
295-
hir::ItemKind::Struct(..) => {
296-
let res = check_type_defn(tcx, item, false);
297-
check_variances_for_type_defn(tcx, def_id);
298-
res
299-
}
300-
hir::ItemKind::Union(..) => {
301-
let res = check_type_defn(tcx, item, true);
302-
check_variances_for_type_defn(tcx, def_id);
303-
res
304-
}
305-
hir::ItemKind::Enum(..) => {
306-
let res = check_type_defn(tcx, item, true);
307-
check_variances_for_type_defn(tcx, def_id);
308-
res
309-
}
295+
hir::ItemKind::Struct(..) => check_type_defn(tcx, item, false),
296+
hir::ItemKind::Union(..) => check_type_defn(tcx, item, true),
297+
hir::ItemKind::Enum(..) => check_type_defn(tcx, item, true),
310298
hir::ItemKind::Trait(..) => check_trait(tcx, item),
311299
hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
312300
hir::ItemKind::TyAlias(.., hir_ty) if tcx.type_alias_is_lazy(item.owner_id) => {
313-
let res = enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
301+
enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
314302
let ty = tcx.type_of(def_id).instantiate_identity();
315303
let item_ty =
316304
wfcx.deeply_normalize(hir_ty.span, Some(WellFormedLoc::Ty(def_id)), ty);
@@ -321,9 +309,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
321309
);
322310
check_where_clauses(wfcx, item.span, def_id);
323311
Ok(())
324-
});
325-
check_variances_for_type_defn(tcx, def_id);
326-
res
312+
})
327313
}
328314
_ => Ok(()),
329315
}
@@ -1977,7 +1963,7 @@ fn legacy_receiver_is_implemented<'tcx>(
19771963
}
19781964
}
19791965

1980-
fn check_variances_for_type_defn<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
1966+
pub(super) fn check_variances_for_type_defn<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
19811967
match tcx.def_kind(def_id) {
19821968
DefKind::Enum | DefKind::Struct | DefKind::Union => {
19831969
// Ok

tests/ui/generic-associated-types/static-lifetime-tip-with-default-type.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ help: consider adding an explicit lifetime bound
8282
LL | struct Far<T: 'static
8383
| +++++++++
8484

85+
error[E0392]: lifetime parameter `'a` is never used
86+
--> $DIR/static-lifetime-tip-with-default-type.rs:22:10
87+
|
88+
LL | struct S<'a, K: 'a = i32>(&'static K);
89+
| ^^ unused lifetime parameter
90+
|
91+
= help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData`
92+
8593
error[E0310]: the parameter type `K` may not live long enough
8694
--> $DIR/static-lifetime-tip-with-default-type.rs:22:27
8795
|
@@ -96,14 +104,6 @@ help: consider adding an explicit lifetime bound
96104
LL | struct S<'a, K: 'a + 'static = i32>(&'static K);
97105
| +++++++++
98106

99-
error[E0392]: lifetime parameter `'a` is never used
100-
--> $DIR/static-lifetime-tip-with-default-type.rs:22:10
101-
|
102-
LL | struct S<'a, K: 'a = i32>(&'static K);
103-
| ^^ unused lifetime parameter
104-
|
105-
= help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData`
106-
107107
error: aborting due to 8 previous errors
108108

109109
Some errors have detailed explanations: E0310, E0392.

tests/ui/lazy-type-alias/inherent-impls-overflow.current.stderr

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ LL | impl Loop {}
1414
|
1515
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
1616

17+
error: type parameter `T` is only used recursively
18+
--> $DIR/inherent-impls-overflow.rs:17:24
19+
|
20+
LL | type Poly0<T> = Poly1<(T,)>;
21+
| - ^
22+
| |
23+
| type parameter must be used non-recursively in the definition
24+
|
25+
= help: consider removing `T` or referring to it in the body of the type alias
26+
= note: all type parameters must be used in a non-recursive way in order to constrain their variance
27+
1728
error[E0275]: overflow normalizing the type alias `Poly0<(((((((...,),),),),),),)>`
1829
--> $DIR/inherent-impls-overflow.rs:17:17
1930
|
@@ -22,6 +33,17 @@ LL | type Poly0<T> = Poly1<(T,)>;
2233
|
2334
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
2435

36+
error: type parameter `T` is only used recursively
37+
--> $DIR/inherent-impls-overflow.rs:21:24
38+
|
39+
LL | type Poly1<T> = Poly0<(T,)>;
40+
| - ^
41+
| |
42+
| type parameter must be used non-recursively in the definition
43+
|
44+
= help: consider removing `T` or referring to it in the body of the type alias
45+
= note: all type parameters must be used in a non-recursive way in order to constrain their variance
46+
2547
error[E0275]: overflow normalizing the type alias `Poly1<(((((((...,),),),),),),)>`
2648
--> $DIR/inherent-impls-overflow.rs:21:17
2749
|
@@ -38,6 +60,6 @@ LL | impl Poly0<()> {}
3860
|
3961
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
4062

41-
error: aborting due to 5 previous errors
63+
error: aborting due to 7 previous errors
4264

4365
For more information about this error, try `rustc --explain E0275`.

tests/ui/lazy-type-alias/inherent-impls-overflow.next.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ error[E0271]: type mismatch resolving `Loop normalizes-to _`
1616
LL | impl Loop {}
1717
| ^^^^ types differ
1818

19-
error[E0275]: overflow evaluating the requirement `Poly1<(T,)> == _`
20-
--> $DIR/inherent-impls-overflow.rs:17:17
21-
|
22-
LL | type Poly0<T> = Poly1<(T,)>;
23-
| ^^^^^^^^^^^
24-
|
25-
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`inherent_impls_overflow`)
26-
2719
error: type parameter `T` is only used recursively
2820
--> $DIR/inherent-impls-overflow.rs:17:24
2921
|
@@ -35,10 +27,10 @@ LL | type Poly0<T> = Poly1<(T,)>;
3527
= help: consider removing `T` or referring to it in the body of the type alias
3628
= note: all type parameters must be used in a non-recursive way in order to constrain their variance
3729

38-
error[E0275]: overflow evaluating the requirement `Poly0<(T,)> == _`
39-
--> $DIR/inherent-impls-overflow.rs:21:17
30+
error[E0275]: overflow evaluating the requirement `Poly1<(T,)> == _`
31+
--> $DIR/inherent-impls-overflow.rs:17:17
4032
|
41-
LL | type Poly1<T> = Poly0<(T,)>;
33+
LL | type Poly0<T> = Poly1<(T,)>;
4234
| ^^^^^^^^^^^
4335
|
4436
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`inherent_impls_overflow`)
@@ -54,6 +46,14 @@ LL | type Poly1<T> = Poly0<(T,)>;
5446
= help: consider removing `T` or referring to it in the body of the type alias
5547
= note: all type parameters must be used in a non-recursive way in order to constrain their variance
5648

49+
error[E0275]: overflow evaluating the requirement `Poly0<(T,)> == _`
50+
--> $DIR/inherent-impls-overflow.rs:21:17
51+
|
52+
LL | type Poly1<T> = Poly0<(T,)>;
53+
| ^^^^^^^^^^^
54+
|
55+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`inherent_impls_overflow`)
56+
5757
error[E0275]: overflow evaluating the requirement `Poly0<()> == _`
5858
--> $DIR/inherent-impls-overflow.rs:26:1
5959
|

tests/ui/lazy-type-alias/inherent-impls-overflow.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ impl Loop {}
1616

1717
type Poly0<T> = Poly1<(T,)>;
1818
//[current]~^ ERROR overflow normalizing the type alias `Poly0<(((((((...,),),),),),),)>`
19-
//[next]~^^ ERROR type parameter `T` is only used recursively
20-
//[next]~| ERROR overflow evaluating the requirement
19+
//~^^ ERROR type parameter `T` is only used recursively
20+
//[next]~^^^ ERROR overflow evaluating the requirement
2121
type Poly1<T> = Poly0<(T,)>;
2222
//[current]~^ ERROR overflow normalizing the type alias `Poly1<(((((((...,),),),),),),)>`
23-
//[next]~^^ ERROR type parameter `T` is only used recursively
24-
//[next]~| ERROR overflow evaluating the requirement
23+
//~^^ ERROR type parameter `T` is only used recursively
24+
//[next]~^^^ ERROR overflow evaluating the requirement
2525

2626
impl Poly0<()> {}
2727
//[current]~^ ERROR overflow normalizing the type alias `Poly1<(((((((...,),),),),),),)>`

tests/ui/lazy-type-alias/unconstrained-params-in-impl-due-to-overflow.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
impl<T> Loop<T> {} //~ ERROR the type parameter `T` is not constrained
55

66
type Loop<T> = Loop<T>; //~ ERROR overflow
7+
//~^ ERROR: `T` is only used recursively
78

89
fn main() {}

tests/ui/lazy-type-alias/unconstrained-params-in-impl-due-to-overflow.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
44
LL | impl<T> Loop<T> {}
55
| ^ unconstrained type parameter
66

7+
error: type parameter `T` is only used recursively
8+
--> $DIR/unconstrained-params-in-impl-due-to-overflow.rs:6:21
9+
|
10+
LL | type Loop<T> = Loop<T>;
11+
| - ^
12+
| |
13+
| type parameter must be used non-recursively in the definition
14+
|
15+
= help: consider removing `T` or referring to it in the body of the type alias
16+
= note: all type parameters must be used in a non-recursive way in order to constrain their variance
17+
718
error[E0275]: overflow normalizing the type alias `Loop<T>`
819
--> $DIR/unconstrained-params-in-impl-due-to-overflow.rs:6:16
920
|
@@ -12,7 +23,7 @@ LL | type Loop<T> = Loop<T>;
1223
|
1324
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
1425

15-
error: aborting due to 2 previous errors
26+
error: aborting due to 3 previous errors
1627

1728
Some errors have detailed explanations: E0207, E0275.
1829
For more information about an error, try `rustc --explain E0207`.

tests/ui/lifetimes/issue-64173-unused-lifetimes.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ LL | beta: [(); foo::<&'a ()>()],
77
= note: lifetime parameters may not be used in const expressions
88
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
99

10-
error: generic `Self` types are currently not permitted in anonymous constants
11-
--> $DIR/issue-64173-unused-lifetimes.rs:4:28
12-
|
13-
LL | array: [(); size_of::<&Self>()],
14-
| ^^^^
15-
1610
error[E0392]: lifetime parameter `'s` is never used
1711
--> $DIR/issue-64173-unused-lifetimes.rs:3:12
1812
|
@@ -21,6 +15,12 @@ LL | struct Foo<'s> {
2115
|
2216
= help: consider removing `'s`, referring to it in a field, or using a marker such as `PhantomData`
2317

18+
error: generic `Self` types are currently not permitted in anonymous constants
19+
--> $DIR/issue-64173-unused-lifetimes.rs:4:28
20+
|
21+
LL | array: [(); size_of::<&Self>()],
22+
| ^^^^
23+
2424
error[E0392]: lifetime parameter `'a` is never used
2525
--> $DIR/issue-64173-unused-lifetimes.rs:15:12
2626
|

tests/ui/regions/region-bounds-on-objects-and-type-parameters.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ error[E0226]: only a single explicit lifetime bound is permitted
44
LL | z: Box<dyn Is<'a>+'b+'c>,
55
| ^^
66

7+
error[E0392]: lifetime parameter `'c` is never used
8+
--> $DIR/region-bounds-on-objects-and-type-parameters.rs:11:18
9+
|
10+
LL | struct Foo<'a,'b,'c> {
11+
| ^^ unused lifetime parameter
12+
|
13+
= help: consider removing `'c`, referring to it in a field, or using a marker such as `PhantomData`
14+
715
error[E0478]: lifetime bound not satisfied
816
--> $DIR/region-bounds-on-objects-and-type-parameters.rs:21:8
917
|
@@ -21,14 +29,6 @@ note: but lifetime parameter must outlive the lifetime `'a` as defined here
2129
LL | struct Foo<'a,'b,'c> {
2230
| ^^
2331

24-
error[E0392]: lifetime parameter `'c` is never used
25-
--> $DIR/region-bounds-on-objects-and-type-parameters.rs:11:18
26-
|
27-
LL | struct Foo<'a,'b,'c> {
28-
| ^^ unused lifetime parameter
29-
|
30-
= help: consider removing `'c`, referring to it in a field, or using a marker such as `PhantomData`
31-
3232
error: aborting due to 3 previous errors
3333

3434
Some errors have detailed explanations: E0226, E0392, E0478.

0 commit comments

Comments
 (0)