Skip to content

Commit b55887d

Browse files
authored
Rollup merge of #119135 - celinval:smir-small-changes, r=compiler-errors
Fix crash due to `CrateItem::kind()` not handling constructors Also add a method to get the instance instantiation arguments, and include that information in the instance debug.
2 parents 5ac4c8a + 7ab38b8 commit b55887d

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1515
use stable_mir::abi::Layout;
1616
use stable_mir::mir::mono::InstanceDef;
1717
use stable_mir::ty::{ConstId, Span};
18-
use stable_mir::ItemKind;
18+
use stable_mir::{CtorKind, ItemKind};
1919
use std::ops::RangeInclusive;
2020
use tracing::debug;
2121

@@ -88,7 +88,6 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
8888
| DefKind::Field
8989
| DefKind::LifetimeParam
9090
| DefKind::Impl { .. }
91-
| DefKind::Ctor(_, _)
9291
| DefKind::GlobalAsm => {
9392
unreachable!("Not a valid item kind: {kind:?}");
9493
}
@@ -97,6 +96,8 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
9796
ItemKind::Const
9897
}
9998
DefKind::Static(_) => ItemKind::Static,
99+
DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const),
100+
DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn),
100101
}
101102
}
102103

compiler/stable_mir/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ pub enum ItemKind {
9191
Fn,
9292
Static,
9393
Const,
94+
Ctor(CtorKind),
95+
}
96+
97+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
98+
pub enum CtorKind {
99+
Const,
100+
Fn,
94101
}
95102

96103
pub type Filename = String;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// run-pass
2+
//! Test that item kind works as expected.
3+
4+
// ignore-stage1
5+
// ignore-cross-compile
6+
// ignore-remote
7+
// ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
8+
// edition: 2021
9+
10+
#![feature(rustc_private)]
11+
#![feature(assert_matches)]
12+
#![feature(control_flow_enum)]
13+
14+
extern crate rustc_middle;
15+
#[macro_use]
16+
extern crate rustc_smir;
17+
extern crate rustc_driver;
18+
extern crate rustc_interface;
19+
extern crate stable_mir;
20+
21+
use rustc_middle::ty::TyCtxt;
22+
use rustc_smir::rustc_internal;
23+
use stable_mir::*;
24+
use std::io::Write;
25+
use std::ops::ControlFlow;
26+
27+
const CRATE_NAME: &str = "input";
28+
29+
/// This function uses the Stable MIR APIs to get information about the test crate.
30+
fn test_item_kind(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
31+
let items = stable_mir::all_local_items();
32+
assert_eq!(items.len(), 4);
33+
// Constructor item.
34+
for item in items {
35+
let expected_kind = match item.name().as_str() {
36+
"Dummy" => ItemKind::Ctor(CtorKind::Fn),
37+
"dummy" => ItemKind::Fn,
38+
"unit" => ItemKind::Fn,
39+
"DUMMY_CONST" => ItemKind::Const,
40+
name => unreachable!("Unexpected item {name}"),
41+
};
42+
assert_eq!(item.kind(), expected_kind, "Mismatched type for {}", item.name());
43+
}
44+
ControlFlow::Continue(())
45+
}
46+
47+
/// This test will generate and analyze a dummy crate using the stable mir.
48+
/// For that, it will first write the dummy crate into a file.
49+
/// Then it will create a `StableMir` using custom arguments and then
50+
/// it will run the compiler.
51+
fn main() {
52+
let path = "item_kind_input.rs";
53+
generate_input(&path).unwrap();
54+
let args = vec![
55+
"rustc".to_string(),
56+
"-Cpanic=abort".to_string(),
57+
"--crate-type=lib".to_string(),
58+
"--crate-name".to_string(),
59+
CRATE_NAME.to_string(),
60+
path.to_string(),
61+
];
62+
run!(args, tcx, test_item_kind(tcx)).unwrap();
63+
}
64+
65+
fn generate_input(path: &str) -> std::io::Result<()> {
66+
let mut file = std::fs::File::create(path)?;
67+
write!(
68+
file,
69+
r#"
70+
pub struct Dummy(u32);
71+
pub const DUMMY_CONST: Dummy = Dummy(0);
72+
pub struct DummyUnit;
73+
74+
pub fn dummy() -> Dummy {{
75+
Dummy(5)
76+
}}
77+
78+
pub fn unit() -> DummyUnit {{
79+
DummyUnit
80+
}}
81+
"#
82+
)?;
83+
Ok(())
84+
}

0 commit comments

Comments
 (0)