Skip to content

Commit

Permalink
Rollup merge of rust-lang#106100 - scottmcm:derived-less-than-test, r…
Browse files Browse the repository at this point in the history
…=compiler-errors

Codegen test for derived `<` on trivial newtype [TEST ONLY]

I originally wrote this for rust-lang#106065, but the libcore changes there aren't necessarily a win.

So I pulled out this test to be its own PR since it's important (see rust-lang#105840 (comment)) and well-intentioned changes to core or the derive could accidentally break it without that being obvious (other than by massive unexplained perf changes).
  • Loading branch information
matthiaskrgr committed Dec 24, 2022
2 parents edb2b71 + 4cb7bd3 commit 09ee704
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/test/codegen/comparison-operators-newtype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// The `derive(PartialOrd)` for a newtype doesn't override `lt`/`le`/`gt`/`ge`.
// This double-checks that the `Option<Ordering>` intermediate values used
// in the operators for such a type all optimize away.

// compile-flags: -C opt-level=1
// min-llvm-version: 15.0

#![crate_type = "lib"]

use std::cmp::Ordering;

#[derive(PartialOrd, PartialEq)]
pub struct Foo(u16);

// CHECK-LABEL: @check_lt
// CHECK-SAME: (i16 %[[A:.+]], i16 %[[B:.+]])
#[no_mangle]
pub fn check_lt(a: Foo, b: Foo) -> bool {
// CHECK: %[[R:.+]] = icmp ult i16 %[[A]], %[[B]]
// CHECK-NEXT: ret i1 %[[R]]
a < b
}

// CHECK-LABEL: @check_le
// CHECK-SAME: (i16 %[[A:.+]], i16 %[[B:.+]])
#[no_mangle]
pub fn check_le(a: Foo, b: Foo) -> bool {
// CHECK: %[[R:.+]] = icmp ule i16 %[[A]], %[[B]]
// CHECK-NEXT: ret i1 %[[R]]
a <= b
}

// CHECK-LABEL: @check_gt
// CHECK-SAME: (i16 %[[A:.+]], i16 %[[B:.+]])
#[no_mangle]
pub fn check_gt(a: Foo, b: Foo) -> bool {
// CHECK: %[[R:.+]] = icmp ugt i16 %[[A]], %[[B]]
// CHECK-NEXT: ret i1 %[[R]]
a > b
}

// CHECK-LABEL: @check_ge
// CHECK-SAME: (i16 %[[A:.+]], i16 %[[B:.+]])
#[no_mangle]
pub fn check_ge(a: Foo, b: Foo) -> bool {
// CHECK: %[[R:.+]] = icmp uge i16 %[[A]], %[[B]]
// CHECK-NEXT: ret i1 %[[R]]
a >= b
}

0 comments on commit 09ee704

Please sign in to comment.