Skip to content

Commit

Permalink
rustc: expose the common DUMMY_SP query case as tcx methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Apr 24, 2017
1 parent 612bb1f commit 9bde6b6
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 108 deletions.
4 changes: 2 additions & 2 deletions src/librustc/lint/context.rs
Expand Up @@ -43,7 +43,7 @@ use std::fmt;
use syntax::attr;
use syntax::ast;
use syntax::symbol::Symbol;
use syntax_pos::{DUMMY_SP, MultiSpan, Span};
use syntax_pos::{MultiSpan, Span};
use errors::{self, Diagnostic, DiagnosticBuilder};
use hir;
use hir::def_id::LOCAL_CRATE;
Expand Down Expand Up @@ -1234,7 +1234,7 @@ fn check_lint_name_cmdline(sess: &Session, lint_cx: &LintStore,
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
let _task = tcx.dep_graph.in_task(DepNode::LateLintCheck);

let access_levels = &ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE);
let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);

let krate = tcx.hir.krate();

Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/dead.rs
Expand Up @@ -26,7 +26,6 @@ use util::nodemap::FxHashSet;

use syntax::{ast, codemap};
use syntax::attr;
use syntax::codemap::DUMMY_SP;
use syntax_pos;

// Any local node that may call something in its body block should be
Expand Down Expand Up @@ -593,7 +592,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
}

pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
let access_levels = &ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE);
let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);
let krate = tcx.hir.krate();
let live_symbols = find_live(tcx, access_levels, krate);
let mut visitor = DeadVisitor { tcx: tcx, live_symbols: live_symbols };
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/reachable.rs
Expand Up @@ -28,7 +28,6 @@ use util::nodemap::{NodeSet, FxHashSet};
use syntax::abi::Abi;
use syntax::ast;
use syntax::attr;
use syntax::codemap::DUMMY_SP;
use hir;
use hir::def_id::LOCAL_CRATE;
use hir::intravisit::{Visitor, NestedVisitorMap};
Expand Down Expand Up @@ -364,13 +363,13 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
}

pub fn find_reachable<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Rc<NodeSet> {
ty::queries::reachable_set::get(tcx, DUMMY_SP, LOCAL_CRATE)
tcx.reachable_set(LOCAL_CRATE)
}

fn reachable_set<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) -> Rc<NodeSet> {
debug_assert!(crate_num == LOCAL_CRATE);

let access_levels = &ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE);
let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);

let any_library = tcx.sess.crate_types.borrow().iter().any(|ty| {
*ty == config::CrateTypeRlib || *ty == config::CrateTypeDylib ||
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/stability.rs
Expand Up @@ -656,7 +656,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
let sess = &tcx.sess;

let access_levels = &ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE);
let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);

if tcx.stability.borrow().staged_api[&LOCAL_CRATE] && tcx.sess.features.borrow().staged_api {
let krate = tcx.hir.krate();
Expand Down
7 changes: 7 additions & 0 deletions src/librustc/ty/maps.rs
Expand Up @@ -351,6 +351,13 @@ macro_rules! define_maps {
}
})*

