Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflect trait #23712

Merged
merged 5 commits into from
Mar 28, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor object-safety test to use def-ids only
  • Loading branch information
nikomatsakis committed Mar 26, 2015
commit 710af0498d086f66de5f2f5fe47b6e16650f8d86
6 changes: 4 additions & 2 deletions src/librustc/middle/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub use self::util::get_vtable_index_of_object_method;
pub use self::util::trait_ref_for_builtin_bound;
pub use self::util::supertraits;
pub use self::util::Supertraits;
pub use self::util::supertrait_def_ids;
pub use self::util::SupertraitDefIds;
pub use self::util::transitive_bounds;
pub use self::util::upcast;

Expand Down Expand Up @@ -640,7 +642,7 @@ impl<'tcx> FulfillmentError<'tcx> {
}

impl<'tcx> TraitObligation<'tcx> {
fn self_ty(&self) -> Ty<'tcx> {
self.predicate.0.self_ty()
fn self_ty(&self) -> ty::Binder<Ty<'tcx>> {
ty::Binder(self.predicate.skip_binder().self_ty())
}
}
16 changes: 8 additions & 8 deletions src/librustc/middle/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,36 @@ pub enum MethodViolationCode {
}

pub fn is_object_safe<'tcx>(tcx: &ty::ctxt<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>)
trait_def_id: ast::DefId)
-> bool
{
// Because we query yes/no results frequently, we keep a cache:
let cached_result =
tcx.object_safety_cache.borrow().get(&trait_ref.def_id()).cloned();
tcx.object_safety_cache.borrow().get(&trait_def_id).cloned();

let result =
cached_result.unwrap_or_else(|| {
let result = object_safety_violations(tcx, trait_ref.clone()).is_empty();
let result = object_safety_violations(tcx, trait_def_id).is_empty();

// Record just a yes/no result in the cache; this is what is
// queried most frequently. Note that this may overwrite a
// previous result, but always with the same thing.
tcx.object_safety_cache.borrow_mut().insert(trait_ref.def_id(), result);
tcx.object_safety_cache.borrow_mut().insert(trait_def_id, result);

result
});

debug!("is_object_safe({}) = {}", trait_ref.repr(tcx), result);
debug!("is_object_safe({}) = {}", trait_def_id.repr(tcx), result);

result
}

pub fn object_safety_violations<'tcx>(tcx: &ty::ctxt<'tcx>,
sub_trait_ref: ty::PolyTraitRef<'tcx>)
trait_def_id: ast::DefId)
-> Vec<ObjectSafetyViolation<'tcx>>
{
supertraits(tcx, sub_trait_ref)
.flat_map(|tr| object_safety_violations_for_trait(tcx, tr.def_id()).into_iter())
traits::supertrait_def_ids(tcx, trait_def_id)
.flat_map(|def_id| object_safety_violations_for_trait(tcx, def_id).into_iter())
.collect()
}

Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// self-type from one of the other inputs. Without this check,
// these cases wind up being considered ambiguous due to a
// (spurious) ambiguity introduced here.
if !object_safety::is_object_safe(self.tcx(), obligation.predicate.to_poly_trait_ref()) {
let predicate_trait_ref = obligation.predicate.to_poly_trait_ref();
if !object_safety::is_object_safe(self.tcx(), predicate_trait_ref.def_id()) {
return;
}

Expand Down
41 changes: 41 additions & 0 deletions src/librustc/middle/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,47 @@ pub fn transitive_bounds<'cx, 'tcx>(tcx: &'cx ty::ctxt<'tcx>,
elaborate_trait_refs(tcx, bounds).filter_to_traits()
}

///////////////////////////////////////////////////////////////////////////
// Iterator over def-ids of supertraits

pub struct SupertraitDefIds<'cx, 'tcx:'cx> {
tcx: &'cx ty::ctxt<'tcx>,
stack: Vec<ast::DefId>,
visited: FnvHashSet<ast::DefId>,
}

pub fn supertrait_def_ids<'cx, 'tcx>(tcx: &'cx ty::ctxt<'tcx>,
trait_def_id: ast::DefId)
-> SupertraitDefIds<'cx, 'tcx>
{
SupertraitDefIds {
tcx: tcx,
stack: vec![trait_def_id],
visited: Some(trait_def_id).into_iter().collect(),
}
}

impl<'cx, 'tcx> Iterator for SupertraitDefIds<'cx, 'tcx> {
type Item = ast::DefId;

fn next(&mut self) -> Option<ast::DefId> {
let def_id = match self.stack.pop() {
Some(def_id) => def_id,
None => { return None; }
};

let predicates = ty::lookup_super_predicates(self.tcx, def_id);
let visited = &mut self.visited;
self.stack.extend(
predicates.predicates
.iter()
.filter_map(|p| p.to_opt_poly_trait_ref())
.map(|t| t.def_id())
.filter(|&super_def_id| visited.insert(super_def_id)));
Some(def_id)
}
}

///////////////////////////////////////////////////////////////////////////
// Other
///////////////////////////////////////////////////////////////////////////
Expand Down
9 changes: 4 additions & 5 deletions src/librustc_typeck/check/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@ pub fn check_object_safety<'tcx>(tcx: &ty::ctxt<'tcx>,
object_trait: &ty::TyTrait<'tcx>,
span: Span)
{
let object_trait_ref =
object_trait.principal_trait_ref_with_self_ty(tcx, tcx.types.err);
let trait_def_id = object_trait.principal_def_id();

if traits::is_object_safe(tcx, object_trait_ref.clone()) {
if traits::is_object_safe(tcx, trait_def_id) {
return;
}

span_err!(tcx.sess, span, E0038,
"cannot convert to a trait object because trait `{}` is not object-safe",
ty::item_path_str(tcx, object_trait_ref.def_id()));
ty::item_path_str(tcx, trait_def_id));

let violations = traits::object_safety_violations(tcx, object_trait_ref.clone());
let violations = traits::object_safety_violations(tcx, trait_def_id);
for violation in violations {
match violation {
ObjectSafetyViolation::SizedSelf => {
Expand Down