Skip to content
/ rust Public
forked from rust-lang/rust

Commit d99c775

Browse files
committed
unconstrained NormalizesTo term for opaques
1 parent 0accf4e commit d99c775

File tree

8 files changed

+54
-42
lines changed

8 files changed

+54
-42
lines changed

compiler/rustc_trait_selection/src/solve/alias_relate.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
2424
use super::EvalCtxt;
2525
use rustc_infer::traits::query::NoSolution;
26-
use rustc_infer::traits::solve::GoalSource;
2726
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
2827
use rustc_middle::ty::{self, Ty};
2928

@@ -147,25 +146,18 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
147146
return None;
148147
}
149148

150-
let ty::Alias(kind, alias) = *ty.kind() else {
149+
let ty::Alias(_, alias) = *ty.kind() else {
151150
return Some(ty);
152151
};
153152

154153
match self.commit_if_ok(|this| {
155154
let tcx = this.tcx();
156155
let normalized_ty = this.next_ty_infer();
157-
let normalizes_to = ty::NormalizesTo { alias, term: normalized_ty.into() };
158-
match kind {
159-
ty::AliasKind::Opaque => {
160-
// HACK: Unlike for associated types, `normalizes-to` for opaques
161-
// is currently not treated as a function. We do not erase the
162-
// expected term.
163-
this.add_goal(GoalSource::Misc, Goal::new(tcx, param_env, normalizes_to));
164-
}
165-
ty::AliasKind::Projection | ty::AliasKind::Inherent | ty::AliasKind::Weak => {
166-
this.add_normalizes_to_goal(Goal::new(tcx, param_env, normalizes_to))
167-
}
168-
}
156+
this.add_normalizes_to_goal(Goal::new(
157+
tcx,
158+
param_env,
159+
ty::NormalizesTo { alias, term: normalized_ty.into() },
160+
));
169161
this.try_evaluate_added_goals()?;
170162
Ok(this.resolve_vars_if_possible(normalized_ty))
171163
}) {

compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,9 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
3030
&mut self,
3131
goal: Goal<'tcx, NormalizesTo<'tcx>>,
3232
) -> QueryResult<'tcx> {
33-
let def_id = goal.predicate.def_id();
34-
let def_kind = self.tcx().def_kind(def_id);
35-
match def_kind {
36-
DefKind::OpaqueTy => return self.normalize_opaque_type(goal),
37-
_ => self.set_is_normalizes_to_goal(),
38-
}
39-
33+
self.set_is_normalizes_to_goal();
4034
debug_assert!(self.term_is_fully_unconstrained(goal));
35+
let def_id = goal.predicate.def_id();
4136
match self.tcx().def_kind(def_id) {
4237
DefKind::AssocTy | DefKind::AssocConst => {
4338
match self.tcx().associated_item(def_id).container {
@@ -52,6 +47,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
5247
}
5348
DefKind::AnonConst => self.normalize_anon_const(goal),
5449
DefKind::TyAlias => self.normalize_weak_type(goal),
50+
DefKind::OpaqueTy => self.normalize_opaque_type(goal),
5551
kind => bug!("unknown DefKind {} in normalizes-to goal: {goal:#?}", kind.descr(def_id)),
5652
}
5753
}

compiler/rustc_trait_selection/src/solve/normalizes_to/opaque_types.rs

-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
5858
}
5959
}
6060

61-
let expected = self.structurally_normalize_ty(goal.param_env, expected)?;
62-
if expected.is_ty_var() {
63-
return self
64-
.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS);
65-
}
66-
6761
// Otherwise, define a new opaque type
6862
self.insert_hidden_type(opaque_type_key, goal.param_env, expected)?;
6963
self.add_item_bounds_for_hidden_type(

tests/ui/generic-associated-types/ambig-hr-projection-issue-93340.next.stderr

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0283]: type annotations needed
2-
--> $DIR/ambig-hr-projection-issue-93340.rs:16:5
2+
--> $DIR/ambig-hr-projection-issue-93340.rs:17:5
33
|
44
LL | cmp_eq
55
| ^^^^^^ cannot infer type of the type parameter `A` declared on the function `cmp_eq`
@@ -16,13 +16,13 @@ LL | cmp_eq::<A, B, O>
1616
| +++++++++++
1717

1818
error[E0275]: overflow evaluating the requirement `impl for<'a, 'b> Fn(<A as Scalar>::RefType<'a>, <B as Scalar>::RefType<'b>) -> O == for<'a, 'b> fn(..., ...) -> ... {cmp_eq::<..., ..., ...>}`
19-
--> $DIR/ambig-hr-projection-issue-93340.rs:16:5
19+
--> $DIR/ambig-hr-projection-issue-93340.rs:17:5
2020
|
2121
LL | cmp_eq
2222
| ^^^^^^
2323

