|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +mod aux; |
| 4 | + |
| 5 | +macro_rules! pos { |
| 6 | + () => { |
| 7 | + (file!(), line!()) |
| 8 | + }; |
| 9 | +} |
| 10 | + |
| 11 | +macro_rules! check { |
| 12 | + ($($pos:expr),*) => ({ |
| 13 | + verify(&[$($pos),*]); |
| 14 | + }) |
| 15 | +} |
| 16 | + |
| 17 | +type Pos = (&'static str, u32); |
| 18 | + |
| 19 | +#[test] |
| 20 | +fn doit() { |
| 21 | + outer(pos!()); |
| 22 | +} |
| 23 | + |
| 24 | +#[inline(never)] |
| 25 | +fn outer(main_pos: Pos) { |
| 26 | + inner(main_pos, pos!()); |
| 27 | + inner_inlined(main_pos, pos!()); |
| 28 | +} |
| 29 | + |
| 30 | +#[inline(never)] |
| 31 | +#[rustfmt::skip] |
| 32 | +fn inner(main_pos: Pos, outer_pos: Pos) { |
| 33 | + check!(main_pos, outer_pos); |
| 34 | + check!(main_pos, outer_pos); |
| 35 | + let inner_pos = pos!(); aux::callback(|aux_pos| { |
| 36 | + check!(main_pos, outer_pos, inner_pos, aux_pos); |
| 37 | + }); |
| 38 | + let inner_pos = pos!(); aux::callback_inlined(|aux_pos| { |
| 39 | + check!(main_pos, outer_pos, inner_pos, aux_pos); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +// We emit the wrong location for the caller here when inlined on MSVC |
| 44 | +#[cfg_attr(not(target_env = "msvc"), inline(always))] |
| 45 | +#[cfg_attr(target_env = "msvc", inline(never))] |
| 46 | +#[rustfmt::skip] |
| 47 | +fn inner_inlined(main_pos: Pos, outer_pos: Pos) { |
| 48 | + check!(main_pos, outer_pos); |
| 49 | + check!(main_pos, outer_pos); |
| 50 | + |
| 51 | + // Again, disable inlining for MSVC. |
| 52 | + #[cfg_attr(not(target_env = "msvc"), inline(always))] |
| 53 | + #[cfg_attr(target_env = "msvc", inline(never))] |
| 54 | + fn inner_further_inlined(main_pos: Pos, outer_pos: Pos, inner_pos: Pos) { |
| 55 | + check!(main_pos, outer_pos, inner_pos); |
| 56 | + } |
| 57 | + inner_further_inlined(main_pos, outer_pos, pos!()); |
| 58 | + |
| 59 | + let inner_pos = pos!(); aux::callback(|aux_pos| { |
| 60 | + check!(main_pos, outer_pos, inner_pos, aux_pos); |
| 61 | + }); |
| 62 | + let inner_pos = pos!(); aux::callback_inlined(|aux_pos| { |
| 63 | + check!(main_pos, outer_pos, inner_pos, aux_pos); |
| 64 | + }); |
| 65 | + |
| 66 | + // this tests a distinction between two independent calls to the inlined function. |
| 67 | + // (un)fortunately, LLVM somehow merges two consecutive such calls into one node. |
| 68 | + inner_further_inlined(main_pos, outer_pos, pos!()); |
| 69 | +} |
| 70 | + |
| 71 | +fn verify(filelines: &[Pos]) { |
| 72 | + let trace = backtrace::Backtrace::new(); |
| 73 | + println!("-----------------------------------"); |
| 74 | + println!("looking for:"); |
| 75 | + for (file, line) in filelines.iter().rev() { |
| 76 | + println!("\t{}:{}", file, line); |
| 77 | + } |
| 78 | + println!("found:\n{:?}", trace); |
| 79 | + let mut symbols = trace.frames().iter().flat_map(|frame| frame.symbols()); |
| 80 | + let mut iter = filelines.iter().rev(); |
| 81 | + while let Some((file, line)) = iter.next() { |
| 82 | + loop { |
| 83 | + let sym = match symbols.next() { |
| 84 | + Some(sym) => sym, |
| 85 | + None => panic!("failed to find {}:{}", file, line), |
| 86 | + }; |
| 87 | + if let Some(filename) = sym.filename() { |
| 88 | + if let Some(lineno) = sym.lineno() { |
| 89 | + if filename == Path::new(file) && lineno == *line { |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments