From b44a1b0022fe6483f5d280727e3ad1c7963bb0cf Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 28 Apr 2025 18:16:34 +0000 Subject: [PATCH] uwu --- .../src/solve/assembly/mod.rs | 60 +++++++++++++++++-- .../src/solve/eval_ctxt/mod.rs | 8 +++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs index 381a732b8dee9..dc9d6dbbcee3b 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs @@ -6,9 +6,10 @@ use derive_where::derive_where; use rustc_type_ir::inherent::*; use rustc_type_ir::lang_items::TraitSolverLangItem; use rustc_type_ir::{ - self as ty, Interner, TypeFoldable, TypeVisitableExt as _, TypingMode, Upcast as _, elaborate, + self as ty, Interner, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt as _, + TypingMode, Upcast as _, elaborate, }; -use tracing::{debug, instrument}; +use tracing::instrument; use super::has_only_region_constraints; use super::trait_goals::TraitGoalProvenVia; @@ -321,8 +322,7 @@ where }; if normalized_self_ty.is_ty_var() { - debug!("self type has been normalized to infer"); - return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect(); + return self.try_assemble_bounds_via_registered_opaque(goal, normalized_self_ty); } let goal: Goal = @@ -836,6 +836,58 @@ where } } + fn try_assemble_bounds_via_registered_opaque>( + &mut self, + goal: Goal, + self_ty: I::Ty, + ) -> Vec> { + let Some(alias_ty) = self.find_sup_as_registered_opaque(self_ty) else { + return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect(); + }; + + let mut candidates = vec![]; + for item_bound in + self.cx().item_self_bounds(alias_ty.def_id).iter_instantiated(self.cx(), alias_ty.args) + { + // TODO: comment + let assumption = + item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty }); + candidates.extend(G::probe_and_match_goal_against_assumption( + self, + CandidateSource::AliasBound, + goal, + assumption, + |ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS), + )); + } + + struct ReplaceOpaque { + cx: I, + alias_ty: ty::AliasTy, + self_ty: I::Ty, + } + impl TypeFolder for ReplaceOpaque { + fn cx(&self) -> I { + self.cx + } + fn fold_ty(&mut self, ty: I::Ty) -> I::Ty { + if let ty::Alias(ty::Opaque, alias_ty) = ty.kind() { + if alias_ty == self.alias_ty { + return self.self_ty; + } + } + ty.super_fold_with(self) + } + } + + // TODO: + if candidates.is_empty() { + candidates.extend(self.forced_ambiguity(MaybeCause::Ambiguity)); + } + + candidates + } + /// Assemble and merge candidates for goals which are related to an underlying trait /// goal. Right now, this is normalizes-to and host effect goals. /// diff --git a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs index 6dd554299a697..bc2155a27eb83 100644 --- a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs @@ -1098,6 +1098,14 @@ where ) -> Result { self.delegate.is_transmutable(dst, src, assume) } + + pub(crate) fn find_sup_as_registered_opaque(&self, self_ty: I::Ty) -> Option> { + self.delegate + .clone_opaque_types_for_query_response() + .into_iter() + .find(|(_, hidden_ty)| *hidden_ty == self_ty) + .map(|(key, _)| ty::AliasTy::new_from_args(self.cx(), key.def_id.into(), key.args)) + } } /// Eagerly replace aliases with inference variables, emitting `AliasRelate`