2424
error[E0275]: overflow evaluating the requirement `impl for<'a, 'b> Fn(<A as Scalar>::RefType<'a>, <B as Scalar>::RefType<'b>) -> O == for<'a, 'b> fn(..., ...) -> ... {cmp_eq::<..., ..., ...>}`
25-
--> $DIR/ambig-hr-projection-issue-93340.rs:16:5
25+
--> $DIR/ambig-hr-projection-issue-93340.rs:17:5
2626
|
2727
LL | cmp_eq
2828
| ^^^^^^
@@ -35,14 +35,25 @@ error[E0275]: overflow evaluating the requirement `for<'a, 'b> fn(<O as Scalar>:
3535
LL | ) -> impl Fn(A::RefType<'_>, B::RefType<'_>) -> O {
3636
| ___________________________________________________^
3737
LL | |
38-
LL | | cmp_eq
39-
LL | |
4038
LL | |
39+
LL | | cmp_eq
40+
... |
4141
LL | |
4242
LL | | }
4343
| |_^
4444

45-
error: aborting due to 4 previous errors
45+
error[E0275]: overflow evaluating the requirement `impl for<'a, 'b> Fn(<A as Scalar>::RefType<'a>, <B as Scalar>::RefType<'b>) -> O: Sized`
46+
--> $DIR/ambig-hr-projection-issue-93340.rs:14:6
47+
|
48+
LL | ) -> impl Fn(A::RefType<'_>, B::RefType<'_>) -> O {
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
...
51+
LL | cmp_eq
52+
| ------ this returned value is of type `for<'a, 'b> fn(<O as Scalar>::RefType<'a>, <_ as Scalar>::RefType<'b>) -> _ {cmp_eq::<O, _, _>}`
53+
|
54+
= note: the return type of a function must have a statically known size
55+
56+
error: aborting due to 5 previous errors
4657

4758
Some errors have detailed explanations: E0275, E0283.
4859
For more information about an error, try `rustc --explain E0275`.

tests/ui/generic-associated-types/ambig-hr-projection-issue-93340.old.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0283]: type annotations needed
2-
--> $DIR/ambig-hr-projection-issue-93340.rs:16:5
2+
--> $DIR/ambig-hr-projection-issue-93340.rs:17:5
33
|
44
LL | cmp_eq
55
| ^^^^^^ cannot infer type of the type parameter `A` declared on the function `cmp_eq`

tests/ui/generic-associated-types/ambig-hr-projection-issue-93340.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn cmp_eq<'a, 'b, A: Scalar, B: Scalar, O: Scalar>(a: A::RefType<'a>, b: B::RefT
1313
fn build_expression<A: Scalar, B: Scalar, O: Scalar>(
1414
) -> impl Fn(A::RefType<'_>, B::RefType<'_>) -> O {
1515
//[next]~^ ERROR overflow evaluating the requirement
16+
//[next]~| ERROR overflow evaluating the requirement
1617
cmp_eq
1718
//~^ ERROR type annotations needed
1819
//[next]~| ERROR overflow evaluating the requirement
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/recursive-coroutine-boxed.rs:12:23
2+
--> $DIR/recursive-coroutine-boxed.rs:14:23
33
|
44
LL | let mut gen = Box::pin(foo());
55
| ^^^^^^^^ cannot infer type of the type parameter `T` declared on the struct `Box`
@@ -12,12 +12,28 @@ help: consider specifying the generic argument
1212
LL | let mut gen = Box::<T>::pin(foo());
1313
| +++++
1414

15-
error[E0282]: type annotations needed
16-
--> $DIR/recursive-coroutine-boxed.rs:9:13
15+
error[E0308]: mismatched types
16+
--> $DIR/recursive-coroutine-boxed.rs:13:5
17+
|
18+
LL | fn foo() -> impl Coroutine<Yield = (), Return = ()> {
19+
| ---------------------------------------
20+
| |
21+
| the expected opaque type
22+
| expected `impl Coroutine<Yield = (), Return = ()>` because of return type
23+
...
24+
LL | / || {
25+
LL | | let mut gen = Box::pin(foo());
26+
LL | |
27+
LL | | let mut r = gen.as_mut().resume(());
28+
... |
29+
LL | | }
30+
LL | | }
31+
| |_____^ types differ
1732
|
18-
LL | fn foo() -> impl Coroutine<Yield = (), Return = ()> {
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for opaque type `impl Coroutine<Yield = (), Return = ()>`
33+
= note: expected opaque type `impl Coroutine<Yield = (), Return = ()>`
34+
found coroutine `{coroutine@$DIR/recursive-coroutine-boxed.rs:13:5: 13:7}`
2035

2136
error: aborting due to 2 previous errors
2237

23-
For more information about this error, try `rustc --explain E0282`.
38+
Some errors have detailed explanations: E0282, E0308.
39+
For more information about an error, try `rustc --explain E0282`.

tests/ui/impl-trait/recursive-coroutine-boxed.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use std::ops::{Coroutine, CoroutineState};
88

99
fn foo() -> impl Coroutine<Yield = (), Return = ()> {
10-
//[next]~^ ERROR type annotations needed
11-
|| {
10+
// FIXME(-Znext-solver): this fails with a mismatched types as the
11+
// hidden type of the opaque ends up as {type error}. We should not
12+
// emit errors for such goals.
13+
|| { //[next]~ ERROR mismatched types
1214
let mut gen = Box::pin(foo());
1315
//[next]~^ ERROR type annotations needed
1416
let mut r = gen.as_mut().resume(());

0 commit comments

Comments
 (0)