Skip to content

Commit 632a921

Browse files
committed
Move lazy type alias checks to non-hir-wfck
1 parent 28f023c commit 632a921

10 files changed

+63
-91
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_errors::{EmissionGuarantee, MultiSpan};
1010
use rustc_hir::def::{CtorKind, DefKind};
1111
use rustc_hir::{LangItem, Node, intravisit};
1212
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
13-
use rustc_infer::traits::{Obligation, ObligationCauseCode};
13+
use rustc_infer::traits::{Obligation, ObligationCauseCode, WellFormedLoc};
1414
use rustc_lint_defs::builtin::{
1515
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS, UNSUPPORTED_CALLING_CONVENTIONS,
1616
};
@@ -36,7 +36,9 @@ 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;
39+
use crate::check::wfcheck::{
40+
check_variances_for_type_defn, check_where_clauses, enter_wf_checking_ctxt,
41+
};
4042

4143
fn add_abi_diag_help<T: EmissionGuarantee>(abi: ExternAbi, diag: &mut Diag<'_, T>) {
4244
if let ExternAbi::Cdecl { unwind } = abi {
@@ -731,6 +733,7 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
731733
}
732734

733735
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
736+
let mut res = Ok(());
734737
let generics = tcx.generics_of(def_id);
735738

736739
for param in &generics.own_params {
@@ -837,6 +840,18 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
837840
DefKind::TyAlias => {
838841
check_type_alias_type_params_are_used(tcx, def_id);
839842
if tcx.type_alias_is_lazy(def_id) {
843+
res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
844+
let ty = tcx.type_of(def_id).instantiate_identity();
845+
let span = tcx.def_span(def_id);
846+
let item_ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), ty);
847+
wfcx.register_wf_obligation(
848+
span,
849+
Some(WellFormedLoc::Ty(def_id)),
850+
item_ty.into(),
851+
);
852+
check_where_clauses(wfcx, def_id);
853+
Ok(())
854+
}));
840855
check_variances_for_type_defn(tcx, def_id);
841856
}
842857
}
@@ -901,7 +916,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
901916
}
902917
_ => {}
903918
}
904-
Ok(())
919+
res
905920
}
906921

907922
pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, def_id: LocalDefId) {

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
8383
/// signature types for implied bounds when checking regions.
8484
// FIXME(-Znext-solver): This should be removed when we compute implied outlives
8585
// bounds using the unnormalized signature of the function we're checking.
86-
fn deeply_normalize<T>(&self, span: Span, loc: Option<WellFormedLoc>, value: T) -> T
86+
pub(super) fn deeply_normalize<T>(&self, span: Span, loc: Option<WellFormedLoc>, value: T) -> T
8787
where
8888
T: TypeFoldable<TyCtxt<'tcx>>,
8989
{
@@ -104,7 +104,12 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
104104
}
105105
}
106106

107-
fn register_wf_obligation(&self, span: Span, loc: Option<WellFormedLoc>, term: ty::Term<'tcx>) {
107+
pub(super) fn register_wf_obligation(
108+
&self,
109+
span: Span,
110+
loc: Option<WellFormedLoc>,
111+
term: ty::Term<'tcx>,
112+
) {
108113
let cause = traits::ObligationCause::new(
109114
span,
110115
self.body_def_id,
@@ -297,20 +302,6 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
297302
hir::ItemKind::Enum(..) => check_type_defn(tcx, item, true),
298303
hir::ItemKind::Trait(..) => check_trait(tcx, item),
299304
hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
300-
hir::ItemKind::TyAlias(.., hir_ty) if tcx.type_alias_is_lazy(item.owner_id) => {
301-
enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
302-
let ty = tcx.type_of(def_id).instantiate_identity();
303-
let item_ty =
304-
wfcx.deeply_normalize(hir_ty.span, Some(WellFormedLoc::Ty(def_id)), ty);
305-
wfcx.register_wf_obligation(
306-
hir_ty.span,
307-
Some(WellFormedLoc::Ty(def_id)),
308-
item_ty.into(),
309-
);
310-
check_where_clauses(wfcx, def_id);
311-
Ok(())
312-
})
313-
}
314305
_ => Ok(()),
315306
}
316307
}

tests/ui/infinite/infinite-type-alias-mutual-recursion.feature.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
error[E0275]: overflow normalizing the type alias `X2`
2-
--> $DIR/infinite-type-alias-mutual-recursion.rs:6:11
2+
--> $DIR/infinite-type-alias-mutual-recursion.rs:6:1
33
|
44
LL | type X1 = X2;
5-
| ^^
5+
| ^^^^^^^
66
|
77
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
88

99
error[E0275]: overflow normalizing the type alias `X3`
10-
--> $DIR/infinite-type-alias-mutual-recursion.rs:9:11
10+
--> $DIR/infinite-type-alias-mutual-recursion.rs:9:1
1111
|
1212
LL | type X2 = X3;
13-
| ^^
13+
| ^^^^^^^
1414
|
1515
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
1616

1717
error[E0275]: overflow normalizing the type alias `X1`
18-
--> $DIR/infinite-type-alias-mutual-recursion.rs:11:11
18+
--> $DIR/infinite-type-alias-mutual-recursion.rs:11:1
1919
|
2020
LL | type X3 = X1;
21-
| ^^
21+
| ^^^^^^^
2222
|
2323
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
2424

tests/ui/infinite/infinite-vec-type-recursion.feature.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0275]: overflow normalizing the type alias `X`
2-
--> $DIR/infinite-vec-type-recursion.rs:6:10
2+
--> $DIR/infinite-vec-type-recursion.rs:6:1
33
|
44
LL | type X = Vec<X>;
5-
| ^^^^^^
5+
| ^^^^^^
66
|
77
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
88

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0275]: overflow normalizing the type alias `Loop`
2-
--> $DIR/inherent-impls-overflow.rs:8:13
2+
--> $DIR/inherent-impls-overflow.rs:8:1
33
|
44
LL | type Loop = Loop;
5-
| ^^^^
5+
| ^^^^^^^^^
66
|
77
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
88

@@ -14,41 +14,19 @@ 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-
2817
error[E0275]: overflow normalizing the type alias `Poly0<(((((((...,),),),),),),)>`
29-
--> $DIR/inherent-impls-overflow.rs:17:17
18+
--> $DIR/inherent-impls-overflow.rs:17:1
3019
|
3120
LL | type Poly0<T> = Poly1<(T,)>;
32-
| ^^^^^^^^^^^
21+
| ^^^^^^^^^^^^^
3322
|
3423
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
3524

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-
4725
error[E0275]: overflow normalizing the type alias `Poly1<(((((((...,),),),),),),)>`
48-
--> $DIR/inherent-impls-overflow.rs:21:17
26+
--> $DIR/inherent-impls-overflow.rs:21:1
4927
|
5028
LL | type Poly1<T> = Poly0<(T,)>;
51-
| ^^^^^^^^^^^
29+
| ^^^^^^^^^^^^^
5230
|
5331
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
5432

@@ -60,6 +38,6 @@ LL | impl Poly0<()> {}
6038
|
6139
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
6240

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

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

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0271]: type mismatch resolving `Loop normalizes-to _`
2-
--> $DIR/inherent-impls-overflow.rs:8:13
2+
--> $DIR/inherent-impls-overflow.rs:8:1
33
|
44
LL | type Loop = Loop;
5-
| ^^^^ types differ
5+
| ^^^^^^^^^ types differ
66

77
error[E0271]: type mismatch resolving `Loop normalizes-to _`
88
--> $DIR/inherent-impls-overflow.rs:12:1
@@ -16,6 +16,14 @@ 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:1
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+
1927
error: type parameter `T` is only used recursively
2028
--> $DIR/inherent-impls-overflow.rs:17:24
2129
|
@@ -27,11 +35,11 @@ LL | type Poly0<T> = Poly1<(T,)>;
2735
= help: consider removing `T` or referring to it in the body of the type alias
2836
= note: all type parameters must be used in a non-recursive way in order to constrain their variance
2937

30-
error[E0275]: overflow evaluating the requirement `Poly1<(T,)> == _`
31-
--> $DIR/inherent-impls-overflow.rs:17:17
38+
error[E0275]: overflow evaluating the requirement `Poly0<(T,)> == _`
39+
--> $DIR/inherent-impls-overflow.rs:21:1
3240
|
33-
LL | type Poly0<T> = Poly1<(T,)>;
34-
| ^^^^^^^^^^^
41+
LL | type Poly1<T> = Poly0<(T,)>;
42+
| ^^^^^^^^^^^^^
3543
|
3644
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`inherent_impls_overflow`)
3745

@@ -46,14 +54,6 @@ LL | type Poly1<T> = Poly0<(T,)>;
4654
= help: consider removing `T` or referring to it in the body of the type alias
4755
= note: all type parameters must be used in a non-recursive way in order to constrain their variance
4856

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-
//~^^ ERROR type parameter `T` is only used recursively
20-
//[next]~^^^ ERROR overflow evaluating the requirement
19+
//[next]~^^ 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-
//~^^ ERROR type parameter `T` is only used recursively
24-
//[next]~^^^ ERROR overflow evaluating the requirement
23+
//[next]~^^ 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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
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
87

98
fn main() {}

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,15 @@ 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-
187
error[E0275]: overflow normalizing the type alias `Loop<T>`
19-
--> $DIR/unconstrained-params-in-impl-due-to-overflow.rs:6:16
8+
--> $DIR/unconstrained-params-in-impl-due-to-overflow.rs:6:1
209
|
2110
LL | type Loop<T> = Loop<T>;
22-
| ^^^^^^^
11+
| ^^^^^^^^^^^^
2312
|
2413
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
2514

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

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

tests/ui/traits/next-solver/issue-118950-root-region.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ LL | #![feature(lazy_type_alias)]
1414
= note: `#[warn(incomplete_features)]` on by default
1515

1616
error[E0277]: the trait bound `*const T: ToUnit<'a>` is not satisfied
17-
--> $DIR/issue-118950-root-region.rs:14:21
17+
--> $DIR/issue-118950-root-region.rs:14:1
1818
|
1919
LL | type Assoc<'a, T> = <*const T as ToUnit<'a>>::Unit;
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ToUnit<'a>` is not implemented for `*const T`
20+
| ^^^^^^^^^^^^^^^^^ the trait `ToUnit<'a>` is not implemented for `*const T`
2121
|
2222
help: this trait has no implementations, consider adding one
2323
--> $DIR/issue-118950-root-region.rs:8:1

0 commit comments

Comments
 (0)