Skip to content
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

Use separate infcx to solve obligations during negative coherence #100789

Merged
merged 1 commit into from
Aug 23, 2022
Merged
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
21 changes: 8 additions & 13 deletions compiler/rustc_trait_selection/src/traits/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,8 @@ fn equate<'cx, 'tcx>(
};

let selcx = &mut SelectionContext::new(&infcx);
let opt_failing_obligation = obligations
.into_iter()
.chain(more_obligations)
.find(|o| negative_impl_exists(selcx, impl_env, o));
let opt_failing_obligation =
obligations.into_iter().chain(more_obligations).find(|o| negative_impl_exists(selcx, o));

if let Some(failing_obligation) = opt_failing_obligation {
debug!("overlap: obligation unsatisfiable {:?}", failing_obligation);
Expand All @@ -359,18 +357,15 @@ fn equate<'cx, 'tcx>(
#[instrument(level = "debug", skip(selcx))]
fn negative_impl_exists<'cx, 'tcx>(
selcx: &SelectionContext<'cx, 'tcx>,
param_env: ty::ParamEnv<'tcx>,
o: &PredicateObligation<'tcx>,
) -> bool {
let infcx = &selcx.infcx().fork();

if resolve_negative_obligation(infcx, param_env, o) {
if resolve_negative_obligation(selcx.infcx().fork(), o) {
return true;
}

// Try to prove a negative obligation exists for super predicates
for o in util::elaborate_predicates(infcx.tcx, iter::once(o.predicate)) {
if resolve_negative_obligation(infcx, param_env, &o) {
for o in util::elaborate_predicates(selcx.tcx(), iter::once(o.predicate)) {
if resolve_negative_obligation(selcx.infcx().fork(), &o) {
return true;
}
}
Expand All @@ -380,8 +375,7 @@ fn negative_impl_exists<'cx, 'tcx>(

#[instrument(level = "debug", skip(infcx))]
fn resolve_negative_obligation<'cx, 'tcx>(
infcx: &InferCtxt<'cx, 'tcx>,
param_env: ty::ParamEnv<'tcx>,
infcx: InferCtxt<'cx, 'tcx>,
o: &PredicateObligation<'tcx>,
) -> bool {
let tcx = infcx.tcx;
Expand All @@ -390,7 +384,8 @@ fn resolve_negative_obligation<'cx, 'tcx>(
return false;
};

let errors = super::fully_solve_obligation(infcx, o);
let param_env = o.param_env;
let errors = super::fully_solve_obligation(&infcx, o);
if !errors.is_empty() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// revisions: stock with_negative_coherence
Copy link
Member Author

Choose a reason for hiding this comment

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

I just made this into a separate revision instead of a different test since it kinda makes sense for this to also be tracked here

#![feature(negative_impls)]
#![cfg_attr(with_negative_coherence, feature(with_negative_coherence))]

// FIXME: this should compile

trait MyPredicate<'a> {}
impl<'a, T> !MyPredicate<'a> for &T where T: 'a {}

impl<'a, T> !MyPredicate<'a> for &'a T where T: 'a {}
Copy link
Member Author

Choose a reason for hiding this comment

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

for &'a T seems like a better representation of the case which should pass, but let me know and I can revert. This ICE happens regardless of it, so it doesn't affect the test for the ICE I'm fixing.


trait MyTrait<'a> {}

impl<'a, T: MyPredicate<'a>> MyTrait<'a> for T {}
impl<'a, T> MyTrait<'a> for &'a T {}
//~^ ERROR: conflicting implementations of trait `MyTrait<'_>` for type `&_`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0119]: conflicting implementations of trait `MyTrait<'_>` for type `&_`
--> $DIR/coherence-negative-outlives-lifetimes.rs:9:1
--> $DIR/coherence-negative-outlives-lifetimes.rs:14:1
|
LL | impl<'a, T: MyPredicate<'a>> MyTrait<'a> for T {}
| ---------------------------------------------- first implementation here
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0119]: conflicting implementations of trait `MyTrait<'_>` for type `&_`
--> $DIR/coherence-negative-outlives-lifetimes.rs:14:1
|
LL | impl<'a, T: MyPredicate<'a>> MyTrait<'a> for T {}
| ---------------------------------------------- first implementation here
LL | impl<'a, T> MyTrait<'a> for &'a T {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0119`.