-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Handle cycles in overlap with negative impls #109673
Changes from 1 commit
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 |
---|---|---|
|
@@ -120,6 +120,48 @@ pub fn overlapping_impls( | |
Some(overlap(selcx, skip_leak_check, impl1_def_id, impl2_def_id, overlap_mode).unwrap()) | ||
} | ||
|
||
/// Given an impl_def_id that "positively" implement a trait, check if the "negative" holds. | ||
pub fn negative_impl_holds(tcx: TyCtxt<'_>, impl_def_id: DefId, overlap_mode: OverlapMode) -> bool { | ||
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. s/negative_impl_holds/negative_impl_may_hold/ |
||
debug!("negative_impl_holds(impl1_header={:?}, overlap_mode={:?})", impl_def_id, overlap_mode); | ||
// `for<T> (Vec<u32>, T): Trait` | ||
let header = tcx.impl_trait_ref(impl_def_id).unwrap(); | ||
|
||
let infcx = tcx | ||
.infer_ctxt() | ||
.with_opaque_type_inference(DefiningAnchor::Bubble) | ||
.intercrate(true) | ||
.build(); | ||
|
||
// `[?t]` | ||
let infer_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id); | ||
|
||
// `(Vec<u32>, ?t): Trait` | ||
let trait_ref = header.subst(tcx, infer_substs); | ||
|
||
// `(Vec<u32>, ?t): !Trait` | ||
let trait_pred = tcx.mk_predicate(ty::Binder::dummy(ty::PredicateKind::Clause( | ||
ty::Clause::Trait(ty::TraitPredicate { | ||
trait_ref, | ||
constness: ty::BoundConstness::NotConst, | ||
polarity: ty::ImplPolarity::Negative, | ||
}), | ||
))); | ||
|
||
// Ideally we would use param_env(impl_def_id) but that's unsound today. | ||
compiler-errors marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let param_env = ty::ParamEnv::empty(); | ||
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. To do this properly, what we ought to do is get the parameter environment and then apply the impl<A..Z> SomeTrait<T1..Tn> for T0
where WC we want to test if...
this would be an error, because that means that for at least some instance of this positive impl, there exists a negative impl. Problem is: applying It should be sound to not assume WC is true, because proving 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. inference variables in the param env generally cause issues, especially in the old solver. Don't remember exactly what breaks but iirc @BoxyUwU encountered a bunch of issues recently when she tried it. I think a bigger issue is that the way |
||
|
||
let selcx = &mut SelectionContext::new(&infcx); | ||
selcx | ||
.evaluate_root_obligation(&Obligation::new( | ||
tcx, | ||
ObligationCause::dummy(), | ||
param_env, | ||
trait_pred, | ||
)) | ||
.expect("Overflow should be caught earlier in standard query mode") | ||
.must_apply_modulo_regions() | ||
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. Why use this over Sorry if this question is misunderstanding what this PR is trying to do. 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. The idea if I got everything correct is that we want to check that there's not a negative impl that applies. This is why I wanted to check for a must. But this is one of the things that I have doubts if using 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. This should be may apply modulo regions, not must apply. |
||
} | ||
|
||
fn with_fresh_ty_vars<'cx, 'tcx>( | ||
selcx: &mut SelectionContext<'cx, 'tcx>, | ||
param_env: ty::ParamEnv<'tcx>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,6 +226,22 @@ fn overlap_check_considering_specialization<'tcx>( | |
return Ok(OverlapResult::SpecializeAll(replace_children)); | ||
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. Block: early returns |
||
} | ||
|
||
if overlap_mode.use_negative_impl() | ||
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. remove this if, but before you do, let's try to figure out why the 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. probably because we didn't remove the old coherence logic |
||
&& tcx.impl_polarity(impl_def_id) == ty::ImplPolarity::Positive | ||
&& traits::negative_impl_holds(tcx, impl_def_id, overlap_mode) | ||
{ | ||
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().skip_binder(); | ||
let self_ty = trait_ref.self_ty(); | ||
|
||
return Err(OverlapError { | ||
with_impl: impl_def_id, | ||
trait_ref, | ||
self_ty: self_ty.has_concrete_skeleton().then_some(self_ty), | ||
intercrate_ambiguity_causes: Default::default(), | ||
involves_placeholder: false, | ||
}); | ||
} | ||
|
||
Ok(OverlapResult::NoOverlap(last_lint)) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![feature(trivial_bounds)] | ||
#![feature(negative_impls)] | ||
#![feature(rustc_attrs)] | ||
#![feature(with_negative_coherence)] | ||
#![allow(trivial_bounds)] | ||
|
||
#[rustc_strict_coherence] | ||
trait MyTrait {} | ||
|
||
struct Foo {} | ||
|
||
impl !MyTrait for Foo {} | ||
|
||
impl MyTrait for Foo where Foo: MyTrait {} | ||
//~^ ERROR: conflicting implementations of trait `MyTrait` | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0119]: conflicting implementations of trait `MyTrait` for type `Foo` | ||
--> $DIR/coherence-overlap-negative-cycles.rs:14:1 | ||
| | ||
LL | impl MyTrait for Foo where Foo: MyTrait {} | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| first implementation here | ||
| conflicting implementation for `Foo` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0119`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/implement/implements/