Skip to content

Commit 4c8c967

Browse files
authored
Rollup merge of #149401 - celinval:smir-109-name, r=scottmcm
Fix `name()` functions for local defs in rustc_public This change fixes the behavior of the `name()` function for `CrateDef` and `Instance` which should return absolute path of items. For local items, the crate name was missing. This resolves: rust-lang/project-stable-mir#109
2 parents 8a6f82e + bb2bfc3 commit 4c8c967

25 files changed

+146
-130
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ define_helper!(
105105
///
106106
/// Overrides `with_crate_prefix`.
107107
108-
// This function is not currently used in-tree, but it's used by a downstream rustc-driver in
108+
// This function is used by `rustc_public` and downstream rustc-driver in
109109
// Ferrocene. Please check with them before removing it.
110110
fn with_resolve_crate_name(CrateNamePrefixGuard, SHOULD_PREFIX_WITH_CRATE_NAME);
111111
/// Adds the `crate::` prefix to paths where appropriate.

compiler/rustc_public/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 7 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_no_trimmed_paths};
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,
@@ -264,7 +266,8 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
264266
if trimmed {
265267
with_forced_trimmed_paths!(self.tcx.def_path_str(def_id))
266268
} else {
267-
with_no_trimmed_paths!(self.tcx.def_path_str(def_id))
269+
// For local definitions, we need to prepend with crate name.
270+
with_resolve_crate_name!(with_no_trimmed_paths!(self.tcx.def_path_str(def_id)))
268271
}
269272
}
270273

@@ -724,9 +727,9 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
724727
self.tcx.def_path_str_with_args(instance.def_id(), instance.args)
725728
)
726729
} else {
727-
with_no_trimmed_paths!(
730+
with_resolve_crate_name!(with_no_trimmed_paths!(
728731
self.tcx.def_path_str_with_args(instance.def_id(), instance.args)
729-
)
732+
))
730733
}
731734
}
732735

tests/ui-fulldeps/rustc_public/check_abi.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#![feature(assert_matches)]
1010
#![feature(ascii_char, ascii_char_variants)]
1111

12-
extern crate rustc_hir;
13-
extern crate rustc_middle;
1412
extern crate rustc_driver;
13+
extern crate rustc_hir;
1514
extern crate rustc_interface;
15+
extern crate rustc_middle;
1616
#[macro_use]
1717
extern crate rustc_public;
1818

@@ -39,7 +39,7 @@ fn test_stable_mir() -> ControlFlow<()> {
3939
let items = rustc_public::all_local_items();
4040

4141
// Test fn_abi
42-
let target_fn = *get_item(&items, (ItemKind::Fn, "fn_abi")).unwrap();
42+
let target_fn = *get_item(&items, (ItemKind::Fn, "input::fn_abi")).unwrap();
4343
let instance = Instance::try_from(target_fn).unwrap();
4444
let fn_abi = instance.fn_abi().unwrap();
4545
assert_eq!(fn_abi.conv, CallConvention::Rust);
@@ -51,11 +51,11 @@ fn test_stable_mir() -> ControlFlow<()> {
5151
check_result(&fn_abi.ret);
5252

5353
// Test variadic function.
54-
let variadic_fn = *get_item(&items, (ItemKind::Fn, "variadic_fn")).unwrap();
54+
let variadic_fn = *get_item(&items, (ItemKind::Fn, "input::variadic_fn")).unwrap();
5555
check_variadic(variadic_fn);
5656

5757
// Extract function pointers.
58-
let fn_ptr_holder = *get_item(&items, (ItemKind::Fn, "fn_ptr_holder")).unwrap();
58+
let fn_ptr_holder = *get_item(&items, (ItemKind::Fn, "input::fn_ptr_holder")).unwrap();
5959
let fn_ptr_holder_instance = Instance::try_from(fn_ptr_holder).unwrap();
6060
let body = fn_ptr_holder_instance.body().unwrap();
6161
let args = body.arg_locals();

tests/ui-fulldeps/rustc_public/check_allocation.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ const CRATE_NAME: &str = "input";
4040
fn test_stable_mir() -> ControlFlow<()> {
4141
// Find items in the local crate.
4242
let items = rustc_public::all_local_items();
43-
check_foo(*get_item(&items, (ItemKind::Static, "FOO")).unwrap());
44-
check_bar(*get_item(&items, (ItemKind::Static, "BAR")).unwrap());
45-
check_len(*get_item(&items, (ItemKind::Static, "LEN")).unwrap());
46-
check_cstr(*get_item(&items, (ItemKind::Static, "C_STR")).unwrap());
47-
check_other_consts(*get_item(&items, (ItemKind::Fn, "other_consts")).unwrap());
43+
check_foo(*get_item(&items, (ItemKind::Static, "input::FOO")).unwrap());
44+
check_bar(*get_item(&items, (ItemKind::Static, "input::BAR")).unwrap());
45+
check_len(*get_item(&items, (ItemKind::Static, "input::LEN")).unwrap());
46+
check_cstr(*get_item(&items, (ItemKind::Static, "input::C_STR")).unwrap());
47+
check_other_consts(*get_item(&items, (ItemKind::Fn, "input::other_consts")).unwrap());
4848
ControlFlow::Continue(())
4949
}
5050

tests/ui-fulldeps/rustc_public/check_assoc_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn test_assoc_items() -> ControlFlow<()> {
7777
/// Note that order doesn't matter.
7878
fn check_items<T: CrateDef>(items: &[T], expected: &[&str]) {
7979
let expected: HashSet<_> = expected.iter().map(|s| s.to_string()).collect();
80-
let item_names: HashSet<_> = items.iter().map(|item| item.name()).collect();
80+
let item_names: HashSet<_> = items.iter().map(|item| item.trimmed_name()).collect();
8181
assert_eq!(item_names, expected);
8282
}
8383

tests/ui-fulldeps/rustc_public/check_attribute.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,13 @@ fn test_tool(items: &CrateItems) {
3838
assert_eq!(rustfmt_attrs[0].as_str(), "#[rustfmt::skip]\n");
3939

4040
let clippy_fn = *get_item(&items, "complex_fn").unwrap();
41-
let clippy_attrs = clippy_fn.tool_attrs(&["clippy".to_string(),
42-
"cyclomatic_complexity".to_string()]);
41+
let clippy_attrs =
42+
clippy_fn.tool_attrs(&["clippy".to_string(), "cyclomatic_complexity".to_string()]);
4343
assert_eq!(clippy_attrs[0].as_str(), "#[clippy::cyclomatic_complexity = \"100\"]\n");
4444
}
4545

46-
fn get_item<'a>(
47-
items: &'a CrateItems,
48-
name: &str,
49-
) -> Option<&'a rustc_public::CrateItem> {
50-
items.iter().find(|crate_item| crate_item.name() == name)
46+
fn get_item<'a>(items: &'a CrateItems, name: &str) -> Option<&'a rustc_public::CrateItem> {
47+
items.iter().find(|crate_item| crate_item.trimmed_name() == name)
5148
}
5249

