Skip to content

Commit 5b48d28

Browse files
committed
Experiments
1 parent 193991a commit 5b48d28

27 files changed

Lines changed: 125 additions & 114 deletions

File tree

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use hir::def::{DefKind, Res};
33
use rustc_ast::*;
44
use rustc_hir as hir;
55
use rustc_hir::def_id::DefId;
6-
use rustc_hir::definitions::{DefPathData, DelegationDefPathKind};
6+
use rustc_hir::definitions::{DefPathData2, DelegationDefPathKind};
77
use rustc_middle::ty::GenericParamDefKind;
88
use rustc_middle::{bug, ty};
99
use rustc_span::symbol::kw;
@@ -302,7 +302,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
302302
GenericParamDefKind::Const { .. } => DelegationDefPathKind::ConstParam,
303303
};
304304

305-
let path_data = DefPathData::Delegation { name: param_ident.name, kind };
305+
let path_data = DefPathData2::Delegation { name: param_ident.name, kind };
306306
let node_id = self.next_node_id();
307307

308308
let def_id = self.create_def(node_id, def_name, def_kind, path_data, span);

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use rustc_data_structures::tagged_ptr::TaggedRef;
5050
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle};
5151
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5252
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
53-
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
53+
use rustc_hir::definitions::{DefPathData, DefPathData2, DisambiguatorState};
5454
use rustc_hir::lints::{AttributeLint, DelayedLint};
5555
use rustc_hir::{
5656
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
@@ -716,7 +716,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
716716
node_id: ast::NodeId,
717717
name: Option<Symbol>,
718718
def_kind: DefKind,
719-
def_path_data: DefPathData,
719+
def_path_data: impl Into<DefPathData2>,
720720
span: Span,
721721
) -> LocalDefId {
722722
let parent = self.current_hir_id_owner.def_id;

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ fn push_unqualified_item_name(
612612
disambiguated_data: DisambiguatedDefPathData,
613613
output: &mut String,
614614
) {
615-
match disambiguated_data.data {
615+
match disambiguated_data.data.unwrap() {
616616
DefPathData::CrateRoot => {
617617
output.push_str(tcx.crate_name(def_id.krate).as_str());
618618
}
@@ -626,7 +626,7 @@ fn push_unqualified_item_name(
626626
output,
627627
);
628628
}
629-
_ => match disambiguated_data.data.name() {
629+
_ => match disambiguated_data.data.unwrap().name() {
630630
DefPathDataName::Named(name) => {
631631
output.push_str(name.as_str());
632632
}

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use hir::def::DefKind;
1717
use rustc_ast::Mutability;
1818
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1919
use rustc_hir as hir;
20-
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
20+
use rustc_hir::definitions::{DefPathData, DefPathData2, DisambiguatorState};
2121
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2222
use rustc_middle::mir::interpret::{
2323
AllocBytes, ConstAllocation, CtfeProvenance, InterpResult, Provenance,
@@ -154,7 +154,7 @@ fn intern_as_new_static<'tcx>(
154154
static_id,
155155
None,
156156
DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true },
157-
Some(DefPathData::NestedStatic),
157+
Some(DefPathData2::Default(DefPathData::NestedStatic)),
158158
disambiguator,
159159
);
160160
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());

compiler/rustc_const_eval/src/interpret/stack.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ pub struct FrameInfo<'tcx> {
210210
impl<'tcx> fmt::Display for FrameInfo<'tcx> {
211211
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212212
ty::tls::with(|tcx| {
213-
if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::Closure {
213+
if tcx.def_key(self.instance.def_id()).disambiguated_data.data.unwrap()
214+
== DefPathData::Closure
215+
{
214216
write!(f, "inside closure")
215217
} else {
216218
// Note: this triggers a `must_produce_diag` state, which means that if we ever
@@ -225,7 +227,9 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
225227
impl<'tcx> FrameInfo<'tcx> {
226228
pub fn as_note(&self, tcx: TyCtxt<'tcx>) -> errors::FrameNote {
227229
let span = self.span;
228-
if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::Closure {
230+
if tcx.def_key(self.instance.def_id()).disambiguated_data.data.unwrap()
231+
== DefPathData::Closure
232+
{
229233
errors::FrameNote {
230234
where_: "closure",
231235
span,

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
115115
) -> Result<(), PrintError> {
116116
print_prefix(self)?;
117117

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

120120
Ok(())
121121
}

compiler/rustc_hir/src/definitions.rs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl DefPathTable {
9999

100100
#[derive(Debug)]
101101
pub struct DisambiguatorState {
102-
next: UnordMap<(LocalDefId, DefPathData), u32>,
102+
next: UnordMap<(LocalDefId, DefPathData2), u32>,
103103
}
104104

105105
impl DisambiguatorState {
@@ -109,7 +109,7 @@ impl DisambiguatorState {
109109

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

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

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

167167
#[inline]
168168
pub fn get_opt_name(&self) -> Option<Symbol> {
169-
self.disambiguated_data.data.get_opt_name()
169+
self.disambiguated_data.data.unwrap().get_opt_name()
170170
}
171171
}
172172

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

185185
impl DisambiguatedDefPathData {
186186
pub fn as_sym(&self, verbose: bool) -> Symbol {
187-
match self.data.name() {
187+
match self.data.unwrap().name() {
188188
DefPathDataName::Named(name) => {
189189
if verbose && self.disambiguator != 0 {
190190
Symbol::intern(&format!("{}#{}", name, self.disambiguator))
@@ -193,7 +193,7 @@ impl DisambiguatedDefPathData {
193193
}
194194
}
195195
DefPathDataName::Anon { namespace } => {
196-
if let DefPathData::AnonAssocTy(method) = self.data {
196+
if let DefPathData::AnonAssocTy(method) = self.data.unwrap() {
197197
Symbol::intern(&format!("{}::{{{}#{}}}", method, namespace, self.disambiguator))
198198
} else {
199199
Symbol::intern(&format!("{{{}#{}}}", namespace, self.disambiguator))
@@ -224,7 +224,7 @@ impl DefPath {
224224
let p = index.unwrap();
225225
let key = get_key(p);
226226
debug!("DefPath::make: key={:?}", key);
227-
match key.disambiguated_data.data {
227+
match key.disambiguated_data.data.unwrap() {
228228
DefPathData::CrateRoot => {
229229
assert!(key.parent.is_none());
230230
break;
@@ -276,6 +276,31 @@ pub enum DelegationDefPathKind {
276276
TyParam,
277277
}
278278

279+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)]
280+
pub enum DefPathData2 {
281+
Default(DefPathData),
282+
Delegation { name: Symbol, kind: DelegationDefPathKind },
283+
}
284+
285+
impl Into<DefPathData2> for DefPathData {
286+
fn into(self) -> DefPathData2 {
287+
DefPathData2::Default(self)
288+
}
289+
}
290+
291+
impl DefPathData2 {
292+
pub fn unwrap(&self) -> DefPathData {
293+
match *self {
294+
DefPathData2::Default(def_path_data) => def_path_data,
295+
DefPathData2::Delegation { name, kind } => match kind {
296+
DelegationDefPathKind::Lifetime => DefPathData::LifetimeNs(name),
297+
DelegationDefPathKind::ConstParam => DefPathData::ValueNs(name),
298+
DelegationDefPathKind::TyParam => DefPathData::TypeNs(name),
299+
},
300+
}
301+
}
302+
}
303+
279304
/// New variants should only be added in synchronization with `enum DefKind`.
280305
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)]
281306
pub enum DefPathData {
@@ -325,10 +350,6 @@ pub enum DefPathData {
325350
SyntheticCoroutineBody,
326351
/// Additional static data referred to by a static.
327352
NestedStatic,
328-
Delegation {
329-
name: Symbol,
330-
kind: DelegationDefPathKind,
331-
},
332353
}
333354

334355
impl Definitions {
@@ -367,7 +388,7 @@ impl Definitions {
367388
let key = DefKey {
368389
parent: None,
369390
disambiguated_data: DisambiguatedDefPathData {
370-
data: DefPathData::CrateRoot,
391+
data: DefPathData2::Default(DefPathData::CrateRoot),
371392
disambiguator: 0,
372393
},
373394
};
@@ -399,7 +420,7 @@ impl Definitions {
399420
pub fn create_def(
400421
&mut self,
401422
parent: LocalDefId,
402-
data: DefPathData,
423+
data: DefPathData2,
403424
disambiguator: &mut DisambiguatorState,
404425
) -> LocalDefId {
405426
// We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
@@ -410,7 +431,7 @@ impl Definitions {
410431
);
411432

412433
// The root node must be created in `new()`.
413-
assert!(data != DefPathData::CrateRoot);
434+
assert!(data.unwrap() != DefPathData::CrateRoot);
414435

415436
// Find the next free disambiguator for this key.
416437
let disambiguator = {
@@ -466,12 +487,8 @@ impl DefPathData {
466487
pub fn get_opt_name(&self) -> Option<Symbol> {
467488
use self::DefPathData::*;
468489
match *self {
469-
TypeNs(name)
470-
| ValueNs(name)
471-
| MacroNs(name)
472-
| LifetimeNs(name)
473-
| OpaqueLifetime(name)
474-
| Delegation { name, .. } => Some(name),
490+
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name)
491+
| OpaqueLifetime(name) => Some(name),
475492

476493
DesugaredAnonymousLifetime => Some(kw::UnderscoreLifetime),
477494

@@ -494,13 +511,8 @@ impl DefPathData {
494511
fn hashed_symbol(&self) -> Option<Symbol> {
495512
use self::DefPathData::*;
496513
match *self {
497-
TypeNs(name)
498-
| ValueNs(name)
499-
| MacroNs(name)
500-
| LifetimeNs(name)
501-
| AnonAssocTy(name)
502-
| OpaqueLifetime(name)
503-
| Delegation { name, .. } => Some(name),
514+
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name)
515+
| OpaqueLifetime(name) => Some(name),
504516

505517
DesugaredAnonymousLifetime => Some(kw::UnderscoreLifetime),
506518

@@ -522,12 +534,8 @@ impl DefPathData {
522534
pub fn name(&self) -> DefPathDataName {
523535
use self::DefPathData::*;
524536
match *self {
525-
TypeNs(name)
526-
| ValueNs(name)
527-
| MacroNs(name)
528-
| LifetimeNs(name)
529-
| OpaqueLifetime(name)
530-
| Delegation { name, .. } => DefPathDataName::Named(name),
537+
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name)
538+
| OpaqueLifetime(name) => DefPathDataName::Named(name),
531539
// Note that this does not show up in user print-outs.
532540
CrateRoot => DefPathDataName::Anon { namespace: kw::Crate },
533541
Impl => DefPathDataName::Anon { namespace: kw::Impl },

compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
423423
) -> String {
424424
self.trait_path(span, expr_hir_id, trait_def_id).unwrap_or_else(|| {
425425
let key = self.tcx.def_key(trait_def_id);
426-
format!("{}", key.disambiguated_data.data)
426+
format!("{}", key.disambiguated_data.data.unwrap())
427427
})
428428
}
429429

compiler/rustc_lint/src/context.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,13 +795,15 @@ impl<'tcx> LateContext<'tcx> {
795795
print_prefix(self)?;
796796

797797
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
798-
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
798+
if let DefPathData::ForeignMod | DefPathData::Ctor =
799+
disambiguated_data.data.unwrap()
800+
{
799801
return Ok(());
800802
}
801803

802-
self.path.push(match disambiguated_data.data.get_opt_name() {
804+
self.path.push(match disambiguated_data.data.unwrap().get_opt_name() {
803805
Some(sym) => sym,
804-
None => Symbol::intern(&disambiguated_data.data.to_string()),
806+
None => Symbol::intern(&disambiguated_data.data.unwrap().to_string()),
805807
});
806808
Ok(())
807809
}

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ impl MetadataBlob {
869869
def_key
870870
.disambiguated_data
871871
.data
872+
.unwrap()
872873
.get_opt_name()
873874
.unwrap_or_else(|| Symbol::intern("???"))
874875
};
@@ -983,10 +984,10 @@ impl<'a> CrateMetadataRef<'a> {
983984

984985
fn opt_item_name(self, item_index: DefIndex) -> Option<Symbol> {
985986
let def_key = self.def_key(item_index);
986-
def_key.disambiguated_data.data.get_opt_name().or_else(|| {
987-
if def_key.disambiguated_data.data == DefPathData::Ctor {
987+
def_key.disambiguated_data.data.unwrap().get_opt_name().or_else(|| {
988+
if def_key.disambiguated_data.data.unwrap() == DefPathData::Ctor {
988989
let parent_index = def_key.parent.expect("no parent for a constructor");
989-
self.def_key(parent_index).disambiguated_data.data.get_opt_name()
990+
self.def_key(parent_index).disambiguated_data.data.unwrap().get_opt_name()
990991
} else {
991992
None
992993
}
@@ -1380,7 +1381,7 @@ impl<'a> CrateMetadataRef<'a> {
13801381
// but we assume that someone passing a constructor ID actually wants to look at
13811382
// the attributes on the corresponding struct or variant.
13821383
let def_key = self.def_key(id);
1383-
assert_eq!(def_key.disambiguated_data.data, DefPathData::Ctor);
1384+
assert_eq!(def_key.disambiguated_data.data.unwrap(), DefPathData::Ctor);
13841385
let parent_id = def_key.parent.expect("no parent for a constructor");
13851386
self.root
13861387
.tables

0 commit comments

Comments
 (0)