|
| 1 | +//@ run-pass |
| 2 | +//! Test information regarding binary operations. |
| 3 | +
|
| 4 | +//@ ignore-stage1 |
| 5 | +//@ ignore-cross-compile |
| 6 | +//@ ignore-remote |
| 7 | +//@ ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837 |
| 8 | + |
| 9 | +#![feature(rustc_private)] |
| 10 | + |
| 11 | +extern crate rustc_hir; |
| 12 | +#[macro_use] |
| 13 | +extern crate rustc_smir; |
| 14 | +extern crate rustc_driver; |
| 15 | +extern crate rustc_interface; |
| 16 | +extern crate stable_mir; |
| 17 | + |
| 18 | +use rustc_smir::rustc_internal; |
| 19 | +use stable_mir::mir::mono::Instance; |
| 20 | +use stable_mir::mir::visit::{Location, MirVisitor}; |
| 21 | +use stable_mir::mir::{LocalDecl, Rvalue, Statement, StatementKind, Terminator, TerminatorKind}; |
| 22 | +use stable_mir::ty::{RigidTy, TyKind}; |
| 23 | +use std::collections::HashSet; |
| 24 | +use std::convert::TryFrom; |
| 25 | +use std::io::Write; |
| 26 | +use std::ops::ControlFlow; |
| 27 | + |
| 28 | +/// This function tests that we can correctly get type information from binary operations. |
| 29 | +fn test_binops() -> ControlFlow<()> { |
| 30 | + // Find items in the local crate. |
| 31 | + let items = stable_mir::all_local_items(); |
| 32 | + let mut instances = |
| 33 | + items.into_iter().map(|item| Instance::try_from(item).unwrap()).collect::<Vec<_>>(); |
| 34 | + while let Some(instance) = instances.pop() { |
| 35 | + // The test below shouldn't have recursion in it. |
| 36 | + let Some(body) = instance.body() else { |
| 37 | + continue; |
| 38 | + }; |
| 39 | + let mut visitor = Visitor { locals: body.locals(), calls: Default::default() }; |
| 40 | + visitor.visit_body(&body); |
| 41 | + instances.extend(visitor.calls.into_iter()); |
| 42 | + } |
| 43 | + ControlFlow::Continue(()) |
| 44 | +} |
| 45 | + |
| 46 | +struct Visitor<'a> { |
| 47 | + locals: &'a [LocalDecl], |
| 48 | + calls: HashSet<Instance>, |
| 49 | +} |
| 50 | + |
| 51 | +impl<'a> MirVisitor for Visitor<'a> { |
| 52 | + fn visit_statement(&mut self, stmt: &Statement, _loc: Location) { |
| 53 | + match &stmt.kind { |
| 54 | + StatementKind::Assign(place, Rvalue::BinaryOp(op, rhs, lhs)) => { |
| 55 | + let ret_ty = place.ty(self.locals).unwrap(); |
| 56 | + let op_ty = op.ty(rhs.ty(self.locals).unwrap(), lhs.ty(self.locals).unwrap()); |
| 57 | + assert_eq!(ret_ty, op_ty, "Operation type should match the assigned place type"); |
| 58 | + } |
| 59 | + _ => {} |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + fn visit_terminator(&mut self, term: &Terminator, _loc: Location) { |
| 64 | + match &term.kind { |
| 65 | + TerminatorKind::Call { func, .. } => { |
| 66 | + let TyKind::RigidTy(RigidTy::FnDef(def, args)) = |
| 67 | + func.ty(self.locals).unwrap().kind() |
| 68 | + else { |
| 69 | + return; |
| 70 | + }; |
| 71 | + self.calls.insert(Instance::resolve(def, &args).unwrap()); |
| 72 | + } |
| 73 | + _ => {} |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// This test will generate and analyze a dummy crate using the stable mir. |
| 79 | +/// For that, it will first write the dummy crate into a file. |
| 80 | +/// Then it will create a `StableMir` using custom arguments and then |
| 81 | +/// it will run the compiler. |
| 82 | +fn main() { |
| 83 | + let path = "binop_input.rs"; |
| 84 | + generate_input(&path).unwrap(); |
| 85 | + let args = vec!["rustc".to_string(), "--crate-type=lib".to_string(), path.to_string()]; |
| 86 | + run!(args, test_binops).unwrap(); |
| 87 | +} |
| 88 | + |
| 89 | +fn generate_input(path: &str) -> std::io::Result<()> { |
| 90 | + let mut file = std::fs::File::create(path)?; |
| 91 | + write!( |
| 92 | + file, |
| 93 | + r#" |
| 94 | + macro_rules! binop_int {{ |
| 95 | + ($fn:ident, $typ:ty) => {{ |
| 96 | + pub fn $fn(lhs: $typ, rhs: $typ) {{ |
| 97 | + let eq = lhs == rhs; |
| 98 | + let lt = lhs < rhs; |
| 99 | + let le = lhs <= rhs; |
| 100 | +
|
| 101 | + let sum = lhs + rhs; |
| 102 | + let mult = lhs * sum; |
| 103 | + let shift = mult << 2; |
| 104 | + let bit_or = shift | rhs; |
| 105 | + let cmp = lhs.cmp(&bit_or); |
| 106 | +
|
| 107 | + // Try to avoid the results above being pruned |
| 108 | + std::hint::black_box(((eq, lt, le), cmp)); |
| 109 | + }} |
| 110 | + }} |
| 111 | + }} |
| 112 | +
|
| 113 | + binop_int!(binop_u8, u8); |
| 114 | + binop_int!(binop_i64, i64); |
| 115 | +
|
| 116 | + pub fn binop_bool(lhs: bool, rhs: bool) {{ |
| 117 | + let eq = lhs == rhs; |
| 118 | + let or = lhs | eq; |
| 119 | + let lt = lhs < or; |
| 120 | + let cmp = lhs.cmp(&rhs); |
| 121 | +
|
| 122 | + // Try to avoid the results above being pruned |
| 123 | + std::hint::black_box((lt, cmp)); |
| 124 | + }} |
| 125 | +
|
| 126 | + pub fn binop_char(lhs: char, rhs: char) {{ |
| 127 | + let eq = lhs == rhs; |
| 128 | + let lt = lhs < rhs; |
| 129 | + let cmp = lhs.cmp(&rhs); |
| 130 | +
|
| 131 | + // Try to avoid the results above being pruned |
| 132 | + std::hint::black_box(([eq, lt], cmp)); |
| 133 | + }} |
| 134 | +
|
| 135 | + pub fn binop_ptr(lhs: *const char, rhs: *const char) {{ |
| 136 | + let eq = lhs == rhs; |
| 137 | + let lt = lhs < rhs; |
| 138 | + let cmp = lhs.cmp(&rhs); |
| 139 | + let off = unsafe {{ lhs.offset(2) }}; |
| 140 | +
|
| 141 | + // Try to avoid the results above being pruned |
| 142 | + std::hint::black_box(([eq, lt], cmp, off)); |
| 143 | + }} |
| 144 | + "# |
| 145 | + )?; |
| 146 | + Ok(()) |
| 147 | +} |
0 commit comments