Skip to content

Commit b44a1b0

Browse files
uwu
1 parent a932eb3 commit b44a1b0

File tree

2 files changed

+64
-4
lines changed
  • compiler/rustc_next_trait_solver/src/solve

2 files changed

+64
-4
lines changed

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

+56-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use derive_where::derive_where;
66
use rustc_type_ir::inherent::*;
77
use rustc_type_ir::lang_items::TraitSolverLangItem;
88
use rustc_type_ir::{
9-
self as ty, Interner, TypeFoldable, TypeVisitableExt as _, TypingMode, Upcast as _, elaborate,
9+
self as ty, Interner, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt as _,
10+
TypingMode, Upcast as _, elaborate,
1011
};
11-
use tracing::{debug, instrument};
12+
use tracing::instrument;
1213

1314
use super::has_only_region_constraints;
1415
use super::trait_goals::TraitGoalProvenVia;
@@ -321,8 +322,7 @@ where
321322
};
322323

323324
if normalized_self_ty.is_ty_var() {
324-
debug!("self type has been normalized to infer");
325-
return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect();
325+
return self.try_assemble_bounds_via_registered_opaque(goal, normalized_self_ty);
326326
}
327327

328328
let goal: Goal<I, G> =
@@ -836,6 +836,58 @@ where
836836
}
837837
}
838838

839+
fn try_assemble_bounds_via_registered_opaque<G: GoalKind<D>>(
840+
&mut self,
841+
goal: Goal<I, G>,
842+
self_ty: I::Ty,
843+
) -> Vec<Candidate<I>> {
844+
let Some(alias_ty) = self.find_sup_as_registered_opaque(self_ty) else {
845+
return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect();
846+
};
847+
848+
let mut candidates = vec![];
849+
for item_bound in
850+
self.cx().item_self_bounds(alias_ty.def_id).iter_instantiated(self.cx(), alias_ty.args)
851+
{
852+
// TODO: comment
853+
let assumption =
854+
item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty });
855+
candidates.extend(G::probe_and_match_goal_against_assumption(
856+
self,
857+
CandidateSource::AliasBound,
858+
goal,
859+
assumption,
860+
|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS),
861+
));
862+
}
863+
864+
struct ReplaceOpaque<I: Interner> {
865+
cx: I,
866+
alias_ty: ty::AliasTy<I>,
867+
self_ty: I::Ty,
868+
}
869+
impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
870+
fn cx(&self) -> I {
871+
self.cx
872+
}
873+
fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
874+
if let ty::Alias(ty::Opaque, alias_ty) = ty.kind() {
875+
if alias_ty == self.alias_ty {
876+
return self.self_ty;
877+
}
878+
}
879+
ty.super_fold_with(self)
880+
}
881+
}
882+
883+
// TODO:
884+
if candidates.is_empty() {
885+
candidates.extend(self.forced_ambiguity(MaybeCause::Ambiguity));
886+
}
887+
888+
candidates
889+
}
890+
839891
/// Assemble and merge candidates for goals which are related to an underlying trait
840892
/// goal. Right now, this is normalizes-to and host effect goals.
841893
///

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

+8
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,14 @@ where
10981098
) -> Result<Certainty, NoSolution> {
10991099
self.delegate.is_transmutable(dst, src, assume)
11001100
}
1101+
1102+
pub(crate) fn find_sup_as_registered_opaque(&self, self_ty: I::Ty) -> Option<ty::AliasTy<I>> {
1103+
self.delegate
1104+
.clone_opaque_types_for_query_response()
1105+
.into_iter()
1106+
.find(|(_, hidden_ty)| *hidden_ty == self_ty)
1107+
.map(|(key, _)| ty::AliasTy::new_from_args(self.cx(), key.def_id.into(), key.args))
1108+
}
11011109
}
11021110

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

0 commit comments

Comments
 (0)