Skip to content

Commit 848de8e

Browse files
Rollup merge of rust-lang#117994 - compiler-errors:throw-away-regions-in-coherence, r=lcnr
Ignore but do not assume region obligations from unifying headers in negative coherence Partly addresses a FIXME that was added in rust-lang#112875. Just as we can throw away the nested trait/projection obligations from unifying two impl headers, we can also just throw away the region obligations too. I removed part of the FIXME that was incorrect, namely: > Given that the only region constraints we get are involving inference regions in the root, it shouldn't matter, but still sus. This is not true when unifying `fn(A)` and `for<'b> fn(&'b B)` which ends up with placeholder region outlives from non-root universes. I'm pretty sure this is okay, though it would be nice if we were to use them as assumptions. See the `explicit` revision of the test I committed, which still fails. Fixes rust-lang#117986 r? lcnr, feel free to reassign tho.
2 parents e97ff13 + 488dcb7 commit 848de8e

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

compiler/rustc_trait_selection/src/traits/coherence.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,21 @@ fn impl_intersection_has_negative_obligation(
408408

409409
// Equate the headers to find their intersection (the general type, with infer vars,
410410
// that may apply both impls).
411-
let Some(_equate_obligations) =
411+
let Some(equate_obligations) =
412412
equate_impl_headers(infcx, param_env, &impl1_header, &impl2_header)
413413
else {
414414
return false;
415415
};
416416

417417
plug_infer_with_placeholders(infcx, universe, (impl1_header.impl_args, impl2_header.impl_args));
418418

419+
// FIXME(with_negative_coherence): the infcx has constraints from equating
420+
// the impl headers. We should use these constraints as assumptions, not as
421+
// requirements, when proving the negated where clauses below.
422+
drop(equate_obligations);
423+
drop(infcx.take_registered_region_obligations());
424+
drop(infcx.take_and_reset_region_constraints());
425+
419426
util::elaborate(tcx, tcx.predicates_of(impl2_def_id).instantiate(tcx, impl2_header.impl_args))
420427
.any(|(clause, _)| try_prove_negated_where_clause(infcx, clause, param_env))
421428
}
@@ -541,14 +548,6 @@ fn try_prove_negated_where_clause<'tcx>(
541548
return false;
542549
};
543550

544-
// FIXME(with_negative_coherence): the infcx has region contraints from equating
545-
// the impl headers as requirements. Given that the only region constraints we
546-
// get are involving inference regions in the root, it shouldn't matter, but
547-
// still sus.
548-
//
549-
// We probably should just throw away the region obligations registered up until
550-
// now, or ideally use them as assumptions when proving the region obligations
551-
// that we get from proving the negative predicate below.
552551
let ref infcx = root_infcx.fork();
553552
let ocx = ObligationCtxt::new(infcx);
554553

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: conflicting implementations of trait `FnMarker` for type `fn(&_)`
2+
--> $DIR/negative-coherence-placeholder-region-constraints-on-unification.rs:21:1
3+
|
4+
LL | impl<T: ?Sized + Marker> FnMarker for fn(T) {}
5+
| ------------------------------------------- first implementation here
6+
LL | impl<T: ?Sized> FnMarker for fn(&T) {}
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `fn(&_)`
8+
|
9+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
10+
= note: for more information, see issue #56105 <https://github.com/rust-lang/rust/issues/56105>
11+
= note: this behavior recently changed as a result of a bug fix; see rust-lang/rust#56105 for details
12+
note: the lint level is defined here
13+
--> $DIR/negative-coherence-placeholder-region-constraints-on-unification.rs:4:11
14+
|
15+
LL | #![forbid(coherence_leak_check)]
16+
| ^^^^^^^^^^^^^^^^^^^^
17+
18+
error: aborting due to previous error
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// revisions: explicit implicit
2+
//[implicit] check-pass
3+
4+
#![forbid(coherence_leak_check)]
5+
#![feature(negative_impls, with_negative_coherence)]
6+
7+
pub trait Marker {}
8+
9+
#[cfg(implicit)]
10+
impl<T: ?Sized> !Marker for &T {}
11+
12+
#[cfg(explicit)]
13+
impl<'a, T: ?Sized + 'a> !Marker for &'a T {}
14+
15+
trait FnMarker {}
16+
17+
// Unifying these two impls below results in a `T: '!0` obligation
18+
// that we shouldn't need to care about. Ideally, we'd treat that
19+
// as an assumption when proving `&'!0 T: Marker`...
20+
impl<T: ?Sized + Marker> FnMarker for fn(T) {}
21+
impl<T: ?Sized> FnMarker for fn(&T) {}
22+
//[explicit]~^ ERROR conflicting implementations of trait `FnMarker` for type `fn(&_)`
23+
//[explicit]~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
24+
25+
fn main() {}

0 commit comments

Comments
 (0)