Skip to content

Commit 2c1b732

Browse files
authored
Rollup merge of #73795 - JohnTitor:tests-for-const-fn-ptrs, r=oli-obk
Add some `const_compare_raw_pointers`-related regression tests Closes #71381 Closes #71382 Closes #71611 Closes #72352 r? @oli-obk, the author of #73398
2 parents 3f826a8 + 1d16aed commit 2c1b732

8 files changed

+125
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![feature(const_generics)]
2+
#![allow(incomplete_features)]
3+
4+
struct Test(*const usize);
5+
6+
type PassArg = ();
7+
8+
unsafe extern "C" fn pass(args: PassArg) {
9+
println!("Hello, world!");
10+
}
11+
12+
impl Test {
13+
pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args)>(&self) {
14+
//~^ ERROR: using function pointers as const generic parameters is forbidden
15+
self.0 = Self::trampiline::<Args, IDX, FN> as _
16+
}
17+
18+
unsafe extern "C" fn trampiline<
19+
Args: Sized,
20+
const IDX: usize,
21+
const FN: unsafe extern "C" fn(Args),
22+
//~^ ERROR: using function pointers as const generic parameters is forbidden
23+
>(
24+
args: Args,
25+
) {
26+
FN(args)
27+
}
28+
}
29+
30+
fn main() {
31+
let x = Test();
32+
x.call_me::<PassArg, 30, pass>()
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: using function pointers as const generic parameters is forbidden
2+
--> $DIR/issue-71381.rs:13:61
3+
|
4+
LL | pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args)>(&self) {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: using function pointers as const generic parameters is forbidden
8+
--> $DIR/issue-71381.rs:21:19
9+
|
10+
LL | const FN: unsafe extern "C" fn(Args),
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(const_generics)]
2+
#![allow(incomplete_features)]
3+
4+
struct Test();
5+
6+
fn pass() {
7+
println!("Hello, world!");
8+
}
9+
10+
impl Test {
11+
pub fn call_me(&self) {
12+
self.test::<pass>();
13+
}
14+
15+
fn test<const FN: fn()>(&self) {
16+
//~^ ERROR: using function pointers as const generic parameters is forbidden
17+
FN();
18+
}
19+
}
20+
21+
fn main() {
22+
let x = Test();
23+
x.call_me()
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: using function pointers as const generic parameters is forbidden
2+
--> $DIR/issue-71382.rs:15:23
3+
|
4+
LL | fn test<const FN: fn()>(&self) {
5+
| ^^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(const_generics)]
2+
#![allow(incomplete_features)]
3+
4+
fn func<A, const F: fn(inner: A)>(outer: A) {
5+
//~^ ERROR: using function pointers as const generic parameters is forbidden
6+
F(outer);
7+
}
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: using function pointers as const generic parameters is forbidden
2+
--> $DIR/issue-71611.rs:4:21
3+
|
4+
LL | fn func<A, const F: fn(inner: A)>(outer: A) {
5+
| ^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(const_generics)]
2+
#![allow(incomplete_features)]
3+
4+
use std::ffi::{CStr, CString};
5+
6+
unsafe fn unsafely_do_the_thing<const F: fn(&CStr) -> usize>(ptr: *const i8) -> usize {
7+
//~^ ERROR: using function pointers as const generic parameters is forbidden
8+
F(CStr::from_ptr(ptr))
9+
}
10+
11+
fn safely_do_the_thing(s: &CStr) -> usize {
12+
s.to_bytes().len()
13+
}
14+
15+
fn main() {
16+
let baguette = CString::new("baguette").unwrap();
17+
let ptr = baguette.as_ptr();
18+
println!("{}", unsafe {
19+
unsafely_do_the_thing::<safely_do_the_thing>(ptr)
20+
});
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: using function pointers as const generic parameters is forbidden
2+
--> $DIR/issue-72352.rs:6:42
3+
|
4+
LL | unsafe fn unsafely_do_the_thing<const F: fn(&CStr) -> usize>(ptr: *const i8) -> usize {
5+
| ^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)