MIR episode 6

This commit is contained in:
hkalbasi 2023-05-26 00:45:37 +03:30
parent 505fd09f9e
commit 51368793b4
35 changed files with 1474 additions and 556 deletions

View file

@ -156,11 +156,23 @@ fn casts() {
check_number(
r#"
//- minicore: coerce_unsized, index, slice
const GOAL: usize = {
let a = &[10, 20, 30, 40] as &[i32];
a.len()
};
"#,
4,
);
check_number(
r#"
//- minicore: coerce_unsized, index, slice
const GOAL: usize = {
let a = [10, 20, 3, 15];
let x: &[i32] = &a;
let y: *const [i32] = x;
let z = y as *const [u8]; // slice fat pointer cast don't touch metadata
let q = z as *const str;
let p = q as *const [u8];
let w = unsafe { &*z };
w.len()
};
@ -987,11 +999,15 @@ fn path_pattern_matching() {
const MY_SEASON: Season = Summer;
impl Season {
const FALL: Season = Fall;
}
const fn f(x: Season) -> i32 {
match x {
Spring => 1,
MY_SEASON => 2,
Fall => 3,
Season::FALL => 3,
Winter => 4,
}
}
@ -1031,6 +1047,27 @@ fn pattern_matching_literal() {
);
}
#[test]
fn pattern_matching_range() {
check_number(
r#"
pub const L: i32 = 6;
mod x {
pub const R: i32 = 100;
}
const fn f(x: i32) -> i32 {
match x {
-1..=5 => x * 10,
L..=x::R => x * 100,
_ => x,
}
}
const GOAL: i32 = f(-1) + f(2) + f(100) + f(-2) + f(1000);
"#,
11008,
);
}
#[test]
fn pattern_matching_slice() {
check_number(
@ -1045,6 +1082,22 @@ fn pattern_matching_slice() {
"#,
10 + 4 + 60 + 16,
);
check_number(
r#"
//- minicore: slice, index, coerce_unsized, copy
const fn f(x: &[usize]) -> usize {
match x {
[] => 0,
[a] => *a,
&[a, b] => a + b,
[a, b @ .., c, d] => *a + b.len() + *c + *d,
}
}
const GOAL: usize = f(&[]) + f(&[10]) + f(&[100, 100])
+ f(&[1000, 1000, 1000]) + f(&[10000, 57, 34, 46, 10000, 10000]);
"#,
33213,
);
}
#[test]
@ -2105,6 +2158,26 @@ fn const_generic_subst_fn() {
"#,
11,
);
check_number(
r#"
fn f<const N: usize>(x: [i32; N]) -> usize {
N
}
trait ArrayExt {
fn f(self) -> usize;
}
impl<T, const N: usize> ArrayExt for [T; N] {
fn g(self) -> usize {
f(self)
}
}
const GOAL: usize = f([1, 2, 5]);
"#,
3,
);
}
#[test]

View file

@ -67,6 +67,30 @@ fn wrapping_add() {
);
}
#[test]
fn saturating_add() {
check_number(
r#"
extern "rust-intrinsic" {
pub fn saturating_add<T>(a: T, b: T) -> T;
}
const GOAL: u8 = saturating_add(10, 250);
"#,
255,
);
check_number(
r#"
extern "rust-intrinsic" {
pub fn saturating_add<T>(a: T, b: T) -> T;
}
const GOAL: i8 = saturating_add(5, 8);
"#,
13,
);
}
#[test]
fn allocator() {
check_number(