Skip to content

Commit 3dfd50d

Browse files
committed
Move lazy type alias checks to non-hir-wfck
1 parent a13a1d2 commit 3dfd50d

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
@@ -9,7 +9,7 @@ use rustc_errors::codes::*;
99
use rustc_hir::def::{CtorKind, DefKind};
1010
use rustc_hir::{LangItem, Node, intravisit};
1111
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
12-
use rustc_infer::traits::{Obligation, ObligationCauseCode};
12+
use rustc_infer::traits::{Obligation, ObligationCauseCode, WellFormedLoc};
1313
use rustc_lint_defs::builtin::{
1414
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS, UNSUPPORTED_FN_PTR_CALLING_CONVENTIONS,
1515
};
@@ -34,7 +34,9 @@ use {rustc_attr_data_structures as attrs, rustc_hir as hir};
3434

3535
use super::compare_impl_item::check_type_bounds;
3636
use super::*;
37-
use crate::check::wfcheck::check_variances_for_type_defn;
37+
use crate::check::wfcheck::{
38+
check_variances_for_type_defn, check_where_clauses, enter_wf_checking_ctxt,
39+
};
3840

3941
pub fn check_abi(tcx: TyCtxt<'_>, span: Span, abi: ExternAbi) {
4042
if !tcx.sess.target.is_abi_supported(abi) {
@@ -703,6 +705,7 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
703705
}
704706

705707
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
708+
let mut res = Ok(());
706709
match tcx.def_kind(def_id) {
707710
DefKind::Static { .. } => {
708711
check_static_inhabited(tcx, def_id);
@@ -776,6 +779,18 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
776779
DefKind::TyAlias => {
777780
check_type_alias_type_params_are_used(tcx, def_id);
778781
if tcx.type_alias_is_lazy(def_id) {
782+
res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
783+
let ty = tcx.type_of(def_id).instantiate_identity();
784+
let span = tcx.def_span(def_id);
785+
let item_ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), ty);
786+
wfcx.register_wf_obligation(
787+
span,
788+
Some(WellFormedLoc::Ty(def_id)),
789+
item_ty.into(),
790+
);
791+
check_where_clauses(wfcx, def_id);
792+
Ok(())
793+
}));
779794
check_variances_for_type_defn(tcx, def_id);
780795
}
781796
}
@@ -827,7 +842,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
827842
}
828843
_ => {}
829844
}
830-
Ok(())
845+
res
831846
}
832847

833848
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
@@ -84,7 +84,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
8484
/// signature types for implied bounds when checking regions.
8585
// FIXME(-Znext-solver): This should be removed when we compute implied outlives
8686
// bounds using the unnormalized signature of the function we're checking.
87-
fn deeply_normalize<T>(&self, span: Span, loc: Option<WellFormedLoc>, value: T) -> T
87+
pub(super) fn deeply_normalize<T>(&self, span: Span, loc: Option<WellFormedLoc>, value: T) -> T
8888
where
8989
T: TypeFoldable<TyCtxt<'tcx>>,
9090
{
@@ -105,7 +105,12 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
105105
}
106106
}
107107

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

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)