Skip to content

Commit 5d234bc

Browse files
uwu
1 parent e358fae commit 5d234bc

File tree

2 files changed

+62
-3
lines changed
  • compiler/rustc_next_trait_solver/src/solve

2 files changed

+62
-3
lines changed

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

+54-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_type_ir::{
1111
self as ty, Interner, TypeFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt as _,
1212
TypeVisitor, TypingMode, Upcast as _, elaborate,
1313
};
14-
use tracing::{debug, instrument};
14+
use tracing::instrument;
1515

1616
use super::trait_goals::TraitGoalProvenVia;
1717
use super::{has_only_region_constraints, has_only_region_constraints, inspect, inspect};
@@ -369,8 +369,7 @@ where
369369
};
370370

371371
if normalized_self_ty.is_ty_var() {
372-
debug!("self type has been normalized to infer");
373-
return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect();
372+
return self.try_assemble_bounds_via_registered_opaque(goal, normalized_self_ty);
374373
}
375374

376375
let goal: Goal<I, G> =
@@ -875,6 +874,58 @@ where
875874
}
876875
}
877876

877+
fn try_assemble_bounds_via_registered_opaque<G: GoalKind<D>>(
878+
&mut self,
879+
goal: Goal<I, G>,
880+
self_ty: I::Ty,
881+
) -> Vec<Candidate<I>> {
882+
let Some(alias_ty) = self.find_sup_as_registered_opaque(self_ty) else {
883+
return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect();
884+
};
885+
886+
let mut candidates = vec![];
887+
for item_bound in
888+
self.cx().item_self_bounds(alias_ty.def_id).iter_instantiated(self.cx(), alias_ty.args)
889+
{
890+
// TODO: comment
891+
let assumption =
892+
item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty });
893+
candidates.extend(G::probe_and_match_goal_against_assumption(
894+
self,
895+
CandidateSource::AliasBound,
896+
goal,
897+
assumption,
898+
|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS),
899+
));
900+
}
901+
902+
struct ReplaceOpaque<I: Interner> {
903+
cx: I,
904+
alias_ty: ty::AliasTy<I>,
905+
self_ty: I::Ty,
906+
}
907+
impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
908+
fn cx(&self) -> I {
909+
self.cx
910+
}
911+
fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
912+
if let ty::Alias(ty::Opaque, alias_ty) = ty.kind() {
913+
if alias_ty == self.alias_ty {
914+
return self.self_ty;
915+
}
916+
}
917+
ty.super_fold_with(self)
918+
}
919+
}
920+
921+
// TODO:
922+
if candidates.is_empty() {
923+
candidates.extend(self.forced_ambiguity(MaybeCause::Ambiguity));
924+
}
925+
926+
candidates
927+
}
928+
878929
/// Assemble and merge candidates for goals which are related to an underlying trait
879930
/// goal. Right now, this is normalizes-to and host effect goals.
880931
///

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

+8
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,14 @@ where
11101110
) -> Result<Certainty, NoSolution> {
11111111
self.delegate.is_transmutable(dst, src, assume)
11121112
}
1113+
1114+
pub(crate) fn find_sup_as_registered_opaque(&self, self_ty: I::Ty) -> Option<ty::AliasTy<I>> {
1115+
self.delegate
1116+
.clone_opaque_types_for_query_response()
1117+
.into_iter()
1118+
.find(|(_, hidden_ty)| *hidden_ty == self_ty)
1119+
.map(|(key, _)| ty::AliasTy::new_from_args(self.cx(), key.def_id.into(), key.args))
1120+
}
11131121
}
11141122

11151123
/// Eagerly replace aliases with inference variables, emitting `AliasRelate`

0 commit comments

Comments
 (0)