Skip to content

Commit 0270afe

Browse files
authored
Rollup merge of #117992 - compiler-errors:sound-but-not-complete, r=lcnr,aliemjay
Don't require intercrate mode for negative coherence Negative coherence needs to be *sound*, but does not need to be *complete*, since it's looking for the *existence* of a negative goal, not the non-existence of a positive goal. This removes some trivial and annoying ambiguities when a negative impl has region constraints. r? lcnr idk if this needs an fcp but if it does, pls kick it off
2 parents 7fd7dad + 253f502 commit 0270afe

10 files changed

+32
-50
lines changed

compiler/rustc_infer/src/infer/at.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,15 @@ impl<'tcx> InferCtxt<'tcx> {
6565

6666
/// Forks the inference context, creating a new inference context with the same inference
6767
/// variables in the same state. This can be used to "branch off" many tests from the same
68-
/// common state. Used in coherence.
68+
/// common state.
6969
pub fn fork(&self) -> Self {
70+
self.fork_with_intercrate(self.intercrate)
71+
}
72+
73+
/// Forks the inference context, creating a new inference context with the same inference
74+
/// variables in the same state, except possibly changing the intercrate mode. This can be
75+
/// used to "branch off" many tests from the same common state. Used in negative coherence.
76+
pub fn fork_with_intercrate(&self, intercrate: bool) -> Self {
7077
Self {
7178
tcx: self.tcx,
7279
defining_use_anchor: self.defining_use_anchor,
@@ -81,7 +88,7 @@ impl<'tcx> InferCtxt<'tcx> {
8188
tainted_by_errors: self.tainted_by_errors.clone(),
8289
err_count_on_creation: self.err_count_on_creation,
8390
universe: self.universe.clone(),
84-
intercrate: self.intercrate,
91+
intercrate,
8592
next_trait_solver: self.next_trait_solver,
8693
}
8794
}

compiler/rustc_trait_selection/src/traits/coherence.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ fn impl_intersection_has_negative_obligation(
397397
) -> bool {
398398
debug!("negative_impl(impl1_def_id={:?}, impl2_def_id={:?})", impl1_def_id, impl2_def_id);
399399

400+
// N.B. We need to unify impl headers *with* intercrate mode, even if proving negative predicates
401+
// do not need intercrate mode enabled.
400402
let ref infcx = tcx.infer_ctxt().intercrate(true).with_next_trait_solver(true).build();
401403
let root_universe = infcx.universe();
402404
assert_eq!(root_universe, ty::UniverseIndex::ROOT);
@@ -415,20 +417,20 @@ fn impl_intersection_has_negative_obligation(
415417
return false;
416418
};
417419

418-
plug_infer_with_placeholders(
419-
infcx,
420-
root_universe,
421-
(impl1_header.impl_args, impl2_header.impl_args),
422-
);
423-
let param_env = infcx.resolve_vars_if_possible(param_env);
424-
425420
// FIXME(with_negative_coherence): the infcx has constraints from equating
426421
// the impl headers. We should use these constraints as assumptions, not as
427422
// requirements, when proving the negated where clauses below.
428423
drop(equate_obligations);
429424
drop(infcx.take_registered_region_obligations());
430425
drop(infcx.take_and_reset_region_constraints());
431426

427+
plug_infer_with_placeholders(
428+
infcx,
429+
root_universe,
430+
(impl1_header.impl_args, impl2_header.impl_args),
431+
);
432+
let param_env = infcx.resolve_vars_if_possible(param_env);
433+
432434
util::elaborate(tcx, tcx.predicates_of(impl2_def_id).instantiate(tcx, impl2_header.impl_args))
433435
.any(|(clause, _)| try_prove_negated_where_clause(infcx, clause, param_env))
434436
}
@@ -554,7 +556,11 @@ fn try_prove_negated_where_clause<'tcx>(
554556
return false;
555557
};
556558

557-
let ref infcx = root_infcx.fork();
559+
// N.B. We don't need to use intercrate mode here because we're trying to prove
560+
// the *existence* of a negative goal, not the non-existence of a positive goal.
561+
// Without this, we over-eagerly register coherence ambiguity candidates when
562+
// impl candidates do exist.
563+
let ref infcx = root_infcx.fork_with_intercrate(false);
558564
let ocx = ObligationCtxt::new(infcx);
559565

560566
ocx.register_obligation(Obligation::new(

tests/ui/coherence/coherence-negative-outlives-lifetimes.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// revisions: stock with_negative_coherence
2+
23
//[with_negative_coherence] known-bug: unknown
4+
// Ideally this would work, but we don't use `&'a T` to imply that `T: 'a`
5+
// which is required for `&'a T: !MyPredicate` to hold. This is similar to the
6+
// test `negative-coherence-placeholder-region-constraints-on-unification.explicit.stderr`
37

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

tests/ui/coherence/coherence-negative-outlives-lifetimes.stock.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0119]: conflicting implementations of trait `MyTrait<'_>` for type `&_`
2-
--> $DIR/coherence-negative-outlives-lifetimes.rs:14:1
2+
--> $DIR/coherence-negative-outlives-lifetimes.rs:18:1
33
|
44
LL | impl<'a, T: MyPredicate<'a>> MyTrait<'a> for T {}
55
| ---------------------------------------------- first implementation here

tests/ui/coherence/coherence-negative-outlives-lifetimes.with_negative_coherence.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0119]: conflicting implementations of trait `MyTrait<'_>` for type `&_`
2-
--> $DIR/coherence-negative-outlives-lifetimes.rs:14:1
2+
--> $DIR/coherence-negative-outlives-lifetimes.rs:18:1
33
|
44
LL | impl<'a, T: MyPredicate<'a>> MyTrait<'a> for T {}
55
| ---------------------------------------------- first implementation here

tests/ui/coherence/coherence-overlap-with-regions.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
// known-bug: unknown
2-
3-
// This fails because we currently perform negative coherence in coherence mode.
4-
// This means that when looking for a negative predicate, we also assemble a
5-
// coherence-unknowable predicate. Since confirming the negative impl has region
6-
// obligations, we don't prefer the impl over the unknowable predicate
7-
// unconditionally and instead flounder.
1+
// check-pass
82

93
#![feature(negative_impls)]
104
#![feature(rustc_attrs)]

tests/ui/coherence/coherence-overlap-with-regions.stderr

-11
This file was deleted.

tests/ui/coherence/negative-coherence-considering-regions.any_lt.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0119]: conflicting implementations of trait `Bar` for type `&_`
2-
--> $DIR/negative-coherence-considering-regions.rs:22:1
2+
--> $DIR/negative-coherence-considering-regions.rs:16:1
33
|
44
LL | impl<T> Bar for T where T: Foo {}
55
| ------------------------------ first implementation here

tests/ui/coherence/negative-coherence-considering-regions.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
// revisions: any_lt static_lt
2-
//[static_lt] known-bug: unknown
3-
4-
// This fails because we currently perform negative coherence in coherence mode.
5-
// This means that when looking for a negative predicate, we also assemble a
6-
// coherence-unknowable predicate. Since confirming the negative impl has region
7-
// obligations, we don't prefer the impl over the unknowable predicate
8-
// unconditionally and instead flounder.
2+
//[static_lt] check-pass
93

104
#![feature(negative_impls)]
115
#![feature(with_negative_coherence)]

tests/ui/coherence/negative-coherence-considering-regions.static_lt.stderr

-12
This file was deleted.

0 commit comments

Comments
 (0)