Skip to content

Commit e77f9fe

Browse files
committed
Fix name and pretty print now uses trimmed_name
1 parent 8072264 commit e77f9fe

File tree

7 files changed

+37
-20
lines changed

7 files changed

+37
-20
lines changed

compiler/rustc_public/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ use std::fmt::Debug;
2020
use std::marker::PhantomData;
2121
use std::{fmt, io};
2222

23+
use rustc_public_bridge::context::CompilerCtxt;
2324
pub(crate) use rustc_public_bridge::IndexedVal;
2425
use rustc_public_bridge::Tables;
25-
use rustc_public_bridge::context::CompilerCtxt;
2626
/// Export the rustc_internal APIs. Note that this module has no stability
2727
/// guarantees and it is not taken into account for semver.
2828
#[cfg(feature = "rustc_internal")]
@@ -35,8 +35,8 @@ pub use crate::error::*;
3535
use crate::mir::mono::StaticDef;
3636
use crate::mir::{Body, Mutability};
3737
use crate::ty::{
38-
AssocItem, FnDef, ForeignModuleDef, ImplDef, ProvenanceMap, Span, TraitDef, Ty,
39-
serialize_index_impl,
38+
serialize_index_impl, AssocItem, FnDef, ForeignModuleDef, ImplDef, ProvenanceMap, Span,
39+
TraitDef, Ty,
4040
};
4141
use crate::unstable::Stable;
4242

@@ -178,7 +178,7 @@ impl CrateItem {
178178
pub fn emit_mir<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
179179
self.body()
180180
.ok_or_else(|| io::Error::other(format!("No body found for `{}`", self.name())))?
181-
.dump(w, &self.name())
181+
.dump(w, &self.trimmed_name())
182182
}
183183
}
184184

compiler/rustc_public/src/mir/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ fn pretty_aggregate<W: Write>(
412412
}
413413
AggregateKind::Adt(def, var, _, _, _) => {
414414
if def.kind() == AdtKind::Enum {
415-
write!(writer, "{}::{}", def.name(), def.variant(*var).unwrap().name())?;
415+
write!(writer, "{}::{}", def.trimmed_name(), def.variant(*var).unwrap().name())?;
416416
} else {
417417
write!(writer, "{}", def.variant(*var).unwrap().name())?;
418418
}

compiler/rustc_public/src/ty.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,9 @@ pub struct VariantDef {
876876
}
877877

878878
impl VariantDef {
879+
/// The name of the variant, struct or union.
880+
///
881+
/// This will not include the name of the enum or qualified path.
879882
pub fn name(&self) -> Symbol {
880883
with(|cx| cx.variant_name(*self))
881884
}

compiler/rustc_public_bridge/src/context/impls.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use rustc_hir::{Attribute, LangItem};
1010
use rustc_middle::mir::interpret::{AllocId, ConstAllocation, ErrorHandled, GlobalAlloc, Scalar};
1111
use rustc_middle::mir::{BinOp, Body, Const as MirConst, ConstValue, UnOp};
1212
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
13-
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_resolve_crate_name};
13+
use rustc_middle::ty::print::{
14+
with_forced_trimmed_paths, with_no_trimmed_paths, with_resolve_crate_name,
15+
};
1416
use rustc_middle::ty::util::Discr;
1517
use rustc_middle::ty::{
1618
AdtDef, AdtKind, AssocItem, Binder, ClosureKind, CoroutineArgsExt, EarlyBinder,
@@ -265,7 +267,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
265267
with_forced_trimmed_paths!(self.tcx.def_path_str(def_id))
266268
} else {
267269
// For local definitions, we need to prepend with crate name.
268-
with_resolve_crate_name!(self.tcx.def_path_str(def_id))
270+
with_resolve_crate_name!(with_no_trimmed_paths!(self.tcx.def_path_str(def_id)))
269271
}
270272
}
271273

@@ -720,9 +722,9 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
720722
self.tcx.def_path_str_with_args(instance.def_id(), instance.args)
721723
)
722724
} else {
723-
with_resolve_crate_name!(
725+
with_resolve_crate_name!(with_no_trimmed_paths!(
724726
self.tcx.def_path_str_with_args(instance.def_id(), instance.args)
725-
)
727+
))
726728
}
727729
}
728730

tests/ui-fulldeps/rustc_public/check_def_ty.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ extern crate rustc_middle;
1515
extern crate rustc_driver;
1616
extern crate rustc_interface;
1717
extern crate rustc_public;
18+
extern crate rustc_public_bridge;
1819

19-
use rustc_public::ty::{ForeignItemKind, Ty};
20+
use rustc_public::ty::VariantIdx;
21+
use rustc_public::ty::{ForeignItemKind, RigidTy, Ty};
2022
use rustc_public::*;
23+
use rustc_public_bridge::IndexedVal;
2124
use std::io::Write;
2225
use std::ops::ControlFlow;
2326

