1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
LL| |// This demonstrated Issue #84561: function-like macros produce unintuitive coverage results.
LL| |
LL| |//@ failure-status: 101
LL| |#[derive(PartialEq, Eq)]
LL| |struct Foo(u32);
LL| |
LL| |#[rustfmt::skip]
LL| 1|fn test3() {
LL| 1| let is_true = std::env::args().len() == 1;
LL| 1| let bar = Foo(1);
LL| 1| assert_eq!(bar, Foo(1));
LL| 1| let baz = Foo(0);
LL| 1| assert_ne!(baz, Foo(1));
LL| 1| println!("{:?}", Foo(1));
LL| 1| println!("{:?}", bar);
LL| 1| println!("{:?}", baz);
LL| 1|
LL| 1| assert_eq!(Foo(1), Foo(1));
LL| 1| assert_ne!(Foo(0), Foo(1));
LL| 1| assert_eq!(Foo(2), Foo(2));
LL| 1| let bar = Foo(0);
LL| 1| assert_ne!(bar, Foo(3));
LL| 1| assert_ne!(Foo(0), Foo(4));
LL| 1| assert_eq!(Foo(3), Foo(3), "with a message");
^0
LL| 1| println!("{:?}", bar);
LL| 1| println!("{:?}", Foo(1));
LL| 1|
LL| 1| assert_ne!(Foo(0), Foo(5), "{}", if is_true { "true message" } else { "false message" });
^0 ^0 ^0
LL| 1| assert_ne!(
LL| | Foo(0)
LL| | ,
LL| | Foo(5)
LL| | ,
LL| 0| "{}"
LL| 0| ,
LL| 0| if
LL| 0| is_true
LL| | {
LL| 0| "true message"
LL| | } else {
LL| 0| "false message"
LL| | }
LL| | );
LL| |
LL| 1| let is_true = std::env::args().len() == 1;
LL| 1|
LL| 1| assert_eq!(
LL| 1| Foo(1),
LL| 1| Foo(1)
LL| 1| );
LL| 1| assert_ne!(
LL| 1| Foo(0),
LL| 1| Foo(1)
LL| 1| );
LL| 1| assert_eq!(
LL| 1| Foo(2),
LL| 1| Foo(2)
LL| 1| );
LL| 1| let bar = Foo(1);
LL| 1| assert_ne!(
LL| 1| bar,
LL| 1| Foo(3)
LL| 1| );
LL| 1| if is_true {
LL| 1| assert_ne!(
LL| 1| Foo(0),
LL| 1| Foo(4)
LL| 1| );
LL| | } else {
LL| 0| assert_eq!(
LL| 0| Foo(3),
LL| 0| Foo(3)
LL| 0| );
LL| | }
LL| 1| if is_true {
LL| 1| assert_ne!(
LL| | Foo(0),
LL| | Foo(4),
LL| 0| "with a message"
LL| | );
LL| | } else {
LL| 0| assert_eq!(
LL| | Foo(3),
LL| | Foo(3),
LL| 0| "with a message"
LL| | );
LL| | }
LL| 1| assert_ne!(
LL| 1| if is_true {
LL| 1| Foo(0)
LL| | } else {
LL| 0| Foo(1)
LL| | },
LL| | Foo(5)
LL| | );
LL| 1| assert_ne!(
LL| 1| Foo(5),
LL| 1| if is_true {
LL| 1| Foo(0)
LL| | } else {
LL| 0| Foo(1)
LL| | }
LL| | );
LL| 1| assert_ne!(
LL| 1| if is_true {
LL| 1| assert_eq!(
LL| 1| Foo(3),
LL| 1| Foo(3)
LL| 1| );
LL| 1| Foo(0)
LL| | } else {
LL| 0| assert_ne!(
LL| 0| if is_true {
LL| 0| Foo(0)
LL| | } else {
LL| 0| Foo(1)
LL| | },
LL| | Foo(5)
LL| | );
LL| 0| Foo(1)
LL| | },
LL| | Foo(5),
LL| 0| "with a message"
LL| | );
LL| 1| assert_eq!(
LL| | Foo(1),
LL| | Foo(3),
LL| 1| "this assert should fail"
LL| | );
LL| 0| assert_eq!(
LL| | Foo(3),
LL| | Foo(3),
LL| 0| "this assert should not be reached"
LL| | );
LL| 0|}
LL| |
LL| |impl std::fmt::Debug for Foo {
LL| 7| fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
LL| 7| write!(f, "try and succeed")?;
^0
LL| 7| Ok(())
LL| 7| }
LL| |}
LL| |
LL| |static mut DEBUG_LEVEL_ENABLED: bool = false;
LL| |
LL| |macro_rules! debug {
LL| | ($($arg:tt)+) => (
LL| | if unsafe { DEBUG_LEVEL_ENABLED } {
LL| | println!($($arg)+);
LL| | }
LL| | );
LL| |}
LL| |
LL| 1|fn test1() {
LL| 1| debug!("debug is enabled");
^0
LL| 1| debug!("debug is enabled");
^0
LL| 1| let _ = 0;
LL| 1| debug!("debug is enabled");
^0
LL| 1| unsafe {
LL| 1| DEBUG_LEVEL_ENABLED = true;
LL| 1| }
LL| 1| debug!("debug is enabled");
LL| 1|}
LL| |
LL| |macro_rules! call_debug {
LL| | ($($arg:tt)+) => (
LL| 1| fn call_print(s: &str) {
LL| 1| print!("{}", s);
LL| 1| }
LL| |
LL| | call_print("called from call_debug: ");
LL| | debug!($($arg)+);
LL| | );
LL| |}
LL| |
LL| 1|fn test2() {
LL| 1| call_debug!("debug is enabled");
LL| 1|}
LL| |
LL| 1|fn main() {
LL| 1| test1();
LL| 1| test2();
LL| 1| test3();
LL| 1|}
|