impl<'a, $tcx, 'lcx> TyCtxt<'a, $tcx, 'lcx> {
$($(#[$attr])*
pub fn $name(self, key: $K) -> $V {
queries::$name::get(self, DUMMY_SP, key)
})*
}

pub struct Providers<$tcx> {
$(pub $name: for<'a> fn(TyCtxt<'a, $tcx, $tcx>, $K) -> $V),*
}
Expand Down
79 changes: 5 additions & 74 deletions src/librustc/ty/mod.rs
Expand Up @@ -1686,7 +1686,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
let mut discr = prev_discr.map_or(initial, |d| d.wrap_incr());
if let VariantDiscr::Explicit(expr_did) = v.discr {
let substs = Substs::empty();
match queries::const_eval::get(tcx, DUMMY_SP, (expr_did, substs)) {
match tcx.const_eval((expr_did, substs)) {
Ok(ConstVal::Integral(v)) => {
discr = v;
}
Expand Down Expand Up @@ -1725,7 +1725,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
}
ty::VariantDiscr::Explicit(expr_did) => {
let substs = Substs::empty();
match queries::const_eval::get(tcx, DUMMY_SP, (expr_did, substs)) {
match tcx.const_eval((expr_did, substs)) {
Ok(ConstVal::Integral(v)) => {
explicit_value = v;
break;
Expand Down Expand Up @@ -1760,7 +1760,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
}

pub fn destructor(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Destructor> {
queries::adt_destructor::get(tcx, DUMMY_SP, self.did)
tcx.adt_destructor(self.did)
}

/// Returns a list of types such that `Self: Sized` if and only
Expand Down Expand Up @@ -2045,10 +2045,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.typeck_tables_of(self.hir.body_owner_def_id(body))
}

pub fn typeck_tables_of(self, def_id: DefId) -> &'gcx TypeckTables<'gcx> {
queries::typeck_tables_of::get(self, DUMMY_SP, def_id)
}

pub fn expr_span(self, id: NodeId) -> Span {
match self.hir.find(id) {
Some(hir_map::NodeExpr(e)) => {
Expand Down Expand Up @@ -2136,24 +2132,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
.collect()
}

pub fn impl_polarity(self, id: DefId) -> hir::ImplPolarity {
queries::impl_polarity::get(self, DUMMY_SP, id)
}

pub fn trait_relevant_for_never(self, did: DefId) -> bool {
self.associated_items(did).any(|item| {
item.relevant_for_never()
})
}

pub fn coerce_unsized_info(self, did: DefId) -> adjustment::CoerceUnsizedInfo {
queries::coerce_unsized_info::get(self, DUMMY_SP, did)
}

pub fn associated_item(self, def_id: DefId) -> AssociatedItem {
queries::associated_item::get(self, DUMMY_SP, def_id)
}

fn associated_item_from_trait_item_ref(self,
parent_def_id: DefId,
trait_item_ref: &hir::TraitItemRef)
Expand Down Expand Up @@ -2207,23 +2191,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

pub fn associated_item_def_ids(self, def_id: DefId) -> Rc<Vec<DefId>> {
queries::associated_item_def_ids::get(self, DUMMY_SP, def_id)
}

#[inline] // FIXME(#35870) Avoid closures being unexported due to impl Trait.
pub fn associated_items(self, def_id: DefId)
-> impl Iterator<Item = ty::AssociatedItem> + 'a {
let def_ids = self.associated_item_def_ids(def_id);
(0..def_ids.len()).map(move |i| self.associated_item(def_ids[i]))
}

/// Returns the trait-ref corresponding to a given impl, or None if it is
/// an inherent impl.
pub fn impl_trait_ref(self, id: DefId) -> Option<TraitRef<'gcx>> {
queries::impl_trait_ref::get(self, DUMMY_SP, id)
}

/// Returns true if the impls are the same polarity and are implementing
/// a trait which contains no items
pub fn impls_are_allowed_to_overlap(self, def_id1: DefId, def_id2: DefId) -> bool {
Expand Down Expand Up @@ -2325,40 +2299,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

// If the given item is in an external crate, looks up its type and adds it to
// the type cache. Returns the type parameters and type.
pub fn type_of(self, did: DefId) -> Ty<'gcx> {
queries::type_of::get(self, DUMMY_SP, did)
}

/// Given the did of a trait, returns its canonical trait ref.
pub fn trait_def(self, did: DefId) -> &'gcx TraitDef {
queries::trait_def::get(self, DUMMY_SP, did)
}

/// Given the did of an ADT, return a reference to its definition.
pub fn adt_def(self, did: DefId) -> &'gcx AdtDef {
queries::adt_def::get(self, DUMMY_SP, did)
}

/// Given the did of an item, returns its generics.
pub fn generics_of(self, did: DefId) -> &'gcx Generics {
queries::generics_of::get(self, DUMMY_SP, did)
}

/// Given the did of an item, returns its full set of predicates.
pub fn predicates_of(self, did: DefId) -> GenericPredicates<'gcx> {
queries::predicates_of::get(self, DUMMY_SP, did)
}

/// Given the did of a trait, returns its superpredicates.
pub fn super_predicates_of(self, did: DefId) -> GenericPredicates<'gcx> {
queries::super_predicates_of::get(self, DUMMY_SP, did)
}

