From 9bdd7f00401c0002011ab20b70b405a15fa9128c Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 5 Dec 2014 01:59:17 -0500 Subject: [PATCH] Thread a id to `Obligation` --- src/librustc/middle/check_static.rs | 4 ++-- src/librustc/middle/traits/coherence.rs | 4 ++-- src/librustc/middle/traits/mod.rs | 20 +++++++++++++------- src/librustc/middle/ty.rs | 14 ++++++++++---- src/librustc_trans/trans/common.rs | 3 ++- src/librustc_typeck/check/method/confirm.rs | 2 +- src/librustc_typeck/check/method/mod.rs | 4 ++-- src/librustc_typeck/check/method/probe.rs | 2 +- src/librustc_typeck/check/mod.rs | 5 +++-- src/librustc_typeck/check/regionck.rs | 2 +- src/librustc_typeck/check/vtable.rs | 4 +++- src/librustc_typeck/check/wf.rs | 10 ++++++++-- 12 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/librustc/middle/check_static.rs b/src/librustc/middle/check_static.rs index a495d1e049d1..9aa258c16aa1 100644 --- a/src/librustc/middle/check_static.rs +++ b/src/librustc/middle/check_static.rs @@ -36,7 +36,7 @@ use util::nodemap::NodeSet; use syntax::ast; use syntax::print::pprust; use syntax::visit::Visitor; -use syntax::codemap::{DUMMY_SP, Span}; +use syntax::codemap::Span; use syntax::visit; #[deriving(Eq, PartialEq)] @@ -119,7 +119,7 @@ impl<'a, 'tcx> CheckStaticVisitor<'a, 'tcx> { let ty = ty::node_id_to_type(self.tcx, e.id); let infcx = infer::new_infer_ctxt(self.tcx); let mut fulfill_cx = traits::FulfillmentContext::new(); - let cause = traits::ObligationCause::misc(DUMMY_SP); + let cause = traits::ObligationCause::dummy(); let obligation = traits::obligation_for_builtin_bound(self.tcx, cause, ty, ty::BoundSync); fulfill_cx.register_obligation(self.tcx, obligation.unwrap()); diff --git a/src/librustc/middle/traits/coherence.rs b/src/librustc/middle/traits/coherence.rs index 1bce353cb0cf..822979c86017 100644 --- a/src/librustc/middle/traits/coherence.rs +++ b/src/librustc/middle/traits/coherence.rs @@ -11,7 +11,7 @@ //! See `doc.rs` for high-level documentation use super::SelectionContext; -use super::Obligation; +use super::{Obligation, ObligationCause}; use super::util; use middle::subst; @@ -48,7 +48,7 @@ pub fn impl_can_satisfy(infcx: &InferCtxt, // same types. let param_env = ty::empty_parameter_environment(); let mut selcx = SelectionContext::intercrate(infcx, ¶m_env, infcx.tcx); - let obligation = Obligation::misc(DUMMY_SP, impl1_trait_ref); + let obligation = Obligation::new(ObligationCause::dummy(), impl1_trait_ref); debug!("impl_can_satisfy(obligation={})", obligation.repr(infcx.tcx)); selcx.evaluate_impl(impl2_def_id, &obligation) } diff --git a/src/librustc/middle/traits/mod.rs b/src/librustc/middle/traits/mod.rs index 1505fe7561b6..2600640a17da 100644 --- a/src/librustc/middle/traits/mod.rs +++ b/src/librustc/middle/traits/mod.rs @@ -59,6 +59,10 @@ pub type TraitObligation<'tcx> = Obligation<'tcx, Rc>>; #[deriving(Copy, Clone)] pub struct ObligationCause<'tcx> { pub span: Span, + + // the id of XXX + pub scope_id: ast::NodeId, + pub code: ObligationCauseCode<'tcx> } @@ -303,8 +307,8 @@ impl<'tcx,O> Obligation<'tcx,O> { trait_ref: trait_ref } } - pub fn misc(span: Span, trait_ref: O) -> Obligation<'tcx, O> { - Obligation::new(ObligationCause::misc(span), trait_ref) + pub fn misc(span: Span, scope_id: ast::NodeId, trait_ref: O) -> Obligation<'tcx, O> { + Obligation::new(ObligationCause::misc(span, scope_id), trait_ref) } } @@ -315,17 +319,19 @@ impl<'tcx> Obligation<'tcx,Rc>> { } impl<'tcx> ObligationCause<'tcx> { - pub fn new(span: Span, code: ObligationCauseCode<'tcx>) + pub fn new(span: Span, + scope_id: ast::NodeId, + code: ObligationCauseCode<'tcx>) -> ObligationCause<'tcx> { - ObligationCause { span: span, code: code } + ObligationCause { span: span, scope_id: scope_id, code: code } } - pub fn misc(span: Span) -> ObligationCause<'tcx> { - ObligationCause { span: span, code: MiscObligation } + pub fn misc(span: Span, scope_id: ast::NodeId) -> ObligationCause<'tcx> { + ObligationCause { span: span, scope_id: scope_id, code: MiscObligation } } pub fn dummy() -> ObligationCause<'tcx> { - ObligationCause { span: DUMMY_SP, code: MiscObligation } + ObligationCause { span: DUMMY_SP, scope_id: 0, code: MiscObligation } } } diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 5f5709f87f77..ee13fc5e3460 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -78,7 +78,7 @@ use std::rc::Rc; use std::collections::hash_map::{HashMap, Occupied, Vacant}; use arena::TypedArena; use syntax::abi; -use syntax::ast::{CrateNum, DefId, FnStyle, Ident, ItemTrait, LOCAL_CRATE}; +use syntax::ast::{CrateNum, DefId, DUMMY_NODE_ID, FnStyle, Ident, ItemTrait, LOCAL_CRATE}; use syntax::ast::{MutImmutable, MutMutable, Name, NamedField, NodeId}; use syntax::ast::{Onceness, StmtExpr, StmtSemi, StructField, UnnamedField}; use syntax::ast::{Visibility}; @@ -3158,9 +3158,15 @@ pub fn type_moves_by_default<'tcx>(cx: &ctxt<'tcx>, let infcx = infer::new_infer_ctxt(cx); let mut fulfill_cx = traits::FulfillmentContext::new(); + + // we can use dummy values here because we won't report any errors + // that result nor will we pay any mind to region obligations that arise + // (there shouldn't really be any anyhow) + let cause = ObligationCause::misc(DUMMY_SP, DUMMY_NODE_ID); + let obligation = traits::obligation_for_builtin_bound( cx, - ObligationCause::misc(DUMMY_SP), + cause, ty, ty::BoundCopy).unwrap(); fulfill_cx.register_obligation(cx, obligation); @@ -5846,7 +5852,7 @@ pub fn empty_parameter_environment<'tcx>() -> ParameterEnvironment<'tcx> { /// See `ParameterEnvironment` struct def'n for details pub fn construct_parameter_environment<'tcx>( tcx: &ctxt<'tcx>, - span: Span, + _span: Span, generics: &ty::Generics<'tcx>, free_id: ast::NodeId) -> ParameterEnvironment<'tcx> @@ -5884,7 +5890,7 @@ pub fn construct_parameter_environment<'tcx>( let bounds = generics.to_bounds(tcx, &free_substs); let bounds = liberate_late_bound_regions(tcx, free_id_scope, &bind(bounds)).value; let obligations = traits::obligations_for_generics(tcx, - traits::ObligationCause::misc(span), + traits::ObligationCause::dummy(), &bounds, &free_substs.types); let type_bounds = bounds.types.subst(tcx, &free_substs); diff --git a/src/librustc_trans/trans/common.rs b/src/librustc_trans/trans/common.rs index 77412b00299b..da6aa84cfa11 100644 --- a/src/librustc_trans/trans/common.rs +++ b/src/librustc_trans/trans/common.rs @@ -793,7 +793,8 @@ pub fn fulfill_obligation<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, // Do the initial selection for the obligation. This yields the // shallow result we are looking for -- that is, what specific impl. let mut selcx = traits::SelectionContext::new(&infcx, ¶m_env, tcx); - let obligation = traits::Obligation::misc(span, trait_ref.clone()); + let obligation = traits::Obligation::new(traits::ObligationCause::dummy(), + trait_ref.clone()); let selection = match selcx.select(&obligation) { Ok(Some(selection)) => selection, Ok(None) => { diff --git a/src/librustc_typeck/check/method/confirm.rs b/src/librustc_typeck/check/method/confirm.rs index b6a9e2cbc59e..7b43b97a4bb3 100644 --- a/src/librustc_typeck/check/method/confirm.rs +++ b/src/librustc_typeck/check/method/confirm.rs @@ -462,7 +462,7 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> { method_bounds.repr(self.tcx())); self.fcx.add_obligations_for_parameters( - traits::ObligationCause::misc(self.span), + traits::ObligationCause::misc(self.span, self.fcx.body_id), method_bounds_substs, method_bounds); diff --git a/src/librustc_typeck/check/method/mod.rs b/src/librustc_typeck/check/method/mod.rs index 53bda93b28e0..1ab76740962b 100644 --- a/src/librustc_typeck/check/method/mod.rs +++ b/src/librustc_typeck/check/method/mod.rs @@ -169,7 +169,7 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>, let trait_ref = Rc::new(ty::TraitRef::new(trait_def_id, substs)); // Construct an obligation - let obligation = traits::Obligation::misc(span, trait_ref.clone()); + let obligation = traits::Obligation::misc(span, fcx.body_id, trait_ref.clone()); // Now we want to know if this can be matched let mut selcx = traits::SelectionContext::new(fcx.infcx(), @@ -219,7 +219,7 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>, let method_bounds = method_ty.generics.to_bounds(fcx.tcx(), &trait_ref.substs); assert!(!method_bounds.has_escaping_regions()); fcx.add_obligations_for_parameters( - traits::ObligationCause::misc(span), + traits::ObligationCause::misc(span, fcx.body_id), &trait_ref.substs, &method_bounds); diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 4d23161f6dee..3d7590db748e 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -804,7 +804,7 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> { let obligations = traits::obligations_for_generics( self.tcx(), - traits::ObligationCause::misc(self.span), + traits::ObligationCause::misc(self.span, self.fcx.body_id), &impl_bounds, &substs.types); debug!("impl_obligations={}", obligations.repr(self.tcx())); diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 7270e66ab530..265766029b50 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1790,6 +1790,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.add_obligations_for_parameters( traits::ObligationCause::new( span, + self.body_id, traits::ItemObligation(def_id)), &substs, &bounds); @@ -1817,7 +1818,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { let obligation = traits::obligation_for_builtin_bound( self.tcx(), - traits::ObligationCause::new(span, code), + traits::ObligationCause::new(span, self.body_id, code), ty, bound); if let Ok(ob) = obligation { @@ -5197,7 +5198,7 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, debug!("after late-bounds have been replaced: bounds={}", bounds.repr(fcx.tcx())); fcx.add_obligations_for_parameters( - traits::ObligationCause::new(span, traits::ItemObligation(def.def_id())), + traits::ObligationCause::new(span, fcx.body_id, traits::ItemObligation(def.def_id())), &substs, &bounds); diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index 80ee2cce4ce7..c53ad34a6d22 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -938,7 +938,7 @@ fn check_expr_fn_block(rcx: &mut Rcx, // Check that the type meets the criteria of the existential bounds: for builtin_bound in bounds.builtin_bounds.iter() { let code = traits::ClosureCapture(var_node_id, expr.span); - let cause = traits::ObligationCause::new(freevar.span, code); + let cause = traits::ObligationCause::new(freevar.span, rcx.fcx.body_id, code); let obligation = traits::obligation_for_builtin_bound(rcx.tcx(), cause, var_ty, builtin_bound); if let Ok(obligation) = obligation { diff --git a/src/librustc_typeck/check/vtable.rs b/src/librustc_typeck/check/vtable.rs index 1f0a486edd38..17651b9bfb61 100644 --- a/src/librustc_typeck/check/vtable.rs +++ b/src/librustc_typeck/check/vtable.rs @@ -252,7 +252,7 @@ pub fn register_object_cast_obligations<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, let sized_obligation = traits::obligation_for_builtin_bound( fcx.tcx(), - traits::ObligationCause::new(span, traits::ObjectSized), + traits::ObligationCause::new(span, fcx.body_id, traits::ObjectSized), referent_ty, ty::BoundSized); match sized_obligation { @@ -287,6 +287,7 @@ pub fn register_object_cast_obligations<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, let object_obligation = Obligation::new( ObligationCause::new(span, + fcx.body_id, traits::ObjectCastObligation(object_trait_ty)), object_trait_ref.clone()); fcx.register_obligation(object_obligation); @@ -299,6 +300,7 @@ pub fn register_object_cast_obligations<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, let obligation = obligation_for_builtin_bound( fcx.tcx(), ObligationCause::new(span, + fcx.body_id, traits::ObjectCastObligation(object_trait_ty)), referent_ty, builtin_bound); diff --git a/src/librustc_typeck/check/wf.rs b/src/librustc_typeck/check/wf.rs index a011982a1fc5..23db98b31758 100644 --- a/src/librustc_typeck/check/wf.rs +++ b/src/librustc_typeck/check/wf.rs @@ -122,7 +122,9 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> { // For DST, all intermediate types must be sized. if variant.fields.len() > 0 { for field in variant.fields.init().iter() { - let cause = traits::ObligationCause::new(field.span, traits::FieldSized); + let cause = traits::ObligationCause::new(field.span, + fcx.body_id, + traits::FieldSized); let obligation = traits::obligation_for_builtin_bound(fcx.tcx(), cause, field.ty, @@ -223,6 +225,7 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> { let cause = traits::ObligationCause::new( item.span, + fcx.body_id, traits::ItemObligation(trait_ref.def_id)); // Find the supertrait bounds. This will add `int:Bar`. @@ -291,6 +294,7 @@ impl<'cx,'tcx> BoundsChecker<'cx,'tcx> { self.fcx.add_obligations_for_parameters( traits::ObligationCause::new( self.span, + self.fcx.body_id, traits::ItemObligation(trait_ref.def_id)), &trait_ref.substs, &bounds); @@ -341,6 +345,7 @@ impl<'cx,'tcx> TypeFolder<'tcx> for BoundsChecker<'cx,'tcx> { if self.binding_count == 0 { self.fcx.add_obligations_for_parameters( traits::ObligationCause::new(self.span, + self.fcx.body_id, traits::ItemObligation(type_id)), substs, &polytype.generics.to_bounds(self.tcx(), substs)); @@ -369,6 +374,7 @@ impl<'cx,'tcx> TypeFolder<'tcx> for BoundsChecker<'cx,'tcx> { // that will require an RFC. -nmatsakis) self.fcx.add_trait_obligations_for_generics( traits::ObligationCause::new(self.span, + self.fcx.body_id, traits::ItemObligation(type_id)), substs, &polytype.generics.to_bounds(self.tcx(), substs)); @@ -469,7 +475,7 @@ fn check_struct_safe_for_destructor<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, if !struct_tpt.generics.has_type_params(subst::TypeSpace) && !struct_tpt.generics.has_region_params(subst::TypeSpace) { - let cause = traits::ObligationCause::new(span, traits::DropTrait); + let cause = traits::ObligationCause::new(span, fcx.body_id, traits::DropTrait); let obligation = traits::obligation_for_builtin_bound(fcx.tcx(), cause, self_ty,