Skip to content

Commit 7ab38b8

Browse files
committed
Add ItemKind::Ctor to stable mir
1 parent 48c7cc2 commit 7ab38b8

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10-
use rustc_hir::def::{CtorKind, DefKind};
10+
use rustc_hir::def::DefKind;
1111
use rustc_middle::mir;
1212
use rustc_middle::mir::interpret::AllocId;
1313
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
1414
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

@@ -91,15 +91,13 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
9191
| DefKind::GlobalAsm => {
9292
unreachable!("Not a valid item kind: {kind:?}");
9393
}
94-
DefKind::Ctor(_, CtorKind::Fn) | DefKind::Closure | DefKind::AssocFn | DefKind::Fn => {
95-
ItemKind::Fn
94+
DefKind::Closure | DefKind::AssocFn | DefKind::Fn => ItemKind::Fn,
95+
DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => {
96+
ItemKind::Const
9697
}
97-
DefKind::Ctor(_, CtorKind::Const)
98-
| DefKind::Const
99-
| DefKind::InlineConst
100-
| DefKind::AssocConst
101-
| DefKind::AnonConst => ItemKind::Const,
10298
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),
103101
}
104102
}
105103

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;

tests/ui-fulldeps/stable-mir/check_item_kind.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ const CRATE_NAME: &str = "input";
2929
/// This function uses the Stable MIR APIs to get information about the test crate.
3030
fn test_item_kind(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
3131
let items = stable_mir::all_local_items();
32-
assert_eq!(items.len(), 3);
32+
assert_eq!(items.len(), 4);
3333
// Constructor item.
3434
for item in items {
3535
let expected_kind = match item.name().as_str() {
36-
"Dummy" => ItemKind::Fn,
36+
"Dummy" => ItemKind::Ctor(CtorKind::Fn),
3737
"dummy" => ItemKind::Fn,
38+
"unit" => ItemKind::Fn,
3839
"DUMMY_CONST" => ItemKind::Const,
3940
name => unreachable!("Unexpected item {name}"),
4041
};
@@ -68,10 +69,15 @@ fn generate_input(path: &str) -> std::io::Result<()> {
6869
r#"
6970
pub struct Dummy(u32);
7071
pub const DUMMY_CONST: Dummy = Dummy(0);
72+
pub struct DummyUnit;
7173
7274
pub fn dummy() -> Dummy {{
7375
Dummy(5)
7476
}}
77+
78+
pub fn unit() -> DummyUnit {{
79+
DummyUnit
80+
}}
7581
"#
7682
)?;
7783
Ok(())

0 commit comments

Comments
 (0)