Skip to content

Commit c5c610a

Browse files
committed
Auto merge of #93652 - spastorino:fix-negative-overlap-check-regions, r=nikomatsakis
Fix negative overlap check regions r? `@nikomatsakis`
2 parents 52dd59e + 3c7fa0b commit c5c610a

File tree

17 files changed

+134
-33
lines changed

17 files changed

+134
-33
lines changed

compiler/rustc_data_structures/src/snapshot_map/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod tests;
1313
pub type SnapshotMapStorage<K, V> = SnapshotMap<K, V, FxHashMap<K, V>, ()>;
1414
pub type SnapshotMapRef<'a, K, V, L> = SnapshotMap<K, V, &'a mut FxHashMap<K, V>, &'a mut L>;
1515

16+
#[derive(Clone)]
1617
pub struct SnapshotMap<K, V, M = FxHashMap<K, V>, L = VecLog<UndoLog<K, V>>> {
1718
map: M,
1819
undo_log: L,
@@ -30,6 +31,7 @@ where
3031
}
3132
}
3233

34+
#[derive(Clone)]
3335
pub enum UndoLog<K, V> {
3436
Inserted(K),
3537
Overwrite(K, V),

compiler/rustc_infer/src/infer/at.rs

+22
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
5151
) -> At<'a, 'tcx> {
5252
At { infcx: self, cause, param_env }
5353
}
54+
55+
/// Forks the inference context, creating a new inference context with the same inference
56+
/// variables in the same state. This can be used to "branch off" many tests from the same
57+
/// common state. Used in coherence.
58+
pub fn fork(&self) -> Self {
59+
Self {
60+
tcx: self.tcx.clone(),
61+
defining_use_anchor: self.defining_use_anchor.clone(),
62+
in_progress_typeck_results: self.in_progress_typeck_results.clone(),
63+
inner: self.inner.clone(),
64+
skip_leak_check: self.skip_leak_check.clone(),
65+
lexical_region_resolutions: self.lexical_region_resolutions.clone(),
66+
selection_cache: self.selection_cache.clone(),
67+
evaluation_cache: self.evaluation_cache.clone(),
68+
reported_trait_errors: self.reported_trait_errors.clone(),
69+
reported_closure_mismatch: self.reported_closure_mismatch.clone(),
70+
tainted_by_errors_flag: self.tainted_by_errors_flag.clone(),
71+
err_count_on_creation: self.err_count_on_creation,
72+
in_snapshot: self.in_snapshot.clone(),
73+
universe: self.universe.clone(),
74+
}
75+
}
5476
}
5577

5678
pub trait ToTrace<'tcx>: Relate<'tcx> + Copy {

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub(crate) fn resolve<'tcx>(
6161

6262
/// Contains the result of lexical region resolution. Offers methods
6363
/// to lookup up the final value of a region variable.
64+
#[derive(Clone)]
6465
pub struct LexicalRegionResolutions<'tcx> {
6566
values: IndexVec<RegionVid, VarValue<'tcx>>,
6667
error_region: ty::Region<'tcx>,

compiler/rustc_infer/src/infer/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ impl RegionckMode {
130130
/// `RefCell` and are involved with taking/rolling back snapshots. Snapshot
131131
/// operations are hot enough that we want only one call to `borrow_mut` per
132132
/// call to `start_snapshot` and `rollback_to`.
133+
#[derive(Clone)]
133134
pub struct InferCtxtInner<'tcx> {
134135
/// Cache for projections. This cache is snapshotted along with the infcx.
135136
///

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod leak_check;
2828

2929
pub use rustc_middle::infer::MemberConstraint;
3030

31-
#[derive(Default)]
31+
#[derive(Clone, Default)]
3232
pub struct RegionConstraintStorage<'tcx> {
3333
/// For each `RegionVid`, the corresponding `RegionVariableOrigin`.
3434
var_infos: IndexVec<RegionVid, RegionVariableInfo>,

compiler/rustc_infer/src/infer/type_variable.rs

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::ops::Range;
1414
use rustc_data_structures::undo_log::{Rollback, UndoLogs};
1515

1616
/// Represents a single undo-able action that affects a type inference variable.
17+
#[derive(Clone)]
1718
pub(crate) enum UndoLog<'tcx> {
1819
EqRelation(sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>),
1920
SubRelation(sv::UndoLog<ut::Delegate<ty::TyVid>>),
@@ -58,6 +59,7 @@ impl<'tcx> Rollback<UndoLog<'tcx>> for TypeVariableStorage<'tcx> {
5859
}
5960
}
6061

