mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Fix unsized struct problems in mir eval
This commit is contained in:
parent
a9d81ae89c
commit
6990d0f26a
7 changed files with 220 additions and 35 deletions
|
@ -1186,6 +1186,25 @@ fn pattern_matching_ergonomics() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn destructing_assignment() {
|
||||
check_number(
|
||||
r#"
|
||||
//- minicore: add
|
||||
const fn f(i: &mut u8) -> &mut u8 {
|
||||
*i += 1;
|
||||
i
|
||||
}
|
||||
const GOAL: u8 = {
|
||||
let mut i = 4;
|
||||
_ = f(&mut i);
|
||||
i
|
||||
};
|
||||
"#,
|
||||
5,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn let_else() {
|
||||
check_number(
|
||||
|
@ -1745,6 +1764,24 @@ fn function_pointer_in_constants() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_pointer_and_niche_optimization() {
|
||||
check_number(
|
||||
r#"
|
||||
//- minicore: option
|
||||
const GOAL: i32 = {
|
||||
let f: fn(i32) -> i32 = |x| x + 2;
|
||||
let init = Some(f);
|
||||
match init {
|
||||
Some(t) => t(3),
|
||||
None => 222,
|
||||
}
|
||||
};
|
||||
"#,
|
||||
5,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_pointer() {
|
||||
check_number(
|
||||
|
@ -2359,11 +2396,14 @@ fn const_loop() {
|
|||
fn const_transfer_memory() {
|
||||
check_number(
|
||||
r#"
|
||||
const A1: &i32 = &2;
|
||||
const A2: &i32 = &5;
|
||||
const GOAL: i32 = *A1 + *A2;
|
||||
//- minicore: slice, index, coerce_unsized
|
||||
const A1: &i32 = &1;
|
||||
const A2: &i32 = &10;
|
||||
const A3: [&i32; 3] = [&1, &2, &100];
|
||||
const A4: (i32, &i32) = (1, &1000);
|
||||
const GOAL: i32 = *A1 + *A2 + *A3[2] + *A4.1;
|
||||
"#,
|
||||
7,
|
||||
1111,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -2634,9 +2674,9 @@ fn exec_limits() {
|
|||
}
|
||||
sum
|
||||
}
|
||||
const GOAL: i32 = f(10000);
|
||||
const GOAL: i32 = f(1000);
|
||||
"#,
|
||||
10000 * 10000,
|
||||
1000 * 1000,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -2683,7 +2723,7 @@ fn unsized_field() {
|
|||
//- minicore: coerce_unsized, index, slice, transmute
|
||||
use core::mem::transmute;
|
||||
|
||||
struct Slice([u8]);
|
||||
struct Slice([usize]);
|
||||
struct Slice2(Slice);
|
||||
|
||||
impl Slice2 {
|
||||
|
@ -2691,19 +2731,19 @@ fn unsized_field() {
|
|||
&self.0
|
||||
}
|
||||
|
||||
fn as_bytes(&self) -> &[u8] {
|
||||
fn as_bytes(&self) -> &[usize] {
|
||||
&self.as_inner().0
|
||||
}
|
||||
}
|
||||
|
||||
const GOAL: u8 = unsafe {
|
||||
let x: &[u8] = &[1, 2, 3];
|
||||
const GOAL: usize = unsafe {
|
||||
let x: &[usize] = &[1, 2, 3];
|
||||
let x: &Slice2 = transmute(x);
|
||||
let x = x.as_bytes();
|
||||
x[0] + x[1] + x[2]
|
||||
x[0] + x[1] + x[2] + x.len() * 100
|
||||
};
|
||||
"#,
|
||||
6,
|
||||
306,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -251,6 +251,28 @@ fn wrapping_add() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ptr_offset_from() {
|
||||
check_number(
|
||||
r#"
|
||||
//- minicore: index, slice, coerce_unsized
|
||||
extern "rust-intrinsic" {
|
||||
pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
|
||||
pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
|
||||
}
|
||||
|
||||
const GOAL: isize = {
|
||||
let x = [1, 2, 3, 4, 5i32];
|
||||
let r1 = -ptr_offset_from(&x[0], &x[4]);
|
||||
let r2 = ptr_offset_from(&x[3], &x[1]);
|
||||
let r3 = ptr_offset_from_unsigned(&x[3], &x[0]) as isize;
|
||||
r3 * 100 + r2 * 10 + r1
|
||||
};
|
||||
"#,
|
||||
324,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saturating() {
|
||||
check_number(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue