Skip to content

Commit fbd1a87

Browse files
committed
use alias-relate to structurally normalize in the solver
1 parent b4e53c0 commit fbd1a87

14 files changed

+116
-108
lines changed

compiler/rustc_trait_selection/src/solve/alias_relate.rs

+34-7
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
//! `NormalizesTo` goal, at which point the opaque is actually defined.
2222
2323
use super::{EvalCtxt, GoalSource};
24-
use rustc_infer::infer::DefineOpaqueTypes;
2524
use rustc_infer::traits::query::NoSolution;
2625
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
27-
use rustc_middle::ty;
26+
use rustc_middle::ty::{self, Ty};
2827

2928
impl<'tcx> EvalCtxt<'_, 'tcx> {
3029
#[instrument(level = "debug", skip(self), ret)]
@@ -79,7 +78,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
7978
}
8079

8180
// FIXME: This needs a name that reflects that it's okay to bottom-out with an inference var.
82-
/// Normalize the `term` to equate it later. This does not define opaque types.
81+
/// Normalize the `term` to equate it later.
8382
#[instrument(level = "debug", skip(self, param_env), ret)]
8483
fn try_normalize_term(
8584
&mut self,
@@ -88,10 +87,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
8887
) -> Result<Option<ty::Term<'tcx>>, NoSolution> {
8988
match term.unpack() {
9089
ty::TermKind::Ty(ty) => {
91-
// We do no define opaque types here but instead do so in `relate_rigid_alias_or_opaque`.
92-
Ok(self
93-
.try_normalize_ty_recur(param_env, DefineOpaqueTypes::No, 0, ty)
94-
.map(Into::into))
90+
Ok(self.try_normalize_ty_recur(param_env, 0, ty).map(Into::into))
9591
}
9692
ty::TermKind::Const(_) => {
9793
if let Some(alias) = term.to_alias_ty(self.tcx()) {
@@ -108,4 +104,35 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
108104
}
109105
}
110106
}
107+
108+
fn try_normalize_ty_recur(
109+
&mut self,
110+
param_env: ty::ParamEnv<'tcx>,
111+
depth: usize,
112+
ty: Ty<'tcx>,
113+
) -> Option<Ty<'tcx>> {
114+
if !self.tcx().recursion_limit().value_within_limit(depth) {
115+
return None;
116+
}
117+
118+
let ty::Alias(_, alias) = *ty.kind() else {
119+
return Some(ty);
120+
};
121+
122+
match self.commit_if_ok(|this| {
123+
let normalized_ty = this.next_ty_infer();
124+
let normalizes_to_goal = Goal::new(
125+
this.tcx(),
126+
param_env,
127+
ty::NormalizesTo { alias, term: normalized_ty.into() },
128+
);
129+
this.add_goal(GoalSource::Misc, normalizes_to_goal);
130+
this.try_evaluate_added_goals()?;
131+
let ty = this.resolve_vars_if_possible(normalized_ty);
132+
Ok(this.try_normalize_ty_recur(param_env, depth + 1, ty))
133+
}) {
134+
Ok(ty) => ty,
135+
Err(NoSolution) => Some(ty),
136+
}
137+
}
111138
}

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

+7-17
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,11 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
269269
vec![Candidate { source, result }]
270270
};
271271

