Auto merge of #58673 - matthewjasper:typeck-ptr-coercions, r=pnkfelix

[NLL] Type check operations with pointer types

It seems these were forgotten about. Moving to `Rvalue::AddressOf` simplifies the coercions from references, but I want this to be fixed as soon as possible.

r? @pnkfelix
This commit is contained in:
bors 2019-03-03 16:46:12 +00:00
commit 2cfd6444a7
18 changed files with 408 additions and 16 deletions

View file

@ -0,0 +1,39 @@
#![feature(nll)]
fn shared_to_const<'a, 'b>(x: &&'a i32) -> *const &'b i32 {
x //~ ERROR
}
fn unique_to_const<'a, 'b>(x: &mut &'a i32) -> *const &'b i32 {
x //~ ERROR
}
fn unique_to_mut<'a, 'b>(x: &mut &'a i32) -> *mut &'b i32 {
// Two errors because *mut is invariant
x //~ ERROR
//~| ERROR
}
fn mut_to_const<'a, 'b>(x: *mut &'a i32) -> *const &'b i32 {
x //~ ERROR
}
fn array_elem<'a, 'b>(x: &'a i32) -> *const &'b i32 {
let z = &[x; 3];
let y = z as *const &i32;
y //~ ERROR
}
fn array_coerce<'a, 'b>(x: &'a i32) -> *const [&'b i32; 3] {
let z = &[x; 3];
let y = z as *const [&i32; 3];
y //~ ERROR
}
fn nested_array<'a, 'b>(x: &'a i32) -> *const [&'b i32; 2] {
let z = &[[x; 2]; 3];
let y = z as *const [&i32; 2];
y //~ ERROR
}
fn main() {}

View file

@ -0,0 +1,87 @@
error: lifetime may not live long enough
--> $DIR/type-check-pointer-coercions.rs:4:5
|
LL | fn shared_to_const<'a, 'b>(x: &&'a i32) -> *const &'b i32 {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | x //~ ERROR
| ^ returning this value requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-coercions.rs:8:5
|
LL | fn unique_to_const<'a, 'b>(x: &mut &'a i32) -> *const &'b i32 {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | x //~ ERROR
| ^ returning this value requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-coercions.rs:13:5
|
LL | fn unique_to_mut<'a, 'b>(x: &mut &'a i32) -> *mut &'b i32 {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | // Two errors because *mut is invariant
LL | x //~ ERROR
| ^ function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'b`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-coercions.rs:13:5
|
LL | fn unique_to_mut<'a, 'b>(x: &mut &'a i32) -> *mut &'b i32 {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | // Two errors because *mut is invariant
LL | x //~ ERROR
| ^ returning this value requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-coercions.rs:18:5
|
LL | fn mut_to_const<'a, 'b>(x: *mut &'a i32) -> *const &'b i32 {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | x //~ ERROR
| ^ returning this value requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-coercions.rs:24:5
|
LL | fn array_elem<'a, 'b>(x: &'a i32) -> *const &'b i32 {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
...
LL | y //~ ERROR
| ^ returning this value requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-coercions.rs:30:5
|
LL | fn array_coerce<'a, 'b>(x: &'a i32) -> *const [&'b i32; 3] {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
...
LL | y //~ ERROR
| ^ returning this value requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-coercions.rs:36:5
|
LL | fn nested_array<'a, 'b>(x: &'a i32) -> *const [&'b i32; 2] {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
...
LL | y //~ ERROR
| ^ returning this value requires that `'a` must outlive `'b`
error: aborting due to 8 previous errors

View file

@ -0,0 +1,33 @@
#![feature(nll)]
// Check that we assert that pointers have a common subtype for comparisons
fn compare_const<'a, 'b>(x: *const &mut &'a i32, y: *const &mut &'b i32) {
x == y;
//~^ ERROR lifetime may not live long enough
//~| ERROR lifetime may not live long enough
}
fn compare_mut<'a, 'b>(x: *mut &'a i32, y: *mut &'b i32) {
x == y;
//~^ ERROR lifetime may not live long enough
//~| ERROR lifetime may not live long enough
}
fn compare_fn_ptr<'a, 'b, 'c>(f: fn(&'c mut &'a i32), g: fn(&'c mut &'b i32)) {
f == g;
//~^ ERROR lifetime may not live long enough
//~| ERROR lifetime may not live long enough
}
fn compare_hr_fn_ptr<'a>(f: fn(&'a i32), g: fn(&i32)) {
// Ideally this should compile with the operands swapped as well, but HIR
// type checking prevents it (and stops compilation) for now.
f == g; // OK
}
fn compare_const_fn_ptr<'a>(f: *const fn(&'a i32), g: *const fn(&i32)) {
f == g; // OK
}
fn main() {}

View file

@ -0,0 +1,62 @@
error: lifetime may not live long enough
--> $DIR/type-check-pointer-comparisons.rs:6:5
|
LL | fn compare_const<'a, 'b>(x: *const &mut &'a i32, y: *const &mut &'b i32) {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | x == y;
| ^ requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-comparisons.rs:6:10
|
LL | fn compare_const<'a, 'b>(x: *const &mut &'a i32, y: *const &mut &'b i32) {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | x == y;
| ^ requires that `'b` must outlive `'a`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-comparisons.rs:12:5
|
LL | fn compare_mut<'a, 'b>(x: *mut &'a i32, y: *mut &'b i32) {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | x == y;
| ^ requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-comparisons.rs:12:10
|
LL | fn compare_mut<'a, 'b>(x: *mut &'a i32, y: *mut &'b i32) {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | x == y;
| ^ requires that `'b` must outlive `'a`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-comparisons.rs:18:5
|
LL | fn compare_fn_ptr<'a, 'b, 'c>(f: fn(&'c mut &'a i32), g: fn(&'c mut &'b i32)) {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | f == g;
| ^ requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/type-check-pointer-comparisons.rs:18:10
|
LL | fn compare_fn_ptr<'a, 'b, 'c>(f: fn(&'c mut &'a i32), g: fn(&'c mut &'b i32)) {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | f == g;
| ^ requires that `'b` must outlive `'a`
error: aborting due to 6 previous errors