Skip to content

Incompletely prefer opaque type bounds when self type bottoms out in infer #140405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
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;
Expand Down Expand Up @@ -321,8 +322,7 @@
};

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<I, G> =
Expand Down Expand Up @@ -836,6 +836,58 @@
}
}

fn try_assemble_bounds_via_registered_opaque<G: GoalKind<D>>(
&mut self,
goal: Goal<I, G>,
self_ty: I::Ty,
) -> Vec<Candidate<I>> {
let Some(alias_ty) = self.find_sup_as_registered_opaque(self_ty) else {

Check failure on line 844 in compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
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<I: Interner> {
cx: I,
alias_ty: ty::AliasTy<I>,
self_ty: I::Ty,
}
impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
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 {

Check failure on line 875 in compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
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.
///
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,14 @@ where
) -> Result<Certainty, NoSolution> {
self.delegate.is_transmutable(dst, src, assume)
}

pub(crate) fn find_sup_as_registered_opaque(&self, self_ty: I::Ty) -> Option<ty::AliasTy<I>> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) fn find_sup_as_registered_opaque(&self, self_ty: I::Ty) -> Option<ty::AliasTy<I>> {
pub(crate) fn find_sup_as_registered_opaque(&self, self_ty: ty::TyVid) -> Option<ty::AliasTy<I>> {

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`
Expand Down
Loading