-
Notifications
You must be signed in to change notification settings - Fork 13.3k
handle global trait bounds defining assoc types #135766
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ use rustc_type_ir::lang_items::TraitSolverLangItem; | |
use rustc_type_ir::solve::CanonicalResponse; | ||
use rustc_type_ir::visit::TypeVisitableExt as _; | ||
use rustc_type_ir::{self as ty, Interner, TraitPredicate, TypingMode, Upcast as _, elaborate}; | ||
use smallvec::SmallVec; | ||
use tracing::{instrument, trace}; | ||
|
||
use crate::delegate::SolverDelegate; | ||
|
@@ -1199,33 +1198,42 @@ where | |
// nested requirements, over all others. This is a fix for #53123 and | ||
// prevents where-bounds from accidentally extending the lifetime of a | ||
// variable. | ||
if candidates | ||
.iter() | ||
.any(|c| matches!(c.source, CandidateSource::BuiltinImpl(BuiltinImplSource::Trivial))) | ||
{ | ||
let trivial_builtin_impls: SmallVec<[_; 1]> = candidates | ||
.iter() | ||
.filter(|c| { | ||
matches!(c.source, CandidateSource::BuiltinImpl(BuiltinImplSource::Trivial)) | ||
}) | ||
.map(|c| c.result) | ||
.collect(); | ||
let mut trivial_builtin_impls = candidates.iter().filter(|c| { | ||
lcnr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
matches!(c.source, CandidateSource::BuiltinImpl(BuiltinImplSource::Trivial)) | ||
}); | ||
if let Some(candidate) = trivial_builtin_impls.next() { | ||
// There should only ever be a single trivial builtin candidate | ||
// as they would otherwise overlap. | ||
assert_eq!(trivial_builtin_impls.len(), 1); | ||
return if let Some(response) = self.try_merge_responses(&trivial_builtin_impls) { | ||
Ok((response, Some(TraitGoalProvenVia::Misc))) | ||
} else { | ||
Ok((self.bail_with_ambiguity(&trivial_builtin_impls), None)) | ||
}; | ||
assert!(trivial_builtin_impls.next().is_none()); | ||
return Ok((candidate.result, Some(TraitGoalProvenVia::Misc))); | ||
} | ||
|
||
// If there are non-global where-bounds, prefer where-bounds | ||
// (including global ones) over everything else. | ||
let has_non_global_where_bounds = candidates.iter().any(|c| match c.source { | ||
CandidateSource::ParamEnv(idx) => { | ||
let where_bound = goal.param_env.caller_bounds().get(idx); | ||
where_bound.has_bound_vars() || !where_bound.is_global() | ||
let where_bound = goal.param_env.caller_bounds().get(idx).unwrap(); | ||
let ty::ClauseKind::Trait(trait_pred) = where_bound.kind().skip_binder() else { | ||
unreachable!("expected trait-bound: {where_bound:?}"); | ||
}; | ||
|
||
if trait_pred.has_bound_vars() || !trait_pred.is_global() { | ||
return true; | ||
} | ||
|
||
// We don't consider a trait-bound global if it has a projection bound. | ||
// | ||
// See ui/traits/next-solver/normalization-shadowing/global-trait-with-project.rs | ||
// for an example where this is necessary. | ||
for p in goal.param_env.caller_bounds().iter() { | ||
if let ty::ClauseKind::Projection(proj) = p.kind().skip_binder() { | ||
if proj.projection_term.trait_ref(self.cx()) == trait_pred.trait_ref { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. structural equality seems sketchy here 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like, this seems like it will not interact well with HRTBs due to anonymization. |
||
return true; | ||
} | ||
} | ||
} | ||
|
||
false | ||
} | ||
_ => false, | ||
}); | ||
|
21 changes: 21 additions & 0 deletions
21
tests/ui/traits/next-solver/normalization-shadowing/global-trait-with-project.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//@ compile-flags: -Znext-solver | ||
//@ check-pass | ||
|
||
// `(): Trait` is a global where-bound with a projection bound. | ||
// This previously resulted in ambiguity as we considered both | ||
// the impl and the where-bound while normalizing. | ||
|
||
trait Trait { | ||
type Assoc; | ||
} | ||
impl Trait for () { | ||
type Assoc = &'static (); | ||
} | ||
|
||
fn foo<'a>(x: <() as Trait>::Assoc) | ||
where | ||
(): Trait<Assoc = &'a ()>, | ||
{ | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.