5350
/// This test will generate and analyze a dummy crate using the stable mir.

tests/ui-fulldeps/rustc_public/check_coroutine_body.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ fn test_coroutine_body() -> ControlFlow<()> {
2929
if let Some(body) = crate_items.iter().find_map(|item| {
3030
let item_ty = item.ty();
3131
if let TyKind::RigidTy(RigidTy::Coroutine(def, ..)) = &item_ty.kind() {
32-
if def.0.name() == "gbc::{closure#0}".to_string() {
33-
def.body()
34-
} else {
35-
None
36-
}
32+
if def.0.trimmed_name() == "gbc::{closure#0}".to_string() { def.body() } else { None }
3733
} else {
3834
None
3935
}
@@ -51,26 +47,23 @@ fn check_coroutine_body(body: Body) {
5147
let local_3 = &body.locals()[3].ty;
5248
let local_4 = &body.locals()[4].ty;
5349

54-
let TyKind::RigidTy(RigidTy::Adt(def, ..)) = &ret_ty.kind()
55-
else {
50+
let TyKind::RigidTy(RigidTy::Adt(def, ..)) = &ret_ty.kind() else {
5651
panic!("Expected RigidTy::Adt, got: {:#?}", ret_ty);
5752
};
5853

5954
assert_eq!("std::task::Poll", def.0.name());
6055

61-
let TyKind::RigidTy(RigidTy::Coroutine(def, ..)) = &local_3.kind()
62-
else {
56+
let TyKind::RigidTy(RigidTy::Coroutine(def, ..)) = &local_3.kind() else {
6357
panic!("Expected RigidTy::Coroutine, got: {:#?}", local_3);
6458
};
6559

66-
assert_eq!("gbc::{closure#0}::{closure#0}", def.0.name());
60+
assert_eq!("crate_coroutine_body::gbc::{closure#0}::{closure#0}", def.0.name());
6761

68-
let TyKind::RigidTy(RigidTy::Coroutine(def, ..)) = &local_4.kind()
69-
else {
62+
let TyKind::RigidTy(RigidTy::Coroutine(def, ..)) = &local_4.kind() else {
7063
panic!("Expected RigidTy::Coroutine, got: {:#?}", local_4);
7164
};
7265

73-
assert_eq!("gbc::{closure#0}::{closure#0}", def.0.name());
66+
assert_eq!("crate_coroutine_body::gbc::{closure#0}::{closure#0}", def.0.name());
7467
}
7568

7669
fn main() {

0 commit comments

Comments
 (0)