Add test and remove double ref

This commit is contained in:
Celina G. Val 2023-10-23 21:35:30 -07:00
parent 421631a3a1
commit ae86f59cc9
3 changed files with 85 additions and 18 deletions

View File

@ -142,7 +142,7 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
pub(crate) fn init<'tcx>(tables: &TablesWrapper<'tcx>, f: impl FnOnce()) { pub(crate) fn init<'tcx>(tables: &TablesWrapper<'tcx>, f: impl FnOnce()) {
assert!(!TLV.is_set()); assert!(!TLV.is_set());
let ptr: *const () = &tables as *const &_ as _; let ptr = tables as *const _ as *const ();
TLV.set(&Cell::new(ptr), || { TLV.set(&Cell::new(ptr), || {
f(); f();
}); });
@ -155,8 +155,8 @@ pub(crate) fn with_tables<'tcx, R>(f: impl FnOnce(&mut Tables<'tcx>) -> R) -> R
TLV.with(|tlv| { TLV.with(|tlv| {
let ptr = tlv.get(); let ptr = tlv.get();
assert!(!ptr.is_null()); assert!(!ptr.is_null());
let wrapper = unsafe { *(ptr as *const &TablesWrapper<'tcx>) }; let wrapper = ptr as *const TablesWrapper<'tcx>;
let mut tables = wrapper.0.borrow_mut(); let mut tables = unsafe { (*wrapper).0.borrow_mut() };
f(&mut *tables) f(&mut *tables)
}) })
} }

View File

@ -1,5 +1,5 @@
// run-pass // run-pass
// Test that users are able to use stable mir APIs to retrieve monomorphized instances //! Test that users are able to use stable mir APIs to retrieve monomorphized instances
// ignore-stage1 // ignore-stage1
// ignore-cross-compile // ignore-cross-compile
@ -14,15 +14,15 @@
extern crate rustc_middle; extern crate rustc_middle;
#[macro_use] #[macro_use]
extern crate rustc_smir; extern crate rustc_smir;
extern crate stable_mir;
extern crate rustc_driver; extern crate rustc_driver;
extern crate rustc_interface; extern crate rustc_interface;
extern crate stable_mir;
use rustc_middle::ty::TyCtxt;
use mir::{mono::Instance, TerminatorKind::*}; use mir::{mono::Instance, TerminatorKind::*};
use stable_mir::ty::{TyKind, RigidTy}; use rustc_middle::ty::TyCtxt;
use stable_mir::*;
use rustc_smir::rustc_internal; use rustc_smir::rustc_internal;
use stable_mir::ty::{RigidTy, TyKind};
use stable_mir::*;
use std::io::Write; use std::io::Write;
use std::ops::ControlFlow; use std::ops::ControlFlow;
@ -33,16 +33,16 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
let items = stable_mir::all_local_items(); let items = stable_mir::all_local_items();
// Get all items and split generic vs monomorphic items. // Get all items and split generic vs monomorphic items.
let (generic, mono) : (Vec<_>, Vec<_>) = items.into_iter().partition(|item| { let (generic, mono): (Vec<_>, Vec<_>) =
item.requires_monomorphization() items.into_iter().partition(|item| item.requires_monomorphization());
});
assert_eq!(mono.len(), 3, "Expected 2 mono functions and one constant"); assert_eq!(mono.len(), 3, "Expected 2 mono functions and one constant");
assert_eq!(generic.len(), 2, "Expected 2 generic functions"); assert_eq!(generic.len(), 2, "Expected 2 generic functions");
// For all monomorphic items, get the correspondent instances. // For all monomorphic items, get the correspondent instances.
let instances = mono.iter().filter_map(|item| { let instances = mono
mir::mono::Instance::try_from(*item).ok() .iter()
}).collect::<Vec<mir::mono::Instance>>(); .filter_map(|item| mir::mono::Instance::try_from(*item).ok())
.collect::<Vec<mir::mono::Instance>>();
assert_eq!(instances.len(), mono.len()); assert_eq!(instances.len(), mono.len());
// For all generic items, try_from should fail. // For all generic items, try_from should fail.
@ -64,12 +64,15 @@ fn test_body(body: mir::Body) {
let result = Instance::resolve(def, &args); let result = Instance::resolve(def, &args);
assert!(result.is_ok()); assert!(result.is_ok());
} }
Goto {..} | Assert{..} | SwitchInt{..} | Return | Drop {..} => { /* Do nothing */} Goto { .. } | Assert { .. } | SwitchInt { .. } | Return | Drop { .. } => {
_ => { unreachable!("Unexpected terminator {term:?}") } /* Do nothing */
}
_ => {
unreachable!("Unexpected terminator {term:?}")
}
} }
} }
} }
/// This test will generate and analyze a dummy crate using the stable mir. /// 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. /// For that, it will first write the dummy crate into a file.

View File

@ -0,0 +1,64 @@
// run-pass
//! Test that users are able to use retrieve internal constructs from stable ones to help with
//! the migration.
// 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)]
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
extern crate rustc_interface;
extern crate rustc_middle;
extern crate stable_mir;
use rustc_middle::ty::TyCtxt;
use rustc_smir::rustc_internal;
use std::io::Write;
use std::ops::ControlFlow;
const CRATE_NAME: &str = "input";
fn test_translation(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
let main_fn = stable_mir::entry_fn().unwrap();
let body = main_fn.body();
let orig_ty = body.locals[0].ty;
let rustc_ty = rustc_internal::internal(&orig_ty);
assert!(rustc_ty.is_unit());
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 = "internal_input.rs";
generate_input(&path).unwrap();
let args = vec![
"rustc".to_string(),
"--crate-name".to_string(),
CRATE_NAME.to_string(),
path.to_string(),
];
run!(args, tcx, test_translation(tcx)).unwrap();
}
fn generate_input(path: &str) -> std::io::Result<()> {
let mut file = std::fs::File::create(path)?;
write!(
file,
r#"
pub fn main() {{
}}
"#
)?;
Ok(())
}