-
Couldn't load subscription status.
- Fork 13.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
| }); | ||
|
|
||
| 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() {} |
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.