Skip to content

Commit 47ebb61

Browse files
committed
next solver cleanups
1 parent 27d403a commit 47ebb61

File tree

12 files changed

+287
-317
lines changed

12 files changed

+287
-317
lines changed

compiler/rustc_next_trait_solver/src/canonicalizer.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,8 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
289289
for var in var_infos.iter_mut() {
290290
// We simply put all regions from the input into the highest
291291
// compressed universe, so we only deal with them at the end.
292-
if !var.is_region() {
293-
if is_existential == var.is_existential() {
294-
update_uv(var, orig_uv, is_existential)
295-
}
292+
if !var.is_region() && is_existential == var.is_existential() {
293+
update_uv(var, orig_uv, is_existential)
296294
}
297295
}
298296
}
@@ -317,40 +315,39 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
317315

318316
fn cached_fold_ty(&mut self, t: I::Ty) -> I::Ty {
319317
let kind = match t.kind() {
320-
ty::Infer(i) => match i {
321-
ty::TyVar(vid) => {
322-
assert_eq!(
323-
self.delegate.opportunistic_resolve_ty_var(vid),
324-
t,
325-
"ty vid should have been resolved fully before canonicalization"
326-
);
318+
ty::Infer(ty::TyVar(vid)) => {
319+
assert_eq!(
320+
self.delegate.opportunistic_resolve_ty_var(vid),
321+
t,
322+
"ty vid should have been resolved fully before canonicalization"
323+
);
324+
325+
CanonicalVarKind::Ty(CanonicalTyVarKind::General(
326+
self.delegate
327+
.universe_of_ty(vid)
328+
.unwrap_or_else(|| panic!("ty var should have been resolved: {t:?}")),
329+
))
330+
}
331+
ty::Infer(ty::IntVar(vid)) => {
332+
assert_eq!(
333+
self.delegate.opportunistic_resolve_int_var(vid),
334+
t,
335+
"ty vid should have been resolved fully before canonicalization"
336+
);
337+
CanonicalVarKind::Ty(CanonicalTyVarKind::Int)
338+
}
339+
ty::Infer(ty::FloatVar(vid)) => {
340+
assert_eq!(
341+
self.delegate.opportunistic_resolve_float_var(vid),
342+
t,
343+
"ty vid should have been resolved fully before canonicalization"
344+
);
345+
CanonicalVarKind::Ty(CanonicalTyVarKind::Float)
346+
}
347+
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
348+
panic!("fresh vars not expected in canonicalization")
349+
}
327350

328-
CanonicalVarKind::Ty(CanonicalTyVarKind::General(
329-
self.delegate
330-
.universe_of_ty(vid)
331-
.unwrap_or_else(|| panic!("ty var should have been resolved: {t:?}")),
332-
))
333-
}
334-
ty::IntVar(vid) => {
335-
assert_eq!(
336-
self.delegate.opportunistic_resolve_int_var(vid),
337-
t,
338-
"ty vid should have been resolved fully before canonicalization"
339-
);
340-
CanonicalVarKind::Ty(CanonicalTyVarKind::Int)
341-
}
342-
ty::FloatVar(vid) => {
343-
assert_eq!(
344-
self.delegate.opportunistic_resolve_float_var(vid),
345-
t,
346-
"ty vid should have been resolved fully before canonicalization"
347-
);
348-
CanonicalVarKind::Ty(CanonicalTyVarKind::Float)
349-
}
350-
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
351-
panic!("fresh vars not expected in canonicalization")
352-
}
353-
},
354351
ty::Placeholder(placeholder) => match self.canonicalize_mode {
355352
CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderTy(
356353
PlaceholderLike::new(placeholder.universe(), self.variables.len().into()),

compiler/rustc_next_trait_solver/src/resolve.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,15 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for EagerResolv
4444
}
4545
ty::Infer(ty::IntVar(vid)) => self.delegate.opportunistic_resolve_int_var(vid),
4646
ty::Infer(ty::FloatVar(vid)) => self.delegate.opportunistic_resolve_float_var(vid),
47-
_ => {
48-
if t.has_infer() {
49-
if let Some(&ty) = self.cache.get(&t) {
50-
return ty;
51-
}
52-
let res = t.super_fold_with(self);
53-
assert!(self.cache.insert(t, res));
54-
res
55-
} else {
56-
t
47+
_ if t.has_infer() => {
48+
if let Some(&ty) = self.cache.get(&t) {
49+
return ty;
5750
}
51+
let res = t.super_fold_with(self);
52+
assert!(self.cache.insert(t, res));
53+
res
5854
}
55+
_ => t,
5956
}
6057
}
6158

@@ -76,13 +73,8 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for EagerResolv
7673
resolved
7774
}
7875
}
79-
_ => {
80-
if c.has_infer() {
81-
c.super_fold_with(self)
82-
} else {
83-
c
84-
}
85-
}
76+
_ if c.has_infer() => c.super_fold_with(self),
77+
_ => c,
8678
}
8779
}
8880
}

