Skip to content

Move from {{closure}}#0 syntax to {closure#0} for (def) path components #76176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Move from {{closure}}#0 syntax to {closure#0} for (def) path components
  • Loading branch information
marmeladema committed Sep 25, 2020
commit f1878d19faf9472f8860a3ff4fe605fdf93cef2c
10 changes: 8 additions & 2 deletions compiler/rustc_codegen_llvm/src/debuginfo/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::common::CodegenCx;
use crate::llvm;
use crate::llvm::debuginfo::DIScope;
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::DefPathData;
use rustc_hir::definitions::{DefPathData, DefPathDataName};
use rustc_span::symbol::Symbol;

pub fn mangled_name_of_instance<'a, 'tcx>(
cx: &CodegenCx<'a, 'tcx>,
Expand All @@ -29,7 +30,12 @@ pub fn item_namespace(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {

let namespace_name = match def_key.disambiguated_data.data {
DefPathData::CrateRoot => cx.tcx.crate_name(def_id.krate),
data => data.as_symbol(),
data => match data.get_name() {
DefPathDataName::Named(name) => name,
DefPathDataName::Anon { namespace } => {
Symbol::intern(&format!("{{{{{}}}}}", namespace))
}
},
};
let namespace_name = namespace_name.as_str();

Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::DefPathDataName;
use rustc_middle::ty::{self, subst::SubstsRef, Ty, TyCtxt};

use std::fmt::Write;

// Compute the name of the type as it should be stored in debuginfo. Does not do
// any caching, i.e., calling the function twice with the same type will also do
// the work twice. The `qualified` parameter only affects the first level of the
Expand Down Expand Up @@ -229,7 +232,12 @@ pub fn push_debuginfo_type_name<'tcx>(
output.push_str(&tcx.crate_name(def_id.krate).as_str());
for path_element in tcx.def_path(def_id).data {
output.push_str("::");
output.push_str(&path_element.data.as_symbol().as_str());
match path_element.data.get_name() {
DefPathDataName::Named(name) => output.push_str(&name.as_str()),
DefPathDataName::Anon { namespace } => {
write!(output, "{{{{{}}}}}", namespace).unwrap()
}
}
}
} else {
output.push_str(&tcx.item_name(def_id).as_str());
Expand Down
57 changes: 37 additions & 20 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_index::vec::IndexVec;
use rustc_span::hygiene::ExpnId;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::symbol::{kw, sym, Symbol};

use std::fmt::Write;
use std::hash::Hash;
Expand Down Expand Up @@ -202,7 +202,12 @@ impl DefPath {
let mut s = String::with_capacity(self.data.len() * 16);

for component in &self.data {
write!(s, "::{}[{}]", component.data.as_symbol(), component.disambiguator).unwrap();
match component.data.get_name() {
DefPathDataName::Named(name) => write!(s, "::{}", name).unwrap(),
DefPathDataName::Anon { namespace } => {
write!(s, "::{{{}#{}}}", namespace, component.disambiguator).unwrap()
}
}
}

s
Expand All @@ -220,10 +225,11 @@ impl DefPath {
write!(s, "::{}", crate_name_str).unwrap();

for component in &self.data {
if component.disambiguator == 0 {
write!(s, "::{}", component.data.as_symbol()).unwrap();
} else {
write!(s, "{}[{}]", component.data.as_symbol(), component.disambiguator).unwrap();
match component.data.get_name() {
DefPathDataName::Named(name) => write!(s, "::{}", name).unwrap(),
DefPathDataName::Anon { namespace } => {
write!(s, "{{{}#{}}}", namespace, component.disambiguator).unwrap()
}
}
}

Expand All @@ -240,10 +246,11 @@ impl DefPath {
for component in &self.data {
s.extend(opt_delimiter);
opt_delimiter = Some('-');
if component.disambiguator == 0 {
write!(s, "{}", component.data.as_symbol()).unwrap();
} else {
write!(s, "{}[{}]", component.data.as_symbol(), component.disambiguator).unwrap();
match component.data.get_name() {
DefPathDataName::Named(name) => write!(s, "{}", name).unwrap(),
DefPathDataName::Anon { namespace } => {
write!(s, "{{{}#{}}}", namespace, component.disambiguator).unwrap()
}
}
}
s
Expand Down Expand Up @@ -427,6 +434,11 @@ impl Definitions {
}
}

pub enum DefPathDataName {
Named(Symbol),
Anon { namespace: Symbol },
}

impl DefPathData {
pub fn get_opt_name(&self) -> Option<Symbol> {
use self::DefPathData::*;
Expand All @@ -437,22 +449,27 @@ impl DefPathData {
}
}

pub fn as_symbol(&self) -> Symbol {
pub fn get_name(&self) -> DefPathDataName {
use self::DefPathData::*;
match *self {
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => name,
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => {
DefPathDataName::Named(name)
}
// Note that this does not show up in user print-outs.
CrateRoot => sym::double_braced_crate,
Impl => sym::double_braced_impl,
Misc => sym::double_braced_misc,
ClosureExpr => sym::double_braced_closure,
Ctor => sym::double_braced_constructor,
AnonConst => sym::double_braced_constant,
ImplTrait => sym::double_braced_opaque,
CrateRoot => DefPathDataName::Anon { namespace: kw::Crate },
Impl => DefPathDataName::Anon { namespace: kw::Impl },
Misc => DefPathDataName::Anon { namespace: sym::misc },
ClosureExpr => DefPathDataName::Anon { namespace: sym::closure },
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
ImplTrait => DefPathDataName::Anon { namespace: sym::opaque },
}
}

pub fn to_string(&self) -> String {
self.as_symbol().to_string()
match self.get_name() {
DefPathDataName::Named(name) => name.to_string(),
DefPathDataName::Anon { namespace } => format!("{{{{{}}}}}", namespace),
}
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
let mut path = print_prefix(self)?;
path.push(disambiguated_data.data.as_symbol().to_string());
path.push(disambiguated_data.data.to_string());
Ok(path)
}
fn path_generic_args(
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rustc_errors::{struct_span_err, Applicability};
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::def_id::{CrateNum, DefId};
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
use rustc_middle::lint::LintDiagnosticBuilder;
use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::middle::stability;
Expand Down Expand Up @@ -846,7 +846,12 @@ impl<'tcx> LateContext<'tcx> {
return Ok(path);
}

path.push(disambiguated_data.data.as_symbol());
path.push(match disambiguated_data.data.get_name() {
DefPathDataName::Named(name) => name,
DefPathDataName::Anon { namespace } => {
Symbol::intern(&format!("{{{{{}}}}}", namespace))
}
});
Ok(path)
}

Expand Down
21 changes: 11 additions & 10 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
use rustc_hir::ItemKind;
use rustc_session::config::TrimmedDefPaths;
use rustc_span::symbol::{kw, Ident, Symbol};
Expand Down Expand Up @@ -1496,25 +1496,26 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
return Ok(self);
}

let name = match disambiguated_data.data.get_name() {
DefPathDataName::Named(name) => name,
DefPathDataName::Anon { namespace } => namespace,
};

// FIXME(eddyb) `name` should never be empty, but it
// currently is for `extern { ... }` "foreign modules".
let name = disambiguated_data.data.as_symbol();
if name != kw::Invalid {
if !self.empty_path {
write!(self, "::")?;
}
if Ident::with_dummy_span(name).is_raw_guess() {
write!(self, "r#")?;
}
write!(self, "{}", name)?;

// FIXME(eddyb) this will print e.g. `{{closure}}#3`, but it
// might be nicer to use something else, e.g. `{closure#3}`.
let dis = disambiguated_data.disambiguator;
let print_dis = disambiguated_data.data.get_opt_name().is_none()
|| dis != 0 && self.tcx.sess.verbose();
if print_dis {
write!(self, "#{}", dis)?;
match disambiguated_data.data.get_name() {
DefPathDataName::Named(name) => self.write_str(&name.as_str())?,
DefPathDataName::Anon { namespace } => {
write!(self, "{{{}#{}}}", namespace, disambiguated_data.disambiguator)?
}
}

self.empty_path = false;
Expand Down
30 changes: 19 additions & 11 deletions compiler/rustc_middle/src/ty/query/profiling_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use measureme::{StringComponent, StringId};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::profiling::SelfProfiler;
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::DefPathData;
use rustc_hir::definitions::{DefPathData, DefPathDataName};
use rustc_query_system::query::QueryCache;
use rustc_query_system::query::QueryState;
use std::fmt::Debug;
Expand Down Expand Up @@ -66,17 +66,25 @@ impl<'p, 'c, 'tcx> QueryKeyStringBuilder<'p, 'c, 'tcx> {
end_index = 3;
}
other => {
name = other.as_symbol();
if def_key.disambiguated_data.disambiguator == 0 {
dis = "";
end_index = 3;
} else {
write!(&mut dis_buffer[..], "[{}]", def_key.disambiguated_data.disambiguator)
name = match other.get_name() {
DefPathDataName::Named(name) => {
dis = "";
end_index = 3;
name
}
DefPathDataName::Anon { namespace } => {
write!(
&mut dis_buffer[..],
"[{}]",
def_key.disambiguated_data.disambiguator
)
.unwrap();
let end_of_dis = dis_buffer.iter().position(|&c| c == b']').unwrap();
dis = std::str::from_utf8(&dis_buffer[..end_of_dis + 1]).unwrap();
end_index = 4;
}
let end_of_dis = dis_buffer.iter().position(|&c| c == b']').unwrap();
dis = std::str::from_utf8(&dis_buffer[..end_of_dis + 1]).unwrap();
end_index = 4;
namespace
}
};
}
}

Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_mir/src/interpret/intrinsics/type_name.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_hir::def_id::CrateNum;
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
use rustc_middle::mir::interpret::Allocation;
use rustc_middle::ty::{
self,
Expand Down Expand Up @@ -134,7 +134,12 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {

self.path.push_str("::");

self.path.push_str(&disambiguated_data.data.as_symbol().as_str());
match disambiguated_data.data.get_name() {
DefPathDataName::Named(name) => self.path.write_str(&name.as_str()).unwrap(),
DefPathDataName::Anon { namespace } => {
write!(self.path, "{{{{{}}}}}", namespace).unwrap()
}
}
Ok(self)
}

Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_mir/src/monomorphize/partitioning/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::hash_map::Entry;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::DefPathDataName;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::exported_symbols::SymbolExportLevel;
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, Linkage, Visibility};
Expand Down Expand Up @@ -354,7 +355,12 @@ fn compute_codegen_unit_name(
*cache.entry((cgu_def_id, volatile)).or_insert_with(|| {
let def_path = tcx.def_path(cgu_def_id);

let components = def_path.data.iter().map(|part| part.data.as_symbol());
let components = def_path.data.iter().map(|part| match part.data.get_name() {
DefPathDataName::Named(name) => name,
DefPathDataName::Anon { namespace } => {
Symbol::intern(&format!("{{{{{}}}}}", namespace))
}
});

let volatile_suffix = volatile.then_some("volatile");

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ symbols! {
clone,
clone_closures,
clone_from,
closure,
closure_to_fn_coercion,
cmp,
cmpxchg16b_target_feature,
Expand Down Expand Up @@ -369,6 +370,8 @@ symbols! {
const_trait_bound_opt_out,
const_trait_impl,
const_transmute,
constant,
constructor,
contents,
context,
convert,
Expand Down Expand Up @@ -679,6 +682,7 @@ symbols! {
minnumf32,
minnumf64,
mips_target_feature,
misc,
module,
module_path,
more_struct_aliases,
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_symbol_mangling/src/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::CrateNum;
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
use rustc_middle::ich::NodeIdHashingMode;
use rustc_middle::mir::interpret::{ConstValue, Scalar};
use rustc_middle::ty::print::{PrettyPrinter, Print, Printer};
Expand Down Expand Up @@ -316,7 +316,10 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
self.path.finalize_pending_component();
}

self.write_str(&disambiguated_data.data.as_symbol().as_str())?;
match disambiguated_data.data.get_name() {
DefPathDataName::Named(name) => self.write_str(&name.as_str())?,
DefPathDataName::Anon { namespace } => write!(self, "{{{{{}}}}}", namespace)?,
}
Ok(self)
}
fn path_generic_args(
Expand Down
Loading