Skip to content

Commit ae86f59

Browse files
committed
Add test and remove double ref
1 parent 421631a commit ae86f59

File tree

3 files changed

+85
-18
lines changed

3 files changed

+85
-18
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ scoped_thread_local! (static TLV: Cell<*const ()>);
142142

143143
pub(crate) fn init<'tcx>(tables: &TablesWrapper<'tcx>, f: impl FnOnce()) {
144144
assert!(!TLV.is_set());
145-
let ptr: *const () = &tables as *const &_ as _;
145+
let ptr = tables as *const _ as *const ();
146146
TLV.set(&Cell::new(ptr), || {
147147
f();
148148
});
@@ -155,8 +155,8 @@ pub(crate) fn with_tables<'tcx, R>(f: impl FnOnce(&mut Tables<'tcx>) -> R) -> R
155155
TLV.with(|tlv| {
156156
let ptr = tlv.get();
157157
assert!(!ptr.is_null());
158-
let wrapper = unsafe { *(ptr as *const &TablesWrapper<'tcx>) };
159-
let mut tables = wrapper.0.borrow_mut();
158+
let wrapper = ptr as *const TablesWrapper<'tcx>;
159+
let mut tables = unsafe { (*wrapper).0.borrow_mut() };
160160
f(&mut *tables)
161161
})
162162
}

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

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-pass
2-
// Test that users are able to use stable mir APIs to retrieve monomorphized instances
2+
//! Test that users are able to use stable mir APIs to retrieve monomorphized instances
33
44
// ignore-stage1
55
// ignore-cross-compile
@@ -14,15 +14,15 @@
1414
extern crate rustc_middle;
1515
#[macro_use]
1616
extern crate rustc_smir;
17-
extern crate stable_mir;
1817
extern crate rustc_driver;
1918
extern crate rustc_interface;
19+
extern crate stable_mir;
2020

21-
use rustc_middle::ty::TyCtxt;
2221
use mir::{mono::Instance, TerminatorKind::*};
23-
use stable_mir::ty::{TyKind, RigidTy};
24-
use stable_mir::*;
22+
use rustc_middle::ty::TyCtxt;
2523
use rustc_smir::rustc_internal;
24+
use stable_mir::ty::{RigidTy, TyKind};
25+
use stable_mir::*;
2626
use std::io::Write;
2727
use std::ops::ControlFlow;
2828

@@ -33,16 +33,16 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
3333
let items = stable_mir::all_local_items();
3434

3535
// Get all items and split generic vs monomorphic items.
36-
let (generic, mono) : (Vec<_>, Vec<_>) = items.into_iter().partition(|item| {
37-
item.requires_monomorphization()
38-
});
36+
let (generic, mono): (Vec<_>, Vec<_>) =
37+
items.into_iter().partition(|item| item.requires_monomorphization());
3938
assert_eq!(mono.len(), 3, "Expected 2 mono functions and one constant");
4039
assert_eq!(generic.len(), 2, "Expected 2 generic functions");
4140

4241
// For all monomorphic items, get the correspondent instances.
43-
let instances = mono.iter().filter_map(|item| {
44-
mir::mono::Instance::try_from(*item).ok()
45-
}).collect::<Vec<mir::mono::Instance>>();
42+
let instances = mono
43+
.iter()
44+
.filter_map(|item| mir::mono::Instance::try_from(*item).ok())
45+
.collect::<Vec<mir::mono::Instance>>();
4646
assert_eq!(instances.len(), mono.len());
4747

4848
// For all generic items, try_from should fail.
@@ -58,19 +58,22 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
5858
fn test_body(body: mir::Body) {
5959
for term in body.blocks.iter().map(|bb| &bb.terminator) {
6060
match &term.kind {
61-
Call{ func, .. } => {
61+
Call { func, .. } => {
6262
let TyKind::RigidTy(ty) = func.ty(&body.locals).kind() else { unreachable!() };
6363
let RigidTy::FnDef(def, args) = ty else { unreachable!() };
6464
let result = Instance::resolve(def, &args);
6565
assert!(result.is_ok());
6666
}
67-
Goto {..} | Assert{..} | SwitchInt{..} | Return | Drop {..} => { /* Do nothing */}
68-
_ => { unreachable!("Unexpected terminator {term:?}") }
67+
Goto { .. } | Assert { .. } | SwitchInt { .. } | Return | Drop { .. } => {
68+
/* Do nothing */
69+
}
70+
_ => {
71+
unreachable!("Unexpected terminator {term:?}")
72+
}
6973
}
7074
}
7175
}
7276

73-
7477
/// This test will generate and analyze a dummy crate using the stable mir.
7578
/// For that, it will first write the dummy crate into a file.
7679
/// Then it will create a `StableMir` using custom arguments and then
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// run-pass
2+
//! Test that users are able to use retrieve internal constructs from stable ones to help with
3+
//! the migration.
4+
5+
// ignore-stage1
6+
// ignore-cross-compile
7+
// ignore-remote
8+
// ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
9+
// edition: 2021
10+
11+
#![feature(rustc_private)]
12+
#![feature(assert_matches)]
13+
#![feature(control_flow_enum)]
14+
15+
#[macro_use]
16+
extern crate rustc_smir;
17+
extern crate rustc_driver;
18+
extern crate rustc_interface;
19+
extern crate rustc_middle;
20+
extern crate stable_mir;
21+
22+
use rustc_middle::ty::TyCtxt;
23+
use rustc_smir::rustc_internal;
24+
use std::io::Write;
25+
use std::ops::ControlFlow;
26+
27+
const CRATE_NAME: &str = "input";
28+
29+
fn test_translation(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
30+
let main_fn = stable_mir::entry_fn().unwrap();
31+
let body = main_fn.body();
32+
let orig_ty = body.locals[0].ty;
33+
let rustc_ty = rustc_internal::internal(&orig_ty);
34+
assert!(rustc_ty.is_unit());
35+
ControlFlow::Continue(())
36+
}
37+
38+
/// This test will generate and analyze a dummy crate using the stable mir.
39+
/// For that, it will first write the dummy crate into a file.
40+
/// Then it will create a `StableMir` using custom arguments and then
41+
/// it will run the compiler.
42+
fn main() {
43+
let path = "internal_input.rs";
44+
generate_input(&path).unwrap();
45+
let args = vec![
46+
"rustc".to_string(),
47+
"--crate-name".to_string(),
48+
CRATE_NAME.to_string(),
49+
path.to_string(),
50+
];
51+
run!(args, tcx, test_translation(tcx)).unwrap();
52+
}
53+
54+
fn generate_input(path: &str) -> std::io::Result<()> {
55+
let mut file = std::fs::File::create(path)?;
56+
write!(
57+
file,
58+
r#"
59+
pub fn main() {{
60+
}}
61+
"#
62+
)?;
63+
Ok(())
64+
}

0 commit comments

Comments
 (0)