compiler/rustc_next_trait_solver/src/solve/alias_relate.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,21 @@ where
3636
let Goal { param_env, predicate: (lhs, rhs, direction) } = goal;
3737
debug_assert!(lhs.to_alias_term().is_some() || rhs.to_alias_term().is_some());
3838

39-
// Structurally normalize the lhs.
40-
let lhs = if let Some(alias) = lhs.to_alias_term() {
41-
let term = self.next_term_infer_of_kind(lhs);
42-
self.add_normalizes_to_goal(goal.with(cx, ty::NormalizesTo { alias, term }));
43-
term
44-
} else {
45-
lhs
39+
let mut normalize = |term: I::Term| {
40+
if let Some(alias) = term.to_alias_term() {
41+
let term = self.next_term_infer_of_kind(term);
42+
self.add_normalizes_to_goal(goal.with(cx, ty::NormalizesTo { alias, term }));
43+
term
44+
} else {
45+
term
46+
}
4647
};
4748

49+
// Structurally normalize the lhs.
50+
let lhs = normalize(lhs);
51+
4852
// Structurally normalize the rhs.
49-
let rhs = if let Some(alias) = rhs.to_alias_term() {
50-
let term = self.next_term_infer_of_kind(rhs);
51-
self.add_normalizes_to_goal(goal.with(cx, ty::NormalizesTo { alias, term }));
52-
term
53-
} else {
54-
rhs
55-
};
53+
let rhs = normalize(rhs);
5654

5755
// Add a `make_canonical_response` probe step so that we treat this as
5856
// a candidate, even if `try_evaluate_added_goals` bails due to an error.
@@ -73,12 +71,10 @@ where
7371
match (lhs.to_alias_term(), rhs.to_alias_term()) {
7472
(None, None) => {
7573
self.relate(param_env, lhs, variance, rhs)?;
76-
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
7774
}
7875

7976
(Some(alias), None) => {
8077
self.relate_rigid_alias_non_alias(param_env, alias, variance, rhs)?;
81-
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
8278
}
8379
(None, Some(alias)) => {
8480
self.relate_rigid_alias_non_alias(
@@ -87,13 +83,12 @@ where
8783
variance.xform(ty::Contravariant),
8884
lhs,
8985
)?;
90-
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
9186
}
9287

9388
(Some(alias_lhs), Some(alias_rhs)) => {
9489
self.relate(param_env, alias_lhs, variance, alias_rhs)?;
95-
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
9690
}
9791
}
92+
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
9893
}
9994
}

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

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -492,15 +492,18 @@ where
492492
goal: Goal<I, G>,
493493
candidates: &mut Vec<Candidate<I>>,
494494
) {
495-
for (i, assumption) in goal.param_env.caller_bounds().iter().enumerate() {
496-
candidates.extend(G::probe_and_consider_implied_clause(
497-
self,
498-
CandidateSource::ParamEnv(i),
499-
goal,
500-
assumption,
501-
[],
502-
));
503-
}
495+
candidates.extend(goal.param_env.caller_bounds().iter().enumerate().filter_map(
496+
|(i, assumption)| {
497+
G::probe_and_consider_implied_clause(
498+
self,
499+
CandidateSource::ParamEnv(i),
500+
goal,
501+
assumption,
502+
[],
503+
)
504+
.ok()
505+
},
506+
));
504507
}
505508

