Skip to content

Commit 7d6da8e

Browse files
committed
Add some tests for non-trivial ADTs
These tests test building and matching some non-trivial ADT configurations such as C-like enums and packed structs.
1 parent 52ffeda commit 7d6da8e

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(rustc_attrs)]
12+
13+
#[repr(C, u32)]
14+
enum CEnum {
15+
Hello = 30,
16+
World = 60
17+
}
18+
19+
#[rustc_mir]
20+
fn test1(c: CEnum) -> i32 {
21+
let c2 = CEnum::Hello;
22+
match (c, c2) {
23+
(CEnum::Hello, CEnum::Hello) => 42,
24+
(CEnum::World, CEnum::Hello) => 0,
25+
_ => 1
26+
}
27+
}
28+
29+
#[repr(packed)]
30+
#[derive(PartialEq, Debug)]
31+
struct Pakd {
32+
a: u64,
33+
b: u32,
34+
c: u16,
35+
d: u8,
36+
e: ()
37+
}
38+
39+
impl Drop for Pakd {
40+
fn drop(&mut self) {}
41+
}
42+
43+
#[rustc_mir]
44+
fn test2() -> Pakd {
45+
Pakd { a: 42, b: 42, c: 42, d: 42, e: () }
46+
}
47+
48+
#[derive(PartialEq, Debug)]
49+
struct TupleLike(u64, u32);
50+
51+
#[rustc_mir]
52+
fn test3() -> TupleLike {
53+
TupleLike(42, 42)
54+
}
55+
56+
#[rustc_mir]
57+
fn test4(x: fn(u64, u32) -> TupleLike) -> (TupleLike, TupleLike) {
58+
let y = TupleLike;
59+
(x(42, 84), y(42, 84))
60+
}
61+
62+
#[rustc_mir]
63+
fn test5(x: fn(u32) -> Option<u32>) -> (Option<u32>, Option<u32>) {
64+
let y = Some;
65+
(x(42), y(42))
66+
}
67+
68+
fn main() {
69+
assert_eq!(test1(CEnum::Hello), 42);
70+
assert_eq!(test1(CEnum::World), 0);
71+
assert_eq!(test2(), Pakd { a: 42, b: 42, c: 42, d: 42, e: () });
72+
assert_eq!(test3(), TupleLike(42, 42));
73+
let t4 = test4(TupleLike);
74+
assert_eq!(t4.0, t4.1);
75+
let t5 = test5(Some);
76+
assert_eq!(t5.0, t5.1);
77+
}

src/test/run-pass/mir_build_match_comparisons.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![feature(rustc_attrs)]
1212

1313
#[rustc_mir]
14-
pub fn test1(x: i8) -> i32 {
14+
fn test1(x: i8) -> i32 {
1515
match x {
1616
1...10 => 0,
1717
_ => 1,
@@ -22,21 +22,36 @@ const U: Option<i8> = Some(10);
2222
const S: &'static str = "hello";
2323

2424
#[rustc_mir]
25-
pub fn test2(x: i8) -> i32 {
25+
fn test2(x: i8) -> i32 {
2626
match Some(x) {
2727
U => 0,
2828
_ => 1,
2929
}
3030
}
3131

3232
#[rustc_mir]
33-
pub fn test3(x: &'static str) -> i32 {
33+
fn test3(x: &'static str) -> i32 {
3434
match x {
3535
S => 0,
3636
_ => 1,
3737
}
3838
}
3939

40+
enum Opt<T> {
41+
Some { v: T },
42+
None
43+
}
44+
45+
#[rustc_mir]
46+
fn test4(x: u64) -> i32 {
47+
let opt = Opt::Some{ v: x };
48+
match opt {
49+
Opt::Some { v: 10 } => 0,
50+
_ => 1,
51+
}
52+
}
53+
54+
4055
fn main() {
4156
assert_eq!(test1(0), 1);
4257
assert_eq!(test1(1), 0);
@@ -52,4 +67,7 @@ fn main() {
5267
assert_eq!(test3("hello"), 0);
5368
assert_eq!(test3(""), 1);
5469
assert_eq!(test3("world"), 1);
70+
assert_eq!(test4(10), 0);
71+
assert_eq!(test4(0), 1);
72+
assert_eq!(test4(20), 1);
5573
}

0 commit comments

Comments
 (0)