Skip to content

Commit

Permalink
Add tests for raw_ref_op
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Nov 24, 2019
1 parent 064bed0 commit f4efc5d
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/test/ui-fulldeps/pprust-expr-roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ fn iter_exprs(depth: usize, f: &mut dyn FnMut(P<Expr>)) {
Some(make_x()), Some(e), RangeLimits::HalfOpen)));
},
15 => {
iter_exprs(depth - 1, &mut |e| g(ExprKind::AddrOf(Mutability::Immutable, e)));
iter_exprs(
depth - 1,
&mut |e| g(ExprKind::AddrOf(BorrowKind::Ref, Mutability::Immutable, e)),
);
},
16 => {
g(ExprKind::Ret(None));
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/raw-ref-op/feature-raw-ref-op.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// gate-test-raw_ref_op

macro_rules! is_expr {
($e:expr) => {}
}

is_expr!(&raw const a); //~ ERROR raw address of syntax is experimental
is_expr!(&raw mut a); //~ ERROR raw address of syntax is experimental

#[cfg(FALSE)]
fn cfgd_out() {
let mut a = 0;
&raw const a; //~ ERROR raw address of syntax is experimental
&raw mut a; //~ ERROR raw address of syntax is experimental
}

fn main() {
let mut y = 123;
let x = &raw const y; //~ ERROR raw address of syntax is experimental
let x = &raw mut y; //~ ERROR raw address of syntax is experimental
}
57 changes: 57 additions & 0 deletions src/test/ui/raw-ref-op/feature-raw-ref-op.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
error[E0658]: raw address of syntax is experimental
--> $DIR/feature-raw-ref-op.rs:13:5
|
LL | &raw const a;
| ^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490
= help: add `#![feature(raw_ref_op)]` to the crate attributes to enable

error[E0658]: raw address of syntax is experimental
--> $DIR/feature-raw-ref-op.rs:14:5
|
LL | &raw mut a;
| ^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490
= help: add `#![feature(raw_ref_op)]` to the crate attributes to enable

error[E0658]: raw address of syntax is experimental
--> $DIR/feature-raw-ref-op.rs:19:13
|
LL | let x = &raw const y;
| ^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490
= help: add `#![feature(raw_ref_op)]` to the crate attributes to enable

error[E0658]: raw address of syntax is experimental
--> $DIR/feature-raw-ref-op.rs:20:13
|
LL | let x = &raw mut y;
| ^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490
= help: add `#![feature(raw_ref_op)]` to the crate attributes to enable

error[E0658]: raw address of syntax is experimental
--> $DIR/feature-raw-ref-op.rs:7:10
|
LL | is_expr!(&raw const a);
| ^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490
= help: add `#![feature(raw_ref_op)]` to the crate attributes to enable

error[E0658]: raw address of syntax is experimental
--> $DIR/feature-raw-ref-op.rs:8:10
|
LL | is_expr!(&raw mut a);
| ^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490
= help: add `#![feature(raw_ref_op)]` to the crate attributes to enable

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0658`.
13 changes: 13 additions & 0 deletions src/test/ui/raw-ref-op/raw-ref-op.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// FIXME(#64490): make this run-pass

#![feature(raw_ref_op)]

fn main() {
let mut x = 123;
let c_p = &raw const x; //~ ERROR not yet implemented
let m_p = &raw mut x; //~ ERROR not yet implemented
let i_r = &x;
assert!(c_p == i_r);
assert!(c_p == m_p);
unsafe { assert!(*c_p == *i_r ); }
}
18 changes: 18 additions & 0 deletions src/test/ui/raw-ref-op/raw-ref-op.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: raw borrows are not yet implemented
--> $DIR/raw-ref-op.rs:7:15
|
LL | let c_p = &raw const x;
| ^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: raw borrows are not yet implemented
--> $DIR/raw-ref-op.rs:8:15
|
LL | let m_p = &raw mut x;
| ^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: aborting due to 2 previous errors

25 changes: 15 additions & 10 deletions src/test/ui/raw-ref-op/raw-ref-temp-deref.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
// Ensure that we don't allow taking the address of temporary values
#![feature(raw_ref_op)]
// FIXME(#64490) This should be check-pass
// Check that taking the address of a place that contains a dereference is
// allowed.
#![feature(raw_ref_op, type_ascription)]

const PAIR: (i32, i64) = (1, 2);
const PAIR_REF: &(i32, i64) = &(1, 2);

const ARRAY: [i32; 2] = [1, 2];
const ARRAY_REF: &[i32; 2] = &[3, 4];
const SLICE_REF: &[i32] = &[5, 6];

fn main() {
// These are all OK, we're not taking the address of the temporary
let deref_ref = &raw const *PAIR_REF; //~ ERROR not yet implemented
let field_deref_ref = &raw const PAIR_REF.0; //~ ERROR not yet implemented
let deref_ref = &raw const *ARRAY_REF; //~ ERROR not yet implemented
let field_deref_ref = &raw const ARRAY_REF[0]; //~ ERROR not yet implemented
let deref_ref = &raw const *SLICE_REF; //~ ERROR not yet implemented
let field_deref_ref = &raw const SLICE_REF[1]; //~ ERROR not yet implemented
let deref_ref = &raw const *PAIR_REF; //~ ERROR not yet implemented
let field_deref_ref = &raw const PAIR_REF.0; //~ ERROR not yet implemented
let deref_ref = &raw const *ARRAY_REF; //~ ERROR not yet implemented
let index_deref_ref = &raw const ARRAY_REF[0]; //~ ERROR not yet implemented
let deref_ref = &raw const *SLICE_REF; //~ ERROR not yet implemented
let index_deref_ref = &raw const SLICE_REF[1]; //~ ERROR not yet implemented

let x = 0;
let ascribe_ref = &raw const (x: i32); //~ ERROR not yet implemented
let ascribe_deref = &raw const (*ARRAY_REF: [i32; 2]); //~ ERROR not yet implemented
let ascribe_index_deref = &raw const (ARRAY_REF[0]: i32); //~ ERROR not yet implemented
}
74 changes: 74 additions & 0 deletions src/test/ui/raw-ref-op/raw-ref-temp-deref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
error: raw borrows are not yet implemented
--> $DIR/raw-ref-temp-deref.rs:13:21
|
LL | let deref_ref = &raw const *PAIR_REF;
| ^^^^^^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: raw borrows are not yet implemented
--> $DIR/raw-ref-temp-deref.rs:14:27
|
LL | let field_deref_ref = &raw const PAIR_REF.0;
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: raw borrows are not yet implemented
--> $DIR/raw-ref-temp-deref.rs:15:21
|
LL | let deref_ref = &raw const *ARRAY_REF;
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: raw borrows are not yet implemented
--> $DIR/raw-ref-temp-deref.rs:16:27
|
LL | let index_deref_ref = &raw const ARRAY_REF[0];
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: raw borrows are not yet implemented
--> $DIR/raw-ref-temp-deref.rs:17:21
|
LL | let deref_ref = &raw const *SLICE_REF;
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: raw borrows are not yet implemented
--> $DIR/raw-ref-temp-deref.rs:18:27
|
LL | let index_deref_ref = &raw const SLICE_REF[1];
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: raw borrows are not yet implemented
--> $DIR/raw-ref-temp-deref.rs:21:23
|
LL | let ascribe_ref = &raw const (x: i32);
| ^^^^^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: raw borrows are not yet implemented
--> $DIR/raw-ref-temp-deref.rs:22:25
|
LL | let ascribe_deref = &raw const (*ARRAY_REF: [i32; 2]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: raw borrows are not yet implemented
--> $DIR/raw-ref-temp-deref.rs:23:31
|
LL | let ascribe_index_deref = &raw const (ARRAY_REF[0]: i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/64490

error: aborting due to 9 previous errors

29 changes: 29 additions & 0 deletions src/test/ui/raw-ref-op/raw-ref-temp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Ensure that we don't allow taking the address of temporary values
#![feature(raw_ref_op, type_ascription)]

const PAIR: (i32, i64) = (1, 2);

const ARRAY: [i32; 2] = [1, 2];

fn main() {
let ref_expr = &raw const 2; //~ ERROR cannot take address
let mut_ref_expr = &raw mut 3; //~ ERROR cannot take address
let ref_const = &raw const 4; //~ ERROR cannot take address
let mut_ref_const = &raw mut 5; //~ ERROR cannot take address

let field_ref_expr = &raw const (1, 2).0; //~ ERROR cannot take address
let mut_field_ref_expr = &raw mut (1, 2).0; //~ ERROR cannot take address
let field_ref = &raw const PAIR.0; //~ ERROR cannot take address
let mut_field_ref = &raw mut PAIR.0; //~ ERROR cannot take address

let index_ref_expr = &raw const [1, 2][0]; //~ ERROR cannot take address
let mut_index_ref_expr = &raw mut [1, 2][0]; //~ ERROR cannot take address
let index_ref = &raw const ARRAY[0]; //~ ERROR cannot take address
let mut_index_ref = &raw mut ARRAY[1]; //~ ERROR cannot take address

let ref_ascribe = &raw const (2: i32); //~ ERROR cannot take address
let mut_ref_ascribe = &raw mut (3: i32); //~ ERROR cannot take address

let ascribe_field_ref = &raw const (PAIR.0: i32); //~ ERROR cannot take address
let ascribe_index_ref = &raw mut (ARRAY[0]: i32); //~ ERROR cannot take address
}
99 changes: 99 additions & 0 deletions src/test/ui/raw-ref-op/raw-ref-temp.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:9:31
|
LL | let ref_expr = &raw const 2;
| ^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:10:33
|
LL | let mut_ref_expr = &raw mut 3;
| ^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:11:32
|
LL | let ref_const = &raw const 4;
| ^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:12:34
|
LL | let mut_ref_const = &raw mut 5;
| ^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:14:37
|
LL | let field_ref_expr = &raw const (1, 2).0;
| ^^^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:15:39
|
LL | let mut_field_ref_expr = &raw mut (1, 2).0;
| ^^^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:16:32
|
LL | let field_ref = &raw const PAIR.0;
| ^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:17:34
|
LL | let mut_field_ref = &raw mut PAIR.0;
| ^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:19:37
|
LL | let index_ref_expr = &raw const [1, 2][0];
| ^^^^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:20:39
|
LL | let mut_index_ref_expr = &raw mut [1, 2][0];
| ^^^^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:21:32
|
LL | let index_ref = &raw const ARRAY[0];
| ^^^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:22:34
|
LL | let mut_index_ref = &raw mut ARRAY[1];
| ^^^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:24:34
|
LL | let ref_ascribe = &raw const (2: i32);
| ^^^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:25:36
|
LL | let mut_ref_ascribe = &raw mut (3: i32);
| ^^^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:27:40
|
LL | let ascribe_field_ref = &raw const (PAIR.0: i32);
| ^^^^^^^^^^^^^ temporary value

error[E0745]: cannot take address of a temporary
--> $DIR/raw-ref-temp.rs:28:38
|
LL | let ascribe_index_ref = &raw mut (ARRAY[0]: i32);
| ^^^^^^^^^^^^^^^ temporary value

error: aborting due to 16 previous errors

For more information about this error, try `rustc --explain E0745`.
25 changes: 25 additions & 0 deletions src/test/ui/raw-ref-op/unusual_locations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// FIXME(#64490): make this check-pass

#![feature(raw_ref_op)]

const USES_PTR: () = { let u = (); &raw const u; }; //~ ERROR not yet implemented
static ALSO_USES_PTR: () = { let u = (); &raw const u; }; //~ ERROR not yet implemented

fn main() {
#[cfg(FALSE)]
{
let x: [i32; { let u = 2; let x = &raw const u; 4 }]
= [2; { let v = 3; let y = &raw const v; 4 }];
let mut one = 1;
let two = 2;
if &raw const one == &raw mut one {
match &raw const two {
_ => {}
}
}
let three = 3;
let mut four = 4;
println!("{:p}", &raw const three);
unsafe { &raw mut four; }
}
}
Loading

0 comments on commit f4efc5d

Please sign in to comment.