|
| 1 | +// run-pass |
| 2 | +//! Test that users are able to use stable mir APIs to retrieve monomorphized types, and that |
| 3 | +//! we have an error handling for trying to instantiate types with incorrect arguments. |
| 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 | +extern crate rustc_middle; |
| 16 | +#[macro_use] |
| 17 | +extern crate rustc_smir; |
| 18 | +extern crate rustc_driver; |
| 19 | +extern crate rustc_interface; |
| 20 | +extern crate stable_mir; |
| 21 | + |
| 22 | +use rustc_middle::ty::TyCtxt; |
| 23 | +use rustc_smir::rustc_internal; |
| 24 | +use stable_mir::ty::{RigidTy, TyKind, Ty, }; |
| 25 | +use stable_mir::mir::{Body, MirVisitor, FieldIdx, Place, ProjectionElem, visit::{Location, |
| 26 | + PlaceContext}}; |
| 27 | +use std::io::Write; |
| 28 | +use std::ops::ControlFlow; |
| 29 | + |
| 30 | +const CRATE_NAME: &str = "input"; |
| 31 | + |
| 32 | +/// This function uses the Stable MIR APIs to get information about the test crate. |
| 33 | +fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> { |
| 34 | + let main_fn = stable_mir::entry_fn(); |
| 35 | + let body = main_fn.unwrap().body(); |
| 36 | + let mut visitor = PlaceVisitor{ body: &body, tested: false}; |
| 37 | + visitor.visit_body(&body); |
| 38 | + assert!(visitor.tested); |
| 39 | + ControlFlow::Continue(()) |
| 40 | +} |
| 41 | + |
| 42 | +struct PlaceVisitor<'a> { |
| 43 | + body: &'a Body, |
| 44 | + /// Used to ensure that the test was reachable. Otherwise this test would vacuously succeed. |
| 45 | + tested: bool, |
| 46 | +} |
| 47 | + |
| 48 | +/// Check that `wrapper.inner` place projection can be correctly interpreted. |
| 49 | +/// Ensure that instantiation is correct. |
| 50 | +fn check_tys(local_ty: Ty, idx: FieldIdx, expected_ty: Ty) { |
| 51 | + let TyKind::RigidTy(RigidTy::Adt(def, args)) = local_ty.kind() else { unreachable!() }; |
| 52 | + assert_eq!(def.ty_with_args(&args), local_ty); |
| 53 | + |
| 54 | + let field_def = &def.variants_iter().next().unwrap().fields()[idx]; |
| 55 | + let field_ty = field_def.ty_with_args(&args); |
| 56 | + assert_eq!(field_ty, expected_ty); |
| 57 | + |
| 58 | + // Check that the generic version is different than the instantiated one. |
| 59 | + let field_ty_gen = field_def.ty(); |
| 60 | + assert_ne!(field_ty_gen, field_ty); |
| 61 | +} |
| 62 | + |
| 63 | +impl<'a> MirVisitor for PlaceVisitor<'a> { |
| 64 | + fn visit_place(&mut self, place: &Place, _ptx: PlaceContext, _loc: Location) { |
| 65 | + let start_ty = self.body.locals()[place.local].ty; |
| 66 | + match place.projection.as_slice() { |
| 67 | + [ProjectionElem::Field(idx, ty)] => { |
| 68 | + check_tys(start_ty, *idx, *ty); |
| 69 | + self.tested = true; |
| 70 | + } |
| 71 | + _ => {} |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// This test will generate and analyze a dummy crate using the stable mir. |
| 77 | +/// For that, it will first write the dummy crate into a file. |
| 78 | +/// Then it will create a `StableMir` using custom arguments and then |
| 79 | +/// it will run the compiler. |
| 80 | +fn main() { |
| 81 | + let path = "ty_fold_input.rs"; |
| 82 | + generate_input(&path).unwrap(); |
| 83 | + let args = vec![ |
| 84 | + "rustc".to_string(), |
| 85 | + "-Cpanic=abort".to_string(), |
| 86 | + "--crate-name".to_string(), |
| 87 | + CRATE_NAME.to_string(), |
| 88 | + path.to_string(), |
| 89 | + ]; |
| 90 | + run!(args, tcx, test_stable_mir(tcx)).unwrap(); |
| 91 | +} |
| 92 | + |
| 93 | +fn generate_input(path: &str) -> std::io::Result<()> { |
| 94 | + let mut file = std::fs::File::create(path)?; |
| 95 | + write!( |
| 96 | + file, |
| 97 | + r#" |
| 98 | + struct Wrapper<T: Default> {{ |
| 99 | + pub inner: T |
| 100 | + }} |
| 101 | +
|
| 102 | + impl<T: Default> Wrapper<T> {{ |
| 103 | + pub fn new() -> Wrapper<T> {{ |
| 104 | + Wrapper {{ inner: T::default() }} |
| 105 | + }} |
| 106 | + }} |
| 107 | +
|
| 108 | + fn main() {{ |
| 109 | + let wrapper = Wrapper::<u8>::new(); |
| 110 | + let _inner = wrapper.inner; |
| 111 | + }} |
| 112 | + "# |
| 113 | + )?; |
| 114 | + Ok(()) |
| 115 | +} |
0 commit comments