| 
 | 1 | +//@ run-pass  | 
 | 2 | +//! Test that users are able to use stable mir APIs to retrieve  | 
 | 3 | +//! discriminant value and type for AdtDef and Coroutine variants  | 
 | 4 | +
  | 
 | 5 | +//@ ignore-stage1  | 
 | 6 | +//@ ignore-cross-compile  | 
 | 7 | +//@ ignore-remote  | 
 | 8 | +//@ edition: 2024  | 
 | 9 | + | 
 | 10 | +#![feature(rustc_private)]  | 
 | 11 | +#![feature(assert_matches)]  | 
 | 12 | + | 
 | 13 | +extern crate rustc_middle;  | 
 | 14 | +#[macro_use]  | 
 | 15 | +extern crate rustc_smir;  | 
 | 16 | +extern crate rustc_driver;  | 
 | 17 | +extern crate rustc_interface;  | 
 | 18 | +extern crate stable_mir;  | 
 | 19 | + | 
 | 20 | +use std::io::Write;  | 
 | 21 | +use std::ops::ControlFlow;  | 
 | 22 | + | 
 | 23 | +use stable_mir::CrateItem;  | 
 | 24 | +use stable_mir::crate_def::CrateDef;  | 
 | 25 | +use stable_mir::mir::{AggregateKind, Rvalue, Statement, StatementKind};  | 
 | 26 | +use stable_mir::ty::{IntTy, RigidTy, Ty};  | 
 | 27 | + | 
 | 28 | +const CRATE_NAME: &str = "crate_variant_ty";  | 
 | 29 | + | 
 | 30 | +/// Test if we can retrieve discriminant info for different types.  | 
 | 31 | +fn test_def_tys() -> ControlFlow<()> {  | 
 | 32 | +    check_adt_mono();  | 
 | 33 | +    check_adt_poly();  | 
 | 34 | +    check_adt_poly2();  | 
 | 35 | + | 
 | 36 | +    ControlFlow::Continue(())  | 
 | 37 | +}  | 
 | 38 | + | 
 | 39 | +fn check_adt_mono() {  | 
 | 40 | +    let mono = get_fn("mono").expect_body();  | 
 | 41 | + | 
 | 42 | +    check_statement_is_aggregate_assign(  | 
 | 43 | +        &mono.blocks[0].statements[0],  | 
 | 44 | +        0,  | 
 | 45 | +        RigidTy::Int(IntTy::Isize),  | 
 | 46 | +    );  | 
 | 47 | +    check_statement_is_aggregate_assign(  | 
 | 48 | +        &mono.blocks[1].statements[0],  | 
 | 49 | +        1,  | 
 | 50 | +        RigidTy::Int(IntTy::Isize),  | 
 | 51 | +    );  | 
 | 52 | +    check_statement_is_aggregate_assign(  | 
 | 53 | +        &mono.blocks[2].statements[0],  | 
 | 54 | +        2,  | 
 | 55 | +        RigidTy::Int(IntTy::Isize),  | 
 | 56 | +    );  | 
 | 57 | +}  | 
 | 58 | + | 
 | 59 | +fn check_adt_poly() {  | 
 | 60 | +    let poly = get_fn("poly").expect_body();  | 
 | 61 | + | 
 | 62 | +    check_statement_is_aggregate_assign(  | 
 | 63 | +        &poly.blocks[0].statements[0],  | 
 | 64 | +        0,  | 
 | 65 | +        RigidTy::Int(IntTy::Isize),  | 
 | 66 | +    );  | 
 | 67 | +    check_statement_is_aggregate_assign(  | 
 | 68 | +        &poly.blocks[1].statements[0],  | 
 | 69 | +        1,  | 
 | 70 | +        RigidTy::Int(IntTy::Isize),  | 
 | 71 | +    );  | 
 | 72 | +    check_statement_is_aggregate_assign(  | 
 | 73 | +        &poly.blocks[2].statements[0],  | 
 | 74 | +        2,  | 
 | 75 | +        RigidTy::Int(IntTy::Isize),  | 
 | 76 | +    );  | 
 | 77 | +}  | 
 | 78 | + | 
 | 79 | +fn check_adt_poly2() {  | 
 | 80 | +    let poly = get_fn("poly2").expect_body();  | 
 | 81 | + | 
 | 82 | +    check_statement_is_aggregate_assign(  | 
 | 83 | +        &poly.blocks[0].statements[0],  | 
 | 84 | +        0,  | 
 | 85 | +        RigidTy::Int(IntTy::Isize),  | 
 | 86 | +    );  | 
 | 87 | +    check_statement_is_aggregate_assign(  | 
 | 88 | +        &poly.blocks[1].statements[0],  | 
 | 89 | +        1,  | 
 | 90 | +        RigidTy::Int(IntTy::Isize),  | 
 | 91 | +    );  | 
 | 92 | +    check_statement_is_aggregate_assign(  | 
 | 93 | +        &poly.blocks[2].statements[0],  | 
 | 94 | +        2,  | 
 | 95 | +        RigidTy::Int(IntTy::Isize),  | 
 | 96 | +    );  | 
 | 97 | +}  | 
 | 98 | + | 
 | 99 | +fn get_fn(name: &str) -> CrateItem {  | 
 | 100 | +    stable_mir::all_local_items().into_iter().find(|it| it.name().eq(name)).unwrap()  | 
 | 101 | +}  | 
 | 102 | + | 
 | 103 | +fn check_statement_is_aggregate_assign(  | 
 | 104 | +    statement: &Statement,  | 
 | 105 | +    expected_discr_val: u128,  | 
 | 106 | +    expected_discr_ty: RigidTy,  | 
 | 107 | +) {  | 
 | 108 | +    if let Statement { kind: StatementKind::Assign(_, rvalue), .. } = statement  | 
 | 109 | +        && let Rvalue::Aggregate(aggregate, _) = rvalue  | 
 | 110 | +        && let AggregateKind::Adt(adt_def, variant_idx, ..) = aggregate  | 
 | 111 | +    {  | 
 | 112 | +        let discr = adt_def.discriminant_for_variant(*variant_idx);  | 
 | 113 | + | 
 | 114 | +        assert_eq!(discr.val, expected_discr_val);  | 
 | 115 | +        assert_eq!(discr.ty, Ty::from_rigid_kind(expected_discr_ty));  | 
 | 116 | +    } else {  | 
 | 117 | +        unreachable!("Unexpected statement");  | 
 | 118 | +    }  | 
 | 119 | +}  | 
 | 120 | + | 
 | 121 | +/// This test will generate and analyze a dummy crate using the stable mir.  | 
 | 122 | +/// For that, it will first write the dummy crate into a file.  | 
 | 123 | +/// Then it will create a `StableMir` using custom arguments and then  | 
 | 124 | +/// it will run the compiler.  | 
 | 125 | +fn main() {  | 
 | 126 | +    let path = "defs_ty_input.rs";  | 
 | 127 | +    generate_input(&path).unwrap();  | 
 | 128 | +    let args = &[  | 
 | 129 | +        "rustc".to_string(),  | 
 | 130 | +        "-Cpanic=abort".to_string(),  | 
 | 131 | +        "--crate-name".to_string(),  | 
 | 132 | +        CRATE_NAME.to_string(),  | 
 | 133 | +        path.to_string(),  | 
 | 134 | +    ];  | 
 | 135 | +    run!(args, test_def_tys).unwrap();  | 
 | 136 | +}  | 
 | 137 | + | 
 | 138 | +fn generate_input(path: &str) -> std::io::Result<()> {  | 
 | 139 | +    let mut file = std::fs::File::create(path)?;  | 
 | 140 | +    write!(  | 
 | 141 | +        file,  | 
 | 142 | +        r#"  | 
 | 143 | +        use std::hint::black_box;  | 
 | 144 | +
  | 
 | 145 | +        enum Mono {{  | 
 | 146 | +            A,  | 
 | 147 | +            B(i32),  | 
 | 148 | +            C {{ a: i32, b: u32 }},  | 
 | 149 | +        }}  | 
 | 150 | +
  | 
 | 151 | +        enum Poly<T> {{  | 
 | 152 | +            A,  | 
 | 153 | +            B(T),  | 
 | 154 | +            C {{ t: T }},  | 
 | 155 | +        }}  | 
 | 156 | +
  | 
 | 157 | +        pub fn main() {{  | 
 | 158 | +            mono();  | 
 | 159 | +            poly();  | 
 | 160 | +            poly2::<i32>(1);  | 
 | 161 | +        }}  | 
 | 162 | +
  | 
 | 163 | +        fn mono() {{  | 
 | 164 | +            black_box(Mono::A);  | 
 | 165 | +            black_box(Mono::B(6));  | 
 | 166 | +            black_box(Mono::C {{a: 1, b: 10 }});  | 
 | 167 | +        }}  | 
 | 168 | +
  | 
 | 169 | +        fn poly() {{  | 
 | 170 | +            black_box(Poly::<i32>::A);  | 
 | 171 | +            black_box(Poly::B(1i32));  | 
 | 172 | +            black_box(Poly::C {{ t: 1i32 }});  | 
 | 173 | +        }}  | 
 | 174 | +
  | 
 | 175 | +        fn poly2<T: Copy>(t: T) {{  | 
 | 176 | +            black_box(Poly::<T>::A);  | 
 | 177 | +            black_box(Poly::B(t));  | 
 | 178 | +            black_box(Poly::C {{ t: t }});  | 
 | 179 | +        }}  | 
 | 180 | +    "#  | 
 | 181 | +    )?;  | 
 | 182 | +    Ok(())  | 
 | 183 | +}  | 
0 commit comments