/// Given the did of an item, returns its MIR, borrowed immutably.
pub fn item_mir(self, did: DefId) -> Ref<'gcx, Mir<'gcx>> {
queries::mir::get(self, DUMMY_SP, did).borrow()
self.mir(did).borrow()
}

/// Return the possibly-auto-generated MIR of a (DefId, Subst) pair.
Expand All @@ -2367,7 +2310,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
{
match instance {
ty::InstanceDef::Item(did) if true => self.item_mir(did),
_ => queries::mir_shims::get(self, DUMMY_SP, instance).borrow(),
_ => self.mir_shims(instance).borrow(),
}
}

Expand Down Expand Up @@ -2399,10 +2342,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.get_attrs(did).iter().any(|item| item.check_name(attr))
}

pub fn variances_of(self, item_id: DefId) -> Rc<Vec<ty::Variance>> {
queries::variances_of::get(self, DUMMY_SP, item_id)
}

pub fn trait_has_default_impl(self, trait_def_id: DefId) -> bool {
let def = self.trait_def(trait_def_id);
def.flags.get().intersects(TraitFlags::HAS_DEFAULT_IMPL)
Expand Down Expand Up @@ -2437,14 +2376,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
def.flags.set(def.flags.get() | TraitFlags::HAS_REMOTE_IMPLS);
}

pub fn closure_kind(self, def_id: DefId) -> ty::ClosureKind {
queries::closure_kind::get(self, DUMMY_SP, def_id)
}

pub fn closure_type(self, def_id: DefId) -> ty::PolyFnSig<'tcx> {
queries::closure_type::get(self, DUMMY_SP, def_id)
}

/// Given the def_id of an impl, return the def_id of the trait it implements.
/// If it implements no trait, return `None`.
pub fn trait_id_of_impl(self, def_id: DefId) -> Option<DefId> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/util.rs
Expand Up @@ -369,7 +369,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
return None;
};

ty::queries::coherent_trait::get(self, DUMMY_SP, (LOCAL_CRATE, drop_trait));
self.coherent_trait((LOCAL_CRATE, drop_trait));

let mut dtor_did = None;
let ty = self.type_of(adt_did);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_borrowck/borrowck/mod.rs
Expand Up @@ -42,7 +42,7 @@ use std::fmt;
use std::rc::Rc;
use std::hash::{Hash, Hasher};
use syntax::ast;
use syntax_pos::{DUMMY_SP, MultiSpan, Span};
use syntax_pos::{MultiSpan, Span};
use errors::DiagnosticBuilder;

use rustc::hir;
Expand All @@ -63,7 +63,7 @@ pub type LoanDataFlow<'a, 'tcx> = DataFlowContext<'a, 'tcx, LoanDataFlowOperator

pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
tcx.visit_all_bodies_in_krate(|body_owner_def_id, _body_id| {
ty::queries::borrowck::get(tcx, DUMMY_SP, body_owner_def_id);
tcx.borrowck(body_owner_def_id);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_const_eval/eval.rs
Expand Up @@ -27,7 +27,7 @@ use rustc::util::nodemap::DefIdMap;

use syntax::ast;
use rustc::hir::{self, Expr};
use syntax_pos::{Span, DUMMY_SP};
use syntax_pos::Span;

use std::cmp::Ordering;

Expand Down Expand Up @@ -773,7 +773,7 @@ fn const_eval<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
};

let body = if let Some(id) = tcx.hir.as_local_node_id(def_id) {
ty::queries::mir_const_qualif::get(tcx, DUMMY_SP, def_id);
tcx.mir_const_qualif(def_id);
tcx.hir.body(tcx.hir.body_owned_by(id))
} else {
tcx.sess.cstore.item_body(tcx, def_id)
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/encoder.rs
Expand Up @@ -36,7 +36,7 @@ use syntax::ast::{self, CRATE_NODE_ID};
use syntax::codemap::Spanned;
use syntax::attr;
use syntax::symbol::Symbol;
use syntax_pos::{self, DUMMY_SP};
use syntax_pos;

use rustc::hir::{self, PatKind};
use rustc::hir::itemlikevisit::ItemLikeVisitor;
Expand Down Expand Up @@ -1169,7 +1169,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> EntryBuilder<'a, 'b, 'tcx> {
let body = tcx.hir.body_owned_by(id);

Entry {
kind: EntryKind::Const(ty::queries::mir_const_qualif::get(tcx, DUMMY_SP, def_id)),
kind: EntryKind::Const(tcx.mir_const_qualif(def_id)),
visibility: self.lazy(&ty::Visibility::Public),
span: self.lazy(&tcx.def_span(def_id)),
attributes: LazySeq::empty(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/qualify_consts.rs
Expand Up @@ -959,7 +959,7 @@ impl<'tcx> MirMapPass<'tcx> for QualifyAndPromoteConstants {
let src = MirSource::from_node(tcx, id);

if let MirSource::Const(_) = src {
ty::queries::mir_const_qualif::get(tcx, DUMMY_SP, def_id);
tcx.mir_const_qualif(def_id);
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_privacy/lib.rs
Expand Up @@ -38,7 +38,7 @@ use rustc::ty::fold::TypeVisitor;
use rustc::ty::maps::Providers;
use rustc::util::nodemap::NodeSet;
use syntax::ast;
use syntax_pos::{DUMMY_SP, Span};
use syntax_pos::Span;

use std::cmp;
use std::mem::replace;
Expand Down Expand Up @@ -1222,7 +1222,7 @@ pub fn provide(providers: &mut Providers) {

pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Rc<AccessLevels> {
tcx.dep_graph.with_ignore(|| { // FIXME
ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE)
tcx.privacy_access_levels(LOCAL_CRATE)
})
}

Expand Down
5 changes: 2 additions & 3 deletions src/librustc_trans/callee.rs
Expand Up @@ -21,9 +21,8 @@ use declare;
use llvm::{self, ValueRef};
use monomorphize::{self, Instance};
use rustc::hir::def_id::DefId;
use rustc::ty::{self, TypeFoldable};
use rustc::ty::TypeFoldable;
use rustc::ty::subst::Substs;
use syntax_pos::DUMMY_SP;
use trans_item::TransItem;
use type_of;

Expand Down Expand Up @@ -105,7 +104,7 @@ pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
// *in Rust code* may unwind. Foreign items like `extern "C" {
// fn foo(); }` are assumed not to unwind **unless** they have
// a `#[unwind]` attribute.
if !ty::queries::is_foreign_item::get(tcx, DUMMY_SP, instance.def_id()) {
if !tcx.is_foreign_item(instance.def_id()) {
attributes::unwind(llfn, true);
unsafe {
llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Expand Up @@ -621,7 +621,7 @@ pub fn check_item_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> CompileResult
}

pub fn check_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> CompileResult {
ty::queries::typeck_item_bodies::get(tcx, DUMMY_SP, LOCAL_CRATE)
tcx.typeck_item_bodies(LOCAL_CRATE)
}

fn typeck_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) -> CompileResult {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/coherence/inherent_impls.rs
Expand Up @@ -26,7 +26,7 @@ use rustc::util::nodemap::DefIdMap;

use std::rc::Rc;
use syntax::ast;
use syntax_pos::{DUMMY_SP, Span};
use syntax_pos::Span;

/// On-demand query: yields a map containing all types mapped to their inherent impls.
pub fn crate_inherent_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
Expand Down Expand Up @@ -67,7 +67,7 @@ pub fn inherent_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// [the plan]: https://github.com/rust-lang/rust-roadmap/issues/4

let result = tcx.dep_graph.with_ignore(|| {
let crate_map = ty::queries::crate_inherent_impls::get(tcx, DUMMY_SP, ty_def_id.krate);
let crate_map = tcx.crate_inherent_impls(ty_def_id.krate);
match crate_map.inherent_impls.get(&ty_def_id) {
Some(v) => v.clone(),
None => Rc::new(vec![]),
Expand Down

0 comments on commit 9bde6b6

Please sign in to comment.