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.
This commit is contained in:
parent
52ffeda8c8
commit
7d6da8e421
2 changed files with 98 additions and 3 deletions
77
src/test/run-pass/mir_adt_construction.rs
Normal file
77
src/test/run-pass/mir_adt_construction.rs
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
#![feature(rustc_attrs)]
|
||||||
|
|
||||||
|
#[repr(C, u32)]
|
||||||
|
enum CEnum {
|
||||||
|
Hello = 30,
|
||||||
|
World = 60
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rustc_mir]
|
||||||
|
fn test1(c: CEnum) -> i32 {
|
||||||
|
let c2 = CEnum::Hello;
|
||||||
|
match (c, c2) {
|
||||||
|
(CEnum::Hello, CEnum::Hello) => 42,
|
||||||
|
(CEnum::World, CEnum::Hello) => 0,
|
||||||
|
_ => 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(packed)]
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
struct Pakd {
|
||||||
|
a: u64,
|
||||||
|
b: u32,
|
||||||
|
c: u16,
|
||||||
|
d: u8,
|
||||||
|
e: ()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Pakd {
|
||||||
|
fn drop(&mut self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rustc_mir]
|
||||||
|
fn test2() -> Pakd {
|
||||||
|
Pakd { a: 42, b: 42, c: 42, d: 42, e: () }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
struct TupleLike(u64, u32);
|
||||||
|
|
||||||
|
#[rustc_mir]
|
||||||
|
fn test3() -> TupleLike {
|
||||||
|
TupleLike(42, 42)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rustc_mir]
|
||||||
|
fn test4(x: fn(u64, u32) -> TupleLike) -> (TupleLike, TupleLike) {
|
||||||
|
let y = TupleLike;
|
||||||
|
(x(42, 84), y(42, 84))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rustc_mir]
|
||||||
|
fn test5(x: fn(u32) -> Option<u32>) -> (Option<u32>, Option<u32>) {
|
||||||
|
let y = Some;
|
||||||
|
(x(42), y(42))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert_eq!(test1(CEnum::Hello), 42);
|
||||||
|
assert_eq!(test1(CEnum::World), 0);
|
||||||
|
assert_eq!(test2(), Pakd { a: 42, b: 42, c: 42, d: 42, e: () });
|
||||||
|
assert_eq!(test3(), TupleLike(42, 42));
|
||||||
|
let t4 = test4(TupleLike);
|
||||||
|
assert_eq!(t4.0, t4.1);
|
||||||
|
let t5 = test5(Some);
|
||||||
|
assert_eq!(t5.0, t5.1);
|
||||||
|
}
|
|
@ -11,7 +11,7 @@
|
||||||
#![feature(rustc_attrs)]
|
#![feature(rustc_attrs)]
|
||||||
|
|
||||||
#[rustc_mir]
|
#[rustc_mir]
|
||||||
pub fn test1(x: i8) -> i32 {
|
fn test1(x: i8) -> i32 {
|
||||||
match x {
|
match x {
|
||||||
1...10 => 0,
|
1...10 => 0,
|
||||||
_ => 1,
|
_ => 1,
|
||||||
|
@ -22,7 +22,7 @@ const U: Option<i8> = Some(10);
|
||||||
const S: &'static str = "hello";
|
const S: &'static str = "hello";
|
||||||
|
|
||||||
#[rustc_mir]
|
#[rustc_mir]
|
||||||
pub fn test2(x: i8) -> i32 {
|
fn test2(x: i8) -> i32 {
|
||||||
match Some(x) {
|
match Some(x) {
|
||||||
U => 0,
|
U => 0,
|
||||||
_ => 1,
|
_ => 1,
|
||||||
|
@ -30,13 +30,28 @@ pub fn test2(x: i8) -> i32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustc_mir]
|
#[rustc_mir]
|
||||||
pub fn test3(x: &'static str) -> i32 {
|
fn test3(x: &'static str) -> i32 {
|
||||||
match x {
|
match x {
|
||||||
S => 0,
|
S => 0,
|
||||||
_ => 1,
|
_ => 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Opt<T> {
|
||||||
|
Some { v: T },
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rustc_mir]
|
||||||
|
fn test4(x: u64) -> i32 {
|
||||||
|
let opt = Opt::Some{ v: x };
|
||||||
|
match opt {
|
||||||
|
Opt::Some { v: 10 } => 0,
|
||||||
|
_ => 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(test1(0), 1);
|
assert_eq!(test1(0), 1);
|
||||||
assert_eq!(test1(1), 0);
|
assert_eq!(test1(1), 0);
|
||||||
|
@ -52,4 +67,7 @@ fn main() {
|
||||||
assert_eq!(test3("hello"), 0);
|
assert_eq!(test3("hello"), 0);
|
||||||
assert_eq!(test3(""), 1);
|
assert_eq!(test3(""), 1);
|
||||||
assert_eq!(test3("world"), 1);
|
assert_eq!(test3("world"), 1);
|
||||||
|
assert_eq!(test4(10), 0);
|
||||||
|
assert_eq!(test4(0), 1);
|
||||||
|
assert_eq!(test4(20), 1);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue