From 4e8392459565f245a33b56916e570957dc3d1774 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 2 Feb 2022 14:32:21 -0300 Subject: [PATCH 1/9] Remove extra negative_impl_exists check --- compiler/rustc_trait_selection/src/traits/coherence.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 21775a5c49f09..89909914bc2c5 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -279,9 +279,7 @@ fn implicit_negative<'cx, 'tcx>( predicate: p, }) .chain(obligations) - .find(|o| { - loose_check(selcx, o) || tcx.features().negative_impls && negative_impl_exists(selcx, o) - }); + .find(|o| loose_check(selcx, o)); // FIXME: the call to `selcx.predicate_may_hold_fatal` above should be ported // to the canonical trait query form, `infcx.predicate_may_hold`, once // the new system supports intercrate mode (which coherence needs). From 74c431866be8fd687d534c51f6bd3133ab2e5421 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 2 Feb 2022 14:38:39 -0300 Subject: [PATCH 2/9] Move FIXME text to the right place --- compiler/rustc_trait_selection/src/traits/coherence.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 89909914bc2c5..e474edb8dd131 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -280,9 +280,6 @@ fn implicit_negative<'cx, 'tcx>( }) .chain(obligations) .find(|o| loose_check(selcx, o)); - // FIXME: the call to `selcx.predicate_may_hold_fatal` above should be ported - // to the canonical trait query form, `infcx.predicate_may_hold`, once - // the new system supports intercrate mode (which coherence needs). if let Some(failing_obligation) = opt_failing_obligation { debug!("overlap: obligation unsatisfiable {:?}", failing_obligation); @@ -361,6 +358,9 @@ fn loose_check<'cx, 'tcx>( selcx: &mut SelectionContext<'cx, 'tcx>, o: &PredicateObligation<'tcx>, ) -> bool { + // FIXME: the call to `selcx.predicate_may_hold_fatal` below should be ported + // to the canonical trait query form, `infcx.predicate_may_hold`, once + // the new system supports intercrate mode (which coherence needs). !selcx.predicate_may_hold_fatal(o) } From b61e1bbf06334e235e475652bb41a603584e7150 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 3 Feb 2022 22:40:29 -0300 Subject: [PATCH 3/9] Add debug calls for negative impls in coherence --- .../rustc_trait_selection/src/traits/coherence.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index e474edb8dd131..dd1293f1def91 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -150,7 +150,10 @@ fn overlap<'cx, 'tcx>( impl2_def_id: DefId, overlap_mode: OverlapMode, ) -> Option> { - debug!("overlap(impl1_def_id={:?}, impl2_def_id={:?})", impl1_def_id, impl2_def_id); + debug!( + "overlap(impl1_def_id={:?}, impl2_def_id={:?}, overlap_mode={:?})", + impl1_def_id, impl2_def_id, overlap_mode + ); selcx.infcx().probe_maybe_skip_leak_check(skip_leak_check.is_yes(), |snapshot| { overlap_within_probe( @@ -191,9 +194,6 @@ fn overlap_within_probe<'cx, 'tcx>( let impl1_header = with_fresh_ty_vars(selcx, param_env, impl1_def_id); let impl2_header = with_fresh_ty_vars(selcx, param_env, impl2_def_id); - debug!("overlap: impl1_header={:?}", impl1_header); - debug!("overlap: impl2_header={:?}", impl2_header); - let obligations = equate_impl_headers(selcx, &impl1_header, &impl2_header)?; debug!("overlap: unification check succeeded"); @@ -226,6 +226,7 @@ fn equate_impl_headers<'cx, 'tcx>( impl2_header: &ty::ImplHeader<'tcx>, ) -> Option> { // Do `a` and `b` unify? If not, no overlap. + debug!("equate_impl_headers(impl1_header={:?}, impl2_header={:?}", impl1_header, impl2_header); selcx .infcx() .at(&ObligationCause::dummy(), ty::ParamEnv::empty()) @@ -264,6 +265,10 @@ fn implicit_negative<'cx, 'tcx>( // If the obligation `&'?a str: Error` holds, it means that there's overlap. If that doesn't // hold we need to check if `&'?a str: !Error` holds, if doesn't hold there's overlap because // at some point an impl for `&'?a str: Error` could be added. + debug!( + "implicit_negative(impl1_header={:?}, impl2_header={:?}, obligations={:?})", + impl1_header, impl2_header, obligations + ); let infcx = selcx.infcx(); let tcx = infcx.tcx; let opt_failing_obligation = impl1_header @@ -296,6 +301,7 @@ fn negative_impl<'cx, 'tcx>( impl1_def_id: DefId, impl2_def_id: DefId, ) -> bool { + debug!("negative_impl(impl1_def_id={:?}, impl2_def_id={:?})", impl1_def_id, impl2_def_id); let tcx = selcx.infcx().tcx; // create a parameter environment corresponding to a (placeholder) instantiation of impl1 From 3fd89a662a8360bbcd64da1353be8bf3b8833af5 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 2 Feb 2022 14:36:45 -0300 Subject: [PATCH 4/9] Properly check regions on negative overlap check --- .../src/traits/coherence.rs | 42 +++++++++++++++---- src/test/ui/coherence/auxiliary/error_lib.rs | 1 + ...herence-overlap-negate-use-feature-gate.rs | 2 +- .../coherence-overlap-negative-trait.rs | 2 +- .../coherence-overlap-with-regions.rs | 16 +++++++ .../negative-impls/auxiliary/foreign_trait.rs | 1 + .../rely-on-negative-impl-in-coherence.rs | 9 ++-- 7 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 src/test/ui/coherence/coherence-overlap-with-regions.rs diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index dd1293f1def91..8ee578b77dcc4 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -4,8 +4,8 @@ //! [trait-resolution]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html //! [trait-specialization]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html -use crate::infer::{CombinedSnapshot, InferOk, TyCtxtInferExt}; -use crate::traits::query::evaluate_obligation::InferCtxtExt; +use crate::infer::outlives::env::OutlivesEnvironment; +use crate::infer::{CombinedSnapshot, InferOk, RegionckMode}; use crate::traits::select::IntercrateAmbiguityCause; use crate::traits::util::impl_trait_ref_and_oblig; use crate::traits::SkipLeakCheck; @@ -13,7 +13,11 @@ use crate::traits::{ self, FulfillmentContext, Normalized, Obligation, ObligationCause, PredicateObligation, PredicateObligations, SelectionContext, }; +//use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::{DefId, LOCAL_CRATE}; +use rustc_hir::CRATE_HIR_ID; +use rustc_infer::infer::TyCtxtInferExt; +use rustc_infer::traits::TraitEngine; use rustc_middle::traits::specialization_graph::OverlapMode; use rustc_middle::ty::fast_reject::{self, SimplifyParams, StripReferences}; use rustc_middle::ty::fold::TypeFoldable; @@ -270,7 +274,6 @@ fn implicit_negative<'cx, 'tcx>( impl1_header, impl2_header, obligations ); let infcx = selcx.infcx(); - let tcx = infcx.tcx; let opt_failing_obligation = impl1_header .predicates .iter() @@ -349,7 +352,7 @@ fn negative_impl<'cx, 'tcx>( let opt_failing_obligation = obligations .into_iter() .chain(more_obligations) - .find(|o| negative_impl_exists(selcx, o)); + .find(|o| negative_impl_exists(selcx, impl1_env, impl1_def_id, o)); if let Some(failing_obligation) = opt_failing_obligation { debug!("overlap: obligation unsatisfiable {:?}", failing_obligation); @@ -372,15 +375,38 @@ fn loose_check<'cx, 'tcx>( fn negative_impl_exists<'cx, 'tcx>( selcx: &SelectionContext<'cx, 'tcx>, + param_env: ty::ParamEnv<'tcx>, + region_context: DefId, o: &PredicateObligation<'tcx>, ) -> bool { - let infcx = selcx.infcx(); + let infcx = selcx.infcx().clone(); let tcx = infcx.tcx; o.flip_polarity(tcx) - .as_ref() .map(|o| { - // FIXME This isn't quite correct, regions should be included - selcx.infcx().predicate_must_hold_modulo_regions(o) + let mut fulfillment_cx = FulfillmentContext::new(); + fulfillment_cx.register_predicate_obligation(infcx, o); + + let errors = fulfillment_cx.select_all_or_error(infcx); + if !errors.is_empty() { + return false; + } + + let mut outlives_env = OutlivesEnvironment::new(param_env); + outlives_env.save_implied_bounds(CRATE_HIR_ID); + + infcx.process_registered_region_obligations( + outlives_env.region_bound_pairs_map(), + Some(tcx.lifetimes.re_root_empty), + param_env, + ); + + let errors = + infcx.resolve_regions(region_context, &outlives_env, RegionckMode::default()); + if !errors.is_empty() { + return false; + } + + true }) .unwrap_or(false) } diff --git a/src/test/ui/coherence/auxiliary/error_lib.rs b/src/test/ui/coherence/auxiliary/error_lib.rs index 43806cb995c03..19ff9ae62fdc6 100644 --- a/src/test/ui/coherence/auxiliary/error_lib.rs +++ b/src/test/ui/coherence/auxiliary/error_lib.rs @@ -1,5 +1,6 @@ #![crate_type = "lib"] #![feature(negative_impls)] +#![feature(with_negative_coherence)] pub trait Error {} impl !Error for &str {} diff --git a/src/test/ui/coherence/coherence-overlap-negate-use-feature-gate.rs b/src/test/ui/coherence/coherence-overlap-negate-use-feature-gate.rs index e024eae9819ab..a0dd881d1aaad 100644 --- a/src/test/ui/coherence/coherence-overlap-negate-use-feature-gate.rs +++ b/src/test/ui/coherence/coherence-overlap-negate-use-feature-gate.rs @@ -1,6 +1,6 @@ // check-pass -#![feature(negative_impls)] +#![feature(with_negative_coherence)] use std::ops::DerefMut; diff --git a/src/test/ui/coherence/coherence-overlap-negative-trait.rs b/src/test/ui/coherence/coherence-overlap-negative-trait.rs index ab65163bea442..8059d23ffd215 100644 --- a/src/test/ui/coherence/coherence-overlap-negative-trait.rs +++ b/src/test/ui/coherence/coherence-overlap-negative-trait.rs @@ -3,7 +3,7 @@ // // Check that if we promise to not impl what would overlap it doesn't actually overlap -#![feature(negative_impls)] +#![feature(with_negative_coherence)] extern crate error_lib as lib; use lib::Error; diff --git a/src/test/ui/coherence/coherence-overlap-with-regions.rs b/src/test/ui/coherence/coherence-overlap-with-regions.rs new file mode 100644 index 0000000000000..32f01f4180103 --- /dev/null +++ b/src/test/ui/coherence/coherence-overlap-with-regions.rs @@ -0,0 +1,16 @@ +// check-pass + +#![feature(negative_impls)] +#![feature(rustc_attrs)] +#![feature(with_negative_coherence)] + +#[rustc_strict_coherence] +trait Foo {} +impl !Foo for &T where T: 'static {} + +#[rustc_strict_coherence] +trait Bar {} +impl Bar for T {} +impl Bar for &T where T: 'static {} + +fn main() {} diff --git a/src/test/ui/traits/negative-impls/auxiliary/foreign_trait.rs b/src/test/ui/traits/negative-impls/auxiliary/foreign_trait.rs index 681f26438e695..0e17f089048f7 100644 --- a/src/test/ui/traits/negative-impls/auxiliary/foreign_trait.rs +++ b/src/test/ui/traits/negative-impls/auxiliary/foreign_trait.rs @@ -1,4 +1,5 @@ #![feature(negative_impls)] +#![feature(with_negative_coherence)] pub trait ForeignTrait {} diff --git a/src/test/ui/traits/negative-impls/rely-on-negative-impl-in-coherence.rs b/src/test/ui/traits/negative-impls/rely-on-negative-impl-in-coherence.rs index 119ac05c33e4b..c1f96ab8c149e 100644 --- a/src/test/ui/traits/negative-impls/rely-on-negative-impl-in-coherence.rs +++ b/src/test/ui/traits/negative-impls/rely-on-negative-impl-in-coherence.rs @@ -1,6 +1,7 @@ // check-pass #![feature(negative_impls)] +#![feature(with_negative_coherence)] // aux-build: foreign_trait.rs @@ -16,8 +17,8 @@ extern crate foreign_trait; use foreign_trait::ForeignTrait; -trait LocalTrait { } -impl LocalTrait for T { } -impl LocalTrait for String { } +trait LocalTrait {} +impl LocalTrait for T {} +impl LocalTrait for String {} -fn main() { } +fn main() {} From f4bb4500ddb4a54b5c33908861e821317c589850 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 9 Feb 2022 19:37:10 -0300 Subject: [PATCH 5/9] Call the method fork instead of clone and add proper comments --- .../src/snapshot_map/mod.rs | 2 ++ compiler/rustc_infer/src/infer/at.rs | 23 +++++++++++++++++++ .../src/infer/lexical_region_resolve/mod.rs | 1 + compiler/rustc_infer/src/infer/mod.rs | 1 + .../src/infer/region_constraints/mod.rs | 2 +- .../rustc_infer/src/infer/type_variable.rs | 4 ++++ compiler/rustc_infer/src/infer/undo_log.rs | 2 ++ compiler/rustc_infer/src/traits/project.rs | 2 +- .../src/traits/coherence.rs | 2 +- 9 files changed, 36 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_data_structures/src/snapshot_map/mod.rs b/compiler/rustc_data_structures/src/snapshot_map/mod.rs index b4cc85293f7c1..8a50179cd3b64 100644 --- a/compiler/rustc_data_structures/src/snapshot_map/mod.rs +++ b/compiler/rustc_data_structures/src/snapshot_map/mod.rs @@ -13,6 +13,7 @@ mod tests; pub type SnapshotMapStorage = SnapshotMap, ()>; pub type SnapshotMapRef<'a, K, V, L> = SnapshotMap, &'a mut L>; +#[derive(Clone)] pub struct SnapshotMap, L = VecLog>> { map: M, undo_log: L, @@ -30,6 +31,7 @@ where } } +#[derive(Clone)] pub enum UndoLog { Inserted(K), Overwrite(K, V), diff --git a/compiler/rustc_infer/src/infer/at.rs b/compiler/rustc_infer/src/infer/at.rs index c26ea4c966903..63871d428e149 100644 --- a/compiler/rustc_infer/src/infer/at.rs +++ b/compiler/rustc_infer/src/infer/at.rs @@ -51,6 +51,29 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { ) -> At<'a, 'tcx> { At { infcx: self, cause, param_env } } + + /// Forks the inference context, creating a new inference context with the same inference + /// variables in the same state. This can be used to "branch off" many tests from the same + /// common state. Used in coherence. + pub fn fork(&self) -> Self { + Self { + tcx: self.tcx.clone(), + defining_use_anchor: self.defining_use_anchor.clone(), + reveal_defining_opaque_types: self.reveal_defining_opaque_types, + in_progress_typeck_results: self.in_progress_typeck_results.clone(), + inner: self.inner.clone(), + skip_leak_check: self.skip_leak_check.clone(), + lexical_region_resolutions: self.lexical_region_resolutions.clone(), + selection_cache: self.selection_cache.clone(), + evaluation_cache: self.evaluation_cache.clone(), + reported_trait_errors: self.reported_trait_errors.clone(), + reported_closure_mismatch: self.reported_closure_mismatch.clone(), + tainted_by_errors_flag: self.tainted_by_errors_flag.clone(), + err_count_on_creation: self.err_count_on_creation, + in_snapshot: self.in_snapshot.clone(), + universe: self.universe.clone(), + } + } } pub trait ToTrace<'tcx>: Relate<'tcx> + Copy { diff --git a/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs b/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs index a5ec84a4f1446..f5e61d28ac5e5 100644 --- a/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs +++ b/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs @@ -61,6 +61,7 @@ pub(crate) fn resolve<'tcx>( /// Contains the result of lexical region resolution. Offers methods /// to lookup up the final value of a region variable. +#[derive(Clone)] pub struct LexicalRegionResolutions<'tcx> { values: IndexVec>, error_region: ty::Region<'tcx>, diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index c18d36d1f74a2..be38ace3bd2b8 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -130,6 +130,7 @@ impl RegionckMode { /// `RefCell` and are involved with taking/rolling back snapshots. Snapshot /// operations are hot enough that we want only one call to `borrow_mut` per /// call to `start_snapshot` and `rollback_to`. +#[derive(Clone)] pub struct InferCtxtInner<'tcx> { /// Cache for projections. This cache is snapshotted along with the infcx. /// diff --git a/compiler/rustc_infer/src/infer/region_constraints/mod.rs b/compiler/rustc_infer/src/infer/region_constraints/mod.rs index 29775a9668531..078575b537708 100644 --- a/compiler/rustc_infer/src/infer/region_constraints/mod.rs +++ b/compiler/rustc_infer/src/infer/region_constraints/mod.rs @@ -28,7 +28,7 @@ mod leak_check; pub use rustc_middle::infer::MemberConstraint; -#[derive(Default)] +#[derive(Clone, Default)] pub struct RegionConstraintStorage<'tcx> { /// For each `RegionVid`, the corresponding `RegionVariableOrigin`. var_infos: IndexVec, diff --git a/compiler/rustc_infer/src/infer/type_variable.rs b/compiler/rustc_infer/src/infer/type_variable.rs index 82970f214fa66..d320728a43f95 100644 --- a/compiler/rustc_infer/src/infer/type_variable.rs +++ b/compiler/rustc_infer/src/infer/type_variable.rs @@ -14,6 +14,7 @@ use std::ops::Range; use rustc_data_structures::undo_log::{Rollback, UndoLogs}; /// Represents a single undo-able action that affects a type inference variable. +#[derive(Clone)] pub(crate) enum UndoLog<'tcx> { EqRelation(sv::UndoLog>>), SubRelation(sv::UndoLog>), @@ -58,6 +59,7 @@ impl<'tcx> Rollback> for TypeVariableStorage<'tcx> { } } +#[derive(Clone)] pub struct TypeVariableStorage<'tcx> { values: sv::SnapshotVecStorage, @@ -137,6 +139,7 @@ pub enum TypeVariableOriginKind { LatticeVariable, } +#[derive(Clone)] pub(crate) struct TypeVariableData { origin: TypeVariableOrigin, } @@ -165,6 +168,7 @@ impl<'tcx> TypeVariableValue<'tcx> { } } +#[derive(Clone)] pub(crate) struct Instantiate; pub(crate) struct Delegate; diff --git a/compiler/rustc_infer/src/infer/undo_log.rs b/compiler/rustc_infer/src/infer/undo_log.rs index 89db8f464b4e7..ecd886b547834 100644 --- a/compiler/rustc_infer/src/infer/undo_log.rs +++ b/compiler/rustc_infer/src/infer/undo_log.rs @@ -17,6 +17,7 @@ pub struct Snapshot<'tcx> { } /// Records the "undo" data for a single operation that affects some form of inference variable. +#[derive(Clone)] pub(crate) enum UndoLog<'tcx> { TypeVariables(type_variable::UndoLog<'tcx>), ConstUnificationTable(sv::UndoLog>>), @@ -84,6 +85,7 @@ impl<'tcx> Rollback> for InferCtxtInner<'tcx> { /// The combined undo log for all the various unification tables. For each change to the storage /// for any kind of inference variable, we record an UndoLog entry in the vector here. +#[derive(Clone)] pub(crate) struct InferCtxtUndoLogs<'tcx> { logs: Vec>, num_open_snapshots: usize, diff --git a/compiler/rustc_infer/src/traits/project.rs b/compiler/rustc_infer/src/traits/project.rs index a1a1168a21d2e..b84ed3dc68937 100644 --- a/compiler/rustc_infer/src/traits/project.rs +++ b/compiler/rustc_infer/src/traits/project.rs @@ -70,7 +70,7 @@ pub struct ProjectionCache<'a, 'tcx> { undo_log: &'a mut InferCtxtUndoLogs<'tcx>, } -#[derive(Default)] +#[derive(Clone, Default)] pub struct ProjectionCacheStorage<'tcx> { map: SnapshotMapStorage, ProjectionCacheEntry<'tcx>>, } diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 8ee578b77dcc4..7dd01f5b0b0e8 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -379,7 +379,7 @@ fn negative_impl_exists<'cx, 'tcx>( region_context: DefId, o: &PredicateObligation<'tcx>, ) -> bool { - let infcx = selcx.infcx().clone(); + let infcx = &selcx.infcx().fork(); let tcx = infcx.tcx; o.flip_polarity(tcx) .map(|o| { From ff11dfd71d900a05033f3c1b126a792e02a032fb Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 10 Feb 2022 11:55:23 -0300 Subject: [PATCH 6/9] Add failing test that should pass --- .../coherence-negative-outlives-lifetimes.rs | 12 ++++++++++++ .../coherence-negative-outlives-lifetimes.stderr | 11 +++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/test/ui/coherence/coherence-negative-outlives-lifetimes.rs create mode 100644 src/test/ui/coherence/coherence-negative-outlives-lifetimes.stderr diff --git a/src/test/ui/coherence/coherence-negative-outlives-lifetimes.rs b/src/test/ui/coherence/coherence-negative-outlives-lifetimes.rs new file mode 100644 index 0000000000000..159788b1b77f7 --- /dev/null +++ b/src/test/ui/coherence/coherence-negative-outlives-lifetimes.rs @@ -0,0 +1,12 @@ +#![feature(negative_impls)] + +// FIXME: this should compile + +trait MyPredicate<'a> {} +impl<'a, T> !MyPredicate<'a> for &T where T: 'a {} +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 `&_` + +fn main() {} diff --git a/src/test/ui/coherence/coherence-negative-outlives-lifetimes.stderr b/src/test/ui/coherence/coherence-negative-outlives-lifetimes.stderr new file mode 100644 index 0000000000000..263bd19b4249e --- /dev/null +++ b/src/test/ui/coherence/coherence-negative-outlives-lifetimes.stderr @@ -0,0 +1,11 @@ +error[E0119]: conflicting implementations of trait `MyTrait<'_>` for type `&_` + --> $DIR/coherence-negative-outlives-lifetimes.rs:9: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`. From 45983fecff0d4bf66e80c1299697b591757a81c0 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 10 Feb 2022 16:38:27 -0300 Subject: [PATCH 7/9] Add comments about outlives_env --- compiler/rustc_trait_selection/src/traits/coherence.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 7dd01f5b0b0e8..9d690578cfc46 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -392,6 +392,13 @@ fn negative_impl_exists<'cx, 'tcx>( } let mut outlives_env = OutlivesEnvironment::new(param_env); + // FIXME -- add "assumed to be well formed" types into the `outlives_env` + + // "Save" the accumulated implied bounds into the outlives environment + // (due to the FIXME above, there aren't any, but this step is still needed). + // The "body id" is given as `CRATE_HIR_ID`, which is the same body-id used + // by the "dummy" causes elsewhere (body-id is only relevant when checking + // function bodies with closures). outlives_env.save_implied_bounds(CRATE_HIR_ID); infcx.process_registered_region_obligations( From 8c4ffaaa7c3c36249f4f9a26a78e889cf4a831ef Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 10 Feb 2022 16:39:52 -0300 Subject: [PATCH 8/9] Inline loose_check fn on call site --- .../rustc_trait_selection/src/traits/coherence.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 9d690578cfc46..ed55da623cb20 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -287,7 +287,7 @@ fn implicit_negative<'cx, 'tcx>( predicate: p, }) .chain(obligations) - .find(|o| loose_check(selcx, o)); + .find(|o| !selcx.predicate_may_hold_fatal(o)); if let Some(failing_obligation) = opt_failing_obligation { debug!("overlap: obligation unsatisfiable {:?}", failing_obligation); @@ -363,16 +363,6 @@ fn negative_impl<'cx, 'tcx>( }) } -fn loose_check<'cx, 'tcx>( - selcx: &mut SelectionContext<'cx, 'tcx>, - o: &PredicateObligation<'tcx>, -) -> bool { - // FIXME: the call to `selcx.predicate_may_hold_fatal` below should be ported - // to the canonical trait query form, `infcx.predicate_may_hold`, once - // the new system supports intercrate mode (which coherence needs). - !selcx.predicate_may_hold_fatal(o) -} - fn negative_impl_exists<'cx, 'tcx>( selcx: &SelectionContext<'cx, 'tcx>, param_env: ty::ParamEnv<'tcx>, From 3c7fa0bcf36ff5c6e7c2f0096513e300ee4dc20e Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 14 Feb 2022 13:02:22 -0300 Subject: [PATCH 9/9] reveal_defining_opaque_types field doesn't exist after rebase --- compiler/rustc_infer/src/infer/at.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/compiler/rustc_infer/src/infer/at.rs b/compiler/rustc_infer/src/infer/at.rs index 63871d428e149..77e72b042523c 100644 --- a/compiler/rustc_infer/src/infer/at.rs +++ b/compiler/rustc_infer/src/infer/at.rs @@ -59,7 +59,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { Self { tcx: self.tcx.clone(), defining_use_anchor: self.defining_use_anchor.clone(), - reveal_defining_opaque_types: self.reveal_defining_opaque_types, in_progress_typeck_results: self.in_progress_typeck_results.clone(), inner: self.inner.clone(), skip_leak_check: self.skip_leak_check.clone(),