506509
#[instrument(level = "trace", skip_all)]
@@ -509,7 +512,7 @@ where
509512
goal: Goal<I, G>,
510513
candidates: &mut Vec<Candidate<I>>,
511514
) {
512-
let () = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
515+
self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
513516
ecx.assemble_alias_bound_candidates_recur(goal.predicate.self_ty(), goal, candidates);
514517
});
515518
}
@@ -580,17 +583,21 @@ where
580583
}
581584
};
582585

583-
for assumption in
584-
self.cx().item_bounds(alias_ty.def_id).iter_instantiated(self.cx(), alias_ty.args)
585-
{
586-
candidates.extend(G::probe_and_consider_implied_clause(
587-
self,
588-
CandidateSource::AliasBound,
589-
goal,
590-
assumption,
591-
[],
592-
));
593-
}
586+
candidates.extend(
587+
self.cx()
588+
.item_bounds(alias_ty.def_id)
589+
.iter_instantiated(self.cx(), alias_ty.args)
590+
.filter_map(|assumption| {
591+
G::probe_and_consider_implied_clause(
592+
self,
593+
CandidateSource::AliasBound,
594+
goal,
595+
assumption,
596+
[],
597+
)
598+
.ok()
599+
}),
600+
);
594601

595602
candidates.extend(G::consider_additional_alias_assumptions(self, goal, alias_ty));
596603

@@ -599,11 +606,9 @@ where
599606
}
600607

601608
// Recurse on the self type of the projection.
602-
match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
603-
Ok(next_self_ty) => {
604-
self.assemble_alias_bound_candidates_recur(next_self_ty, goal, candidates)
605-
}
606-
Err(NoSolution) => {}
609+
if let Ok(next_self_ty) = self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty())
610+
{
611+
self.assemble_alias_bound_candidates_recur(next_self_ty, goal, candidates)
607612
}
608613
}
609614

@@ -660,36 +665,43 @@ where
660665
// Consider all of the auto-trait and projection bounds, which don't
661666
// need to be recorded as a `BuiltinImplSource::Object` since they don't
662667
// really have a vtable base...
663-
for bound in bounds.iter() {
668+
candidates.extend(bounds.iter().filter_map(|bound| {
664669
match bound.skip_binder() {
665670
ty::ExistentialPredicate::Trait(_) => {
666671
// Skip principal
672+
None
667673
}
668674
ty::ExistentialPredicate::Projection(_)
669675
| ty::ExistentialPredicate::AutoTrait(_) => {
670-
candidates.extend(G::probe_and_consider_object_bound_candidate(
676+
G::probe_and_consider_object_bound_candidate(
671677
self,
672678
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
673679
goal,
674680
bound.with_self_ty(cx, self_ty),
675-
));
681+
)
682+
.ok()
676683
}
677684
}
678-
}
685+
}));
679686

680687
// FIXME: We only need to do *any* of this if we're considering a trait goal,
681688
// since we don't need to look at any supertrait or anything if we are doing
682689
// a projection goal.
683690
if let Some(principal) = bounds.principal() {
684691
let principal_trait_ref = principal.with_self_ty(cx, self_ty);
685-
for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
686-
candidates.extend(G::probe_and_consider_object_bound_candidate(
687-
self,
688-
CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
689-
goal,
690-
assumption.upcast(cx),
691-
));
692-
}
692+
candidates.extend(
693+
elaborate::supertraits(cx, principal_trait_ref).enumerate().filter_map(
694+
|(idx, assumption)| {
695+
G::probe_and_consider_object_bound_candidate(
696+
self,
697+
CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
698+
goal,
699+
assumption.upcast(cx),
700+
)
701+
.ok()
702+
},
703+
),
704+
);
693705
}
694706
}
695707

compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,11 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_callable<I: Intern
342342
args,
343343
sig,
344344
)
345-
} else {
345+
} else if goal_kind != ty::ClosureKind::FnOnce {
346346
// Closure kind is not yet determined, so we return ambiguity unless
347347
// the expected kind is `FnOnce` as that is always implemented.
348-
if goal_kind != ty::ClosureKind::FnOnce {
349-
return Ok(None);
350-
}
351-
348+
return Ok(None);
349+
} else {
352350
coroutine_closure_to_ambiguous_coroutine(
353351
cx,
354352
goal_kind, // No captures by ref, so this doesn't matter.

0 commit comments

Comments
 (0)