diff --git a/compiler/rustc_public/src/lib.rs b/compiler/rustc_public/src/lib.rs index f470eebc8b38..5da79196dd4e 100644 --- a/compiler/rustc_public/src/lib.rs +++ b/compiler/rustc_public/src/lib.rs @@ -178,7 +178,7 @@ impl CrateItem { pub fn emit_mir(&self, w: &mut W) -> io::Result<()> { self.body() .ok_or_else(|| io::Error::other(format!("No body found for `{}`", self.name())))? - .dump(w, &self.name()) + .dump(w, &self.trimmed_name()) } } diff --git a/compiler/rustc_public/src/mir/pretty.rs b/compiler/rustc_public/src/mir/pretty.rs index d80e227f5a9c..08e4e0c7e0f9 100644 --- a/compiler/rustc_public/src/mir/pretty.rs +++ b/compiler/rustc_public/src/mir/pretty.rs @@ -412,7 +412,7 @@ fn pretty_aggregate( } AggregateKind::Adt(def, var, _, _, _) => { if def.kind() == AdtKind::Enum { - write!(writer, "{}::{}", def.name(), def.variant(*var).unwrap().name())?; + write!(writer, "{}::{}", def.trimmed_name(), def.variant(*var).unwrap().name())?; } else { write!(writer, "{}", def.variant(*var).unwrap().name())?; } diff --git a/compiler/rustc_public/src/ty.rs b/compiler/rustc_public/src/ty.rs index f24d98f7e552..14656a2e594a 100644 --- a/compiler/rustc_public/src/ty.rs +++ b/compiler/rustc_public/src/ty.rs @@ -876,6 +876,9 @@ pub struct VariantDef { } impl VariantDef { + /// The name of the variant, struct or union. + /// + /// This will not include the name of the enum or qualified path. pub fn name(&self) -> Symbol { with(|cx| cx.variant_name(*self)) } diff --git a/compiler/rustc_public_bridge/src/context/impls.rs b/compiler/rustc_public_bridge/src/context/impls.rs index 1047bbbef69e..26728a34b84a 100644 --- a/compiler/rustc_public_bridge/src/context/impls.rs +++ b/compiler/rustc_public_bridge/src/context/impls.rs @@ -10,7 +10,9 @@ use rustc_hir::{Attribute, LangItem}; use rustc_middle::mir::interpret::{AllocId, ConstAllocation, ErrorHandled, GlobalAlloc, Scalar}; use rustc_middle::mir::{BinOp, Body, Const as MirConst, ConstValue, UnOp}; use rustc_middle::ty::layout::{FnAbiOf, LayoutOf}; -use rustc_middle::ty::print::{with_forced_trimmed_paths, with_resolve_crate_name}; +use rustc_middle::ty::print::{ + with_forced_trimmed_paths, with_no_trimmed_paths, with_resolve_crate_name, +}; use rustc_middle::ty::util::Discr; use rustc_middle::ty::{ AdtDef, AdtKind, AssocItem, Binder, ClosureKind, CoroutineArgsExt, EarlyBinder, @@ -265,7 +267,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { with_forced_trimmed_paths!(self.tcx.def_path_str(def_id)) } else { // For local definitions, we need to prepend with crate name. - with_resolve_crate_name!(self.tcx.def_path_str(def_id)) + with_resolve_crate_name!(with_no_trimmed_paths!(self.tcx.def_path_str(def_id))) } } @@ -725,9 +727,9 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { self.tcx.def_path_str_with_args(instance.def_id(), instance.args) ) } else { - with_resolve_crate_name!( + with_resolve_crate_name!(with_no_trimmed_paths!( self.tcx.def_path_str_with_args(instance.def_id(), instance.args) - ) + )) } } diff --git a/tests/ui-fulldeps/rustc_public/check_def_parent.rs b/tests/ui-fulldeps/rustc_public/check_def_parent.rs index 192ad471de33..7f633a627b19 100644 --- a/tests/ui-fulldeps/rustc_public/check_def_parent.rs +++ b/tests/ui-fulldeps/rustc_public/check_def_parent.rs @@ -47,44 +47,47 @@ fn test_stable_mir() -> ControlFlow<()> { let krate = rustc_public::local_crate(); for it in rustc_public::all_local_items() { match &*it.0.name() { - "wrapper_mod::CONST_ITEM" => { + "input::wrapper_mod::CONST_ITEM" => { set_once(&mut const_item, it.0); } - "wrapper_mod::STATIC_ITEM" => { + "input::wrapper_mod::STATIC_ITEM" => { set_once(&mut static_item, it.0); } - "::trait_method" => { + "::trait_method" => { set_once(&mut trait_method, it.0); } - "::trait_method::trait_method_helper" => { + "::trait_method::trait_method_helper" => + { set_once(&mut trait_method_helper, it.0); } - "wrapper_mod::MyStruct::inherent_method" => { + "input::wrapper_mod::MyStruct::inherent_method" => { set_once(&mut inherent_method, it.0); } - "wrapper_mod::MyStruct::inherent_method::inherent_method_helper" => { + "input::wrapper_mod::MyStruct::inherent_method::inherent_method_helper" => { set_once(&mut inherent_method_helper, it.0); } - "main" => { + "input::main" => { set_once(&mut main, it.0); } - "wrapper_mod::MyStruct" => { + "input::wrapper_mod::MyStruct" => { set_once(&mut mystruct_ctor, it.0); mystruct_ctor_ty = Some(it.ty()); } - _ => (), + name => panic!("Unexpected item: `{name}`"), } } for it in krate.trait_decls() { match &*it.0.name() { - "wrapper_mod::MyTrait" => set_once(&mut trait_decl, it.0), + "input::wrapper_mod::MyTrait" => set_once(&mut trait_decl, it.0), _ => (), } } for it in krate.trait_impls() { match &*it.0.name() { - "" => set_once(&mut trait_impl, it.0), - _ => (), + "" => { + set_once(&mut trait_impl, it.0) + } + name => panic!("Unexpected trait impl: `{name}`"), } } @@ -106,9 +109,10 @@ fn test_stable_mir() -> ControlFlow<()> { let inherent_impl = inherent_method.parent().unwrap(); let wrapper_mod = const_item.parent().unwrap(); let crate_root = wrapper_mod.parent().unwrap(); - assert_eq!(&*wrapper_mod.name(), "wrapper_mod"); + assert_eq!(&*wrapper_mod.name(), "input::wrapper_mod"); // Check that each def-id has the correct parent + assert_eq!(crate_root.name(), "input"); assert_eq!(crate_root.parent(), None); assert_eq!(inherent_impl.parent(), Some(wrapper_mod)); assert_eq!(const_item.parent(), Some(wrapper_mod)); diff --git a/tests/ui-fulldeps/rustc_public/check_def_ty.rs b/tests/ui-fulldeps/rustc_public/check_def_ty.rs index 242b06dcd229..5984d6f8821a 100644 --- a/tests/ui-fulldeps/rustc_public/check_def_ty.rs +++ b/tests/ui-fulldeps/rustc_public/check_def_ty.rs @@ -15,9 +15,12 @@ extern crate rustc_middle; extern crate rustc_driver; extern crate rustc_interface; extern crate rustc_public; +extern crate rustc_public_bridge; -use rustc_public::ty::{ForeignItemKind, Ty}; +use rustc_public::ty::VariantIdx; +use rustc_public::ty::{ForeignItemKind, RigidTy, Ty}; use rustc_public::*; +use rustc_public_bridge::IndexedVal; use std::io::Write; use std::ops::ControlFlow; @@ -32,6 +35,14 @@ fn test_def_tys() -> ControlFlow<()> { match item.trimmed_name().as_str() { "STATIC_STR" => assert!(ty.kind().is_ref()), "CONST_U32" => assert!(ty.kind().is_integral()), + "NONE" => { + let RigidTy::Adt(adt, _) = *ty.kind().rigid().unwrap() else { panic!() }; + // Definition names include the entire path. + assert_eq!(adt.name(), "std::option::Option"); + // Variant name only includes the actual variant name. + // I know, probably not the best name schema. o.O + assert_eq!(adt.variant(VariantIdx::to_val(0)).unwrap().name(), "None"); + } "main" => check_fn_def(ty), _ => unreachable!("Unexpected item: `{item:?}`"), } @@ -92,6 +103,7 @@ fn generate_input(path: &str) -> std::io::Result<()> { r#" static STATIC_STR: &str = "foo"; const CONST_U32: u32 = 0u32; + static NONE: Option = Option::None; fn main() {{ let _c = core::char::from_u32(99); diff --git a/tests/ui/rustc_public-ir-print/async-closure.stdout b/tests/ui/rustc_public-ir-print/async-closure.stdout index 3ec816b657f4..8df31e2f680a 100644 --- a/tests/ui/rustc_public-ir-print/async-closure.stdout +++ b/tests/ui/rustc_public-ir-print/async-closure.stdout @@ -56,7 +56,7 @@ fn foo::{closure#0}::{closure#0}(_1: Pin<&mut {async closure body@$DIR/async-clo _3 = (*_4); _5 = (); StorageDead(_3); - _0 = std::task::Poll::Ready(move _5); + _0 = Poll::Ready(move _5); discriminant((*_7)) = 1; return; } @@ -88,7 +88,7 @@ fn foo::{closure#0}::{synthetic#0}(_1: Pin<&mut {async closure body@$DIR/async-c _3 = (*_4); _5 = (); StorageDead(_3); - _0 = std::task::Poll::Ready(move _5); + _0 = Poll::Ready(move _5); discriminant((*_7)) = 1; return; } diff --git a/tests/ui/rustc_public-ir-print/operands.stdout b/tests/ui/rustc_public-ir-print/operands.stdout index b7775416af85..58f229be2e6e 100644 --- a/tests/ui/rustc_public-ir-print/operands.stdout +++ b/tests/ui/rustc_public-ir-print/operands.stdout @@ -206,7 +206,7 @@ fn operands(_1: u8) -> () { StorageDead(_19); StorageDead(_18); StorageLive(_21); - _21 = core::panicking::AssertKind::Eq; + _21 = AssertKind::Eq; StorageLive(_22); StorageLive(_23); _23 = move _21; @@ -219,7 +219,7 @@ fn operands(_1: u8) -> () { _27 = &(*_16); _26 = &(*_27); StorageLive(_28); - _28 = std::option::Option::None; + _28 = Option::None; _22 = core::panicking::assert_failed::(move _23, move _24, move _26, move _28) -> unwind unreachable; } bb6: { @@ -268,7 +268,7 @@ fn operands(_1: u8) -> () { StorageDead(_39); StorageDead(_38); StorageLive(_41); - _41 = core::panicking::AssertKind::Eq; + _41 = AssertKind::Eq; StorageLive(_42); StorageLive(_43); _43 = move _41; @@ -281,7 +281,7 @@ fn operands(_1: u8) -> () { _47 = &(*_36); _46 = &(*_47); StorageLive(_48); - _48 = std::option::Option::None; + _48 = Option::None; _42 = core::panicking::assert_failed::(move _43, move _44, move _46, move _48) -> unwind unreachable; } bb8: { @@ -305,7 +305,7 @@ fn operands(_1: u8) -> () { StorageDead(_62); StorageDead(_61); StorageLive(_64); - _64 = core::panicking::AssertKind::Eq; + _64 = AssertKind::Eq; StorageLive(_65); StorageLive(_66); _66 = move _64; @@ -318,7 +318,7 @@ fn operands(_1: u8) -> () { _70 = &(*_59); _69 = &(*_70); StorageLive(_71); - _71 = std::option::Option::None; + _71 = Option::None; _65 = core::panicking::assert_failed::(move _66, move _67, move _69, move _71) -> unwind unreachable; } bb10: { @@ -380,7 +380,7 @@ fn operands(_1: u8) -> () { StorageDead(_86); StorageDead(_85); StorageLive(_88); - _88 = core::panicking::AssertKind::Eq; + _88 = AssertKind::Eq; StorageLive(_89); StorageLive(_90); _90 = move _88; @@ -393,7 +393,7 @@ fn operands(_1: u8) -> () { _94 = &(*_83); _93 = &(*_94); StorageLive(_95); - _95 = std::option::Option::None; + _95 = Option::None; _89 = core::panicking::assert_failed::(move _90, move _91, move _93, move _95) -> unwind unreachable; } }