Skip to content

Commit 40ded6d

Browse files
Deeply normalize associated type bounds before proving them
1 parent da663c7 commit 40ded6d

File tree

10 files changed

+92
-67
lines changed

10 files changed

+92
-67
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+22-42
Original file line numberDiff line numberDiff line change
@@ -2107,18 +2107,21 @@ pub(super) fn check_type_bounds<'tcx>(
21072107
ObligationCause::new(impl_ty_span, impl_ty_def_id, code)
21082108
};
21092109

2110-
let mut obligations: Vec<_> = tcx
2111-
.explicit_item_bounds(trait_ty.def_id)
2112-
.iter_instantiated_copied(tcx, rebased_args)
2113-
.map(|(concrete_ty_bound, span)| {
2114-
debug!(?concrete_ty_bound);
2115-
traits::Obligation::new(tcx, mk_cause(span), param_env, concrete_ty_bound)
2116-
})
2117-
.collect();
2110+
let mut obligations: Vec<_> = util::elaborate(
2111+
tcx,
2112+
tcx.explicit_item_bounds(trait_ty.def_id).iter_instantiated_copied(tcx, rebased_args).map(
2113+
|(concrete_ty_bound, span)| {
2114+
debug!(?concrete_ty_bound);
2115+
traits::Obligation::new(tcx, mk_cause(span), param_env, concrete_ty_bound)
2116+
},
2117+
),
2118+
)
2119+
.collect();
21182120

21192121
// Only in a const implementation do we need to check that the `~const` item bounds hold.
21202122
if tcx.is_conditionally_const(impl_ty_def_id) {
2121-
obligations.extend(
2123+
obligations.extend(util::elaborate(
2124+
tcx,
21222125
tcx.explicit_implied_const_bounds(trait_ty.def_id)
21232126
.iter_instantiated_copied(tcx, rebased_args)
21242127
.map(|(c, span)| {
@@ -2129,34 +2132,27 @@ pub(super) fn check_type_bounds<'tcx>(
21292132
c.to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
21302133
)
21312134
}),
2132-
);
2135+
));
21332136
}
21342137
debug!(item_bounds=?obligations);
21352138

21362139
// Normalize predicates with the assumption that the GAT may always normalize
21372140
// to its definition type. This should be the param-env we use to *prove* the
21382141
// predicate too, but we don't do that because of performance issues.
21392142
// See <https://github.com/rust-lang/rust/pull/117542#issue-1976337685>.
2140-
let trait_projection_ty = Ty::new_projection_from_args(tcx, trait_ty.def_id, rebased_args);
2141-
let impl_identity_ty = tcx.type_of(impl_ty.def_id).instantiate_identity();
21422143
let normalize_param_env = param_env_with_gat_bounds(tcx, impl_ty, impl_trait_ref);
2143-
for mut obligation in util::elaborate(tcx, obligations) {
2144-
let normalized_predicate = if infcx.next_trait_solver() {
2145-
obligation.predicate.fold_with(&mut ReplaceTy {
2146-
tcx,
2147-
from: trait_projection_ty,
2148-
to: impl_identity_ty,
2149-
})
2150-
} else {
2151-
ocx.normalize(&normalize_cause, normalize_param_env, obligation.predicate)
2152-
};
2153-
debug!(?normalized_predicate);
2154-
obligation.predicate = normalized_predicate;
2155-
2156-
ocx.register_obligation(obligation);
2144+
for obligation in &mut obligations {
2145+
match ocx.deeply_normalize(&normalize_cause, normalize_param_env, obligation.predicate) {
2146+
Ok(pred) => obligation.predicate = pred,
2147+
Err(e) => {
2148+
return Err(infcx.err_ctxt().report_fulfillment_errors(e));
2149+
}
2150+
}
21572151
}
2152+
21582153
// Check that all obligations are satisfied by the implementation's
21592154
// version.
2155+
ocx.register_obligations(obligations);
21602156
let errors = ocx.select_all_or_error();
21612157
if !errors.is_empty() {
21622158
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
@@ -2168,22 +2164,6 @@ pub(super) fn check_type_bounds<'tcx>(
21682164
ocx.resolve_regions_and_report_errors(impl_ty_def_id, param_env, assumed_wf_types)
21692165
}
21702166

2171-
struct ReplaceTy<'tcx> {
2172-
tcx: TyCtxt<'tcx>,
2173-
from: Ty<'tcx>,
2174-
to: Ty<'tcx>,
2175-
}
2176-
2177-
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceTy<'tcx> {
2178-
fn cx(&self) -> TyCtxt<'tcx> {
2179-
self.tcx
2180-
}
2181-
2182-
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
2183-
if self.from == ty { self.to } else { ty.super_fold_with(self) }
2184-
}
2185-
}
2186-
21872167
/// Install projection predicates that allow GATs to project to their own
21882168
/// definition types. This is not allowed in general in cases of default
21892169
/// associated types in trait definitions, or when specialization is involved,

