Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion compiler/rustc_ast_lowering/src/delegation/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use hir::def::{DefKind, Res};
use rustc_ast::*;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::{DefPathData2, DelegationDefPathKind};
use rustc_middle::ty::GenericParamDefKind;
use rustc_middle::{bug, ty};
use rustc_span::symbol::kw;
Expand Down Expand Up @@ -295,7 +296,13 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {

let param_ident = Ident::new(p.name, span);
let def_name = Some(param_ident.name);
let path_data = def_kind.def_path_data(def_name);
let kind = match p.kind {
GenericParamDefKind::Lifetime => DelegationDefPathKind::Lifetime,
GenericParamDefKind::Type { .. } => DelegationDefPathKind::TyParam,
GenericParamDefKind::Const { .. } => DelegationDefPathKind::ConstParam,
};

let path_data = DefPathData2::Delegation { name: param_ident.name, kind };
let node_id = self.next_node_id();

let def_id = self.create_def(node_id, def_name, def_kind, path_data, span);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use rustc_data_structures::tagged_ptr::TaggedRef;
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle};
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
use rustc_hir::definitions::{DefPathData, DefPathData2, DisambiguatorState};
use rustc_hir::lints::{AttributeLint, DelayedLint};
use rustc_hir::{
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
Expand Down Expand Up @@ -716,7 +716,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
node_id: ast::NodeId,
name: Option<Symbol>,
def_kind: DefKind,
def_path_data: DefPathData,
def_path_data: impl Into<DefPathData2>,
span: Span,
) -> LocalDefId {
let parent = self.current_hir_id_owner.def_id;
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ fn push_unqualified_item_name(
disambiguated_data: DisambiguatedDefPathData,
output: &mut String,
) {
match disambiguated_data.data {
match disambiguated_data.data.unwrap() {
DefPathData::CrateRoot => {
output.push_str(tcx.crate_name(def_id.krate).as_str());
}
Expand All @@ -626,7 +626,7 @@ fn push_unqualified_item_name(
output,
);
}
_ => match disambiguated_data.data.name() {
_ => match disambiguated_data.data.unwrap().name() {
DefPathDataName::Named(name) => {
output.push_str(name.as_str());
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_const_eval/src/interpret/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use hir::def::DefKind;
use rustc_ast::Mutability;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_hir as hir;
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
use rustc_hir::definitions::{DefPathData, DefPathData2, DisambiguatorState};
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::mir::interpret::{
AllocBytes, ConstAllocation, CtfeProvenance, InterpResult, Provenance,
Expand Down Expand Up @@ -154,7 +154,7 @@ fn intern_as_new_static<'tcx>(
static_id,
None,
DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true },
Some(DefPathData::NestedStatic),
Some(DefPathData2::Default(DefPathData::NestedStatic)),
disambiguator,
);
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_const_eval/src/interpret/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ pub struct FrameInfo<'tcx> {
impl<'tcx> fmt::Display for FrameInfo<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::Closure {
if tcx.def_key(self.instance.def_id()).disambiguated_data.data.unwrap()
== DefPathData::Closure
{
write!(f, "inside closure")
} else {
// Note: this triggers a `must_produce_diag` state, which means that if we ever
Expand All @@ -225,7 +227,9 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
impl<'tcx> FrameInfo<'tcx> {
pub fn as_note(&self, tcx: TyCtxt<'tcx>) -> errors::FrameNote {
let span = self.span;
if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::Closure {
if tcx.def_key(self.instance.def_id()).disambiguated_data.data.unwrap()
== DefPathData::Closure
{
errors::FrameNote {
where_: "closure",
span,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/util/type_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
) -> Result<(), PrintError> {
print_prefix(self)?;

write!(self.path, "::{}", disambiguated_data.data).unwrap();
write!(self.path, "::{}", disambiguated_data.data.unwrap()).unwrap();

Ok(())
}
Expand Down
56 changes: 44 additions & 12 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl DefPathTable {

#[derive(Debug)]
pub struct DisambiguatorState {
next: UnordMap<(LocalDefId, DefPathData), u32>,
next: UnordMap<(LocalDefId, DefPathData2), u32>,
}

impl DisambiguatorState {
Expand All @@ -109,7 +109,7 @@ impl DisambiguatorState {

/// Creates a `DisambiguatorState` where the next allocated `(LocalDefId, DefPathData)` pair
/// will have `index` as the disambiguator.
pub fn with(def_id: LocalDefId, data: DefPathData, index: u32) -> Self {
pub fn with(def_id: LocalDefId, data: DefPathData2, index: u32) -> Self {
let mut this = Self::new();
this.next.insert((def_id, data), index);
this
Expand Down Expand Up @@ -146,8 +146,8 @@ impl DefKey {

let DisambiguatedDefPathData { ref data, disambiguator } = self.disambiguated_data;

std::mem::discriminant(data).hash(&mut hasher);
if let Some(name) = data.hashed_symbol() {
data.hash(&mut hasher);
if let Some(name) = data.unwrap().hashed_symbol() {
// Get a stable hash by considering the symbol chars rather than
// the symbol index.
name.as_str().hash(&mut hasher);
Expand All @@ -166,7 +166,7 @@ impl DefKey {

#[inline]
pub fn get_opt_name(&self) -> Option<Symbol> {
self.disambiguated_data.data.get_opt_name()
self.disambiguated_data.data.unwrap().get_opt_name()
}
}

Expand All @@ -178,13 +178,13 @@ impl DefKey {
/// the same module, they do get distinct `DefId`s.
#[derive(Copy, Clone, PartialEq, Debug, Encodable, BlobDecodable)]
pub struct DisambiguatedDefPathData {
pub data: DefPathData,
pub data: DefPathData2,
pub disambiguator: u32,
}

impl DisambiguatedDefPathData {
pub fn as_sym(&self, verbose: bool) -> Symbol {
match self.data.name() {
match self.data.unwrap().name() {
DefPathDataName::Named(name) => {
if verbose && self.disambiguator != 0 {
Symbol::intern(&format!("{}#{}", name, self.disambiguator))
Expand All @@ -193,7 +193,7 @@ impl DisambiguatedDefPathData {
}
}
DefPathDataName::Anon { namespace } => {
if let DefPathData::AnonAssocTy(method) = self.data {
if let DefPathData::AnonAssocTy(method) = self.data.unwrap() {
Symbol::intern(&format!("{}::{{{}#{}}}", method, namespace, self.disambiguator))
} else {
Symbol::intern(&format!("{{{}#{}}}", namespace, self.disambiguator))
Expand Down Expand Up @@ -224,7 +224,7 @@ impl DefPath {
let p = index.unwrap();
let key = get_key(p);
debug!("DefPath::make: key={:?}", key);
match key.disambiguated_data.data {
match key.disambiguated_data.data.unwrap() {
DefPathData::CrateRoot => {
assert!(key.parent.is_none());
break;
Expand Down Expand Up @@ -269,6 +269,38 @@ impl DefPath {
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)]
pub enum DelegationDefPathKind {
Lifetime,
ConstParam,
TyParam,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)]
pub enum DefPathData2 {
Default(DefPathData),
Delegation { name: Symbol, kind: DelegationDefPathKind },
}

impl Into<DefPathData2> for DefPathData {
fn into(self) -> DefPathData2 {
DefPathData2::Default(self)
}
}

impl DefPathData2 {
pub fn unwrap(&self) -> DefPathData {
match *self {
DefPathData2::Default(def_path_data) => def_path_data,
DefPathData2::Delegation { name, kind } => match kind {
DelegationDefPathKind::Lifetime => DefPathData::LifetimeNs(name),
DelegationDefPathKind::ConstParam => DefPathData::ValueNs(name),
DelegationDefPathKind::TyParam => DefPathData::TypeNs(name),
},
}
}
}

/// New variants should only be added in synchronization with `enum DefKind`.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)]
pub enum DefPathData {
Expand Down Expand Up @@ -356,7 +388,7 @@ impl Definitions {
let key = DefKey {
parent: None,
disambiguated_data: DisambiguatedDefPathData {
data: DefPathData::CrateRoot,
data: DefPathData2::Default(DefPathData::CrateRoot),
disambiguator: 0,
},
};
Expand Down Expand Up @@ -388,7 +420,7 @@ impl Definitions {
pub fn create_def(
&mut self,
parent: LocalDefId,
data: DefPathData,
data: DefPathData2,
disambiguator: &mut DisambiguatorState,
) -> LocalDefId {
// We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
Expand All @@ -399,7 +431,7 @@ impl Definitions {
);

// The root node must be created in `new()`.
assert!(data != DefPathData::CrateRoot);
assert!(data.unwrap() != DefPathData::CrateRoot);

// Find the next free disambiguator for this key.
let disambiguator = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> String {
self.trait_path(span, expr_hir_id, trait_def_id).unwrap_or_else(|| {
let key = self.tcx.def_key(trait_def_id);
format!("{}", key.disambiguated_data.data)
format!("{}", key.disambiguated_data.data.unwrap())
})
}

Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,13 +795,15 @@ impl<'tcx> LateContext<'tcx> {
print_prefix(self)?;

// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
if let DefPathData::ForeignMod | DefPathData::Ctor =
disambiguated_data.data.unwrap()
{
return Ok(());
}

self.path.push(match disambiguated_data.data.get_opt_name() {
self.path.push(match disambiguated_data.data.unwrap().get_opt_name() {
Some(sym) => sym,
None => Symbol::intern(&disambiguated_data.data.to_string()),
None => Symbol::intern(&disambiguated_data.data.unwrap().to_string()),
});
Ok(())
}
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ impl MetadataBlob {
def_key
.disambiguated_data
.data
.unwrap()
.get_opt_name()
.unwrap_or_else(|| Symbol::intern("???"))
};
Expand Down Expand Up @@ -983,10 +984,10 @@ impl<'a> CrateMetadataRef<'a> {

fn opt_item_name(self, item_index: DefIndex) -> Option<Symbol> {
let def_key = self.def_key(item_index);
def_key.disambiguated_data.data.get_opt_name().or_else(|| {
if def_key.disambiguated_data.data == DefPathData::Ctor {
def_key.disambiguated_data.data.unwrap().get_opt_name().or_else(|| {
if def_key.disambiguated_data.data.unwrap() == DefPathData::Ctor {
let parent_index = def_key.parent.expect("no parent for a constructor");
self.def_key(parent_index).disambiguated_data.data.get_opt_name()
self.def_key(parent_index).disambiguated_data.data.unwrap().get_opt_name()
} else {
None
}
Expand Down Expand Up @@ -1380,7 +1381,7 @@ impl<'a> CrateMetadataRef<'a> {
// but we assume that someone passing a constructor ID actually wants to look at
// the attributes on the corresponding struct or variant.
let def_key = self.def_key(id);
assert_eq!(def_key.disambiguated_data.data, DefPathData::Ctor);
assert_eq!(def_key.disambiguated_data.data.unwrap(), DefPathData::Ctor);
let parent_id = def_key.parent.expect("no parent for a constructor");
self.root
.tables
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_feature::Features;
use rustc_hir as hir;
use rustc_hir::attrs::{AttributeKind, EncodeCrossCrate};
use rustc_hir::def_id::{CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE, LocalDefId, LocalDefIdSet};
use rustc_hir::definitions::DefPathData;
use rustc_hir::definitions::{DefPathData, DefPathData2};
use rustc_hir::find_attr;
use rustc_hir_pretty::id_to_string;
use rustc_middle::dep_graph::WorkProductId;
Expand Down Expand Up @@ -2027,7 +2027,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
};

let mut def_key = self.tcx.hir_def_key(id);
def_key.disambiguated_data.data = DefPathData::MacroNs(name);
def_key.disambiguated_data.data = DefPathData2::Default(DefPathData::MacroNs(name));

let def_id = id.to_def_id();
self.tables.def_kind.set_some(def_id.index, DefKind::Macro(macro_kind.into()));
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use rustc_data_structures::sync::{
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, Diagnostic, MultiSpan};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId};
use rustc_hir::definitions::{DefPathData, Definitions, DisambiguatorState};
use rustc_hir::definitions::{DefPathData2, Definitions, DisambiguatorState};
use rustc_hir::intravisit::VisitorExt;
use rustc_hir::lang_items::LangItem;
use rustc_hir::limit::Limit;
Expand Down Expand Up @@ -1358,7 +1358,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
parent: LocalDefId,
name: Option<Symbol>,
def_kind: DefKind,
override_def_path_data: Option<DefPathData>,
override_def_path_data: Option<impl Into<DefPathData2>>,
disambiguator: &mut DisambiguatorState,
) -> TyCtxtFeed<'tcx, LocalDefId> {
let feed =
Expand All @@ -1376,10 +1376,12 @@ impl<'tcx> TyCtxt<'tcx> {
parent: LocalDefId,
name: Option<Symbol>,
def_kind: DefKind,
override_def_path_data: Option<DefPathData>,
override_def_path_data: Option<impl Into<DefPathData2>>,
disambiguator: &mut DisambiguatorState,
) -> TyCtxtFeed<'tcx, LocalDefId> {
let data = override_def_path_data.unwrap_or_else(|| def_kind.def_path_data(name));
let data = override_def_path_data
.map(|d| d.into())
.unwrap_or_else(|| DefPathData2::Default(def_kind.def_path_data(name)));
// The following call has the side effect of modifying the tables inside `definitions`.
// These very tables are relied on by the incr. comp. engine to decode DepNodes and to
// decode the on-disk cache.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl<'tcx> InstanceKind<'tcx> {
_ => return true,
};
matches!(
tcx.def_key(def_id).disambiguated_data.data,
tcx.def_key(def_id).disambiguated_data.data.unwrap(),
DefPathData::Ctor | DefPathData::Closure
)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ impl<'tcx> TyCtxt<'tcx> {
Some(self.crate_name(cnum))
} else {
let def_key = self.def_key(def_id);
match def_key.disambiguated_data.data {
match def_key.disambiguated_data.data.unwrap() {
// The name of a constructor is that of its parent.
rustc_hir::definitions::DefPathData::Ctor => self
.opt_item_name(DefId { krate: def_id.krate, index: def_key.parent.unwrap() }),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub trait Printer<'tcx>: Sized {
let key = self.tcx().def_key(def_id);
debug!(?key);

match key.disambiguated_data.data {
match key.disambiguated_data.data.unwrap() {
DefPathData::CrateRoot => {
assert!(key.parent.is_none());
self.print_crate_name(def_id.krate)
Expand All @@ -161,7 +161,7 @@ pub trait Printer<'tcx>: Sized {
let generics = self.tcx().generics_of(def_id);
parent_args = &args[..generics.parent_count.min(args.len())];

match key.disambiguated_data.data {
match key.disambiguated_data.data.unwrap() {
DefPathData::Closure => {
// We need to additionally print the `kind` field of a coroutine if
// it is desugared from a coroutine-closure.
Expand Down
Loading
Loading