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.
This commit is contained in:
Matthias Krüger 2023-12-20 21:18:58 +01:00 committed by GitHub
commit b55887dc7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 2 deletions

View File

@ -15,7 +15,7 @@
use stable_mir::abi::Layout; use stable_mir::abi::Layout;
use stable_mir::mir::mono::InstanceDef; use stable_mir::mir::mono::InstanceDef;
use stable_mir::ty::{ConstId, Span}; use stable_mir::ty::{ConstId, Span};
use stable_mir::ItemKind; use stable_mir::{CtorKind, ItemKind};
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
use tracing::debug; use tracing::debug;
@ -88,7 +88,6 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
| DefKind::Field | DefKind::Field
| DefKind::LifetimeParam | DefKind::LifetimeParam
| DefKind::Impl { .. } | DefKind::Impl { .. }
| DefKind::Ctor(_, _)
| DefKind::GlobalAsm => { | DefKind::GlobalAsm => {
unreachable!("Not a valid item kind: {kind:?}"); unreachable!("Not a valid item kind: {kind:?}");
} }
@ -97,6 +96,8 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
ItemKind::Const ItemKind::Const
} }
DefKind::Static(_) => ItemKind::Static, DefKind::Static(_) => ItemKind::Static,
DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const),
DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn),
} }
} }

View File

@ -91,6 +91,13 @@ pub enum ItemKind {
Fn, Fn,
Static, Static,
Const, Const,
Ctor(CtorKind),
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum CtorKind {
Const,
Fn,
} }
pub type Filename = String; pub type Filename = String;

View File

@ -0,0 +1,84 @@
// run-pass
//! Test that item kind works as expected.
// ignore-stage1
// ignore-cross-compile
// ignore-remote
// ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
// edition: 2021
#![feature(rustc_private)]
#![feature(assert_matches)]
#![feature(control_flow_enum)]
extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
extern crate rustc_interface;
extern crate stable_mir;
use rustc_middle::ty::TyCtxt;
use rustc_smir::rustc_internal;
use stable_mir::*;
use std::io::Write;
use std::ops::ControlFlow;
const CRATE_NAME: &str = "input";
/// This function uses the Stable MIR APIs to get information about the test crate.
fn test_item_kind(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
let items = stable_mir::all_local_items();
assert_eq!(items.len(), 4);
// Constructor item.
for item in items {
let expected_kind = match item.name().as_str() {
"Dummy" => ItemKind::Ctor(CtorKind::Fn),
"dummy" => ItemKind::Fn,
"unit" => ItemKind::Fn,
"DUMMY_CONST" => ItemKind::Const,
name => unreachable!("Unexpected item {name}"),
};
assert_eq!(item.kind(), expected_kind, "Mismatched type for {}", item.name());
}
ControlFlow::Continue(())
}
/// This test will generate and analyze a dummy crate using the stable mir.
/// For that, it will first write the dummy crate into a file.
/// Then it will create a `StableMir` using custom arguments and then
/// it will run the compiler.
fn main() {
let path = "item_kind_input.rs";
generate_input(&path).unwrap();
let args = vec![
"rustc".to_string(),
"-Cpanic=abort".to_string(),
"--crate-type=lib".to_string(),
"--crate-name".to_string(),
CRATE_NAME.to_string(),
path.to_string(),
];
run!(args, tcx, test_item_kind(tcx)).unwrap();
}
fn generate_input(path: &str) -> std::io::Result<()> {
let mut file = std::fs::File::create(path)?;
write!(
file,
r#"
pub struct Dummy(u32);
pub const DUMMY_CONST: Dummy = Dummy(0);
pub struct DummyUnit;
pub fn dummy() -> Dummy {{
Dummy(5)
}}
pub fn unit() -> DummyUnit {{
DummyUnit
}}
"#
)?;
Ok(())
}