1
Fork 0

Properly check regions on negative overlap check

This commit is contained in:
Santiago Pastorino 2022-02-02 14:36:45 -03:00
parent b61e1bbf06
commit 3fd89a662a
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
7 changed files with 59 additions and 14 deletions

View file

@ -4,8 +4,8 @@
//! [trait-resolution]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html //! [trait-resolution]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html
//! [trait-specialization]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html //! [trait-specialization]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html
use crate::infer::{CombinedSnapshot, InferOk, TyCtxtInferExt}; use crate::infer::outlives::env::OutlivesEnvironment;
use crate::traits::query::evaluate_obligation::InferCtxtExt; use crate::infer::{CombinedSnapshot, InferOk, RegionckMode};
use crate::traits::select::IntercrateAmbiguityCause; use crate::traits::select::IntercrateAmbiguityCause;
use crate::traits::util::impl_trait_ref_and_oblig; use crate::traits::util::impl_trait_ref_and_oblig;
use crate::traits::SkipLeakCheck; use crate::traits::SkipLeakCheck;
@ -13,7 +13,11 @@ use crate::traits::{
self, FulfillmentContext, Normalized, Obligation, ObligationCause, PredicateObligation, self, FulfillmentContext, Normalized, Obligation, ObligationCause, PredicateObligation,
PredicateObligations, SelectionContext, PredicateObligations, SelectionContext,
}; };
//use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::{DefId, LOCAL_CRATE}; 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::traits::specialization_graph::OverlapMode;
use rustc_middle::ty::fast_reject::{self, SimplifyParams, StripReferences}; use rustc_middle::ty::fast_reject::{self, SimplifyParams, StripReferences};
use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::fold::TypeFoldable;
@ -270,7 +274,6 @@ fn implicit_negative<'cx, 'tcx>(
impl1_header, impl2_header, obligations impl1_header, impl2_header, obligations
); );
let infcx = selcx.infcx(); let infcx = selcx.infcx();
let tcx = infcx.tcx;
let opt_failing_obligation = impl1_header let opt_failing_obligation = impl1_header
.predicates .predicates
.iter() .iter()
@ -349,7 +352,7 @@ fn negative_impl<'cx, 'tcx>(
let opt_failing_obligation = obligations let opt_failing_obligation = obligations
.into_iter() .into_iter()
.chain(more_obligations) .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 { if let Some(failing_obligation) = opt_failing_obligation {
debug!("overlap: obligation unsatisfiable {:?}", failing_obligation); debug!("overlap: obligation unsatisfiable {:?}", failing_obligation);
@ -372,15 +375,38 @@ fn loose_check<'cx, 'tcx>(
fn negative_impl_exists<'cx, 'tcx>( fn negative_impl_exists<'cx, 'tcx>(
selcx: &SelectionContext<'cx, 'tcx>, selcx: &SelectionContext<'cx, 'tcx>,
param_env: ty::ParamEnv<'tcx>,
region_context: DefId,
o: &PredicateObligation<'tcx>, o: &PredicateObligation<'tcx>,
) -> bool { ) -> bool {
let infcx = selcx.infcx(); let infcx = selcx.infcx().clone();
let tcx = infcx.tcx; let tcx = infcx.tcx;
o.flip_polarity(tcx) o.flip_polarity(tcx)
.as_ref()
.map(|o| { .map(|o| {
// FIXME This isn't quite correct, regions should be included let mut fulfillment_cx = FulfillmentContext::new();
selcx.infcx().predicate_must_hold_modulo_regions(o) 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) .unwrap_or(false)
} }

View file

@ -1,5 +1,6 @@
#![crate_type = "lib"] #![crate_type = "lib"]
#![feature(negative_impls)] #![feature(negative_impls)]
#![feature(with_negative_coherence)]
pub trait Error {} pub trait Error {}
impl !Error for &str {} impl !Error for &str {}

View file

@ -1,6 +1,6 @@
// check-pass // check-pass
#![feature(negative_impls)] #![feature(with_negative_coherence)]
use std::ops::DerefMut; use std::ops::DerefMut;

View file

@ -3,7 +3,7 @@
// //
// Check that if we promise to not impl what would overlap it doesn't actually overlap // 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; extern crate error_lib as lib;
use lib::Error; use lib::Error;

View file

@ -0,0 +1,16 @@
// check-pass
#![feature(negative_impls)]
#![feature(rustc_attrs)]
#![feature(with_negative_coherence)]
#[rustc_strict_coherence]
trait Foo {}
impl<T> !Foo for &T where T: 'static {}
#[rustc_strict_coherence]
trait Bar {}
impl<T: Foo> Bar for T {}
impl<T> Bar for &T where T: 'static {}
fn main() {}

View file

@ -1,4 +1,5 @@
#![feature(negative_impls)] #![feature(negative_impls)]
#![feature(with_negative_coherence)]
pub trait ForeignTrait {} pub trait ForeignTrait {}

View file

@ -1,6 +1,7 @@
// check-pass // check-pass
#![feature(negative_impls)] #![feature(negative_impls)]
#![feature(with_negative_coherence)]
// aux-build: foreign_trait.rs // aux-build: foreign_trait.rs
@ -16,8 +17,8 @@
extern crate foreign_trait; extern crate foreign_trait;
use foreign_trait::ForeignTrait; use foreign_trait::ForeignTrait;
trait LocalTrait { } trait LocalTrait {}
impl<T: ForeignTrait> LocalTrait for T { } impl<T: ForeignTrait> LocalTrait for T {}
impl LocalTrait for String { } impl LocalTrait for String {}
fn main() { } fn main() {}