@@ -32,6 +35,14 @@ fn test_def_tys() -> ControlFlow<()> {
3235
match item.trimmed_name().as_str() {
3336
"STATIC_STR" => assert!(ty.kind().is_ref()),
3437
"CONST_U32" => assert!(ty.kind().is_integral()),
38+
"NONE" => {
39+
let RigidTy::Adt(adt, _) = *ty.kind().rigid().unwrap() else { panic!() };
40+
// Definition names include the entire path.
41+
assert_eq!(adt.name(), "std::option::Option");
42+
// Variant name only includes the actual variant name.
43+
// I know, probably not the best name schema. o.O
44+
assert_eq!(adt.variant(VariantIdx::to_val(0)).unwrap().name(), "None");
45+
}
3546
"main" => check_fn_def(ty),
3647
_ => unreachable!("Unexpected item: `{item:?}`"),
3748
}
@@ -92,6 +103,7 @@ fn generate_input(path: &str) -> std::io::Result<()> {
92103
r#"
93104
static STATIC_STR: &str = "foo";
94105
const CONST_U32: u32 = 0u32;
106+
static NONE: Option<i32> = Option::None;
95107
96108
fn main() {{
97109
let _c = core::char::from_u32(99);

tests/ui/rustc_public-ir-print/async-closure.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn foo::{closure#0}::{closure#0}(_1: Pin<&mut {async closure body@$DIR/async-clo
5656
_3 = (*_4);
5757
_5 = ();
5858
StorageDead(_3);
59-
_0 = std::task::Poll::Ready(move _5);
59+
_0 = Poll::Ready(move _5);
6060
discriminant((*_7)) = 1;
6161
return;
6262
}
@@ -88,7 +88,7 @@ fn foo::{closure#0}::{synthetic#0}(_1: Pin<&mut {async closure body@$DIR/async-c
8888
_3 = (*_4);
8989
_5 = ();
9090
StorageDead(_3);
91-
_0 = std::task::Poll::Ready(move _5);
91+
_0 = Poll::Ready(move _5);
9292
discriminant((*_7)) = 1;
9393
return;
9494
}

tests/ui/rustc_public-ir-print/operands.stdout

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ fn operands(_1: u8) -> () {
206206
StorageDead(_19);
207207
StorageDead(_18);
208208
StorageLive(_21);
209-
_21 = core::panicking::AssertKind::Eq;
209+
_21 = AssertKind::Eq;
210210
StorageLive(_22);
211211
StorageLive(_23);
212212
_23 = move _21;
@@ -219,7 +219,7 @@ fn operands(_1: u8) -> () {
219219
_27 = &(*_16);
220220
_26 = &(*_27);
221221
StorageLive(_28);
222-
_28 = std::option::Option::None;
222+
_28 = Option::None;
223223
_22 = core::panicking::assert_failed::<u8, u8>(move _23, move _24, move _26, move _28) -> unwind unreachable;
224224
}
225225
bb6: {
@@ -268,7 +268,7 @@ fn operands(_1: u8) -> () {
268268
StorageDead(_39);
269269
StorageDead(_38);
270270
StorageLive(_41);
271-
_41 = core::panicking::AssertKind::Eq;
271+
_41 = AssertKind::Eq;
272272
StorageLive(_42);
273273
StorageLive(_43);
274274
_43 = move _41;
@@ -281,7 +281,7 @@ fn operands(_1: u8) -> () {
281281
_47 = &(*_36);
282282
_46 = &(*_47);
283283
StorageLive(_48);
284-
_48 = std::option::Option::None;
284+
_48 = Option::None;
285285
_42 = core::panicking::assert_failed::<u8, u8>(move _43, move _44, move _46, move _48) -> unwind unreachable;
286286
}
287287
bb8: {
@@ -305,7 +305,7 @@ fn operands(_1: u8) -> () {
305305
StorageDead(_62);
306306
StorageDead(_61);
307307
StorageLive(_64);
308-
_64 = core::panicking::AssertKind::Eq;
308+
_64 = AssertKind::Eq;
309309
StorageLive(_65);
310310
StorageLive(_66);
311311
_66 = move _64;
@@ -318,7 +318,7 @@ fn operands(_1: u8) -> () {
318318
_70 = &(*_59);
319319
_69 = &(*_70);
320320
StorageLive(_71);
321-
_71 = std::option::Option::None;
321+
_71 = Option::None;
322322
_65 = core::panicking::assert_failed::<u8, u8>(move _66, move _67, move _69, move _71) -> unwind unreachable;
323323
}
324324
bb10: {
@@ -380,7 +380,7 @@ fn operands(_1: u8) -> () {
380380
StorageDead(_86);
381381
StorageDead(_85);
382382
StorageLive(_88);
383-
_88 = core::panicking::AssertKind::Eq;
383+
_88 = AssertKind::Eq;
384384
StorageLive(_89);
385385
StorageLive(_90);
386386
_90 = move _88;
@@ -393,7 +393,7 @@ fn operands(_1: u8) -> () {
393393
_94 = &(*_83);
394394
_93 = &(*_94);
395395
StorageLive(_95);
396-
_95 = std::option::Option::None;
396+
_95 = Option::None;
397397
_89 = core::panicking::assert_failed::<usize, usize>(move _90, move _91, move _93, move _95) -> unwind unreachable;
398398
}
399399
}

0 commit comments

Comments
 (0)