Restrict access to crate_name.

Also remove original_crate_name, which had the exact same implementation
This commit is contained in:
Camille GILLOT 2021-05-10 18:23:32 +02:00
parent 5d9f96ab27
commit 10fb4b2fe5
19 changed files with 23 additions and 30 deletions

View file

@ -524,7 +524,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
} }
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> { fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
Ok(vec![self.tcx.original_crate_name(cnum).to_string()]) Ok(vec![self.tcx.crate_name(cnum).to_string()])
} }
fn path_qualified( fn path_qualified(
self, self,

View file

@ -922,7 +922,7 @@ impl<'tcx> LateContext<'tcx> {
} }
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> { fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
Ok(vec![self.tcx.original_crate_name(cnum)]) Ok(vec![self.tcx.crate_name(cnum)])
} }
fn path_qualified( fn path_qualified(

View file

@ -187,7 +187,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
foreign_modules => { cdata.get_foreign_modules(tcx) } foreign_modules => { cdata.get_foreign_modules(tcx) }
crate_hash => { cdata.root.hash } crate_hash => { cdata.root.hash }
crate_host_hash => { cdata.host_hash } crate_host_hash => { cdata.host_hash }
original_crate_name => { cdata.root.name } crate_name => { cdata.root.name }
extra_filename => { cdata.root.extra_filename.clone() } extra_filename => { cdata.root.extra_filename.clone() }
@ -204,7 +204,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
let r = *cdata.dep_kind.lock(); let r = *cdata.dep_kind.lock();
r r
} }
crate_name => { cdata.root.name }
item_children => { item_children => {
let mut result = SmallVec::<[_; 8]>::new(); let mut result = SmallVec::<[_; 8]>::new();
cdata.each_child_of_item(def_id.index, |child| result.push(child), tcx.sess); cdata.each_child_of_item(def_id.index, |child| result.push(child), tcx.sess);

View file

@ -1673,7 +1673,7 @@ impl EncodeContext<'a, 'tcx> {
.iter() .iter()
.map(|&cnum| { .map(|&cnum| {
let dep = CrateDep { let dep = CrateDep {
name: self.tcx.original_crate_name(cnum), name: self.tcx.crate_name(cnum),
hash: self.tcx.crate_hash(cnum), hash: self.tcx.crate_hash(cnum),
host_hash: self.tcx.crate_host_hash(cnum), host_hash: self.tcx.crate_host_hash(cnum),
kind: self.tcx.dep_kind(cnum), kind: self.tcx.dep_kind(cnum),

View file

@ -285,7 +285,7 @@ pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
// required that their size stay the same, but we don't want to change // required that their size stay the same, but we don't want to change
// it inadvertently. This assert just ensures we're aware of any change. // it inadvertently. This assert just ensures we're aware of any change.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
static_assert_size!(DepNode, 18); static_assert_size!(DepNode, 17);
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
static_assert_size!(DepNode, 24); static_assert_size!(DepNode, 24);

View file

@ -49,7 +49,7 @@ impl<'tcx> ExportedSymbol<'tcx> {
pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String { pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
format!( format!(
"rust_metadata_{}_{:08x}", "rust_metadata_{}_{:08x}",
tcx.original_crate_name(LOCAL_CRATE), tcx.crate_name(LOCAL_CRATE),
tcx.sess.local_stable_crate_id().to_u64(), tcx.sess.local_stable_crate_id().to_u64(),
) )
} }

View file

@ -1252,10 +1252,6 @@ rustc_queries! {
eval_always eval_always
desc { "looking up the hash of a host version of a crate" } desc { "looking up the hash of a host version of a crate" }
} }
query original_crate_name(_: CrateNum) -> Symbol {
eval_always
desc { "looking up the original name a crate" }
}
query extra_filename(_: CrateNum) -> String { query extra_filename(_: CrateNum) -> String {
eval_always eval_always
desc { "looking up the extra filename for a crate" } desc { "looking up the extra filename for a crate" }

View file

@ -985,7 +985,7 @@ pub struct GlobalCtxt<'tcx> {
/// The definite name of the current crate after taking into account /// The definite name of the current crate after taking into account
/// attributes, commandline parameters, etc. /// attributes, commandline parameters, etc.
pub crate_name: Symbol, crate_name: Symbol,
/// Data layout specification for the current target. /// Data layout specification for the current target.
pub data_layout: TargetDataLayout, pub data_layout: TargetDataLayout,

View file

@ -1619,7 +1619,7 @@ impl<'tcx> TyCtxt<'tcx> {
fn item_name_from_def_id(self, def_id: DefId) -> Option<Symbol> { fn item_name_from_def_id(self, def_id: DefId) -> Option<Symbol> {
if def_id.index == CRATE_DEF_INDEX { if def_id.index == CRATE_DEF_INDEX {
Some(self.original_crate_name(def_id.krate)) Some(self.crate_name(def_id.krate))
} else { } else {
let def_key = self.def_key(def_id); let def_key = self.def_key(def_id);
match def_key.disambiguated_data.data { match def_key.disambiguated_data.data {

View file

@ -452,7 +452,7 @@ pub trait PrettyPrinter<'tcx>:
} }
// Re-exported `extern crate` (#43189). // Re-exported `extern crate` (#43189).
DefPathData::CrateRoot => { DefPathData::CrateRoot => {
data = DefPathData::TypeNs(self.tcx().original_crate_name(def_id.krate)); data = DefPathData::TypeNs(self.tcx().crate_name(def_id.krate));
} }
_ => {} _ => {}
} }

View file

@ -88,7 +88,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
} }
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> { fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
self.path.push_str(&self.tcx.original_crate_name(cnum).as_str()); self.path.push_str(&self.tcx.crate_name(cnum).as_str());
Ok(self) Ok(self)
} }

View file

@ -61,7 +61,7 @@ impl<'p, 'c, 'tcx> QueryKeyStringBuilder<'p, 'c, 'tcx> {
match def_key.disambiguated_data.data { match def_key.disambiguated_data.data {
DefPathData::CrateRoot => { DefPathData::CrateRoot => {
crate_name = self.tcx.original_crate_name(def_id.krate).as_str(); crate_name = self.tcx.crate_name(def_id.krate).as_str();
name = &*crate_name; name = &*crate_name;
dis = ""; dis = "";
end_index = 3; end_index = 3;

View file

@ -254,7 +254,7 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
} }
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> { fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
self.write_str(&self.tcx.original_crate_name(cnum).as_str())?; self.write_str(&self.tcx.crate_name(cnum).as_str())?;
Ok(self) Ok(self)
} }
fn path_qualified( fn path_qualified(

View file

@ -594,7 +594,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
self.push("C"); self.push("C");
let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id(); let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
self.push_disambiguator(stable_crate_id.to_u64()); self.push_disambiguator(stable_crate_id.to_u64());
let name = self.tcx.original_crate_name(cnum).as_str(); let name = self.tcx.crate_name(cnum).as_str();
self.push_ident(&name); self.push_ident(&name);
Ok(self) Ok(self)
} }

View file

@ -6,6 +6,7 @@ use rustc_infer::infer::InferCtxt;
use rustc_middle::ty::{self, TraitRef, Ty, TyCtxt, WithConstness}; use rustc_middle::ty::{self, TraitRef, Ty, TyCtxt, WithConstness};
use rustc_middle::ty::{ToPredicate, TypeFoldable}; use rustc_middle::ty::{ToPredicate, TypeFoldable};
use rustc_session::DiagnosticMessageId; use rustc_session::DiagnosticMessageId;
use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::Span; use rustc_span::Span;
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@ -231,7 +232,8 @@ pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Spa
.span_label(span, "deref recursion limit reached") .span_label(span, "deref recursion limit reached")
.help(&format!( .help(&format!(
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)", "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",
suggested_limit, tcx.crate_name, suggested_limit,
tcx.crate_name(LOCAL_CRATE),
)) ))
.emit(); .emit();
} }

View file

@ -21,6 +21,7 @@ use rustc_middle::ty::{
Infer, InferTy, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness, Infer, InferTy, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness,
}; };
use rustc_middle::ty::{TypeAndMut, TypeckResults}; use rustc_middle::ty::{TypeAndMut, TypeckResults};
use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, MultiSpan, Span, DUMMY_SP}; use rustc_span::{BytePos, MultiSpan, Span, DUMMY_SP};
use rustc_target::spec::abi; use rustc_target::spec::abi;
@ -2313,7 +2314,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
let suggested_limit = current_limit * 2; let suggested_limit = current_limit * 2;
err.help(&format!( err.help(&format!(
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)", "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",
suggested_limit, self.tcx.crate_name, suggested_limit,
self.tcx.crate_name(LOCAL_CRATE),
)); ));
} }

View file

@ -1,12 +1,11 @@
use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::fx::FxIndexSet;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::hir::map as hir_map; use rustc_middle::hir::map as hir_map;
use rustc_middle::ty::subst::Subst; use rustc_middle::ty::subst::Subst;
use rustc_middle::ty::{ use rustc_middle::ty::{
self, Binder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt, WithConstness, self, Binder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt, WithConstness,
}; };
use rustc_span::symbol::Symbol;
use rustc_span::Span; use rustc_span::Span;
use rustc_trait_selection::traits; use rustc_trait_selection::traits;
@ -388,11 +387,6 @@ fn param_env_reveal_all_normalized(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamE
tcx.param_env(def_id).with_reveal_all_normalized(tcx) tcx.param_env(def_id).with_reveal_all_normalized(tcx)
} }
fn original_crate_name(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Symbol {
assert_eq!(crate_num, LOCAL_CRATE);
tcx.crate_name
}
fn instance_def_size_estimate<'tcx>( fn instance_def_size_estimate<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
instance_def: ty::InstanceDef<'tcx>, instance_def: ty::InstanceDef<'tcx>,
@ -538,7 +532,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
param_env, param_env,
param_env_reveal_all_normalized, param_env_reveal_all_normalized,
trait_of_item, trait_of_item,
original_crate_name,
instance_def_size_estimate, instance_def_size_estimate,
issue33140_self_ty, issue33140_self_ty,
impl_defaultness, impl_defaultness,

View file

@ -527,7 +527,7 @@ fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::St
} }
fn build_macro(cx: &mut DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind { fn build_macro(cx: &mut DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind {
let imported_from = cx.tcx.original_crate_name(did.krate); let imported_from = cx.tcx.crate_name(did.krate);
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) { match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) {
LoadedMacro::MacroDef(def, _) => { LoadedMacro::MacroDef(def, _) => {
let matchers: Vec<Span> = if let ast::ItemKind::MacroDef(ref def) = def.kind { let matchers: Vec<Span> = if let ast::ItemKind::MacroDef(ref def) = def.kind {

View file

@ -9,6 +9,7 @@ use rustc_hir::Node;
use rustc_middle::middle::privacy::AccessLevel; use rustc_middle::middle::privacy::AccessLevel;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_span; use rustc_span;
use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::symbol::{kw, sym, Symbol};
@ -76,7 +77,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
&Spanned { span, node: hir::VisibilityKind::Public }, &Spanned { span, node: hir::VisibilityKind::Public },
hir::CRATE_HIR_ID, hir::CRATE_HIR_ID,
&krate.item, &krate.item,
self.cx.tcx.crate_name, self.cx.tcx.crate_name(LOCAL_CRATE),
); );
// Attach the crate's exported macros to the top-level module. // Attach the crate's exported macros to the top-level module.
// In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as // In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as