Skip to content

Commit 588d389

Browse files
authored
Rollup merge of #95039 - spastorino:overlap-super-predicates, r=nikomatsakis
Make negative coherence work when there's impl negative on super predicates r? `@nikomatsakis`
2 parents 0e1b897 + 89a00cc commit 588d389

File tree

6 files changed

+97
-45
lines changed

6 files changed

+97
-45
lines changed

compiler/rustc_middle/src/ty/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,13 @@ pub struct TraitPredicate<'tcx> {
748748

749749
pub constness: BoundConstness,
750750

751+
/// If polarity is Positive: we are proving that the trait is implemented.
752+
///
753+
/// If polarity is Negative: we are proving that a negative impl of this trait
754+
/// exists. (Note that coherence also checks whether negative impls of supertraits
755+
/// exist via a series of predicates.)
756+
///
757+
/// If polarity is Reserved: that's a bug.
751758
pub polarity: ImplPolarity,
752759
}
753760

compiler/rustc_trait_selection/src/traits/coherence.rs

+58-32
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::traits::{
1717
use rustc_errors::Diagnostic;
1818
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1919
use rustc_hir::CRATE_HIR_ID;
20-
use rustc_infer::infer::TyCtxtInferExt;
21-
use rustc_infer::traits::TraitEngine;
20+
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
21+
use rustc_infer::traits::{util, TraitEngine};
2222
use rustc_middle::traits::specialization_graph::OverlapMode;
2323
use rustc_middle::ty::fast_reject::{self, TreatParams};
2424
use rustc_middle::ty::fold::TypeFoldable;
@@ -353,49 +353,75 @@ fn negative_impl<'cx, 'tcx>(
353353
})
354354
}
355355

356+
/// Try to prove that a negative impl exist for the given obligation and their super predicates.
357+
#[instrument(level = "debug", skip(selcx))]
356358
fn negative_impl_exists<'cx, 'tcx>(
357359
selcx: &SelectionContext<'cx, 'tcx>,
358360
param_env: ty::ParamEnv<'tcx>,
359361
region_context: DefId,
360362
o: &PredicateObligation<'tcx>,
361363
) -> bool {
362364
let infcx = &selcx.infcx().fork();
365+
366+
if resolve_negative_obligation(infcx, param_env, region_context, o) {
367+
return true;
368+
}
369+
370+
// Try to prove a negative obligation exist for super predicates
371+
for o in util::elaborate_predicates(infcx.tcx, iter::once(o.predicate)) {
372+
if resolve_negative_obligation(infcx, param_env, region_context, &o) {
373+
return true;
374+
}
375+
}
376+
377+
false
378+
}
379+
380+
#[instrument(level = "debug", skip(infcx))]
381+
fn resolve_negative_obligation<'cx, 'tcx>(
382+
infcx: &InferCtxt<'cx, 'tcx>,
383+
param_env: ty::ParamEnv<'tcx>,
384+
region_context: DefId,
385+
o: &PredicateObligation<'tcx>,
386+
) -> bool {
363387
let tcx = infcx.tcx;
364-
o.flip_polarity(tcx)
365-
.map(|o| {
366-
let mut fulfillment_cx = FulfillmentContext::new();
367-
fulfillment_cx.register_predicate_obligation(infcx, o);
368-
369-
let errors = fulfillment_cx.select_all_or_error(infcx);
370-
if !errors.is_empty() {
371-
return false;
372-
}
373388

374-
let mut outlives_env = OutlivesEnvironment::new(param_env);
375-
// FIXME -- add "assumed to be well formed" types into the `outlives_env`
389+
let Some(o) = o.flip_polarity(tcx) else {
390+
return false;
391+
};
376392

377-
// "Save" the accumulated implied bounds into the outlives environment
378-
// (due to the FIXME above, there aren't any, but this step is still needed).
379-
// The "body id" is given as `CRATE_HIR_ID`, which is the same body-id used
380-
// by the "dummy" causes elsewhere (body-id is only relevant when checking
381-
// function bodies with closures).
382-
outlives_env.save_implied_bounds(CRATE_HIR_ID);
393+
let mut fulfillment_cx = FulfillmentContext::new();
394+
fulfillment_cx.register_predicate_obligation(infcx, o);
383395

384-
infcx.process_registered_region_obligations(
385-
outlives_env.region_bound_pairs_map(),
386-
Some(tcx.lifetimes.re_root_empty),
387-
param_env,
388-
);
396+
let errors = fulfillment_cx.select_all_or_error(infcx);
389397

390-
let errors =
391-
infcx.resolve_regions(region_context, &outlives_env, RegionckMode::default());
392-
if !errors.is_empty() {
393-
return false;
394-
}
398+
if !errors.is_empty() {
399+
return false;
400+
}
395401

396-
true
397-
})
398-
.unwrap_or(false)
402+
let mut outlives_env = OutlivesEnvironment::new(param_env);
403+
// FIXME -- add "assumed to be well formed" types into the `outlives_env`
404+
405+
// "Save" the accumulated implied bounds into the outlives environment
406+
// (due to the FIXME above, there aren't any, but this step is still needed).
407+
// The "body id" is given as `CRATE_HIR_ID`, which is the same body-id used
408+
// by the "dummy" causes elsewhere (body-id is only relevant when checking
409+
// function bodies with closures).
410+
outlives_env.save_implied_bounds(CRATE_HIR_ID);
411+
412+
infcx.process_registered_region_obligations(
413+
outlives_env.region_bound_pairs_map(),
414+
Some(tcx.lifetimes.re_root_empty),
415+
param_env,
416+
);
417+
418+
let errors = infcx.resolve_regions(region_context, &outlives_env, RegionckMode::default());
419+
420+
if !errors.is_empty() {
421+
return false;
422+
}
423+
424+
true
399425
}
400426

401427
pub fn trait_ref_is_knowable<'tcx>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-pass
2+
3+
#![feature(negative_impls)]
4+
#![feature(with_negative_coherence)]
5+
6+
trait A {}
7+
trait B: A {}
8+
9+
impl !A for u32 {}
10+
impl !B for u32 {}
11+
12+
fn main() {}

src/test/ui/coherence/coherence-overlap-negate-alias-strict.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// check-pass
2+
13
#![feature(negative_impls)]
24
#![feature(rustc_attrs)]
35
#![feature(trait_alias)]
@@ -13,7 +15,5 @@ impl !A for u32 {}
1315
trait C {}
1416
impl<T: AB> C for T {}
1517
impl C for u32 {}
16-
//~^ ERROR: conflicting implementations of trait `C` for type `u32` [E0119]
17-
// FIXME this should work, we should implement an `assemble_neg_candidates` fn
1818

1919
fn main() {}

src/test/ui/coherence/coherence-overlap-negate-alias-strict.stderr

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// check-pass
2+
3+
#![feature(negative_impls)]
4+
#![feature(rustc_attrs)]
5+
#![feature(with_negative_coherence)]
6+
7+
trait Trait1: Trait2 {}
8+
trait Trait2 {}
9+
10+
struct MyType {}
11+
impl !Trait2 for MyType {}
12+
13+
#[rustc_strict_coherence]
14+
trait Foo {}
15+
impl<T: Trait1> Foo for T {}
16+
impl Foo for MyType {}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)