Skip to content

Commit

Permalink
Remove BuiltinBound and BuiltinBounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Nov 28, 2016
1 parent 607af72 commit 64e97d9
Show file tree
Hide file tree
Showing 31 changed files with 138 additions and 333 deletions.
6 changes: 5 additions & 1 deletion src/librustc/infer/mod.rs
Expand Up @@ -24,6 +24,7 @@ use middle::free_region::FreeRegionMap;
use middle::mem_categorization as mc;
use middle::mem_categorization::McResult;
use middle::region::CodeExtent;
use middle::lang_items;
use mir::tcx::LvalueTy;
use ty::subst::{Kind, Subst, Substs};
use ty::adjustment;
Expand Down Expand Up @@ -1492,11 +1493,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}
}

let copy_def_id = self.tcx.lang_items.require(lang_items::CopyTraitLangItem)
.unwrap_or_else(|msg| self.tcx.sess.fatal(&msg[..]));

// this can get called from typeck (by euv), and moves_by_default
// rightly refuses to work with inference variables, but
// moves_by_default has a cache, which we want to use in other
// cases.
!traits::type_known_to_meet_builtin_bound(self, ty, ty::BoundCopy, span)
!traits::type_known_to_meet_bound(self, ty, copy_def_id, span)
}

pub fn node_method_ty(&self, method_call: ty::MethodCall)
Expand Down
1 change: 0 additions & 1 deletion src/librustc/lib.rs
Expand Up @@ -32,7 +32,6 @@
#![feature(const_fn)]
#![feature(core_intrinsics)]
#![cfg_attr(stage0, feature(dotdot_in_tuple_patterns))]
#![feature(enumset)]
#![cfg_attr(stage0, feature(item_like_imports))]
#![feature(libc)]
#![feature(nonzero)]
Expand Down
25 changes: 0 additions & 25 deletions src/librustc/middle/lang_items.rs
Expand Up @@ -90,31 +90,6 @@ impl LanguageItems {
self.require(OwnedBoxLangItem)
}

pub fn from_builtin_kind(&self, bound: ty::BuiltinBound)
-> Result<DefId, String>
{
match bound {
ty::BoundSend => self.require(SendTraitLangItem),
ty::BoundSized => self.require(SizedTraitLangItem),
ty::BoundCopy => self.require(CopyTraitLangItem),
ty::BoundSync => self.require(SyncTraitLangItem),
}
}

pub fn to_builtin_kind(&self, id: DefId) -> Option<ty::BuiltinBound> {
if Some(id) == self.send_trait() {
Some(ty::BoundSend)
} else if Some(id) == self.sized_trait() {
Some(ty::BoundSized)
} else if Some(id) == self.copy_trait() {
Some(ty::BoundCopy)
} else if Some(id) == self.sync_trait() {
Some(ty::BoundSync)
} else {
None
}
}

