From 193991a57b8fdcac0b969ba572ed908643041423 Mon Sep 17 00:00:00 2001 From: aerooneqq Date: Fri, 20 Mar 2026 16:15:11 +0300 Subject: [PATCH 1/2] Fix delegation def path hash collision --- .../src/delegation/generics.rs | 9 ++- compiler/rustc_hir/src/definitions.rs | 36 ++++++++++-- .../src/cfi/typeid/itanium_cxx_abi/encode.rs | 47 ++++++++------- compiler/rustc_symbol_mangling/src/v0.rs | 14 +++-- .../generics/def-id-isnt-local-ice-143498.rs | 29 ++++++++++ .../def-id-isnt-local-ice-143498.stderr | 45 ++++++++++++++ .../def-path-hash-collision-ice-153410.rs | 29 ++++++++++ .../def-path-hash-collision-ice-153410.stderr | 58 +++++++++++++++++++ 8 files changed, 236 insertions(+), 31 deletions(-) create mode 100644 tests/ui/delegation/generics/def-id-isnt-local-ice-143498.rs create mode 100644 tests/ui/delegation/generics/def-id-isnt-local-ice-143498.stderr create mode 100644 tests/ui/delegation/generics/def-path-hash-collision-ice-153410.rs create mode 100644 tests/ui/delegation/generics/def-path-hash-collision-ice-153410.stderr diff --git a/compiler/rustc_ast_lowering/src/delegation/generics.rs b/compiler/rustc_ast_lowering/src/delegation/generics.rs index ee1ac9aa5cd8d..97aa70e1be51d 100644 --- a/compiler/rustc_ast_lowering/src/delegation/generics.rs +++ b/compiler/rustc_ast_lowering/src/delegation/generics.rs @@ -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::{DefPathData, DelegationDefPathKind}; use rustc_middle::ty::GenericParamDefKind; use rustc_middle::{bug, ty}; use rustc_span::symbol::kw; @@ -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 = DefPathData::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); diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index 5e361891f6d04..d4b37f5607f82 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -269,6 +269,13 @@ impl DefPath { } } +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)] +pub enum DelegationDefPathKind { + Lifetime, + ConstParam, + TyParam, +} + /// New variants should only be added in synchronization with `enum DefKind`. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)] pub enum DefPathData { @@ -318,6 +325,10 @@ pub enum DefPathData { SyntheticCoroutineBody, /// Additional static data referred to by a static. NestedStatic, + Delegation { + name: Symbol, + kind: DelegationDefPathKind, + }, } impl Definitions { @@ -455,8 +466,12 @@ impl DefPathData { pub fn get_opt_name(&self) -> Option { use self::DefPathData::*; match *self { - TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) - | OpaqueLifetime(name) => Some(name), + TypeNs(name) + | ValueNs(name) + | MacroNs(name) + | LifetimeNs(name) + | OpaqueLifetime(name) + | Delegation { name, .. } => Some(name), DesugaredAnonymousLifetime => Some(kw::UnderscoreLifetime), @@ -479,8 +494,13 @@ impl DefPathData { fn hashed_symbol(&self) -> Option { use self::DefPathData::*; match *self { - TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name) - | OpaqueLifetime(name) => Some(name), + TypeNs(name) + | ValueNs(name) + | MacroNs(name) + | LifetimeNs(name) + | AnonAssocTy(name) + | OpaqueLifetime(name) + | Delegation { name, .. } => Some(name), DesugaredAnonymousLifetime => Some(kw::UnderscoreLifetime), @@ -502,8 +522,12 @@ impl DefPathData { pub fn name(&self) -> DefPathDataName { use self::DefPathData::*; match *self { - TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) - | OpaqueLifetime(name) => DefPathDataName::Named(name), + TypeNs(name) + | ValueNs(name) + | MacroNs(name) + | LifetimeNs(name) + | OpaqueLifetime(name) + | Delegation { name, .. } => DefPathDataName::Named(name), // Note that this does not show up in user print-outs. CrateRoot => DefPathDataName::Anon { namespace: kw::Crate }, Impl => DefPathDataName::Anon { namespace: kw::Impl }, diff --git a/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs b/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs index 26979c24bdb68..97fd57e34882c 100644 --- a/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs +++ b/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs @@ -10,7 +10,7 @@ use std::fmt::Write as _; use rustc_abi::{ExternAbi, Integer}; use rustc_data_structures::base_n::{ALPHANUMERIC_ONLY, CASE_INSENSITIVE, ToBaseN}; use rustc_data_structures::fx::FxHashMap; -use rustc_hir as hir; +use rustc_hir::definitions::{DefPathData, DelegationDefPathKind}; use rustc_hir::find_attr; use rustc_middle::bug; use rustc_middle::ty::layout::IntegerExt; @@ -672,26 +672,33 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String { def_path.data.reverse(); for disambiguated_data in &def_path.data { s.push('N'); + + const TYPE_NS: &str = "t"; + const VALUE_NS: &str = "v"; s.push_str(match disambiguated_data.data { - hir::definitions::DefPathData::Impl => "I", // Not specified in v0's - hir::definitions::DefPathData::ForeignMod => "F", // Not specified in v0's - hir::definitions::DefPathData::TypeNs(..) => "t", - hir::definitions::DefPathData::ValueNs(..) => "v", - hir::definitions::DefPathData::Closure => "C", - hir::definitions::DefPathData::Ctor => "c", - hir::definitions::DefPathData::AnonConst => "K", - hir::definitions::DefPathData::LateAnonConst => "k", - hir::definitions::DefPathData::OpaqueTy => "i", - hir::definitions::DefPathData::SyntheticCoroutineBody => "s", - hir::definitions::DefPathData::NestedStatic => "n", - hir::definitions::DefPathData::CrateRoot - | hir::definitions::DefPathData::Use - | hir::definitions::DefPathData::GlobalAsm - | hir::definitions::DefPathData::MacroNs(..) - | hir::definitions::DefPathData::OpaqueLifetime(..) - | hir::definitions::DefPathData::LifetimeNs(..) - | hir::definitions::DefPathData::DesugaredAnonymousLifetime - | hir::definitions::DefPathData::AnonAssocTy(..) => { + DefPathData::Impl => "I", // Not specified in v0's + DefPathData::ForeignMod => "F", // Not specified in v0's + DefPathData::TypeNs(..) => TYPE_NS, + DefPathData::ValueNs(..) => VALUE_NS, + DefPathData::Closure => "C", + DefPathData::Ctor => "c", + DefPathData::AnonConst => "K", + DefPathData::LateAnonConst => "k", + DefPathData::OpaqueTy => "i", + DefPathData::SyntheticCoroutineBody => "s", + DefPathData::NestedStatic => "n", + DefPathData::Delegation { kind: DelegationDefPathKind::ConstParam, .. } => VALUE_NS, + DefPathData::Delegation { kind: DelegationDefPathKind::TyParam, .. } => TYPE_NS, + + DefPathData::CrateRoot + | DefPathData::Use + | DefPathData::GlobalAsm + | DefPathData::MacroNs(..) + | DefPathData::OpaqueLifetime(..) + | DefPathData::LifetimeNs(..) + | DefPathData::DesugaredAnonymousLifetime + | DefPathData::AnonAssocTy(..) + | DefPathData::Delegation { kind: DelegationDefPathKind::Lifetime, .. } => { bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data); } }); diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 95cbb9e07ebb7..205332b90d55b 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -12,7 +12,7 @@ use rustc_hashes::Hash64; use rustc_hir as hir; use rustc_hir::def::CtorKind; use rustc_hir::def_id::{CrateNum, DefId}; -use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData}; +use rustc_hir::definitions::{DefPathData, DelegationDefPathKind, DisambiguatedDefPathData}; use rustc_middle::bug; use rustc_middle::ty::layout::IntegerExt; use rustc_middle::ty::print::{Print, PrintError, Printer}; @@ -877,14 +877,16 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> { print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>, disambiguated_data: &DisambiguatedDefPathData, ) -> Result<(), PrintError> { + const TYPE_NS: char = 't'; + const VALUE_NS: char = 'v'; let ns = match disambiguated_data.data { // Extern block segments can be skipped, names from extern blocks // are effectively living in their parent modules. DefPathData::ForeignMod => return print_prefix(self), // Uppercase categories are more stable than lowercase ones. - DefPathData::TypeNs(_) => 't', - DefPathData::ValueNs(_) => 'v', + DefPathData::TypeNs(_) => TYPE_NS, + DefPathData::ValueNs(_) => VALUE_NS, DefPathData::Closure => 'C', DefPathData::Ctor => 'c', DefPathData::AnonConst => 'K', @@ -894,6 +896,9 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> { DefPathData::NestedStatic => 'n', DefPathData::GlobalAsm => 'a', + DefPathData::Delegation { kind: DelegationDefPathKind::ConstParam, .. } => VALUE_NS, + DefPathData::Delegation { kind: DelegationDefPathKind::TyParam, .. } => TYPE_NS, + // These should never show up as `print_path_with_simple` arguments. DefPathData::CrateRoot | DefPathData::Use @@ -902,7 +907,8 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> { | DefPathData::LifetimeNs(_) | DefPathData::DesugaredAnonymousLifetime | DefPathData::OpaqueLifetime(_) - | DefPathData::AnonAssocTy(..) => { + | DefPathData::AnonAssocTy(..) + | DefPathData::Delegation { kind: DelegationDefPathKind::Lifetime, .. } => { bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data) } }; diff --git a/tests/ui/delegation/generics/def-id-isnt-local-ice-143498.rs b/tests/ui/delegation/generics/def-id-isnt-local-ice-143498.rs new file mode 100644 index 0000000000000..f300e4d85fb5d --- /dev/null +++ b/tests/ui/delegation/generics/def-id-isnt-local-ice-143498.rs @@ -0,0 +1,29 @@ +//@ compile-flags: -Z deduplicate-diagnostics=yes +//@ edition:2024 + +#![feature(fn_delegation)] +#![feature(iter_advance_by)] +#![feature(iter_array_chunks)] +#![feature(iterator_try_collect)] +#![feature(iterator_try_reduce)] +#![feature(iter_collect_into)] +#![feature(iter_intersperse)] +#![feature(iter_is_partitioned)] +#![feature(iter_map_windows)] +#![feature(iter_next_chunk)] +#![feature(iter_order_by)] +#![feature(iter_partition_in_place)] +#![feature(trusted_random_access)] +#![feature(try_find)] +#![allow(incomplete_features)] + +impl X { +//~^ ERROR: cannot find type `X` in this scope + reuse< std::fmt::Debug as Iterator >::*; + //~^ ERROR: expected method or associated constant, found associated type `Iterator::Item` + //~| ERROR: expected a type, found a trait +} + +fn main() {} +//~? ERROR: method takes at most 1 generic argument but 2 generic arguments were supplied +//~? ERROR: method takes at most 1 generic argument but 2 generic arguments were supplied diff --git a/tests/ui/delegation/generics/def-id-isnt-local-ice-143498.stderr b/tests/ui/delegation/generics/def-id-isnt-local-ice-143498.stderr new file mode 100644 index 0000000000000..64ee9cd474012 --- /dev/null +++ b/tests/ui/delegation/generics/def-id-isnt-local-ice-143498.stderr @@ -0,0 +1,45 @@ +error[E0425]: cannot find type `X` in this scope + --> $DIR/def-id-isnt-local-ice-143498.rs:20:6 + | +LL | impl X { + | ^ not found in this scope + +error[E0575]: expected method or associated constant, found associated type `Iterator::Item` + --> $DIR/def-id-isnt-local-ice-143498.rs:22:10 + | +LL | reuse< std::fmt::Debug as Iterator >::*; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a method or associated constant + +error[E0782]: expected a type, found a trait + --> $DIR/def-id-isnt-local-ice-143498.rs:22:12 + | +LL | reuse< std::fmt::Debug as Iterator >::*; + | ^^^^^^^^^^^^^^^ + | +help: you can add the `dyn` keyword if you want a trait object + | +LL | reuse< dyn std::fmt::Debug as Iterator >::*; + | +++ + +error[E0107]: method takes at most 1 generic argument but 2 generic arguments were supplied + --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + | + = note: expected at most 1 generic argument + | + = note: help: remove the unnecessary generic argument + | + = note: `impl Trait` cannot be explicitly specified as a generic argument + +error[E0107]: method takes at most 1 generic argument but 2 generic arguments were supplied + --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + | + = note: expected at most 1 generic argument + | + = note: help: remove the unnecessary generic argument + | + = note: `impl Trait` cannot be explicitly specified as a generic argument + +error: aborting due to 5 previous errors + +Some errors have detailed explanations: E0107, E0425, E0575, E0782. +For more information about an error, try `rustc --explain E0107`. diff --git a/tests/ui/delegation/generics/def-path-hash-collision-ice-153410.rs b/tests/ui/delegation/generics/def-path-hash-collision-ice-153410.rs new file mode 100644 index 0000000000000..6082fe349abab --- /dev/null +++ b/tests/ui/delegation/generics/def-path-hash-collision-ice-153410.rs @@ -0,0 +1,29 @@ +//@ compile-flags: -Z deduplicate-diagnostics=yes +//@ edition:2024 + +#![feature(fn_delegation)] +#![feature(iter_advance_by)] +#![feature(iter_array_chunks)] +#![feature(iterator_try_collect)] +#![feature(iterator_try_reduce)] +#![feature(iter_collect_into)] +#![feature(iter_intersperse)] +#![feature(iter_is_partitioned)] +#![feature(iter_map_windows)] +#![feature(iter_next_chunk)] +#![feature(iter_order_by)] +#![feature(iter_partition_in_place)] +#![feature(trusted_random_access)] +#![feature(try_find)] +#![allow(incomplete_features)] + +impl Iterator { +//~^ ERROR: expected a type, found a trait [E0782] + reuse< < fn()>::Output>::Item as Iterator>::*; + //~^ ERROR: expected method or associated constant, found associated type `Iterator::Item` + //~| ERROR: ambiguous associated type +} + +fn main() {} +//~? ERROR: method takes at most 1 generic argument but 2 generic arguments were supplied +//~? ERROR: method takes at most 1 generic argument but 2 generic arguments were supplied diff --git a/tests/ui/delegation/generics/def-path-hash-collision-ice-153410.stderr b/tests/ui/delegation/generics/def-path-hash-collision-ice-153410.stderr new file mode 100644 index 0000000000000..44522b4ec206f --- /dev/null +++ b/tests/ui/delegation/generics/def-path-hash-collision-ice-153410.stderr @@ -0,0 +1,58 @@ +error[E0575]: expected method or associated constant, found associated type `Iterator::Item` + --> $DIR/def-path-hash-collision-ice-153410.rs:22:10 + | +LL | reuse< < fn()>::Output>::Item as Iterator>::*; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a method or associated constant + +error[E0782]: expected a type, found a trait + --> $DIR/def-path-hash-collision-ice-153410.rs:20:6 + | +LL | impl Iterator { + | ^^^^^^^^ + | +help: you can add the `dyn` keyword if you want a trait object + | +LL | impl dyn Iterator { + | +++ +help: you might have intended to implement this trait for a given type + | +LL | impl Iterator for /* Type */ { + | ++++++++++++++ + +error[E0223]: ambiguous associated type + --> $DIR/def-path-hash-collision-ice-153410.rs:22:14 + | +LL | reuse< < fn()>::Output>::Item as Iterator>::*; + | ^^^^^^^^^^^^^^^^^^^^^^ + | +help: use fully-qualified syntax + | +LL - reuse< < fn()>::Output>::Item as Iterator>::*; +LL + reuse< < ::Output>::Item as Iterator>::*; + | +LL - reuse< < fn()>::Output>::Item as Iterator>::*; +LL + reuse< < ::Output>::Item as Iterator>::*; + | + +error[E0107]: method takes at most 1 generic argument but 2 generic arguments were supplied + --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + | + = note: expected at most 1 generic argument + | + = note: help: remove the unnecessary generic argument + | + = note: `impl Trait` cannot be explicitly specified as a generic argument + +error[E0107]: method takes at most 1 generic argument but 2 generic arguments were supplied + --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + | + = note: expected at most 1 generic argument + | + = note: help: remove the unnecessary generic argument + | + = note: `impl Trait` cannot be explicitly specified as a generic argument + +error: aborting due to 5 previous errors + +Some errors have detailed explanations: E0107, E0223, E0575, E0782. +For more information about an error, try `rustc --explain E0107`. From 21a904d772b582c883bf20115758b429f2292434 Mon Sep 17 00:00:00 2001 From: aerooneqq Date: Thu, 26 Mar 2026 11:23:10 +0300 Subject: [PATCH 2/2] Experiments --- .../src/delegation/generics.rs | 4 +- compiler/rustc_ast_lowering/src/lib.rs | 4 +- .../src/debuginfo/type_names.rs | 4 +- .../rustc_const_eval/src/interpret/intern.rs | 4 +- .../rustc_const_eval/src/interpret/stack.rs | 8 +- .../rustc_const_eval/src/util/type_name.rs | 2 +- compiler/rustc_hir/src/definitions.rs | 78 ++++++++++--------- .../src/method/prelude_edition_lints.rs | 2 +- compiler/rustc_lint/src/context.rs | 8 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 9 ++- compiler/rustc_metadata/src/rmeta/encoder.rs | 4 +- compiler/rustc_middle/src/ty/context.rs | 10 ++- compiler/rustc_middle/src/ty/instance.rs | 2 +- compiler/rustc_middle/src/ty/mod.rs | 2 +- compiler/rustc_middle/src/ty/print/mod.rs | 4 +- compiler/rustc_middle/src/ty/print/pretty.rs | 18 ++--- .../src/ty/significant_drop_order.rs | 2 +- .../src/coroutine/by_move_body.rs | 4 +- .../rustc_monomorphize/src/partitioning.rs | 2 +- .../rustc_query_impl/src/profiling_support.rs | 2 +- compiler/rustc_resolve/src/lib.rs | 10 ++- .../src/cfi/typeid/itanium_cxx_abi/encode.rs | 20 ++--- compiler/rustc_symbol_mangling/src/legacy.rs | 6 +- compiler/rustc_symbol_mangling/src/v0.rs | 22 ++---- .../src/error_reporting/infer/mod.rs | 4 +- .../error_reporting/infer/need_type_info.rs | 2 +- compiler/rustc_ty_utils/src/assoc.rs | 2 +- src/librustdoc/clean/inline.rs | 6 +- src/librustdoc/html/format.rs | 4 +- src/librustdoc/visit_ast.rs | 3 +- 30 files changed, 134 insertions(+), 118 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/delegation/generics.rs b/compiler/rustc_ast_lowering/src/delegation/generics.rs index 97aa70e1be51d..bfc111f134e90 100644 --- a/compiler/rustc_ast_lowering/src/delegation/generics.rs +++ b/compiler/rustc_ast_lowering/src/delegation/generics.rs @@ -3,7 +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::{DefPathData, DelegationDefPathKind}; +use rustc_hir::definitions::{DefPathData2, DelegationDefPathKind}; use rustc_middle::ty::GenericParamDefKind; use rustc_middle::{bug, ty}; use rustc_span::symbol::kw; @@ -302,7 +302,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> { GenericParamDefKind::Const { .. } => DelegationDefPathKind::ConstParam, }; - let path_data = DefPathData::Delegation { name: param_ident.name, kind }; + 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); diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 5fcc8f0161194..76daa2402f20b 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -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, @@ -716,7 +716,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> { node_id: ast::NodeId, name: Option, def_kind: DefKind, - def_path_data: DefPathData, + def_path_data: impl Into, span: Span, ) -> LocalDefId { let parent = self.current_hir_id_owner.def_id; diff --git a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs index d207cdca4d4bb..6391bc7076b57 100644 --- a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs +++ b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs @@ -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()); } @@ -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()); } diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index 5d510a9835264..fd2b4c3372db2 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -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, @@ -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()); diff --git a/compiler/rustc_const_eval/src/interpret/stack.rs b/compiler/rustc_const_eval/src/interpret/stack.rs index b0f34264ad841..1e822b7dff466 100644 --- a/compiler/rustc_const_eval/src/interpret/stack.rs +++ b/compiler/rustc_const_eval/src/interpret/stack.rs @@ -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 @@ -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, diff --git a/compiler/rustc_const_eval/src/util/type_name.rs b/compiler/rustc_const_eval/src/util/type_name.rs index db651811551f3..36a72deb647c1 100644 --- a/compiler/rustc_const_eval/src/util/type_name.rs +++ b/compiler/rustc_const_eval/src/util/type_name.rs @@ -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(()) } diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index d4b37f5607f82..f568bb411f3a1 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -99,7 +99,7 @@ impl DefPathTable { #[derive(Debug)] pub struct DisambiguatorState { - next: UnordMap<(LocalDefId, DefPathData), u32>, + next: UnordMap<(LocalDefId, DefPathData2), u32>, } impl DisambiguatorState { @@ -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 @@ -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); @@ -166,7 +166,7 @@ impl DefKey { #[inline] pub fn get_opt_name(&self) -> Option { - self.disambiguated_data.data.get_opt_name() + self.disambiguated_data.data.unwrap().get_opt_name() } } @@ -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)) @@ -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)) @@ -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; @@ -276,6 +276,31 @@ pub enum DelegationDefPathKind { TyParam, } +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)] +pub enum DefPathData2 { + Default(DefPathData), + Delegation { name: Symbol, kind: DelegationDefPathKind }, +} + +impl Into 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 { @@ -325,10 +350,6 @@ pub enum DefPathData { SyntheticCoroutineBody, /// Additional static data referred to by a static. NestedStatic, - Delegation { - name: Symbol, - kind: DelegationDefPathKind, - }, } impl Definitions { @@ -367,7 +388,7 @@ impl Definitions { let key = DefKey { parent: None, disambiguated_data: DisambiguatedDefPathData { - data: DefPathData::CrateRoot, + data: DefPathData2::Default(DefPathData::CrateRoot), disambiguator: 0, }, }; @@ -399,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 @@ -410,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 = { @@ -466,12 +487,8 @@ impl DefPathData { pub fn get_opt_name(&self) -> Option { use self::DefPathData::*; match *self { - TypeNs(name) - | ValueNs(name) - | MacroNs(name) - | LifetimeNs(name) - | OpaqueLifetime(name) - | Delegation { name, .. } => Some(name), + TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) + | OpaqueLifetime(name) => Some(name), DesugaredAnonymousLifetime => Some(kw::UnderscoreLifetime), @@ -494,13 +511,8 @@ impl DefPathData { fn hashed_symbol(&self) -> Option { use self::DefPathData::*; match *self { - TypeNs(name) - | ValueNs(name) - | MacroNs(name) - | LifetimeNs(name) - | AnonAssocTy(name) - | OpaqueLifetime(name) - | Delegation { name, .. } => Some(name), + TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name) + | OpaqueLifetime(name) => Some(name), DesugaredAnonymousLifetime => Some(kw::UnderscoreLifetime), @@ -522,12 +534,8 @@ impl DefPathData { pub fn name(&self) -> DefPathDataName { use self::DefPathData::*; match *self { - TypeNs(name) - | ValueNs(name) - | MacroNs(name) - | LifetimeNs(name) - | OpaqueLifetime(name) - | Delegation { name, .. } => DefPathDataName::Named(name), + TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) + | OpaqueLifetime(name) => DefPathDataName::Named(name), // Note that this does not show up in user print-outs. CrateRoot => DefPathDataName::Anon { namespace: kw::Crate }, Impl => DefPathDataName::Anon { namespace: kw::Impl }, diff --git a/compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs b/compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs index d846f4433dc70..c377388f218d5 100644 --- a/compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs +++ b/compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs @@ -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()) }) } diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index 752c2220d4147..979463b793150 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -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(()) } diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index fc541f952d229..f62b219604777 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -869,6 +869,7 @@ impl MetadataBlob { def_key .disambiguated_data .data + .unwrap() .get_opt_name() .unwrap_or_else(|| Symbol::intern("???")) }; @@ -983,10 +984,10 @@ impl<'a> CrateMetadataRef<'a> { fn opt_item_name(self, item_index: DefIndex) -> Option { 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 } @@ -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 diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index e554da362f83e..7950a4e01ee71 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -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; @@ -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())); diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 58a2edca8ecef..872b900287d76 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -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; @@ -1358,7 +1358,7 @@ impl<'tcx> TyCtxtAt<'tcx> { parent: LocalDefId, name: Option, def_kind: DefKind, - override_def_path_data: Option, + override_def_path_data: Option>, disambiguator: &mut DisambiguatorState, ) -> TyCtxtFeed<'tcx, LocalDefId> { let feed = @@ -1376,10 +1376,12 @@ impl<'tcx> TyCtxt<'tcx> { parent: LocalDefId, name: Option, def_kind: DefKind, - override_def_path_data: Option, + override_def_path_data: Option>, 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. diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index 9759e376c0586..5367811bd3e44 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -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 ) } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 674799cb41bad..b63efc035041c 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -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() }), diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index 0fd68e74e0441..8aa043c8cec3e 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -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) @@ -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. diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 2c14c37609d41..ad1a95e525118 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -12,7 +12,7 @@ use rustc_hir as hir; use rustc_hir::LangItem; use rustc_hir::def::{self, CtorKind, DefKind, Namespace}; use rustc_hir::def_id::{DefIdMap, DefIdSet, LOCAL_CRATE, ModDefId}; -use rustc_hir::definitions::{DefKey, DefPathDataName}; +use rustc_hir::definitions::{DefKey, DefPathData2, DefPathDataName}; use rustc_hir::limit::Limit; use rustc_macros::{Lift, extension}; use rustc_session::cstore::{ExternCrate, ExternCrateSource}; @@ -381,7 +381,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { let get_local_name = |this: &Self, name, def_id, key: DefKey| { if let Some(visible_parent) = visible_parent_map.get(&def_id) && let actual_parent = this.tcx().opt_parent(def_id) - && let DefPathData::TypeNs(_) = key.disambiguated_data.data + && let DefPathData::TypeNs(_) = key.disambiguated_data.data.unwrap() && Some(*visible_parent) != actual_parent { this.tcx() @@ -544,7 +544,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { debug!("try_print_visible_def_path: cur_def_key={:?}", cur_def_key); // For a constructor, we want the name of its parent rather than . - if let DefPathData::Ctor = cur_def_key.disambiguated_data.data { + if let DefPathData::Ctor = cur_def_key.disambiguated_data.data.unwrap() { let parent = DefId { krate: def_id.krate, index: cur_def_key @@ -569,7 +569,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { visible_parent, actual_parent, ); - let mut data = cur_def_key.disambiguated_data.data; + let mut data = cur_def_key.disambiguated_data.data.unwrap(); debug!( "try_print_visible_def_path: data={:?} visible_parent={:?} actual_parent={:?}", data, visible_parent, actual_parent, @@ -649,7 +649,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { callers.pop(); self.print_path_with_simple( |_| Ok(()), - &DisambiguatedDefPathData { data, disambiguator: 0 }, + &DisambiguatedDefPathData { data: DefPathData2::Default(data), disambiguator: 0 }, )?; Ok(true) } @@ -2160,7 +2160,7 @@ impl<'a, 'tcx> FmtPrinter<'a, 'tcx> { } fn guess_def_namespace(tcx: TyCtxt<'_>, def_id: DefId) -> Namespace { - match tcx.def_key(def_id).disambiguated_data.data { + match tcx.def_key(def_id).disambiguated_data.data.unwrap() { DefPathData::TypeNs(..) | DefPathData::CrateRoot | DefPathData::OpaqueTy => { Namespace::TypeNS } @@ -2242,7 +2242,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { } let key = self.tcx.def_key(def_id); - if let DefPathData::Impl = key.disambiguated_data.data { + if let DefPathData::Impl = key.disambiguated_data.data.unwrap() { // Always use types for non-local impls, where types are always // available, and filename/line-number is mostly uninteresting. let use_types = !def_id.is_local() || { @@ -2393,11 +2393,11 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, '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(()); } - let name = disambiguated_data.data.name(); + let name = disambiguated_data.data.unwrap().name(); if !self.empty_path { write!(self, "::")?; } diff --git a/compiler/rustc_middle/src/ty/significant_drop_order.rs b/compiler/rustc_middle/src/ty/significant_drop_order.rs index 5035127b8c9ca..786269f74eecc 100644 --- a/compiler/rustc_middle/src/ty/significant_drop_order.rs +++ b/compiler/rustc_middle/src/ty/significant_drop_order.rs @@ -21,7 +21,7 @@ fn true_significant_drop_ty<'tcx>( loop { let key = tcx.def_key(did); - match key.disambiguated_data.data { + match key.disambiguated_data.data.unwrap() { rustc_hir::definitions::DefPathData::CrateRoot => { name_rev.push(tcx.crate_name(did.krate)); } diff --git a/compiler/rustc_mir_transform/src/coroutine/by_move_body.rs b/compiler/rustc_mir_transform/src/coroutine/by_move_body.rs index 951ff69c19e3e..ae320495b0316 100644 --- a/compiler/rustc_mir_transform/src/coroutine/by_move_body.rs +++ b/compiler/rustc_mir_transform/src/coroutine/by_move_body.rs @@ -73,7 +73,7 @@ use rustc_data_structures::unord::UnordMap; use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; -use rustc_hir::definitions::DisambiguatorState; +use rustc_hir::definitions::{DefPathData2, DisambiguatorState}; use rustc_middle::bug; use rustc_middle::hir::place::{Projection, ProjectionKind}; use rustc_middle::mir::visit::MutVisitor; @@ -220,7 +220,7 @@ pub(crate) fn coroutine_by_move_body_def_id<'tcx>( parent_def_id, None, DefKind::SyntheticCoroutineBody, - None, + None::, &mut DisambiguatorState::new(), ); by_move_body.source = diff --git a/compiler/rustc_monomorphize/src/partitioning.rs b/compiler/rustc_monomorphize/src/partitioning.rs index d8f4e01945075..950f6e9009395 100644 --- a/compiler/rustc_monomorphize/src/partitioning.rs +++ b/compiler/rustc_monomorphize/src/partitioning.rs @@ -723,7 +723,7 @@ 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| match part.data.name() { + let components = def_path.data.iter().map(|part| match part.data.unwrap().name() { DefPathDataName::Named(name) => name, DefPathDataName::Anon { .. } => unreachable!(), }); diff --git a/compiler/rustc_query_impl/src/profiling_support.rs b/compiler/rustc_query_impl/src/profiling_support.rs index 980e2b1305245..5230c18ad78d2 100644 --- a/compiler/rustc_query_impl/src/profiling_support.rs +++ b/compiler/rustc_query_impl/src/profiling_support.rs @@ -62,7 +62,7 @@ impl<'a, 'tcx> QueryKeyStringBuilder<'a, 'tcx> { let dis; let end_index; - match def_key.disambiguated_data.data { + match def_key.disambiguated_data.data.unwrap() { DefPathData::CrateRoot => { crate_name = self.tcx.crate_name(def_id.krate); name = crate_name.as_str(); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 10aa1b4019b3c..5b0633ce9e078 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -57,7 +57,7 @@ use rustc_hir::def::{ PerNS, }; use rustc_hir::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap}; -use rustc_hir::definitions::DisambiguatorState; +use rustc_hir::definitions::{DefPathData2, DisambiguatorState}; use rustc_hir::{PrimTy, TraitCandidate, find_attr}; use rustc_index::bit_set::DenseBitSet; use rustc_metadata::creader::CStore; @@ -1551,7 +1551,13 @@ impl<'tcx> Resolver<'_, 'tcx> { ); // FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()` - let feed = self.tcx.create_def(parent, name, def_kind, None, &mut self.disambiguator); + let feed = self.tcx.create_def( + parent, + name, + def_kind, + None::, + &mut self.disambiguator, + ); let def_id = feed.def_id(); // Create the definition. diff --git a/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs b/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs index 97fd57e34882c..a8340c7ecc96e 100644 --- a/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs +++ b/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs @@ -10,7 +10,7 @@ use std::fmt::Write as _; use rustc_abi::{ExternAbi, Integer}; use rustc_data_structures::base_n::{ALPHANUMERIC_ONLY, CASE_INSENSITIVE, ToBaseN}; use rustc_data_structures::fx::FxHashMap; -use rustc_hir::definitions::{DefPathData, DelegationDefPathKind}; +use rustc_hir::definitions::DefPathData; use rustc_hir::find_attr; use rustc_middle::bug; use rustc_middle::ty::layout::IntegerExt; @@ -672,14 +672,11 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String { def_path.data.reverse(); for disambiguated_data in &def_path.data { s.push('N'); - - const TYPE_NS: &str = "t"; - const VALUE_NS: &str = "v"; - s.push_str(match disambiguated_data.data { + s.push_str(match disambiguated_data.data.unwrap() { DefPathData::Impl => "I", // Not specified in v0's DefPathData::ForeignMod => "F", // Not specified in v0's - DefPathData::TypeNs(..) => TYPE_NS, - DefPathData::ValueNs(..) => VALUE_NS, + DefPathData::TypeNs(..) => "t", + DefPathData::ValueNs(..) => "v", DefPathData::Closure => "C", DefPathData::Ctor => "c", DefPathData::AnonConst => "K", @@ -687,8 +684,6 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String { DefPathData::OpaqueTy => "i", DefPathData::SyntheticCoroutineBody => "s", DefPathData::NestedStatic => "n", - DefPathData::Delegation { kind: DelegationDefPathKind::ConstParam, .. } => VALUE_NS, - DefPathData::Delegation { kind: DelegationDefPathKind::TyParam, .. } => TYPE_NS, DefPathData::CrateRoot | DefPathData::Use @@ -697,9 +692,8 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String { | DefPathData::OpaqueLifetime(..) | DefPathData::LifetimeNs(..) | DefPathData::DesugaredAnonymousLifetime - | DefPathData::AnonAssocTy(..) - | DefPathData::Delegation { kind: DelegationDefPathKind::Lifetime, .. } => { - bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data); + | DefPathData::AnonAssocTy(..) => { + bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data.unwrap()); } }); } @@ -718,7 +712,7 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String { s.push_str(&to_disambiguator(num)); } - let name = disambiguated_data.data.to_string(); + let name = disambiguated_data.data.unwrap().to_string(); let _ = write!(s, "{}", name.len()); // Prepend a '_' if name starts with a digit or '_' diff --git a/compiler/rustc_symbol_mangling/src/legacy.rs b/compiler/rustc_symbol_mangling/src/legacy.rs index ea16231880e2c..00f58a36b2346 100644 --- a/compiler/rustc_symbol_mangling/src/legacy.rs +++ b/compiler/rustc_symbol_mangling/src/legacy.rs @@ -27,7 +27,7 @@ pub(super) fn mangle<'tcx>( let instance_ty; loop { let key = tcx.def_key(ty_def_id); - match key.disambiguated_data.data { + match key.disambiguated_data.data.unwrap() { DefPathData::TypeNs(_) | DefPathData::ValueNs(_) | DefPathData::Closure @@ -365,7 +365,7 @@ impl<'tcx> Printer<'tcx> for LegacySymbolMangler<'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(()); } @@ -376,7 +376,7 @@ impl<'tcx> Printer<'tcx> for LegacySymbolMangler<'tcx> { self.path.finalize_pending_component(); } - write!(self, "{}", disambiguated_data.data)?; + write!(self, "{}", disambiguated_data.data.unwrap())?; Ok(()) } diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 205332b90d55b..fddb550a8b3a3 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -12,7 +12,7 @@ use rustc_hashes::Hash64; use rustc_hir as hir; use rustc_hir::def::CtorKind; use rustc_hir::def_id::{CrateNum, DefId}; -use rustc_hir::definitions::{DefPathData, DelegationDefPathKind, DisambiguatedDefPathData}; +use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData}; use rustc_middle::bug; use rustc_middle::ty::layout::IntegerExt; use rustc_middle::ty::print::{Print, PrintError, Printer}; @@ -815,7 +815,7 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> { // just to be able to handle disambiguators. let disambiguated_field = self.tcx.def_key(field_def.did).disambiguated_data; - let field_name = disambiguated_field.data.get_opt_name(); + let field_name = disambiguated_field.data.unwrap().get_opt_name(); self.push_disambiguator(disambiguated_field.disambiguator as u64); self.push_ident(field_name.unwrap().as_str()); @@ -877,16 +877,14 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> { print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>, disambiguated_data: &DisambiguatedDefPathData, ) -> Result<(), PrintError> { - const TYPE_NS: char = 't'; - const VALUE_NS: char = 'v'; - let ns = match disambiguated_data.data { + let ns = match disambiguated_data.data.unwrap() { // Extern block segments can be skipped, names from extern blocks // are effectively living in their parent modules. DefPathData::ForeignMod => return print_prefix(self), // Uppercase categories are more stable than lowercase ones. - DefPathData::TypeNs(_) => TYPE_NS, - DefPathData::ValueNs(_) => VALUE_NS, + DefPathData::TypeNs(_) => 't', + DefPathData::ValueNs(_) => 'v', DefPathData::Closure => 'C', DefPathData::Ctor => 'c', DefPathData::AnonConst => 'K', @@ -896,9 +894,6 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> { DefPathData::NestedStatic => 'n', DefPathData::GlobalAsm => 'a', - DefPathData::Delegation { kind: DelegationDefPathKind::ConstParam, .. } => VALUE_NS, - DefPathData::Delegation { kind: DelegationDefPathKind::TyParam, .. } => TYPE_NS, - // These should never show up as `print_path_with_simple` arguments. DefPathData::CrateRoot | DefPathData::Use @@ -907,13 +902,12 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> { | DefPathData::LifetimeNs(_) | DefPathData::DesugaredAnonymousLifetime | DefPathData::OpaqueLifetime(_) - | DefPathData::AnonAssocTy(..) - | DefPathData::Delegation { kind: DelegationDefPathKind::Lifetime, .. } => { - bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data) + | DefPathData::AnonAssocTy(..) => { + bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data.unwrap()) } }; - let name = disambiguated_data.data.get_opt_name(); + let name = disambiguated_data.data.unwrap().get_opt_name(); self.path_append_ns( print_prefix, diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs index d9ea2e0057895..6a581cf7512f9 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs @@ -1553,7 +1553,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { && let Some(primitive) = found.primitive_symbol() { let path = self.tcx.def_path(expected.did()).data; - let name = path.last().unwrap().data.get_opt_name(); + let name = path.last().unwrap().data.unwrap().get_opt_name(); if name == Some(primitive) { return Some(Similar::PrimitiveFound { expected: *expected, found }); } @@ -1561,7 +1561,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { && let ty::Adt(found, _) = found.kind() { let path = self.tcx.def_path(found.did()).data; - let name = path.last().unwrap().data.get_opt_name(); + let name = path.last().unwrap().data.unwrap().get_opt_name(); if name == Some(primitive) { return Some(Similar::PrimitiveExpected { expected, found: *found }); } diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs index 7fcaea3a629f1..5da2a64e097ba 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs @@ -123,7 +123,7 @@ impl InferenceDiagnosticsParentData { parent_def_id: DefId, ) -> Option { let parent_name = - tcx.def_key(parent_def_id).disambiguated_data.data.get_opt_name()?.to_string(); + tcx.def_key(parent_def_id).disambiguated_data.data.unwrap().get_opt_name()?.to_string(); Some(InferenceDiagnosticsParentData { prefix: tcx.def_descr(parent_def_id), diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 497f9205eb19d..a30900c4e91ce 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -295,7 +295,7 @@ fn associated_type_for_impl_trait_in_impl( // Use the same disambiguator and method name as the anon associated type in the trait. let disambiguated_data = tcx.def_key(trait_assoc_def_id).disambiguated_data; - let DefPathData::AnonAssocTy(name) = disambiguated_data.data else { + let DefPathData::AnonAssocTy(name) = disambiguated_data.data.unwrap() else { bug!("expected anon associated type") }; let data = DefPathData::AnonAssocTy(name); diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 09b2bc5dcef1d..27ed89a0f0098 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -225,7 +225,11 @@ pub(crate) fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> &'hir [hir: } pub(crate) fn item_relative_path(tcx: TyCtxt<'_>, def_id: DefId) -> Vec { - tcx.def_path(def_id).data.into_iter().filter_map(|elem| elem.data.get_opt_name()).collect() + tcx.def_path(def_id) + .data + .into_iter() + .filter_map(|elem| elem.data.unwrap().get_opt_name()) + .collect() } /// Get the public Rust path to an item. This is used to generate the URL to the item's page. diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 7eff0b5402b48..3d85dbe8736fa 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1431,12 +1431,12 @@ pub(crate) fn visibility_print_with_space(item: &clean::Item, cx: &Context<'_>) let path = cx.tcx().def_path(vis_did); debug!("path={path:?}"); // modified from `resolved_path()` to work with `DefPathData` - let last_name = path.data.last().unwrap().data.get_opt_name().unwrap(); + let last_name = path.data.last().unwrap().data.unwrap().get_opt_name().unwrap(); let anchor = print_anchor(vis_did, last_name, cx); f.write_str("pub(in ")?; for seg in &path.data[..path.data.len() - 1] { - write!(f, "{}::", seg.data.get_opt_name().unwrap())?; + write!(f, "{}::", seg.data.unwrap().get_opt_name().unwrap())?; } write!(f, "{anchor}) ")?; } diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 906289ba755e3..7fedb0e2e6a17 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -99,7 +99,8 @@ impl Module<'_> { // FIXME: Should this be replaced with tcx.def_path_str? fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec { let crate_name = tcx.crate_name(did.krate); - let relative = tcx.def_path(did).data.into_iter().filter_map(|elem| elem.data.get_opt_name()); + let relative = + tcx.def_path(did).data.into_iter().filter_map(|elem| elem.data.unwrap().get_opt_name()); std::iter::once(crate_name).chain(relative).collect() }