mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-25 23:14:12 +00:00
Extend coherence check to understand subtyping.
This commit is contained in:
parent
91eedfe18b
commit
8c841f2a31
@ -10,24 +10,27 @@
|
||||
|
||||
//! See `doc.rs` for high-level documentation
|
||||
|
||||
use super::Normalized;
|
||||
use super::SelectionContext;
|
||||
use super::{Obligation, ObligationCause};
|
||||
use super::{ObligationCause};
|
||||
use super::PredicateObligation;
|
||||
use super::project;
|
||||
use super::util;
|
||||
|
||||
use middle::subst::{Subst, TypeSpace};
|
||||
use middle::ty::{self, Ty};
|
||||
use middle::infer::InferCtxt;
|
||||
use middle::ty::{self, ToPolyTraitRef, Ty};
|
||||
use middle::infer::{self, InferCtxt};
|
||||
use std::collections::HashSet;
|
||||
use std::rc::Rc;
|
||||
use syntax::ast;
|
||||
use syntax::codemap::DUMMY_SP;
|
||||
use util::ppaux::Repr;
|
||||
|
||||
pub fn impl_can_satisfy(infcx: &InferCtxt,
|
||||
impl1_def_id: ast::DefId,
|
||||
impl2_def_id: ast::DefId)
|
||||
-> bool
|
||||
/// True if there exist types that satisfy both of the two given impls.
|
||||
pub fn overlapping_impls(infcx: &InferCtxt,
|
||||
impl1_def_id: ast::DefId,
|
||||
impl2_def_id: ast::DefId)
|
||||
-> bool
|
||||
{
|
||||
debug!("impl_can_satisfy(\
|
||||
impl1_def_id={}, \
|
||||
@ -35,28 +38,68 @@ pub fn impl_can_satisfy(infcx: &InferCtxt,
|
||||
impl1_def_id.repr(infcx.tcx),
|
||||
impl2_def_id.repr(infcx.tcx));
|
||||
|
||||
let param_env = ty::empty_parameter_environment(infcx.tcx);
|
||||
let mut selcx = SelectionContext::intercrate(infcx, ¶m_env);
|
||||
let cause = ObligationCause::dummy();
|
||||
let param_env = &ty::empty_parameter_environment(infcx.tcx);
|
||||
let selcx = &mut SelectionContext::intercrate(infcx, param_env);
|
||||
infcx.probe(|_| {
|
||||
overlap(selcx, impl1_def_id, impl2_def_id) || overlap(selcx, impl2_def_id, impl1_def_id)
|
||||
})
|
||||
}
|
||||
|
||||
// `impl1` provides an implementation of `Foo<X,Y> for Z`.
|
||||
let impl1_substs =
|
||||
util::fresh_substs_for_impl(infcx, DUMMY_SP, impl1_def_id);
|
||||
let impl1_trait_ref =
|
||||
(*ty::impl_trait_ref(infcx.tcx, impl1_def_id).unwrap()).subst(infcx.tcx, &impl1_substs);
|
||||
let impl1_trait_ref =
|
||||
project::normalize(&mut selcx, cause.clone(), &impl1_trait_ref);
|
||||
/// Can the types from impl `a` be used to satisfy impl `b`?
|
||||
/// (Including all conditions)
|
||||
fn overlap(selcx: &mut SelectionContext,
|
||||
a_def_id: ast::DefId,
|
||||
b_def_id: ast::DefId)
|
||||
-> bool
|
||||
{
|
||||
let (a_trait_ref, a_obligations) = impl_trait_ref_and_oblig(selcx, a_def_id);
|
||||
let (b_trait_ref, b_obligations) = impl_trait_ref_and_oblig(selcx, b_def_id);
|
||||
|
||||
// Determine whether `impl2` can provide an implementation for those
|
||||
// same types.
|
||||
let obligation = Obligation::new(cause,
|
||||
ty::Binder(ty::TraitPredicate {
|
||||
trait_ref: Rc::new(impl1_trait_ref.value),
|
||||
}));
|
||||
debug!("impl_can_satisfy(obligation={})", obligation.repr(infcx.tcx));
|
||||
selcx.evaluate_impl(impl2_def_id, &obligation) &&
|
||||
impl1_trait_ref.obligations.iter().all(
|
||||
|o| selcx.evaluate_obligation(o))
|
||||
// Does `a <: b` hold? If not, no overlap.
|
||||
if let Err(_) = infer::mk_sub_poly_trait_refs(selcx.infcx(),
|
||||
true,
|
||||
infer::Misc(DUMMY_SP),
|
||||
a_trait_ref.to_poly_trait_ref(),
|
||||
b_trait_ref.to_poly_trait_ref()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Are any of the obligations unsatisfiable? If so, no overlap.
|
||||
a_obligations.iter()
|
||||
.chain(b_obligations.iter())
|
||||
.all(|o| selcx.evaluate_obligation(o))
|
||||
}
|
||||
|
||||
/// Instantiate fresh variables for all bound parameters of the impl
|
||||
/// and return the impl trait ref with those variables substituted.
|
||||
fn impl_trait_ref_and_oblig<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>,
|
||||
impl_def_id: ast::DefId)
|
||||
-> (Rc<ty::TraitRef<'tcx>>,
|
||||
Vec<PredicateObligation<'tcx>>)
|
||||
{
|
||||
let impl_substs =
|
||||
&util::fresh_substs_for_impl(selcx.infcx(), DUMMY_SP, impl_def_id);
|
||||
let impl_trait_ref =
|
||||
ty::impl_trait_ref(selcx.tcx(), impl_def_id).unwrap();
|
||||
let impl_trait_ref =
|
||||
impl_trait_ref.subst(selcx.tcx(), impl_substs);
|
||||
let Normalized { value: impl_trait_ref, obligations: normalization_obligations1 } =
|
||||
project::normalize(selcx, ObligationCause::dummy(), &impl_trait_ref);
|
||||
|
||||
let predicates = ty::lookup_predicates(selcx.tcx(), impl_def_id);
|
||||
let predicates = predicates.instantiate(selcx.tcx(), impl_substs);
|
||||
let Normalized { value: predicates, obligations: normalization_obligations2 } =
|
||||
project::normalize(selcx, ObligationCause::dummy(), &predicates);
|
||||
let impl_obligations =
|
||||
util::predicates_for_generics(selcx.tcx(), ObligationCause::dummy(), 0, &predicates);
|
||||
|
||||
let impl_obligations: Vec<_> =
|
||||
impl_obligations.into_iter()
|
||||
.chain(normalization_obligations1.into_iter())
|
||||
.chain(normalization_obligations2.into_iter())
|
||||
.collect();
|
||||
|
||||
(impl_trait_ref, impl_obligations)
|
||||
}
|
||||
|
||||
pub enum OrphanCheckErr<'tcx> {
|
||||
|
@ -28,6 +28,7 @@ use util::ppaux::{Repr, UserString};
|
||||
pub use self::error_reporting::report_fulfillment_errors;
|
||||
pub use self::error_reporting::suggest_new_overflow_limit;
|
||||
pub use self::coherence::orphan_check;
|
||||
pub use self::coherence::overlapping_impls;
|
||||
pub use self::coherence::OrphanCheckErr;
|
||||
pub use self::fulfill::{FulfillmentContext, RegionObligation};
|
||||
pub use self::project::MismatchedProjectionTypes;
|
||||
@ -270,16 +271,6 @@ pub struct VtableObjectData<'tcx> {
|
||||
pub object_ty: Ty<'tcx>,
|
||||
}
|
||||
|
||||
/// True if there exist types that satisfy both of the two given impls.
|
||||
pub fn overlapping_impls(infcx: &InferCtxt,
|
||||
impl1_def_id: ast::DefId,
|
||||
impl2_def_id: ast::DefId)
|
||||
-> bool
|
||||
{
|
||||
coherence::impl_can_satisfy(infcx, impl1_def_id, impl2_def_id) &&
|
||||
coherence::impl_can_satisfy(infcx, impl2_def_id, impl1_def_id)
|
||||
}
|
||||
|
||||
/// Creates predicate obligations from the generic bounds.
|
||||
pub fn predicates_for_generics<'tcx>(tcx: &ty::ctxt<'tcx>,
|
||||
cause: ObligationCause<'tcx>,
|
||||
|
50
src/test/compile-fail/coherence-subtyping.rs
Normal file
50
src/test/compile-fail/coherence-subtyping.rs
Normal file
@ -0,0 +1,50 @@
|
||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test that two distinct impls which match subtypes of one another
|
||||
// yield coherence errors (or not) depending on the variance.
|
||||
|
||||
trait Contravariant {
|
||||
fn foo(&self) { }
|
||||
}
|
||||
|
||||
impl Contravariant for for<'a,'b> fn(&'a u8, &'b u8) {
|
||||
//~^ ERROR E0119
|
||||
}
|
||||
|
||||
impl Contravariant for for<'a> fn(&'a u8, &'a u8) {
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
trait Covariant {
|
||||
fn foo(&self) { }
|
||||
}
|
||||
|
||||
impl Covariant for for<'a,'b> fn(&'a u8, &'b u8) {
|
||||
//~^ ERROR E0119
|
||||
}
|
||||
|
||||
impl Covariant for for<'a> fn(&'a u8, &'a u8) {
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
trait Invariant {
|
||||
fn foo(&self) -> Self { }
|
||||
}
|
||||
|
||||
impl Invariant for for<'a,'b> fn(&'a u8, &'b u8) {
|
||||
}
|
||||
|
||||
impl Invariant for for<'a> fn(&'a u8, &'a u8) {
|
||||
}
|
||||
|
||||
fn main() { }
|
Loading…
Reference in New Issue
Block a user