pub fn fn_trait_kind(&self, id: DefId) -> Option<ty::ClosureKind> {
let def_id_kinds = [
(self.fn_trait(), ty::ClosureKind::Fn),
Expand Down
10 changes: 0 additions & 10 deletions src/librustc/traits/error_reporting.rs
Expand Up @@ -905,16 +905,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
ObligationCauseCode::StructInitializerSized => {
err.note("structs must have a statically known size to be initialized");
}
ObligationCauseCode::ClosureCapture(var_id, _, builtin_bound) => {
let def_id = tcx.lang_items.from_builtin_kind(builtin_bound).unwrap();
let trait_name = tcx.item_path_str(def_id);
let name = tcx.local_var_name_str(var_id);
err.note(
&format!("the closure that captures `{}` requires that all captured variables \
implement the trait `{}`",
name,
trait_name));
}
ObligationCauseCode::FieldSized => {
err.note("only the last field of a struct may have a dynamically sized type");
}
Expand Down
21 changes: 12 additions & 9 deletions src/librustc/traits/fulfill.rs
Expand Up @@ -17,8 +17,8 @@ use rustc_data_structures::obligation_forest::{ForestObligation, ObligationProce
use std::marker::PhantomData;
use std::mem;
use syntax::ast;
use util::common::ErrorReported;
use util::nodemap::{FxHashSet, NodeMap};
use hir::def_id::DefId;

use super::CodeAmbiguity;
use super::CodeProjectionError;
Expand Down Expand Up @@ -230,18 +230,21 @@ impl<'a, 'gcx, 'tcx> FulfillmentContext<'tcx> {
normalized.value
}

pub fn register_builtin_bound(&mut self,
pub fn register_bound(&mut self,
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
ty: Ty<'tcx>,
builtin_bound: ty::BuiltinBound,
def_id: DefId,
cause: ObligationCause<'tcx>)
{
match infcx.tcx.predicate_for_builtin_bound(cause, builtin_bound, 0, ty) {
Ok(predicate) => {
self.register_predicate_obligation(infcx, predicate);
}
Err(ErrorReported) => { }
}
let trait_ref = ty::TraitRef {
def_id: def_id,
substs: infcx.tcx.mk_substs_trait(ty, &[]),
};
self.register_predicate_obligation(infcx, Obligation {
cause: cause,
recursion_depth: 0,
predicate: trait_ref.to_predicate()
});
}

pub fn register_region_obligation(&mut self,
Expand Down
45 changes: 22 additions & 23 deletions src/librustc/traits/mod.rs
Expand Up @@ -19,7 +19,7 @@ use hir;
use hir::def_id::DefId;
use middle::free_region::FreeRegionMap;
use ty::subst::Substs;
use ty::{self, Ty, TyCtxt, TypeFoldable};
use ty::{self, Ty, TyCtxt, TypeFoldable, ToPredicate};
use infer::InferCtxt;

use std::rc::Rc;
Expand Down Expand Up @@ -125,10 +125,6 @@ pub enum ObligationCauseCode<'tcx> {
ReturnType, // Return type must be Sized
RepeatVec, // [T,..n] --> T must be Copy

// Captures of variable the given id by a closure (span is the
// span of the closure)
ClosureCapture(ast::NodeId, Span, ty::BuiltinBound),

// Types of fields (other than the last) in a struct must be sized.
FieldSized,

Expand Down Expand Up @@ -369,27 +365,30 @@ pub fn predicates_for_generics<'tcx>(cause: ObligationCause<'tcx>,
/// `bound` or is not known to meet bound (note that this is
/// conservative towards *no impl*, which is the opposite of the
/// `evaluate` methods).
pub fn type_known_to_meet_builtin_bound<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
pub fn type_known_to_meet_bound<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
ty: Ty<'tcx>,
bound: ty::BuiltinBound,
def_id: DefId,
span: Span)
-> bool
{
debug!("type_known_to_meet_builtin_bound(ty={:?}, bound={:?})",
debug!("type_known_to_meet_bound(ty={:?}, bound={:?})",
ty,
bound);

let cause = ObligationCause::misc(span, ast::DUMMY_NODE_ID);
let obligation =
infcx.tcx.predicate_for_builtin_bound(cause, bound, 0, ty);
let obligation = match obligation {
Ok(o) => o,
Err(..) => return false
infcx.tcx.item_path_str(def_id));

let trait_ref = ty::TraitRef {
def_id: def_id,
substs: infcx.tcx.mk_substs_trait(ty, &[]),
};
let obligation = Obligation {
cause: ObligationCause::misc(span, ast::DUMMY_NODE_ID),
recursion_depth: 0,
predicate: trait_ref.to_predicate(),
};

let result = SelectionContext::new(infcx)
.evaluate_obligation_conservatively(&obligation);
debug!("type_known_to_meet_builtin_bound: ty={:?} bound={:?} => {:?}",
ty, bound, result);
debug!("type_known_to_meet_ty={:?} bound={} => {:?}",
ty, infcx.tcx.item_path_str(def_id), result);

if result && (ty.has_infer_types() || ty.has_closure_types()) {
// Because of inference "guessing", selection can sometimes claim
Expand All @@ -404,22 +403,22 @@ pub fn type_known_to_meet_builtin_bound<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'g
// anyhow).
let cause = ObligationCause::misc(span, ast::DUMMY_NODE_ID);

fulfill_cx.register_builtin_bound(infcx, ty, bound, cause);
fulfill_cx.register_bound(infcx, ty, def_id, cause);

// Note: we only assume something is `Copy` if we can
// *definitively* show that it implements `Copy`. Otherwise,
// assume it is move; linear is always ok.
match fulfill_cx.select_all_or_error(infcx) {
Ok(()) => {
debug!("type_known_to_meet_builtin_bound: ty={:?} bound={:?} success",
debug!("type_known_to_meet_bound: ty={:?} bound={} success",
ty,
bound);
infcx.tcx.item_path_str(def_id));
true
}
Err(e) => {
debug!("type_known_to_meet_builtin_bound: ty={:?} bound={:?} errors={:?}",
debug!("type_known_to_meet_bound: ty={:?} bound={} errors={:?}",
ty,
bound,
infcx.tcx.item_path_str(def_id),
e);
false
}
Expand Down
16 changes: 7 additions & 9 deletions src/librustc/traits/select.rs
Expand Up @@ -1093,8 +1093,9 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
// Other bounds. Consider both in-scope bounds from fn decl
// and applicable impls. There is a certain set of precedence rules here.

match self.tcx().lang_items.to_builtin_kind(obligation.predicate.def_id()) {
Some(ty::BoundCopy) => {
let def_id = obligation.predicate.def_id();
match obligation.predicate.def_id() {
_ if self.tcx().lang_items.copy_trait() == Some(def_id) => {
debug!("obligation self ty is {:?}",
obligation.predicate.0.self_ty());

Expand All @@ -1106,22 +1107,20 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
let copy_conditions = self.copy_conditions(obligation);
self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates)?;
}
Some(ty::BoundSized) => {
_ if self.tcx().lang_items.sized_trait() == Some(def_id) => {
// Sized is never implementable by end-users, it is
// always automatically computed.
let sized_conditions = self.sized_conditions(obligation);
self.assemble_builtin_bound_candidates(sized_conditions,
&mut candidates)?;
}

None if self.tcx().lang_items.unsize_trait() ==
Some(obligation.predicate.def_id()) => {
_ if self.tcx().lang_items.unsize_trait() == Some(def_id) => {
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
}

Some(ty::BoundSend) |
Some(ty::BoundSync) |
None => {
// For non-builtins and Send/Sync
_ => {
self.assemble_closure_candidates(obligation, &mut candidates)?;
self.assemble_fn_pointer_candidates(obligation, &mut candidates)?;
self.assemble_candidates_from_impls(obligation, &mut candidates)?;
Expand Down Expand Up @@ -2483,7 +2482,6 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
data_b.auto_traits().collect(),
data_a.projection_bounds.clone(),
));
let origin = TypeOrigin::Misc(obligation.cause.span);
let InferOk { obligations, .. } =
self.infcx.sub_types(false, &obligation.cause, new_trait, target)
.map_err(|_| Unimplemented)?;
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/traits/structural_impls.rs
Expand Up @@ -190,9 +190,6 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
super::VariableType(id) => Some(super::VariableType(id)),
super::ReturnType => Some(super::ReturnType),
super::RepeatVec => Some(super::RepeatVec),
super::ClosureCapture(node_id, span, bound) => {
Some(super::ClosureCapture(node_id, span, bound))
}
super::FieldSized => Some(super::FieldSized),
super::ConstSized => Some(super::ConstSized),
super::SharedStatic => Some(super::SharedStatic),
Expand Down Expand Up @@ -507,7 +504,6 @@ impl<'tcx> TypeFoldable<'tcx> for traits::ObligationCauseCode<'tcx> {
super::VariableType(_) |
super::ReturnType |
super::RepeatVec |
super::ClosureCapture(..) |
super::FieldSized |
super::ConstSized |
super::SharedStatic |
Expand Down Expand Up @@ -552,7 +548,6 @@ impl<'tcx> TypeFoldable<'tcx> for traits::ObligationCauseCode<'tcx> {
super::VariableType(_) |
super::ReturnType |
super::RepeatVec |
super::ClosureCapture(..) |
super::FieldSized |
super::ConstSized |
super::SharedStatic |
Expand Down
31 changes: 0 additions & 31 deletions src/librustc/traits/util.rs
Expand Up @@ -12,7 +12,6 @@ use hir::def_id::DefId;
use ty::subst::{Subst, Substs};
use ty::{self, Ty, TyCtxt, ToPredicate, ToPolyTraitRef};
use ty::outlives::Component;
use util::common::ErrorReported;
use util::nodemap::FxHashSet;

use super::{Obligation, ObligationCause, PredicateObligation, SelectionContext, Normalized};
Expand Down Expand Up @@ -408,25 +407,6 @@ pub fn predicate_for_trait_ref<'tcx>(
}

impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn trait_ref_for_builtin_bound(self,
builtin_bound: ty::BuiltinBound,
param_ty: Ty<'tcx>)
-> Result<ty::TraitRef<'tcx>, ErrorReported>
{
match self.lang_items.from_builtin_kind(builtin_bound) {
Ok(def_id) => {
Ok(ty::TraitRef {
def_id: def_id,
substs: self.mk_substs_trait(param_ty, &[])
})
}
Err(e) => {
self.sess.err(&e);
Err(ErrorReported)
}
}
}

pub fn predicate_for_trait_def(self,
cause: ObligationCause<'tcx>,
trait_def_id: DefId,
Expand All @@ -442,17 +422,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
predicate_for_trait_ref(cause, trait_ref, recursion_depth)
}

pub fn predicate_for_builtin_bound(self,
cause: ObligationCause<'tcx>,
builtin_bound: ty::BuiltinBound,
recursion_depth: usize,
param_ty: Ty<'tcx>)
-> Result<PredicateObligation<'tcx>, ErrorReported>
{
let trait_ref = self.trait_ref_for_builtin_bound(builtin_bound, param_ty)?;
Ok(predicate_for_trait_ref(cause, trait_ref, recursion_depth))
}

/// Cast a trait reference into a reference to one of its super
/// traits; returns `None` if `target_trait_def_id` is not a
/// supertrait.
Expand Down
14 changes: 0 additions & 14 deletions src/librustc/ty/error.rs
Expand Up @@ -45,7 +45,6 @@ pub enum TypeError<'tcx> {
IntMismatch(ExpectedFound<ty::IntVarValue>),
FloatMismatch(ExpectedFound<ast::FloatTy>),
Traits(ExpectedFound<DefId>),
BuiltinBoundsMismatch(ExpectedFound<ty::BuiltinBounds>),
VariadicMismatch(ExpectedFound<bool>),
CyclicTy,
ProjectionNameMismatched(ExpectedFound<Name>),
Expand Down Expand Up @@ -135,19 +134,6 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
format!("trait `{}`",
tcx.item_path_str(values.found)))
}),
BuiltinBoundsMismatch(values) => {
if values.expected.is_empty() {
write!(f, "expected no bounds, found `{}`",
values.found)
} else if values.found.is_empty() {
write!(f, "expected bounds `{}`, found no bounds",
values.expected)
} else {
write!(f, "expected bounds `{}`, found bounds `{}`",
values.expected,
values.found)
}
}
IntMismatch(ref values) => {
write!(f, "expected `{:?}`, found `{:?}`",
values.expected,
Expand Down
6 changes: 0 additions & 6 deletions src/librustc/ty/mod.rs
Expand Up @@ -53,7 +53,6 @@ use hir;
use hir::itemlikevisit::ItemLikeVisitor;

pub use self::sty::{Binder, DebruijnIndex};
pub use self::sty::{BuiltinBound, BuiltinBounds};
pub use self::sty::{BareFnTy, FnSig, PolyFnSig};
pub use self::sty::{ClosureTy, InferTy, ParamTy, ProjectionTy, TraitObject};
pub use self::sty::{ClosureSubsts, TypeAndMut};
Expand All @@ -68,11 +67,6 @@ pub use self::sty::InferTy::*;
pub use self::sty::Region::*;
pub use self::sty::TypeVariants::*;

pub use self::sty::BuiltinBound::Send as BoundSend;
pub use self::sty::BuiltinBound::Sized as BoundSized;
pub use self::sty::BuiltinBound::Copy as BoundCopy;
pub use self::sty::BuiltinBound::Sync as BoundSync;

pub use self::contents::TypeContents;
pub use self::context::{TyCtxt, tls};
pub use self::context::{CtxtArenas, Lift, Tables};
Expand Down

0 comments on commit 64e97d9

Please sign in to comment.