Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
rustc: use tcx.at(span) to set the location of a query.
  • Loading branch information
eddyb committed Apr 24, 2017
1 parent 9bde6b6 commit decf759
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/librustc/middle/const_val.rs
Expand Up @@ -14,7 +14,7 @@ pub use rustc_const_math::ConstInt;
use hir;
use hir::def::Def;
use hir::def_id::DefId;
use ty::{self, TyCtxt};
use ty::TyCtxt;
use ty::subst::Substs;
use util::common::ErrorReported;
use rustc_const_math::*;
Expand Down Expand Up @@ -228,7 +228,7 @@ pub fn eval_length(tcx: TyCtxt,
let count_expr = &tcx.hir.body(count).value;
let count_def_id = tcx.hir.body_owner_def_id(count);
let substs = Substs::empty();
match ty::queries::const_eval::get(tcx, count_expr.span, (count_def_id, substs)) {
match tcx.at(count_expr.span).const_eval((count_def_id, substs)) {
Ok(Integral(Usize(count))) => {
let val = count.as_u64(tcx.sess.target.uint_type);
assert_eq!(val as usize as u64, val);
Expand Down
43 changes: 34 additions & 9 deletions src/librustc/ty/maps.rs
Expand Up @@ -21,6 +21,7 @@ use util::nodemap::NodeSet;

use rustc_data_structures::indexed_vec::IndexVec;
use std::cell::{RefCell, RefMut};
use std::ops::Deref;
use std::rc::Rc;
use syntax_pos::{Span, DUMMY_SP};

Expand Down Expand Up @@ -329,14 +330,6 @@ macro_rules! define_maps {
Self::try_get_with(tcx, span, key, Clone::clone)
}

$(#[$attr])*
pub fn get(tcx: TyCtxt<'a, $tcx, 'lcx>, span: Span, key: $K) -> $V {
Self::try_get(tcx, span, key).unwrap_or_else(|e| {
tcx.report_cycle(e);
Value::from_cycle_error(tcx.global_tcx())
})
}

pub fn force(tcx: TyCtxt<'a, $tcx, 'lcx>, span: Span, key: $K) {
// FIXME(eddyb) Move away from using `DepTrackingMap`
// so we don't have to explicitly ignore a false edge:
Expand All @@ -351,10 +344,42 @@ macro_rules! define_maps {
}
})*

#[derive(Copy, Clone)]
pub struct TyCtxtAt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
pub span: Span,
}

impl<'a, 'gcx, 'tcx> Deref for TyCtxtAt<'a, 'gcx, 'tcx> {
type Target = TyCtxt<'a, 'gcx, 'tcx>;
fn deref(&self) -> &Self::Target {
&self.tcx
}
}

impl<'a, $tcx, 'lcx> TyCtxt<'a, $tcx, 'lcx> {
/// Return a transparent wrapper for `TyCtxt` which uses
/// `span` as the location of queries performed through it.
pub fn at(self, span: Span) -> TyCtxtAt<'a, $tcx, 'lcx> {
TyCtxtAt {
tcx: self,
span
}
}

$($(#[$attr])*
pub fn $name(self, key: $K) -> $V {
self.at(DUMMY_SP).$name(key)
})*
}

impl<'a, $tcx, 'lcx> TyCtxtAt<'a, $tcx, 'lcx> {
$($(#[$attr])*
pub fn $name(self, key: $K) -> $V {
queries::$name::get(self, DUMMY_SP, key)
queries::$name::try_get(self.tcx, self.span, key).unwrap_or_else(|e| {
self.report_cycle(e);
Value::from_cycle_error(self.global_tcx())
})
})*
}

Expand Down
5 changes: 2 additions & 3 deletions src/librustc/ty/mod.rs
Expand Up @@ -2699,9 +2699,8 @@ pub fn provide_extern(providers: &mut ty::maps::Providers) {
/// A map for the local crate mapping each type to a vector of its
/// inherent impls. This is not meant to be used outside of coherence;
/// rather, you should request the vector for a specific type via
/// `ty::queries::inherent_impls::get(def_id)` so as to minimize your
/// dependencies (constructing this map requires touching the entire
/// crate).
/// `tcx.inherent_impls(def_id)` so as to minimize your dependencies
/// (constructing this map requires touching the entire crate).
#[derive(Clone, Debug)]
pub struct CrateInherentImpls {
pub inherent_impls: DefIdMap<Rc<Vec<DefId>>>,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/util.rs
Expand Up @@ -522,7 +522,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
ty::TyAdt(def, substs) => {
let ty::DtorckConstraint {
dtorck_types, outlives
} = ty::queries::adt_dtorck_constraint::get(self, span, def.did);
} = self.at(span).adt_dtorck_constraint(def.did);
Ok(ty::DtorckConstraint {
// FIXME: we can try to recursively `dtorck_constraint_on_ty`
// there, but that needs some way to handle cycles.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_const_eval/eval.rs
Expand Up @@ -299,7 +299,7 @@ fn eval_const_expr_partial<'a, 'tcx>(cx: &ConstContext<'a, 'tcx>,
match cx.tables.qpath_def(qpath, e.id) {
Def::Const(def_id) |
Def::AssociatedConst(def_id) => {
match ty::queries::const_eval::get(tcx, e.span, (def_id, substs)) {
match tcx.at(e.span).const_eval((def_id, substs)) {
Ok(val) => val,
Err(ConstEvalErr { kind: TypeckError, .. }) => {
signal!(e, TypeckError);
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_metadata/encoder.rs
Expand Up @@ -547,7 +547,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> EntryBuilder<'a, 'b, 'tcx> {
let kind = match impl_item.kind {
ty::AssociatedKind::Const => {
EntryKind::AssociatedConst(container,
ty::queries::mir_const_qualif::get(self.tcx, ast_item.span, def_id))
self.tcx.at(ast_item.span).mir_const_qualif(def_id))
}
ty::AssociatedKind::Method => {
let fn_data = if let hir::ImplItemKind::Method(ref sig, body) = ast_item.node {
Expand Down Expand Up @@ -656,7 +656,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> EntryBuilder<'a, 'b, 'tcx> {
hir::ItemStatic(_, hir::MutMutable, _) => EntryKind::MutStatic,
hir::ItemStatic(_, hir::MutImmutable, _) => EntryKind::ImmStatic,
hir::ItemConst(..) => {
EntryKind::Const(ty::queries::mir_const_qualif::get(tcx, item.span, def_id))
EntryKind::Const(tcx.at(item.span).mir_const_qualif(def_id))
}
hir::ItemFn(_, _, constness, .., body) => {
let data = FnData {
Expand Down Expand Up @@ -732,7 +732,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> EntryBuilder<'a, 'b, 'tcx> {
let coerce_unsized_info =
trait_ref.and_then(|t| {
if Some(t.def_id) == tcx.lang_items.coerce_unsized_trait() {
Some(ty::queries::coerce_unsized_info::get(tcx, item.span, def_id))
Some(tcx.at(item.span).coerce_unsized_info(def_id))
} else {
None
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/hair/cx/expr.rs
Expand Up @@ -594,7 +594,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
let c = &cx.tcx.hir.body(count).value;
let def_id = cx.tcx.hir.body_owner_def_id(count);
let substs = Substs::empty();
let count = match ty::queries::const_eval::get(cx.tcx, c.span, (def_id, substs)) {
let count = match cx.tcx.at(c.span).const_eval((def_id, substs)) {
Ok(ConstVal::Integral(ConstInt::Usize(u))) => u,
Ok(other) => bug!("constant evaluation of repeat count yielded {:?}", other),
Err(s) => cx.fatal_const_eval_err(&s, c.span, "expression")
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_mir/transform/qualify_consts.rs
Expand Up @@ -573,9 +573,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
if substs.types().next().is_some() {
self.add_type(constant.ty);
} else {
let bits = ty::queries::mir_const_qualif::get(self.tcx,
constant.span,
def_id);
let bits = self.tcx.at(constant.span).mir_const_qualif(def_id);

let qualif = Qualif::from_bits(bits).expect("invalid mir_const_qualif");
self.add(qualif);
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_typeck/astconv.rs
Expand Up @@ -238,7 +238,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
let is_object = self_ty.map_or(false, |ty| ty.sty == TRAIT_OBJECT_DUMMY_SELF);
let default_needs_object_self = |p: &ty::TypeParameterDef| {
if is_object && p.has_default {
if ty::queries::type_of::get(tcx, span, p.def_id).has_self_ty() {
if tcx.at(span).type_of(p.def_id).has_self_ty() {
// There is no suitable inference default for a type parameter
// that references self, in an object type.
return true;
Expand Down Expand Up @@ -307,7 +307,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
// This is a default type parameter.
self.normalize_ty(
span,
ty::queries::type_of::get(tcx, span, def.def_id)
tcx.at(span).type_of(def.def_id)
.subst_spanned(tcx, substs, Some(span))
)
}
Expand Down Expand Up @@ -600,7 +600,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
let substs = self.ast_path_substs_for_ty(span, did, item_segment);
self.normalize_ty(
span,
ty::queries::type_of::get(self.tcx(), span, did).subst(self.tcx(), substs)
self.tcx().at(span).type_of(did).subst(self.tcx(), substs)
)
}

Expand Down Expand Up @@ -1018,7 +1018,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
assert_eq!(opt_self_ty, None);
self.prohibit_type_params(&path.segments);

let ty = ty::queries::type_of::get(tcx, span, def_id);
let ty = tcx.at(span).type_of(def_id);
if let Some(free_substs) = self.get_free_substs() {
ty.subst(tcx, free_substs)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/method/probe.rs
Expand Up @@ -479,7 +479,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
}

fn assemble_inherent_impl_candidates_for_type(&mut self, def_id: DefId) {
let impl_def_ids = ty::queries::inherent_impls::get(self.tcx, self.span, def_id);
let impl_def_ids = self.tcx.at(self.span).inherent_impls(def_id);
for &impl_def_id in impl_def_ids.iter() {
self.assemble_inherent_impl_probe(impl_def_id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/coherence/builtin.rs
Expand Up @@ -170,7 +170,7 @@ fn visit_implementation_of_coerce_unsized<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// course.
if impl_did.is_local() {
let span = tcx.def_span(impl_did);
ty::queries::coerce_unsized_info::get(tcx, span, impl_did);
tcx.at(span).coerce_unsized_info(impl_did);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/coherence/inherent_impls.rs
Expand Up @@ -14,7 +14,7 @@
//! for any change, but it is very cheap to compute. In practice, most
//! code in the compiler never *directly* requests this map. Instead,
//! it requests the inherent impls specific to some type (via
//! `ty::queries::inherent_impls::get(def_id)`). That value, however,
//! `tcx.inherent_impls(def_id)`). That value, however,
//! is computed by selecting an idea from this table.

use rustc::dep_graph::DepNode;
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_typeck/collect.rs
Expand Up @@ -207,7 +207,7 @@ impl<'a, 'tcx> AstConv<'tcx, 'tcx> for ItemCtxt<'a, 'tcx> {
def_id: DefId)
-> ty::GenericPredicates<'tcx>
{
ty::queries::type_param_predicates::get(self.tcx, span, (self.item_def_id, def_id))
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id))
}

fn get_free_substs(&self) -> Option<&Substs<'tcx>> {
Expand Down Expand Up @@ -475,7 +475,7 @@ fn convert_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_id: ast::NodeId) {
hir::ItemTrait(..) => {
tcx.generics_of(def_id);
tcx.trait_def(def_id);
ty::queries::super_predicates_of::get(tcx, it.span, def_id);
tcx.at(it.span).super_predicates_of(def_id);
tcx.predicates_of(def_id);
},
hir::ItemStruct(ref struct_def, _) |
Expand Down Expand Up @@ -556,7 +556,7 @@ fn convert_enum_variant_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
prev_discr = Some(if let Some(e) = variant.node.disr_expr {
let expr_did = tcx.hir.local_def_id(e.node_id);
let substs = Substs::empty();
let result = ty::queries::const_eval::get(tcx, variant.span, (expr_did, substs));
let result = tcx.at(variant.span).const_eval((expr_did, substs));

// enum variant evaluation happens before the global constant check
// so we need to report the real error
Expand Down Expand Up @@ -725,7 +725,7 @@ fn super_predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// Now require that immediate supertraits are converted,
// which will, in turn, reach indirect supertraits.
for bound in superbounds.iter().filter_map(|p| p.to_opt_poly_trait_ref()) {
ty::queries::super_predicates_of::get(tcx, item.span, bound.def_id());
tcx.at(item.span).super_predicates_of(bound.def_id());
}

ty::GenericPredicates {
Expand Down

0 comments on commit decf759

Please sign in to comment.