Skip to content

Commit

Permalink
Rollup merge of rust-lang#73795 - JohnTitor:tests-for-const-fn-ptrs, …
Browse files Browse the repository at this point in the history
…r=oli-obk

Add some `const_compare_raw_pointers`-related regression tests

Closes rust-lang#71381
Closes rust-lang#71382
Closes rust-lang#71611
Closes rust-lang#72352

r? @oli-obk, the author of rust-lang#73398
  • Loading branch information
Manishearth committed Jun 28, 2020
2 parents 3f826a8 + 1d16aed commit 2c1b732
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/test/ui/const-generics/issues/issue-71381.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![feature(const_generics)]
#![allow(incomplete_features)]

struct Test(*const usize);

type PassArg = ();

unsafe extern "C" fn pass(args: PassArg) {
println!("Hello, world!");
}

impl Test {
pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args)>(&self) {
//~^ ERROR: using function pointers as const generic parameters is forbidden
self.0 = Self::trampiline::<Args, IDX, FN> as _
}

unsafe extern "C" fn trampiline<
Args: Sized,
const IDX: usize,
const FN: unsafe extern "C" fn(Args),
//~^ ERROR: using function pointers as const generic parameters is forbidden
>(
args: Args,
) {
FN(args)
}
}

fn main() {
let x = Test();
x.call_me::<PassArg, 30, pass>()
}
14 changes: 14 additions & 0 deletions src/test/ui/const-generics/issues/issue-71381.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: using function pointers as const generic parameters is forbidden
--> $DIR/issue-71381.rs:13:61
|
LL | pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args)>(&self) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: using function pointers as const generic parameters is forbidden
--> $DIR/issue-71381.rs:21:19
|
LL | const FN: unsafe extern "C" fn(Args),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

24 changes: 24 additions & 0 deletions src/test/ui/const-generics/issues/issue-71382.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![feature(const_generics)]
#![allow(incomplete_features)]

struct Test();

fn pass() {
println!("Hello, world!");
}

impl Test {
pub fn call_me(&self) {
self.test::<pass>();
}

fn test<const FN: fn()>(&self) {
//~^ ERROR: using function pointers as const generic parameters is forbidden
FN();
}
}

fn main() {
let x = Test();
x.call_me()
}
8 changes: 8 additions & 0 deletions src/test/ui/const-generics/issues/issue-71382.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: using function pointers as const generic parameters is forbidden
--> $DIR/issue-71382.rs:15:23
|
LL | fn test<const FN: fn()>(&self) {
| ^^^^

error: aborting due to previous error

9 changes: 9 additions & 0 deletions src/test/ui/const-generics/issues/issue-71611.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![feature(const_generics)]
#![allow(incomplete_features)]

fn func<A, const F: fn(inner: A)>(outer: A) {
//~^ ERROR: using function pointers as const generic parameters is forbidden
F(outer);
}

fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/const-generics/issues/issue-71611.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: using function pointers as const generic parameters is forbidden
--> $DIR/issue-71611.rs:4:21
|
LL | fn func<A, const F: fn(inner: A)>(outer: A) {
| ^^^^^^^^^^^^

error: aborting due to previous error

21 changes: 21 additions & 0 deletions src/test/ui/const-generics/issues/issue-72352.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![feature(const_generics)]
#![allow(incomplete_features)]

use std::ffi::{CStr, CString};

unsafe fn unsafely_do_the_thing<const F: fn(&CStr) -> usize>(ptr: *const i8) -> usize {
//~^ ERROR: using function pointers as const generic parameters is forbidden
F(CStr::from_ptr(ptr))
}

fn safely_do_the_thing(s: &CStr) -> usize {
s.to_bytes().len()
}

fn main() {
let baguette = CString::new("baguette").unwrap();
let ptr = baguette.as_ptr();
println!("{}", unsafe {
unsafely_do_the_thing::<safely_do_the_thing>(ptr)
});
}
8 changes: 8 additions & 0 deletions src/test/ui/const-generics/issues/issue-72352.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: using function pointers as const generic parameters is forbidden
--> $DIR/issue-72352.rs:6:42
|
LL | unsafe fn unsafely_do_the_thing<const F: fn(&CStr) -> usize>(ptr: *const i8) -> usize {
| ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

0 comments on commit 2c1b732

Please sign in to comment.