1
Fork 0

Tell LLVM about impossible niche tags

This commit is contained in:
Scott McMurray 2025-03-28 20:50:28 -07:00
parent c2110769cd
commit 1f06a6a252
3 changed files with 465 additions and 19 deletions

View file

@ -1,5 +1,5 @@
//@ compile-flags: -Copt-level=3 -C no-prepopulate-passes
//@ only-x86_64 (because these discriminants are isize)
//@ only-64bit (because these discriminants are isize)
#![crate_type = "lib"]
@ -51,3 +51,64 @@ pub fn result_match(x: Result<u64, i64>) -> u16 {
Ok(_) => 42,
}
}
// CHECK-LABEL: @option_bool_match(
#[no_mangle]
pub fn option_bool_match(x: Option<bool>) -> char {
// CHECK: %[[RAW:.+]] = load i8, ptr %x
// CHECK: %[[IS_NONE:.+]] = icmp eq i8 %[[RAW]], 2
// CHECK: %[[OPT_DISCR:.+]] = select i1 %[[IS_NONE]], i64 0, i64 1
// CHECK: %[[OPT_DISCR_T:.+]] = trunc nuw i64 %[[OPT_DISCR]] to i1
// CHECK: br i1 %[[OPT_DISCR_T]], label %[[BB_SOME:.+]], label %[[BB_NONE:.+]]
// CHECK: [[BB_SOME]]:
// CHECK: %[[FIELD:.+]] = load i8, ptr %x
// CHECK: %[[FIELD_T:.+]] = trunc nuw i8 %[[FIELD]] to i1
// CHECK: br i1 %[[FIELD_T]]
match x {
None => 'n',
Some(false) => 'f',
Some(true) => 't',
}
}
use std::cmp::Ordering::{self, *};
// CHECK-LABEL: @option_ordering_match(
#[no_mangle]
pub fn option_ordering_match(x: Option<Ordering>) -> char {
// CHECK: %[[RAW:.+]] = load i8, ptr %x
// CHECK: %[[IS_NONE:.+]] = icmp eq i8 %[[RAW]], 2
// CHECK: %[[OPT_DISCR:.+]] = select i1 %[[IS_NONE]], i64 0, i64 1
// CHECK: %[[OPT_DISCR_T:.+]] = trunc nuw i64 %[[OPT_DISCR]] to i1
// CHECK: br i1 %[[OPT_DISCR_T]], label %[[BB_SOME:.+]], label %[[BB_NONE:.+]]
// CHECK: [[BB_SOME]]:
// CHECK: %[[FIELD:.+]] = load i8, ptr %x
// CHECK: switch i8 %[[FIELD]], label %[[UNREACHABLE:.+]] [
// CHECK-NEXT: i8 -1, label
// CHECK-NEXT: i8 0, label
// CHECK-NEXT: i8 1, label
// CHECK-NEXT: ]
// CHECK: [[UNREACHABLE]]:
// CHECK-NEXT: unreachable
match x {
None => '?',
Some(Less) => '<',
Some(Equal) => '=',
Some(Greater) => '>',
}
}
// CHECK-LABEL: @option_nonzero_match(
#[no_mangle]
pub fn option_nonzero_match(x: Option<std::num::NonZero<u16>>) -> u16 {
// CHECK: %[[IS_NONE:.+]] = icmp eq i16 %x, 0
// CHECK: %[[OPT_DISCR:.+]] = select i1 %[[IS_NONE]], i64 0, i64 1
// CHECK: %[[OPT_DISCR_T:.+]] = trunc nuw i64 %[[OPT_DISCR]] to i1
// CHECK: br i1 %[[OPT_DISCR_T]], label %[[BB_SOME:.+]], label %[[BB_NONE:.+]]
match x {
None => 123,
Some(_) => 987,
}
}