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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
|
//@ test-mir-pass: MatchBranchSimplification
#![feature(repr128)]
#![feature(core_intrinsics)]
#![feature(custom_mir)]
#![allow(non_camel_case_types)]
use std::intrinsics::mir::*;
// EMIT_MIR matches_reduce_branches.foo.MatchBranchSimplification.diff
fn foo(bar: Option<()>) {
// CHECK-LABEL: fn foo(
// CHECK: = Eq(
// CHECK: switchInt
// CHECK-NOT: switchInt
// CHECK: return
if matches!(bar, None) {
()
}
}
// EMIT_MIR matches_reduce_branches.my_is_some.MatchBranchSimplification.diff
// Test for #131219.
fn my_is_some(bar: Option<()>) -> bool {
// CHECK-LABEL: fn my_is_some(
// CHECK: = Ne
// CHECK: return
match bar {
Some(_) => true,
None => false,
}
}
// EMIT_MIR matches_reduce_branches.bar.MatchBranchSimplification.diff
fn bar(i: i32) -> (bool, bool, bool, bool) {
// CHECK-LABEL: fn bar(
// CHECK: = Ne(
// CHECK: = Eq(
// CHECK-NOT: switchInt
// CHECK: return
let a;
let b;
let c;
let d;
match i {
7 => {
a = false;
b = true;
c = false;
d = true;
()
}
_ => {
a = true;
b = false;
c = false;
d = true;
()
}
};
(a, b, c, d)
}
// EMIT_MIR matches_reduce_branches.match_nested_if.MatchBranchSimplification.diff
fn match_nested_if() -> bool {
// CHECK-LABEL: fn match_nested_if(
// CHECK-NOT: switchInt
// CHECK: return
let val = match () {
() if if if if true { true } else { false } { true } else { false } {
true
} else {
false
} =>
{
true
}
_ => false,
};
val
}
// # Fold switchInt into IntToInt.
// To simplify writing and checking these test cases, I use the first character of
// each case to distinguish the sign of the number:
// 'u' for unsigned, '_' for negative, and 'o' for non-negative.
// Followed by a decimal number, and add the corresponding radix representation.
// For example, o127_0x7f represents 127i8, and _1_0xff represents -1i8.
// ## Cast but without numeric conversion.
#[repr(u8)]
enum EnumAu8 {
u0_0x00 = 0,
u127_0x7f = 127,
u128_0x80 = 128,
u255_0xff = 255,
}
#[repr(i8)]
enum EnumAi8 {
_128_0x80 = -128,
_1_0xff = -1,
o0_0x00 = 0,
o1_0x01 = 1,
o127_0x7f = 127,
}
// EMIT_MIR matches_reduce_branches.match_u8_i8.MatchBranchSimplification.diff
fn match_u8_i8(i: EnumAu8) -> i8 {
// CHECK-LABEL: fn match_u8_i8(
// CHECK-NOT: switchInt
// CHECK: return
match i {
EnumAu8::u0_0x00 => 0,
EnumAu8::u127_0x7f => 127,
EnumAu8::u128_0x80 => -128,
EnumAu8::u255_0xff => -1,
}
}
// EMIT_MIR matches_reduce_branches.match_u8_i8_failed.MatchBranchSimplification.diff
fn match_u8_i8_failed(i: EnumAu8) -> i8 {
// CHECK-LABEL: fn match_u8_i8_failed(
// CHECK: switchInt
// CHECK: return
match i {
EnumAu8::u0_0x00 => 0,
EnumAu8::u127_0x7f => 127,
EnumAu8::u128_0x80 => -128,
EnumAu8::u255_0xff => 1, // failed
}
}
// EMIT_MIR matches_reduce_branches.match_u8_i8_2.MatchBranchSimplification.diff
fn match_u8_i8_2(i: EnumAu8) -> (i8, i8) {
// CHECK-LABEL: fn match_u8_i8_2(
// CHECK-NOT: switchInt
// CHECK: IntToInt
// CHECK: IntToInt
// CHECK: return
let a: i8;
let b: i8;
match i {
EnumAu8::u0_0x00 => {
a = 0;
b = 0;
()
}
EnumAu8::u127_0x7f => {
a = 127;
b = 127;
()
}
EnumAu8::u128_0x80 => {
a = -128;
b = -128;
()
}
EnumAu8::u255_0xff => {
a = -1;
b = -1;
()
}
};
(a, b)
}
// EMIT_MIR matches_reduce_branches.match_u8_i8_2_failed.MatchBranchSimplification.diff
fn match_u8_i8_2_failed(i: EnumAu8) -> (i8, i8) {
// CHECK-LABEL: fn match_u8_i8_2_failed(
// CHECK: switchInt
// CHECK: return
let a: i8;
let b: i8;
match i {
EnumAu8::u0_0x00 => {
a = 0;
b = 0;
()
}
EnumAu8::u127_0x7f => {
a = 127;
b = 127;
()
}
EnumAu8::u128_0x80 => {
a = -128;
b = -128;
()
}
EnumAu8::u255_0xff => {
a = -1;
b = 1; // failed
()
}
};
(a, b)
}
// EMIT_MIR matches_reduce_branches.match_u8_i8_fallback.MatchBranchSimplification.diff
fn match_u8_i8_fallback(i: EnumAu8) -> i8 {
// CHECK-LABEL: fn match_u8_i8_fallback(
// CHECK: switchInt
// CHECK: return
match i {
EnumAu8::u0_0x00 => 0,
EnumAu8::u127_0x7f => 127,
EnumAu8::u128_0x80 => -128,
_ => -1,
}
}
// EMIT_MIR matches_reduce_branches.match_u8_i8_failed_len_1.MatchBranchSimplification.diff
// Check for different instruction lengths
#[custom_mir(dialect = "built")]
fn match_u8_i8_failed_len_1(i: EnumAu8) -> i8 {
// CHECK-LABEL: fn match_u8_i8_failed_len_1(
// CHECK: switchInt
// CHECK: return
mir! {
{
let a = Discriminant(i);
match a {
0 => bb1,
127 => bb2,
128 => bb3,
255 => bb4,
_ => unreachable_bb,
}
}
bb1 = {
RET = 0;
Goto(ret)
}
bb2 = {
RET = 127;
RET = 127;
Goto(ret)
}
bb3 = {
RET = -128;
Goto(ret)
}
bb4 = {
RET = -1;
Goto(ret)
}
unreachable_bb = {
Unreachable()
}
ret = {
Return()
}
}
}
// EMIT_MIR matches_reduce_branches.match_u8_i8_failed_len_2.MatchBranchSimplification.diff
// Check for different instruction lengths
#[custom_mir(dialect = "built")]
fn match_u8_i8_failed_len_2(i: EnumAu8) -> i8 {
// CHECK-LABEL: fn match_u8_i8_failed_len_2(
// CHECK: switchInt
// CHECK: return
mir! {
{
let a = Discriminant(i);
match a {
0 => bb1,
127 => bb2,
128 => bb3,
255 => bb4,
_ => unreachable_bb,
}
}
bb1 = {
RET = 0;
Goto(ret)
}
bb2 = {
RET = 127;
Goto(ret)
}
bb3 = {
RET = -128;
Goto(ret)
}
bb4 = {
RET = -1;
RET = -1;
Goto(ret)
}
unreachable_bb = {
Unreachable()
}
ret = {
Return()
}
}
}
// ## Cast with sext.
// EMIT_MIR matches_reduce_branches.match_sext_i8_i16.MatchBranchSimplification.diff
fn match_sext_i8_i16(i: EnumAi8) -> i16 {
// CHECK-LABEL: fn match_sext_i8_i16(
// CHECK-NOT: switchInt
// CHECK: return
match i {
EnumAi8::_128_0x80 => -128,
EnumAi8::_1_0xff => -1,
EnumAi8::o0_0x00 => 0,
EnumAi8::o1_0x01 => 1,
EnumAi8::o127_0x7f => 127,
}
}
// EMIT_MIR matches_reduce_branches.match_sext_i8_i16_failed.MatchBranchSimplification.diff
// Converting `-1i8` to `255i16` is zext.
fn match_sext_i8_i16_failed(i: EnumAi8) -> i16 {
// CHECK-LABEL: fn match_sext_i8_i16_failed(
// CHECK: switchInt
// CHECK: return
match i {
EnumAi8::_128_0x80 => -128,
EnumAi8::_1_0xff => 255, // failed
EnumAi8::o0_0x00 => 0,
EnumAi8::o1_0x01 => 1,
EnumAi8::o127_0x7f => 127,
}
}
// EMIT_MIR matches_reduce_branches.match_sext_i8_u16.MatchBranchSimplification.diff
fn match_sext_i8_u16(i: EnumAi8) -> u16 {
// CHECK-LABEL: fn match_sext_i8_u16(
// CHECK-NOT: switchInt
// CHECK: return
match i {
EnumAi8::_128_0x80 => 0xff80,
EnumAi8::_1_0xff => 0xffff,
EnumAi8::o0_0x00 => 0,
EnumAi8::o1_0x01 => 1,
EnumAi8::o127_0x7f => 127,
}
}
// EMIT_MIR matches_reduce_branches.match_sext_i8_u16_failed.MatchBranchSimplification.diff
// Converting `-1i8` to `255u16` is zext.
fn match_sext_i8_u16_failed(i: EnumAi8) -> u16 {
// CHECK-LABEL: fn match_sext_i8_u16_failed(
// CHECK: switchInt
// CHECK: return
match i {
EnumAi8::_128_0x80 => 0xff80,
EnumAi8::_1_0xff => 0x00ff, // failed
EnumAi8::o0_0x00 => 0,
EnumAi8::o1_0x01 => 1,
EnumAi8::o127_0x7f => 127,
}
}
// ## Cast with zext.
// EMIT_MIR matches_reduce_branches.match_zext_u8_u16.MatchBranchSimplification.diff
fn match_zext_u8_u16(i: EnumAu8) -> u16 {
// CHECK-LABEL: fn match_zext_u8_u16(
// CHECK-NOT: switchInt
// CHECK: return
match i {
EnumAu8::u0_0x00 => 0,
EnumAu8::u127_0x7f => 0x7f,
EnumAu8::u128_0x80 => 128,
EnumAu8::u255_0xff => 255,
}
}
// EMIT_MIR matches_reduce_branches.match_zext_u8_u16_failed.MatchBranchSimplification.diff
fn match_zext_u8_u16_failed(i: EnumAu8) -> u16 {
// CHECK-LABEL: fn match_zext_u8_u16_failed(
// CHECK: switchInt
// CHECK: return
match i {
EnumAu8::u0_0x00 => 0,
EnumAu8::u127_0x7f => 0xff7f, // failed
EnumAu8::u128_0x80 => 128,
EnumAu8::u255_0xff => 255,
}
}
// EMIT_MIR matches_reduce_branches.match_zext_u8_i16.MatchBranchSimplification.diff
fn match_zext_u8_i16(i: EnumAu8) -> i16 {
// CHECK-LABEL: fn match_zext_u8_i16(
// CHECK-NOT: switchInt
// CHECK: return
match i {
EnumAu8::u0_0x00 => 0,
EnumAu8::u127_0x7f => 127,
EnumAu8::u128_0x80 => 128,
EnumAu8::u255_0xff => 255,
}
}
// EMIT_MIR matches_reduce_branches.match_zext_u8_i16_failed.MatchBranchSimplification.diff
fn match_zext_u8_i16_failed(i: EnumAu8) -> i16 {
// CHECK-LABEL: fn match_zext_u8_i16_failed(
// CHECK: switchInt
// CHECK: return
match i {
EnumAu8::u0_0x00 => 0,
EnumAu8::u127_0x7f => -127, // failed
EnumAu8::u128_0x80 => 128,
EnumAu8::u255_0xff => 255,
}
}
// ## Cast with trunc.
#[repr(u16)]
enum EnumAu16 {
// 0x00nn
u0_0x0000 = 0,
u127_0x007f = 127,
u128_0x0080 = 128,
u255_0x00ff = 255,
// 0xffnn
u65280_0xff00 = 65280,
u65407_0xff7f = 65407,
u65408_0xff80 = 65408,
u65535_0xffff = 65535,
}
#[repr(i16)]
enum EnumAi16 {
// 0x00nn
o128_0x0080 = 128,
o255_0x00ff = 255,
o0_0x0000 = 0,
o1_0x0001 = 1,
o127_0x007f = 127,
// 0xffnn
_128_0xff80 = -128,
_1_0xffff = -1,
o0_0xff00 = -256,
o1_0xff01 = -255,
o127_0xff7f = -129,
}
// EMIT_MIR matches_reduce_branches.match_trunc_i16_i8.MatchBranchSimplification.diff
fn match_trunc_i16_i8(i: EnumAi16) -> i8 {
// CHECK-LABEL: fn match_trunc_i16_i8(
// CHECK-NOT: switchInt
// CHECK: return
match i {
// 0x00nn
EnumAi16::o128_0x0080 => -128,
EnumAi16::o255_0x00ff => -1,
EnumAi16::o0_0x0000 => 0,
EnumAi16::o1_0x0001 => 1,
EnumAi16::o127_0x007f => 127,
// 0xffnn
EnumAi16::_128_0xff80 => -128,
EnumAi16::_1_0xffff => -1,
EnumAi16::o0_0xff00 => 0,
EnumAi16::o1_0xff01 => 1,
EnumAi16::o127_0xff7f => 127,
}
}
// EMIT_MIR matches_reduce_branches.match_trunc_i16_i8_failed.MatchBranchSimplification.diff
fn match_trunc_i16_i8_failed(i: EnumAi16) -> i8 {
// CHECK-LABEL: fn match_trunc_i16_i8_failed(
// CHECK: switchInt
// CHECK: return
match i {
// 0x00nn
EnumAi16::o128_0x0080 => -128,
EnumAi16::o255_0x00ff => -1,
EnumAi16::o0_0x0000 => 0,
EnumAi16::o1_0x0001 => 1,
EnumAi16::o127_0x007f => 127,
// 0xffnn
EnumAi16::_128_0xff80 => -128,
EnumAi16::_1_0xffff => -1,
EnumAi16::o0_0xff00 => 0,
EnumAi16::o1_0xff01 => 1,
EnumAi16::o127_0xff7f => -127, // failed
}
}
// EMIT_MIR matches_reduce_branches.match_trunc_i16_u8.MatchBranchSimplification.diff
fn match_trunc_i16_u8(i: EnumAi16) -> u8 {
// CHECK-LABEL: fn match_trunc_i16_u8(
// CHECK-NOT: switchInt
// CHECK: return
match i {
// 0x00nn
EnumAi16::o128_0x0080 => 128,
EnumAi16::o255_0x00ff => 255,
EnumAi16::o0_0x0000 => 0,
EnumAi16::o1_0x0001 => 1,
EnumAi16::o127_0x007f => 127,
// 0xffnn
EnumAi16::_128_0xff80 => 128,
EnumAi16::_1_0xffff => 255,
EnumAi16::o0_0xff00 => 0,
EnumAi16::o1_0xff01 => 1,
EnumAi16::o127_0xff7f => 127,
}
}
// EMIT_MIR matches_reduce_branches.match_trunc_i16_u8_failed.MatchBranchSimplification.diff
fn match_trunc_i16_u8_failed(i: EnumAi16) -> u8 {
// CHECK-LABEL: fn match_trunc_i16_u8_failed(
// CHECK: switchInt
// CHECK: return
match i {
// 0x00nn
EnumAi16::o128_0x0080 => 128,
EnumAi16::o255_0x00ff => 255,
EnumAi16::o0_0x0000 => 0,
EnumAi16::o1_0x0001 => 1,
EnumAi16::o127_0x007f => 127,
// 0xffnn
EnumAi16::_128_0xff80 => 128,
EnumAi16::_1_0xffff => 255,
EnumAi16::o0_0xff00 => 0,
EnumAi16::o1_0xff01 => 1,
EnumAi16::o127_0xff7f => -127i8 as u8, // failed
}
}
// EMIT_MIR matches_reduce_branches.match_trunc_u16_u8.MatchBranchSimplification.diff
fn match_trunc_u16_u8(i: EnumAu16) -> u8 {
// CHECK-LABEL: fn match_trunc_u16_u8(
// CHECK-NOT: switchInt
// CHECK: return
match i {
// 0x00nn
EnumAu16::u0_0x0000 => 0,
EnumAu16::u127_0x007f => 127,
EnumAu16::u128_0x0080 => 128,
EnumAu16::u255_0x00ff => 255,
// 0xffnn
EnumAu16::u65280_0xff00 => 0,
EnumAu16::u65407_0xff7f => 127,
EnumAu16::u65408_0xff80 => 128,
EnumAu16::u65535_0xffff => 255,
}
}
// EMIT_MIR matches_reduce_branches.match_trunc_u16_u8_failed.MatchBranchSimplification.diff
fn match_trunc_u16_u8_failed(i: EnumAu16) -> u8 {
// CHECK-LABEL: fn match_trunc_u16_u8_failed(
// CHECK: switchInt
// CHECK: return
match i {
// 0x00nn
EnumAu16::u0_0x0000 => 0,
EnumAu16::u127_0x007f => 127,
EnumAu16::u128_0x0080 => 128,
EnumAu16::u255_0x00ff => 255,
// 0xffnn
EnumAu16::u65280_0xff00 => 0,
EnumAu16::u65407_0xff7f => 127,
EnumAu16::u65408_0xff80 => 128,
EnumAu16::u65535_0xffff => 127, // failed
}
}
// EMIT_MIR matches_reduce_branches.match_trunc_u16_i8.MatchBranchSimplification.diff
fn match_trunc_u16_i8(i: EnumAu16) -> i8 {
// CHECK-LABEL: fn match_trunc_u16_i8(
// CHECK-NOT: switchInt
// CHECK: return
match i {
// 0x00nn
EnumAu16::u0_0x0000 => 0,
EnumAu16::u127_0x007f => 127,
EnumAu16::u128_0x0080 => -128,
EnumAu16::u255_0x00ff => -1,
// 0xffnn
EnumAu16::u65280_0xff00 => 0,
EnumAu16::u65407_0xff7f => 127,
EnumAu16::u65408_0xff80 => -128,
EnumAu16::u65535_0xffff => -1,
}
}
// EMIT_MIR matches_reduce_branches.match_trunc_u16_i8_failed.MatchBranchSimplification.diff
fn match_trunc_u16_i8_failed(i: EnumAu16) -> i8 {
// CHECK-LABEL: fn match_trunc_u16_i8_failed(
// CHECK: switchInt
// CHECK: return
match i {
// 0x00nn
EnumAu16::u0_0x0000 => 0,
EnumAu16::u127_0x007f => 127,
EnumAu16::u128_0x0080 => -128,
EnumAu16::u255_0x00ff => -1,
// 0xffnn
EnumAu16::u65280_0xff00 => 0,
EnumAu16::u65407_0xff7f => 127,
EnumAu16::u65408_0xff80 => -128,
EnumAu16::u65535_0xffff => 1,
}
}
#[repr(i128)]
enum EnumAi128 {
A = 1,
B = 2,
C = 3,
D = -1,
}
// EMIT_MIR matches_reduce_branches.match_i128_u128.MatchBranchSimplification.diff
fn match_i128_u128(i: EnumAi128) -> u128 {
// CHECK-LABEL: fn match_i128_u128(
// CHECK-NOT: switchInt
// CHECK: return
match i {
EnumAi128::A => 1,
EnumAi128::B => 2,
EnumAi128::C => 3,
EnumAi128::D => u128::MAX,
}
}
fn main() {
let _ = foo(None);
let _ = foo(Some(()));
let _ = bar(0);
let _ = match_nested_if();
let _: i8 = match_u8_i8(EnumAu8::u0_0x00);
let _: i8 = match_u8_i8_failed(EnumAu8::u0_0x00);
let _: (i8, i8) = match_u8_i8_2(EnumAu8::u0_0x00);
let _: (i8, i8) = match_u8_i8_2_failed(EnumAu8::u0_0x00);
let _: i8 = match_u8_i8_fallback(EnumAu8::u0_0x00);
let _: i8 = match_u8_i8_failed_len_1(EnumAu8::u0_0x00);
let _: i8 = match_u8_i8_failed_len_2(EnumAu8::u0_0x00);
let _: i16 = match_sext_i8_i16(EnumAi8::o0_0x00);
let _: i16 = match_sext_i8_i16_failed(EnumAi8::o0_0x00);
let _: u16 = match_sext_i8_u16(EnumAi8::o0_0x00);
let _: u16 = match_sext_i8_u16_failed(EnumAi8::o0_0x00);
let _: u16 = match_zext_u8_u16(EnumAu8::u0_0x00);
let _: u16 = match_zext_u8_u16_failed(EnumAu8::u0_0x00);
let _: i16 = match_zext_u8_i16(EnumAu8::u0_0x00);
let _: i16 = match_zext_u8_i16_failed(EnumAu8::u0_0x00);
let _: i8 = match_trunc_i16_i8(EnumAi16::o0_0x0000);
let _: i8 = match_trunc_i16_i8_failed(EnumAi16::o0_0x0000);
let _: u8 = match_trunc_i16_u8(EnumAi16::o0_0x0000);
let _: u8 = match_trunc_i16_u8_failed(EnumAi16::o0_0x0000);
let _: i8 = match_trunc_u16_i8(EnumAu16::u0_0x0000);
let _: i8 = match_trunc_u16_i8_failed(EnumAu16::u0_0x0000);
let _: u8 = match_trunc_u16_u8(EnumAu16::u0_0x0000);
let _: u8 = match_trunc_u16_u8_failed(EnumAu16::u0_0x0000);
let _ = match_i128_u128(EnumAi128::A);
let _ = my_is_some(None);
}
|