272-
let Some(normalized_self_ty) =
273-
self.try_normalize_ty(goal.param_env, goal.predicate.self_ty())
272+
let Ok(normalized_self_ty) =
273+
self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
274274
else {
275-
debug!("overflow while evaluating self type");
276-
return dummy_candidate(self, Certainty::OVERFLOW);
275+
return vec![];
277276
};
278-
279277
if normalized_self_ty.is_ty_var() {
280278
debug!("self type has been normalized to infer");
281279
return dummy_candidate(self, Certainty::AMBIGUOUS);
@@ -770,19 +768,11 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
770768

771769
let result = self.probe_misc_candidate("coherence unknowable").enter(|ecx| {
772770
let trait_ref = goal.predicate.trait_ref(tcx);
773-
#[derive(Debug)]
774-
struct Overflow;
775-
let lazily_normalize_ty = |ty| match ecx.try_normalize_ty(goal.param_env, ty) {
776-
Some(ty) => Ok(ty),
777-
None => Err(Overflow),
778-
};
771+
let lazily_normalize_ty = |ty| ecx.structurally_normalize_ty(goal.param_env, ty);
779772

780-
match coherence::trait_ref_is_knowable(tcx, trait_ref, lazily_normalize_ty) {
781-
Err(Overflow) => {
782-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::OVERFLOW)
783-
}
784-
Ok(Ok(())) => Err(NoSolution),
785-
Ok(Err(_)) => {
773+
match coherence::trait_ref_is_knowable(tcx, trait_ref, lazily_normalize_ty)? {
774+
Ok(()) => Err(NoSolution),
775+
Err(_) => {
786776
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
787777
}
788778
}

compiler/rustc_trait_selection/src/solve/mod.rs

+19-37
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
//! about it on zulip.
1616
use rustc_hir::def_id::DefId;
1717
use rustc_infer::infer::canonical::{Canonical, CanonicalVarValues};
18-
use rustc_infer::infer::DefineOpaqueTypes;
1918
use rustc_infer::traits::query::NoSolution;
2019
use rustc_middle::infer::canonical::CanonicalVarInfos;
2120
use rustc_middle::traits::solve::{
2221
CanonicalResponse, Certainty, ExternalConstraintsData, Goal, GoalSource, IsNormalizesToHack,
2322
QueryResult, Response,
2423
};
25-
use rustc_middle::ty::{self, Ty, TyCtxt, UniverseIndex};
24+
use rustc_middle::ty::{self, AliasRelationDirection, Ty, TyCtxt, UniverseIndex};
2625
use rustc_middle::ty::{
2726
CoercePredicate, RegionOutlivesPredicate, SubtypePredicate, TypeOutlivesPredicate,
2827
};
@@ -285,49 +284,32 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
285284
Ok(self.make_ambiguous_response_no_constraints(maybe_cause))
286285
}
287286

288-
/// Normalize a type when it is structually matched on.
287+
/// Normalize a type for when it is structurally matched on.
289288
///
290-
/// In nearly all cases this function must be used before matching on a type.
289+
/// This function is necessary in nearly all cases before matching on a type.
291290
/// Not doing so is likely to be incomplete and therefore unsound during
292291
/// coherence.
293-
#[instrument(level = "debug", skip(self), ret)]
294-
fn try_normalize_ty(
295-
&mut self,
296-
param_env: ty::ParamEnv<'tcx>,
297-
ty: Ty<'tcx>,
298-
) -> Option<Ty<'tcx>> {
299-
self.try_normalize_ty_recur(param_env, DefineOpaqueTypes::Yes, 0, ty)
300-
}
301-
302-
fn try_normalize_ty_recur(
292+
fn structurally_normalize_ty(
303293
&mut self,
304294
param_env: ty::ParamEnv<'tcx>,
305-
define_opaque_types: DefineOpaqueTypes,
306-
depth: usize,
307295
ty: Ty<'tcx>,
308-
) -> Option<Ty<'tcx>> {
309-
if !self.tcx().recursion_limit().value_within_limit(depth) {
310-
return None;
311-
}
312-
313-
let ty::Alias(_, alias) = *ty.kind() else {
314-
return Some(ty);
315-
};
316-
317-
match self.commit_if_ok(|this| {
318-
let normalized_ty = this.next_ty_infer();
319-
let normalizes_to_goal = Goal::new(
320-
this.tcx(),
296+
) -> Result<Ty<'tcx>, NoSolution> {
297+
if let ty::Alias(..) = ty.kind() {
298+
let normalized_ty = self.next_ty_infer();
299+
let alias_relate_goal = Goal::new(
300+
self.tcx(),
321301
param_env,
322-
ty::NormalizesTo { alias, term: normalized_ty.into() },
302+
ty::PredicateKind::AliasRelate(
303+
ty.into(),
304+
normalized_ty.into(),
305+
AliasRelationDirection::Equate,
306+
),
323307
);
324-
this.add_goal(GoalSource::Misc, normalizes_to_goal);
325-
this.try_evaluate_added_goals()?;
326-
let ty = this.resolve_vars_if_possible(normalized_ty);
327-
Ok(this.try_normalize_ty_recur(param_env, define_opaque_types, depth + 1, ty))
328-
}) {
329-
Ok(ty) => ty,
330-
Err(NoSolution) => Some(ty),
308+
self.add_goal(GoalSource::Misc, alias_relate_goal);
309+
self.try_evaluate_added_goals()?;
310+
Ok(self.resolve_vars_if_possible(normalized_ty))
311+
} else {
312+
Ok(ty)
331313
}
332314
}
333315
}

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

+5-15
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,11 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
5858
}
5959
}
6060

61-
let expected = match self.try_normalize_ty(goal.param_env, expected) {
62-
Some(ty) => {
63-
if ty.is_ty_var() {
64-
return self.evaluate_added_goals_and_make_canonical_response(
65-
Certainty::AMBIGUOUS,
66-
);
67-
} else {
68-
ty
69-
}
70-
}
71-
None => {
72-
return self
73-
.evaluate_added_goals_and_make_canonical_response(Certainty::OVERFLOW);
74-
}
75-
};
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+
}
7666

7767
// Otherwise, define a new opaque type
7868
self.insert_hidden_type(opaque_type_key, goal.param_env, expected)?;

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,11 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
521521
let a_ty = goal.predicate.self_ty();
522522
// We need to normalize the b_ty since it's matched structurally
523523
// in the other functions below.
524-
let b_ty = match ecx
525-
.try_normalize_ty(goal.param_env, goal.predicate.trait_ref.args.type_at(1))
526-
{
527-
Some(b_ty) => b_ty,
528-
None => return vec![misc_candidate(ecx, Certainty::OVERFLOW)],
524+
let Ok(b_ty) = ecx.structurally_normalize_ty(
525+
goal.param_env,
526+
goal.predicate.trait_ref.args.type_at(1),
527+
) else {
528+
return vec![];
529529
};
530530

531531
let goal = goal.with(ecx.tcx(), (a_ty, b_ty));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/recursive-coroutine-boxed.rs:10:23
3+
|
4+
LL | let mut gen = Box::pin(foo());
5+
| ^^^^^^^^ cannot infer type of the type parameter `T` declared on the struct `Box`
6+
LL |
7+
LL | let mut r = gen.as_mut().resume(());
8+
| ------ type must be known at this point
9+
|
10+
help: consider specifying the generic argument
11+
|
12+
LL | let mut gen = Box::<T>::pin(foo());
13+
| +++++
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0282`.

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// check-pass
21
// revisions: current next
2+
//[current] check-pass
33
//[next] compile-flags: -Znext-solver
44
#![feature(coroutines, coroutine_trait)]
55

@@ -8,6 +8,7 @@ use std::ops::{Coroutine, CoroutineState};
88
fn foo() -> impl Coroutine<Yield = (), Return = ()> {
99
|| {
1010
let mut gen = Box::pin(foo());
11+
//[next]~^ ERROR type annotations needed
1112
let mut r = gen.as_mut().resume(());
1213
while let CoroutineState::Yielded(v) = r {
1314
yield v;

tests/ui/impl-trait/two_tait_defining_each_other2.next.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0284]: type annotations needed: cannot satisfy `A <: B`
1+
error[E0284]: type annotations needed: cannot satisfy `A == B`
22
--> $DIR/two_tait_defining_each_other2.rs:11:5
33
|
44
LL | x // B's hidden type is A (opaquely)
5-
| ^ cannot satisfy `A <: B`
5+
| ^ cannot satisfy `A == B`
66

77
error: aborting due to 1 previous error
88

tests/ui/impl-trait/two_tait_defining_each_other2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trait Foo {}
1010
fn muh(x: A) -> B {
1111
x // B's hidden type is A (opaquely)
1212
//[current]~^ ERROR opaque type's hidden type cannot be another opaque type
13-
//[next]~^^ ERROR type annotations needed: cannot satisfy `A <: B`
13+
//[next]~^^ ERROR type annotations needed: cannot satisfy `A == B`
1414
}
1515

1616
struct Bar;

tests/ui/traits/next-solver/deduce-closure-signature-after-normalization.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// compile-flags: -Znext-solver
2-
// check-pass
3-
2+
// FIXME(-Znext-solver): This test is currently broken because the `deduce_closure_signature`
3+
// is unable to look at nested obligations.
44
trait Foo {
55
fn test() -> impl Fn(u32) -> u32 {
66
|x| x.count_ones()
7+
//~^ ERROR type annotations needed
78
}
89
}
910

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/deduce-closure-signature-after-normalization.rs:6:10
3+
|
4+
LL | |x| x.count_ones()
5+
| ^ - type must be known at this point
6+
|
7+
help: consider giving this closure parameter an explicit type
8+
|
9+
LL | |x: /* Type */| x.count_ones()
10+
| ++++++++++++
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0282`.
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
error[E0283]: type annotations needed: cannot satisfy `Foo: Send`
1+
error[E0284]: type annotations needed: cannot satisfy `Foo == _`
22
--> $DIR/dont-type_of-tait-in-defining-scope.rs:15:18
33
|
44
LL | needs_send::<Foo>();
5-
| ^^^
6-
|
7-
= note: cannot satisfy `Foo: Send`
8-
note: required by a bound in `needs_send`
9-
--> $DIR/dont-type_of-tait-in-defining-scope.rs:12:18
10-
|
11-
LL | fn needs_send<T: Send>() {}
12-
| ^^^^ required by this bound in `needs_send`
5+
| ^^^ cannot satisfy `Foo == _`
136

147
error: aborting due to 1 previous error
158

16-
For more information about this error, try `rustc --explain E0283`.
9+
For more information about this error, try `rustc --explain E0284`.
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
error[E0283]: type annotations needed: cannot satisfy `Foo: Send`
1+
error[E0284]: type annotations needed: cannot satisfy `Foo == _`
22
--> $DIR/dont-type_of-tait-in-defining-scope.rs:15:18
33
|
44
LL | needs_send::<Foo>();
5-
| ^^^
6-
|
7-
= note: cannot satisfy `Foo: Send`
8-
note: required by a bound in `needs_send`
9-
--> $DIR/dont-type_of-tait-in-defining-scope.rs:12:18
10-
|
11-
LL | fn needs_send<T: Send>() {}
12-
| ^^^^ required by this bound in `needs_send`
5+
| ^^^ cannot satisfy `Foo == _`
136

147
error: aborting due to 1 previous error
158

16-
For more information about this error, try `rustc --explain E0283`.
9+
For more information about this error, try `rustc --explain E0284`.

tests/ui/traits/next-solver/dont-type_of-tait-in-defining-scope.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn needs_send<T: Send>() {}
1313

1414
fn test(_: Foo) {
1515
needs_send::<Foo>();
16-
//~^ ERROR type annotations needed: cannot satisfy `Foo: Send`
16+
//~^ ERROR type annotations needed: cannot satisfy `Foo == _`
1717
}
1818

1919
fn defines(_: Foo) {

0 commit comments

Comments
 (0)