Skip to content

Commit 79ff316

Browse files
committed
Add ItemKind::Ctor to stable mir
1 parent 91bfd14 commit 79ff316

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,14 +7,14 @@
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::mir::mono::InstanceDef;
1616
use stable_mir::ty::{ConstId, Span};
17-
use stable_mir::ItemKind;
17+
use stable_mir::{CtorKind, ItemKind};
1818
use tracing::debug;
1919

2020
use crate::rustc_internal::IndexMap;
@@ -88,15 +88,13 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
8888
| DefKind::GlobalAsm => {
8989
unreachable!("Not a valid item kind: {kind:?}");
9090
}
91-
DefKind::Ctor(_, CtorKind::Fn) | DefKind::Closure | DefKind::AssocFn | DefKind::Fn => {
92-
ItemKind::Fn
91+
DefKind::Closure | DefKind::AssocFn | DefKind::Fn => ItemKind::Fn,
92+
DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => {
93+
ItemKind::Const
9394
}
94-
DefKind::Ctor(_, CtorKind::Const)
95-
| DefKind::Const
96-
| DefKind::InlineConst
97-
| DefKind::AssocConst
98-
| DefKind::AnonConst => ItemKind::Const,
9995
DefKind::Static(_) => ItemKind::Static,
96+
DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const),
97+
DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn),
10098
}
10199
}
102100

compiler/stable_mir/src/lib.rs

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

95102
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)