62+
#[derive(Clone)]
6163
pub struct TypeVariableStorage<'tcx> {
6264
values: sv::SnapshotVecStorage<Delegate>,
6365

@@ -137,6 +139,7 @@ pub enum TypeVariableOriginKind {
137139
LatticeVariable,
138140
}
139141

142+
#[derive(Clone)]
140143
pub(crate) struct TypeVariableData {
141144
origin: TypeVariableOrigin,
142145
}
@@ -165,6 +168,7 @@ impl<'tcx> TypeVariableValue<'tcx> {
165168
}
166169
}
167170

171+
#[derive(Clone)]
168172
pub(crate) struct Instantiate;
169173

170174
pub(crate) struct Delegate;

compiler/rustc_infer/src/infer/undo_log.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct Snapshot<'tcx> {
1717
}
1818

1919
/// Records the "undo" data for a single operation that affects some form of inference variable.
20+
#[derive(Clone)]
2021
pub(crate) enum UndoLog<'tcx> {
2122
TypeVariables(type_variable::UndoLog<'tcx>),
2223
ConstUnificationTable(sv::UndoLog<ut::Delegate<ty::ConstVid<'tcx>>>),
@@ -84,6 +85,7 @@ impl<'tcx> Rollback<UndoLog<'tcx>> for InferCtxtInner<'tcx> {
8485

8586
/// The combined undo log for all the various unification tables. For each change to the storage
8687
/// for any kind of inference variable, we record an UndoLog entry in the vector here.
88+
#[derive(Clone)]
8789
pub(crate) struct InferCtxtUndoLogs<'tcx> {
8890
logs: Vec<UndoLog<'tcx>>,
8991
num_open_snapshots: usize,

compiler/rustc_infer/src/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub struct ProjectionCache<'a, 'tcx> {
7070
undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
7171
}
7272

73-
#[derive(Default)]
73+
#[derive(Clone, Default)]
7474
pub struct ProjectionCacheStorage<'tcx> {
7575
map: SnapshotMapStorage<ProjectionCacheKey<'tcx>, ProjectionCacheEntry<'tcx>>,
7676
}

compiler/rustc_trait_selection/src/traits/coherence.rs

+52-25
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44
//! [trait-resolution]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html
55
//! [trait-specialization]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html
66
7-
use crate::infer::{CombinedSnapshot, InferOk, TyCtxtInferExt};
8-
use crate::traits::query::evaluate_obligation::InferCtxtExt;
7+
use crate::infer::outlives::env::OutlivesEnvironment;
8+
use crate::infer::{CombinedSnapshot, InferOk, RegionckMode};
99
use crate::traits::select::IntercrateAmbiguityCause;
1010
use crate::traits::util::impl_trait_ref_and_oblig;
1111
use crate::traits::SkipLeakCheck;
1212
use crate::traits::{
1313
self, FulfillmentContext, Normalized, Obligation, ObligationCause, PredicateObligation,
1414
PredicateObligations, SelectionContext,
1515
};
16+
//use rustc_data_structures::fx::FxHashMap;
1617
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
18+
use rustc_hir::CRATE_HIR_ID;
19+
use rustc_infer::infer::TyCtxtInferExt;
20+
use rustc_infer::traits::TraitEngine;
1721
use rustc_middle::traits::specialization_graph::OverlapMode;
1822
use rustc_middle::ty::fast_reject::{self, SimplifyParams};
1923
use rustc_middle::ty::fold::TypeFoldable;
@@ -150,7 +154,10 @@ fn overlap<'cx, 'tcx>(
150154
impl2_def_id: DefId,
151155
overlap_mode: OverlapMode,
152156
) -> Option<OverlapResult<'tcx>> {
153-
debug!("overlap(impl1_def_id={:?}, impl2_def_id={:?})", impl1_def_id, impl2_def_id);
157+
debug!(
158+
"overlap(impl1_def_id={:?}, impl2_def_id={:?}, overlap_mode={:?})",
159+
impl1_def_id, impl2_def_id, overlap_mode
160+
);
154161

155162
selcx.infcx().probe_maybe_skip_leak_check(skip_leak_check.is_yes(), |snapshot| {
156163
overlap_within_probe(
@@ -191,9 +198,6 @@ fn overlap_within_probe<'cx, 'tcx>(
191198
let impl1_header = with_fresh_ty_vars(selcx, param_env, impl1_def_id);
192199
let impl2_header = with_fresh_ty_vars(selcx, param_env, impl2_def_id);
193200

194-
debug!("overlap: impl1_header={:?}", impl1_header);
195-
debug!("overlap: impl2_header={:?}", impl2_header);
196-
197201
let obligations = equate_impl_headers(selcx, &impl1_header, &impl2_header)?;
198202
debug!("overlap: unification check succeeded");
199203

@@ -226,6 +230,7 @@ fn equate_impl_headers<'cx, 'tcx>(
226230
impl2_header: &ty::ImplHeader<'tcx>,
227231
) -> Option<PredicateObligations<'tcx>> {
228232
// Do `a` and `b` unify? If not, no overlap.
233+
debug!("equate_impl_headers(impl1_header={:?}, impl2_header={:?}", impl1_header, impl2_header);
229234
selcx
230235
.infcx()
231236
.at(&ObligationCause::dummy(), ty::ParamEnv::empty())
@@ -264,8 +269,11 @@ fn implicit_negative<'cx, 'tcx>(
264269
// If the obligation `&'?a str: Error` holds, it means that there's overlap. If that doesn't
265270
// hold we need to check if `&'?a str: !Error` holds, if doesn't hold there's overlap because
266271
// at some point an impl for `&'?a str: Error` could be added.
272+
debug!(
273+
"implicit_negative(impl1_header={:?}, impl2_header={:?}, obligations={:?})",
274+
impl1_header, impl2_header, obligations
275+
);
267276
let infcx = selcx.infcx();
268-
let tcx = infcx.tcx;
269277
let opt_failing_obligation = impl1_header
270278
.predicates
271279
.iter()
@@ -279,12 +287,7 @@ fn implicit_negative<'cx, 'tcx>(
279287
predicate: p,
280288
})
281289
.chain(obligations)
282-
.find(|o| {
283-
loose_check(selcx, o) || tcx.features().negative_impls && negative_impl_exists(selcx, o)
284-
});
285-
// FIXME: the call to `selcx.predicate_may_hold_fatal` above should be ported
286-
// to the canonical trait query form, `infcx.predicate_may_hold`, once
287-
// the new system supports intercrate mode (which coherence needs).
290+
.find(|o| !selcx.predicate_may_hold_fatal(o));
288291

289292
if let Some(failing_obligation) = opt_failing_obligation {
290293
debug!("overlap: obligation unsatisfiable {:?}", failing_obligation);
@@ -301,6 +304,7 @@ fn negative_impl<'cx, 'tcx>(
301304
impl1_def_id: DefId,
302305
impl2_def_id: DefId,
303306
) -> bool {
307+
debug!("negative_impl(impl1_def_id={:?}, impl2_def_id={:?})", impl1_def_id, impl2_def_id);
304308
let tcx = selcx.infcx().tcx;
305309

306310
// create a parameter environment corresponding to a (placeholder) instantiation of impl1
@@ -348,7 +352,7 @@ fn negative_impl<'cx, 'tcx>(
348352
let opt_failing_obligation = obligations
349353
.into_iter()
350354
.chain(more_obligations)
351-
.find(|o| negative_impl_exists(selcx, o));
355+
.find(|o| negative_impl_exists(selcx, impl1_env, impl1_def_id, o));
352356

353357
if let Some(failing_obligation) = opt_failing_obligation {
354358
debug!("overlap: obligation unsatisfiable {:?}", failing_obligation);
@@ -359,24 +363,47 @@ fn negative_impl<'cx, 'tcx>(
359363
})
360364
}
361365

362-
fn loose_check<'cx, 'tcx>(
363-
selcx: &mut SelectionContext<'cx, 'tcx>,
364-
o: &PredicateObligation<'tcx>,
365-
) -> bool {
366-
!selcx.predicate_may_hold_fatal(o)
367-
}
368-
369366
fn negative_impl_exists<'cx, 'tcx>(
370367
selcx: &SelectionContext<'cx, 'tcx>,
368+
param_env: ty::ParamEnv<'tcx>,
369+
region_context: DefId,
371370
o: &PredicateObligation<'tcx>,
372371
) -> bool {
373-
let infcx = selcx.infcx();
372+
let infcx = &selcx.infcx().fork();
374373
let tcx = infcx.tcx;
375374
o.flip_polarity(tcx)
376-
.as_ref()
377375
.map(|o| {
378-
// FIXME This isn't quite correct, regions should be included
379-
selcx.infcx().predicate_must_hold_modulo_regions(o)
376+
let mut fulfillment_cx = FulfillmentContext::new();
377+
fulfillment_cx.register_predicate_obligation(infcx, o);
378+
379+
let errors = fulfillment_cx.select_all_or_error(infcx);
380+
if !errors.is_empty() {
381+
return false;
382+
}
383+
384+
let mut outlives_env = OutlivesEnvironment::new(param_env);
385+
// FIXME -- add "assumed to be well formed" types into the `outlives_env`
386+
387+
// "Save" the accumulated implied bounds into the outlives environment
388+
// (due to the FIXME above, there aren't any, but this step is still needed).
389+
// The "body id" is given as `CRATE_HIR_ID`, which is the same body-id used
390+
// by the "dummy" causes elsewhere (body-id is only relevant when checking
391+
// function bodies with closures).
392+
outlives_env.save_implied_bounds(CRATE_HIR_ID);
393+
394+
infcx.process_registered_region_obligations(
395+
outlives_env.region_bound_pairs_map(),
396+
Some(tcx.lifetimes.re_root_empty),
397+
param_env,
398+
);
399+
400+
let errors =
401+
infcx.resolve_regions(region_context, &outlives_env, RegionckMode::default());
402+
if !errors.is_empty() {
403+
return false;
404+
}
405+
406+
true
380407
})
381408
.unwrap_or(false)
382409
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![crate_type = "lib"]
22
#![feature(negative_impls)]
3+
#![feature(with_negative_coherence)]
34

45
pub trait Error {}
56
impl !Error for &str {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(negative_impls)]
2+
3+
// FIXME: this should compile
4+
5+
trait MyPredicate<'a> {}
6+
impl<'a, T> !MyPredicate<'a> for &T where T: 'a {}
7+
trait MyTrait<'a> {}
8+
impl<'a, T: MyPredicate<'a>> MyTrait<'a> for T {}
9+
impl<'a, T> MyTrait<'a> for &'a T {}
10+
//~^ ERROR: conflicting implementations of trait `MyTrait<'_>` for type `&_`
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0119]: conflicting implementations of trait `MyTrait<'_>` for type `&_`
2+
--> $DIR/coherence-negative-outlives-lifetimes.rs:9:1
3+
|
4+
LL | impl<'a, T: MyPredicate<'a>> MyTrait<'a> for T {}
5+
| ---------------------------------------------- first implementation here
6+
LL | impl<'a, T> MyTrait<'a> for &'a T {}
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0119`.

src/test/ui/coherence/coherence-overlap-negate-use-feature-gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// check-pass
22

3-
#![feature(negative_impls)]
3+
#![feature(with_negative_coherence)]
44

55
use std::ops::DerefMut;
66

src/test/ui/coherence/coherence-overlap-negative-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// Check that if we promise to not impl what would overlap it doesn't actually overlap
55

6-
#![feature(negative_impls)]
6+
#![feature(with_negative_coherence)]
77

88
extern crate error_lib as lib;
99
use lib::Error;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
3+
#![feature(negative_impls)]
4+
#![feature(rustc_attrs)]
5+
#![feature(with_negative_coherence)]
6+
7+
#[rustc_strict_coherence]
8+
trait Foo {}
9+
impl<T> !Foo for &T where T: 'static {}
10+
11+
#[rustc_strict_coherence]
12+
trait Bar {}
13+
impl<T: Foo> Bar for T {}
14+
impl<T> Bar for &T where T: 'static {}
15+
16+
fn main() {}

src/test/ui/traits/negative-impls/auxiliary/foreign_trait.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(negative_impls)]
2+
#![feature(with_negative_coherence)]
23

34
pub trait ForeignTrait {}
45

src/test/ui/traits/negative-impls/rely-on-negative-impl-in-coherence.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// check-pass
22

33
#![feature(negative_impls)]
4+
#![feature(with_negative_coherence)]
45

56
// aux-build: foreign_trait.rs
67

@@ -16,8 +17,8 @@
1617
extern crate foreign_trait;
1718
use foreign_trait::ForeignTrait;
1819

19-
trait LocalTrait { }
20-
impl<T: ForeignTrait> LocalTrait for T { }
21-
impl LocalTrait for String { }
20+
trait LocalTrait {}
21+
impl<T: ForeignTrait> LocalTrait for T {}
22+
impl LocalTrait for String {}
2223

23-
fn main() { }
24+
fn main() {}

0 commit comments

Comments
 (0)