Skip to content

Commit 8072264

Browse files
committed
Fix name() functions for local defs in rustc_public
The `name()` function specifies that it returns absolute path of items, however it wasn't including the crate name for local items. This change fixes that. This was reported here: rust-lang/project-stable-mir#109
1 parent cf8a955 commit 8072264

File tree

19 files changed

+99
-104
lines changed

19 files changed

+99
-104
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_bridge/src/context/impls.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ 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::{with_forced_trimmed_paths, with_resolve_crate_name};
1414
use rustc_middle::ty::util::Discr;
1515
use rustc_middle::ty::{
1616
AdtDef, AdtKind, AssocItem, Binder, ClosureKind, CoroutineArgsExt, EarlyBinder,
@@ -264,7 +264,8 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
264264
if trimmed {
265265
with_forced_trimmed_paths!(self.tcx.def_path_str(def_id))
266266
} else {
267-
with_no_trimmed_paths!(self.tcx.def_path_str(def_id))
267+
// For local definitions, we need to prepend with crate name.
268+
with_resolve_crate_name!(self.tcx.def_path_str(def_id))
268269
}
269270
}
270271

@@ -719,7 +720,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
719720
self.tcx.def_path_str_with_args(instance.def_id(), instance.args)
720721
)
721722
} else {
722-
with_no_trimmed_paths!(
723+
with_resolve_crate_name!(
723724
self.tcx.def_path_str_with_args(instance.def_id(), instance.args)
724725
)
725726
}

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() {

tests/ui-fulldeps/rustc_public/check_crate_defs.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ const CRATE_NAME: &str = "crate_defs";
2727
fn test_stable_mir() -> ControlFlow<()> {
2828
// Find items in the local crate.
2929
let local = rustc_public::local_crate();
30-
check_items(&local.statics(), &["PRIVATE_STATIC", "dummy::PUBLIC_STATIC"]);
30+
check_items(
31+
&local.statics(),
32+
&["crate_defs::PRIVATE_STATIC", "crate_defs::dummy::PUBLIC_STATIC"],
33+
);
3134
check_items(
3235
&local.fn_defs(),
3336
&[
34-
"top_level",
35-
"dummy::public_fn",
36-
"dummy::private_fn",
37-
"dummy::PrivateStruct::new",
38-
"<dummy::PrivateStruct as std::ops::Drop>::drop",
39-
"DummyTrait::method",
40-
"<T as DummyTrait>::method",
37+
"crate_defs::top_level",
38+
"crate_defs::dummy::public_fn",
39+
"crate_defs::dummy::private_fn",
40+
"crate_defs::dummy::PrivateStruct::new",
41+
"<crate_defs::dummy::PrivateStruct as std::ops::Drop>::drop",
42+
"crate_defs::DummyTrait::method",
43+
"<T as crate_defs::DummyTrait>::method",
4144
],
4245
);
4346

tests/ui-fulldeps/rustc_public/check_def_ty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern crate rustc_driver;
1616
extern crate rustc_interface;
1717
extern crate rustc_public;
1818

19-
use rustc_public::ty::{Ty, ForeignItemKind};
19+
use rustc_public::ty::{ForeignItemKind, Ty};
2020
use rustc_public::*;
2121
use std::io::Write;
2222
use std::ops::ControlFlow;
@@ -29,11 +29,11 @@ fn test_def_tys() -> ControlFlow<()> {
2929
for item in &items {
3030
// Type from crate items.
3131
let ty = item.ty();
32-
match item.name().as_str() {
32+
match item.trimmed_name().as_str() {
3333
"STATIC_STR" => assert!(ty.kind().is_ref()),
3434
"CONST_U32" => assert!(ty.kind().is_integral()),
35-
"main" => { check_fn_def(ty) }
36-
_ => unreachable!("Unexpected item: `{item:?}`")
35+
"main" => check_fn_def(ty),
36+
_ => unreachable!("Unexpected item: `{item:?}`"),
3737
}
3838
}
3939

@@ -42,7 +42,7 @@ fn test_def_tys() -> ControlFlow<()> {
4242
// Type from foreign items.
4343
let ty = item.ty();
4444
let item_kind = item.kind();
45-
let name = item.name();
45+
let name = item.trimmed_name();
4646
match item_kind {
4747
ForeignItemKind::Fn(fn_def) => {
4848
assert_eq!(&name, "extern_fn");
@@ -54,7 +54,7 @@ fn test_def_tys() -> ControlFlow<()> {
5454
assert_eq!(ty, def.ty());
5555
assert!(ty.kind().is_integral())
5656
}
57-
_ => unreachable!("Unexpected kind: {item_kind:?}")
57+
_ => unreachable!("Unexpected kind: {item_kind:?}"),
5858
};
5959
}
6060

tests/ui-fulldeps/rustc_public/check_defs.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ extern crate rustc_driver;
1515
extern crate rustc_interface;
1616
extern crate rustc_public;
1717

18-
use std::assert_matches::assert_matches;
19-
use mir::{mono::Instance, TerminatorKind::*};
18+
use mir::{TerminatorKind::*, mono::Instance};
2019
use rustc_public::mir::mono::InstanceKind;
21-
use rustc_public::ty::{RigidTy, TyKind, Ty, UintTy};
20+
use rustc_public::ty::{RigidTy, Ty, TyKind, UintTy};
2221
use rustc_public::*;
22+
use std::assert_matches::assert_matches;
2323
use std::io::Write;
2424
use std::ops::ControlFlow;
2525

@@ -29,7 +29,7 @@ const CRATE_NAME: &str = "input";
2929
fn test_stable_mir() -> ControlFlow<()> {
3030
let entry = rustc_public::entry_fn().unwrap();
3131
let main_fn = Instance::try_from(entry).unwrap();
32-
assert_eq!(main_fn.name(), "main");
32+
assert_eq!(main_fn.name(), "input::main");
3333
assert_eq!(main_fn.trimmed_name(), "main");
3434

3535
let instances = get_instances(main_fn.body().unwrap());
@@ -65,10 +65,8 @@ fn test_fn(instance: Instance, expected_trimmed: &str, expected_qualified: &str,
6565

6666
fn extract_elem_ty(ty: Ty) -> Ty {
6767
match ty.kind() {
68-
TyKind::RigidTy(RigidTy::Adt(_, args)) => {
69-
*args.0[0].expect_ty()
70-
}
71-
_ => unreachable!("Expected Vec ADT, but found: {ty:?}")
68+
TyKind::RigidTy(RigidTy::Adt(_, args)) => *args.0[0].expect_ty(),
69+
_ => unreachable!("Expected Vec ADT, but found: {ty:?}"),
7270
}
7371
}
7472

@@ -89,19 +87,19 @@ fn test_vec_new(instance: mir::mono::Instance) {
8987

9088
/// Inspect the instance body
9189
fn get_instances(body: mir::Body) -> Vec<Instance> {
92-
body.blocks.iter().filter_map(|bb| {
93-
match &bb.terminator.kind {
90+
body.blocks
91+
.iter()
92+
.filter_map(|bb| match &bb.terminator.kind {
9493
Call { func, .. } => {
95-
let TyKind::RigidTy(ty) = func.ty(body.locals()).unwrap().kind() else { unreachable!
96-
() };
94+
let TyKind::RigidTy(ty) = func.ty(body.locals()).unwrap().kind() else {
95+
unreachable!()
96+
};
9797
let RigidTy::FnDef(def, args) = ty else { unreachable!() };
9898
Instance::resolve(def, &args).ok()
9999
}
100-
_ => {
101-
None
102-
}
103-
}
104-
}).collect::<Vec<_>>()
100+
_ => None,
101+
})
102+
.collect::<Vec<_>>()
105103
}
106104

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

0 commit comments

Comments
 (0)