Skip to content

Commit 97c9d8f

Browse files
Only use normalize_param_env when normalizing predicate in check_item_bounds
1 parent ff0b4b6 commit 97c9d8f

8 files changed

+73
-9
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2162,7 +2162,7 @@ pub(super) fn check_type_bounds<'tcx>(
21622162
impl_ty: ty::AssocItem,
21632163
impl_trait_ref: ty::TraitRef<'tcx>,
21642164
) -> Result<(), ErrorGuaranteed> {
2165-
let param_env = param_env_with_gat_bounds(tcx, impl_ty, impl_trait_ref);
2165+
let param_env = tcx.param_env(impl_ty.def_id);
21662166
debug!(?param_env);
21672167

21682168
let container_id = impl_ty.container_id(tcx);
@@ -2217,8 +2217,14 @@ pub(super) fn check_type_bounds<'tcx>(
22172217
.collect();
22182218
debug!("check_type_bounds: item_bounds={:?}", obligations);
22192219

2220+
// Normalize predicates with the assumption that the GAT may always normalize
2221+
// to its definition type. This should be the param-env we use to *prove* the
2222+
// predicate too, but we don't do that because of performance issues.
2223+
// See <https://github.com/rust-lang/rust/pull/117542#issue-1976337685>.
2224+
let normalize_param_env = param_env_with_gat_bounds(tcx, impl_ty, impl_trait_ref);
22202225
for mut obligation in util::elaborate(tcx, obligations) {
2221-
let normalized_predicate = ocx.normalize(&normalize_cause, param_env, obligation.predicate);
2226+
let normalized_predicate =
2227+
ocx.normalize(&normalize_cause, normalize_param_env, obligation.predicate);
22222228
debug!("compare_projection_bounds: normalized predicate = {:?}", normalized_predicate);
22232229
obligation.predicate = normalized_predicate;
22242230

tests/ui/generic-associated-types/assume-gat-normalization-for-nested-goals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// check-pass
1+
// known-bug: #117606
22

33
#![feature(associated_type_defaults)]
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0277]: the trait bound `<Self as Foo>::Bar<()>: Eq<i32>` is not satisfied
2+
--> $DIR/assume-gat-normalization-for-nested-goals.rs:6:30
3+
|
4+
LL | type Bar<T>: Baz<Self> = i32;
5+
| ^^^ the trait `Eq<i32>` is not implemented for `<Self as Foo>::Bar<()>`
6+
|
7+
note: required for `i32` to implement `Baz<Self>`
8+
--> $DIR/assume-gat-normalization-for-nested-goals.rs:13:23
9+
|
10+
LL | impl<T: Foo + ?Sized> Baz<T> for i32 where T::Bar<()>: Eq<i32> {}
11+
| ^^^^^^ ^^^ ------- unsatisfied trait bound introduced here
12+
note: required by a bound in `Foo::Bar`
13+
--> $DIR/assume-gat-normalization-for-nested-goals.rs:6:18
14+
|
15+
LL | type Bar<T>: Baz<Self> = i32;
16+
| ^^^^^^^^^ required by this bound in `Foo::Bar`
17+
help: consider further restricting the associated type
18+
|
19+
LL | trait Foo where <Self as Foo>::Bar<()>: Eq<i32> {
20+
| +++++++++++++++++++++++++++++++++++++
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// check-pass
2+
3+
trait Database: for<'r> HasValueRef<'r, Database = Self> {}
4+
5+
trait HasValueRef<'r> {
6+
type Database: Database;
7+
}
8+
9+
struct Any;
10+
11+
impl Database for Any {}
12+
13+
impl<'r> HasValueRef<'r> for Any {
14+
// Make sure we don't have issues when the GAT assumption
15+
// `<Any as HasValue<'r>>::Database = Any` isn't universally
16+
// parameterized over `'r`.
17+
type Database = Any;
18+
}
19+
20+
fn main() {}

tests/ui/traits/new-solver/specialization-transmute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trait Default {
1010
}
1111

1212
impl<T> Default for T {
13-
default type Id = T;
13+
default type Id = T; //~ ERROR type annotations needed
1414
// This will be fixed by #111994
1515
fn intu(&self) -> &Self::Id { //~ ERROR type annotations needed
1616
self

tests/ui/traits/new-solver/specialization-transmute.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ LL | fn intu(&self) -> &Self::Id {
1616
|
1717
= note: cannot satisfy `<T as Default>::Id == _`
1818

19-
error: aborting due to previous error; 1 warning emitted
19+
error[E0282]: type annotations needed
20+
--> $DIR/specialization-transmute.rs:13:23
21+
|
22+
LL | default type Id = T;
23+
| ^ cannot infer type for associated type `<T as Default>::Id`
24+
25+
error: aborting due to 2 previous errors; 1 warning emitted
2026

21-
For more information about this error, try `rustc --explain E0284`.
27+
Some errors have detailed explanations: E0282, E0284.
28+
For more information about an error, try `rustc --explain E0282`.

tests/ui/traits/new-solver/specialization-unconstrained.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ trait Default {
1111
}
1212

1313
impl<T> Default for T {
14-
default type Id = T;
14+
default type Id = T; //~ ERROR type annotations needed
1515
}
1616

1717
fn test<T: Default<Id = U>, U>() {}

tests/ui/traits/new-solver/specialization-unconstrained.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ note: required by a bound in `test`
2020
LL | fn test<T: Default<Id = U>, U>() {}
2121
| ^^^^^^ required by this bound in `test`
2222

23-
error: aborting due to previous error; 1 warning emitted
23+
error[E0282]: type annotations needed
24+
--> $DIR/specialization-unconstrained.rs:14:22
25+
|
26+
LL | default type Id = T;
27+
| ^ cannot infer type for associated type `<T as Default>::Id`
28+
29+
error: aborting due to 2 previous errors; 1 warning emitted
2430

25-
For more information about this error, try `rustc --explain E0284`.
31+
Some errors have detailed explanations: E0282, E0284.
32+
For more information about an error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)