tests/ui/generic-associated-types/issue-91883.stderr renamed to tests/ui/generic-associated-types/issue-91883.current.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0478]: lifetime bound not satisfied
2-
--> $DIR/issue-91883.rs:30:24
2+
--> $DIR/issue-91883.rs:34:24
33
|
44
LL | type Cursor<'tx>: Cursor<'tx>
55
| ----------------------------- definition of `Cursor` from trait
@@ -8,12 +8,12 @@ LL | type Cursor<'tx> = CursorImpl<'tx>;
88
| ^^^^^^^^^^^^^^^
99
|
1010
note: lifetime parameter instantiated with the lifetime `'db` as defined here
11-
--> $DIR/issue-91883.rs:29:6
11+
--> $DIR/issue-91883.rs:33:6
1212
|
1313
LL | impl<'db> Transaction<'db> for TransactionImpl<'db> {
1414
| ^^^
1515
note: but lifetime parameter must outlive the lifetime `'tx` as defined here
16-
--> $DIR/issue-91883.rs:30:17
16+
--> $DIR/issue-91883.rs:34:17
1717
|
1818
LL | type Cursor<'tx> = CursorImpl<'tx>;
1919
| ^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0478]: lifetime bound not satisfied
2+
--> $DIR/issue-91883.rs:34:24
3+
|
4+
LL | type Cursor<'tx> = CursorImpl<'tx>;
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
note: lifetime parameter instantiated with the lifetime `'db` as defined here
8+
--> $DIR/issue-91883.rs:33:6
9+
|
10+
LL | impl<'db> Transaction<'db> for TransactionImpl<'db> {
11+
| ^^^
12+
note: but lifetime parameter must outlive the lifetime `'tx` as defined here
13+
--> $DIR/issue-91883.rs:34:17
14+
|
15+
LL | type Cursor<'tx> = CursorImpl<'tx>;
16+
| ^^^
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0478`.

tests/ui/generic-associated-types/issue-91883.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//@ revisions: current next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
3+
//@[next] compile-flags: -Znext-solver
4+
15
use std::fmt::Debug;
26
use std::marker::PhantomData;
37

tests/ui/impl-trait/in-trait/default-body-with-rpit.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//@ edition:2021
22
//@ check-pass
3+
//@ revisions: current next
4+
//@ ignore-compare-mode-next-solver (explicit revisions)
5+
//@[next] compile-flags: -Znext-solver
36

47
use std::fmt::Debug;
58

tests/ui/impl-trait/in-trait/nested-rpitit-bounds.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
25

36
use std::ops::Deref;
47

tests/ui/traits/const-traits/predicate-entailment-passes.rs

-11
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,21 @@
66
#[const_trait] trait Bar {}
77
impl const Bar for () {}
88

9-
109
#[const_trait] trait TildeConst {
11-
type Bar<T> where T: ~const Bar;
12-
1310
fn foo<T>() where T: ~const Bar;
1411
}
1512
impl TildeConst for () {
16-
type Bar<T> = () where T: Bar;
17-
1813
fn foo<T>() where T: Bar {}
1914
}
2015

2116

2217
#[const_trait] trait AlwaysConst {
23-
type Bar<T> where T: const Bar;
24-
2518
fn foo<T>() where T: const Bar;
2619
}
2720
impl AlwaysConst for i32 {
28-
type Bar<T> = () where T: Bar;
29-
3021
fn foo<T>() where T: Bar {}
3122
}
3223
impl const AlwaysConst for u32 {
33-
type Bar<T> = () where T: ~const Bar;
34-
3524
fn foo<T>() where T: ~const Bar {}
3625
}
3726

tests/ui/traits/next-solver/coherence/trait_ref_is_knowable-norm-overflow.stderr

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
error[E0275]: overflow evaluating the requirement `<T as Overflow>::Assoc: Sized`
1+
error[E0275]: overflow evaluating the requirement `<T as Overflow>::Assoc == _`
22
--> $DIR/trait_ref_is_knowable-norm-overflow.rs:10:18
33
|
44
LL | type Assoc = <T as Overflow>::Assoc;
55
| ^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
note: required by a bound in `Overflow::Assoc`
8-
--> $DIR/trait_ref_is_knowable-norm-overflow.rs:7:5
9-
|
10-
LL | type Assoc;
11-
| ^^^^^^^^^^^ required by this bound in `Overflow::Assoc`
12-
help: consider relaxing the implicit `Sized` restriction
13-
|
14-
LL | type Assoc: ?Sized;
15-
| ++++++++
166

177
error[E0119]: conflicting implementations of trait `Trait`
188
--> $DIR/trait_ref_is_knowable-norm-overflow.rs:18:1

tests/ui/traits/next-solver/gat-wf.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Make sure that, like the old trait solver, we end up requiring that the WC of
2+
// impl GAT matches that of the trait. This is not a restriction that we *need*,
3+
// but is a side-effect of registering the where clauses when normalizing the GAT
4+
// when proving it satisfies its item bounds.
5+
6+
trait Foo {
7+
type T<'a>: Sized where Self: 'a;
8+
}
9+
10+
impl Foo for &() {
11+
type T<'a> = (); //~ the type `&()` does not fulfill the required lifetime
12+
}
13+
14+
fn main() {}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0477]: the type `&()` does not fulfill the required lifetime
2+
--> $DIR/gat-wf.rs:11:18
3+
|
4+
LL | type T<'a>: Sized where Self: 'a;
5+
| ----------------- definition of `T` from trait
6+
...
7+
LL | type T<'a> = ();
8+
| ^^
9+
|
10+
note: type must outlive the lifetime `'a` as defined here
11+
--> $DIR/gat-wf.rs:11:12
12+
|
13+
LL | type T<'a> = ();
14+
| ^^
15+
help: copy the `where` clause predicates from the trait
16+
|
17+
LL | type T<'a> = () where Self: 'a;
18+
| ++++++++++++++
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0477`.

0 commit comments

Comments
 (0)