Rollup merge of #64986 - skinny121:fn-ptr-const-generics, r=varkor

Function pointers as const generic arguments

Makes function pointers as const generic arguments usable.

Fixes #62395

r? @varkor
This commit is contained in:
Tyler Mandry 2019-10-11 15:09:42 -07:00 committed by GitHub
commit ea0d155f2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 391 additions and 154 deletions

View file

@ -16,7 +16,7 @@ fn main() {
// START rustc.main.ConstProp.after.mir
// bb0: {
// ...
// _3 = const Scalar(AllocId(0).0x0) : fn();
// _3 = const main;
// _2 = move _3 as usize (Misc);
// ...
// _1 = move _2 as *const fn() (Misc);

View file

@ -0,0 +1,20 @@
// run-pass
#![feature(const_generics, const_compare_raw_pointers)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
fn function() -> u32 {
17
}
struct Wrapper<const F: fn() -> u32>;
impl<const F: fn() -> u32> Wrapper<{F}> {
fn call() -> u32 {
F()
}
}
fn main() {
assert_eq!(Wrapper::<{function}>::call(), 17);
}

View file

@ -0,0 +1,8 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/fn-const-param-call.rs:3:12
|
LL | #![feature(const_generics, const_compare_raw_pointers)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

View file

@ -0,0 +1,26 @@
#![feature(const_generics, const_compare_raw_pointers)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
struct Checked<const F: fn(usize) -> bool>;
fn not_one(val: usize) -> bool { val != 1 }
fn not_two(val: usize) -> bool { val != 2 }
fn generic_arg<T>(val: T) -> bool { true }
fn generic<T>(val: usize) -> bool { val != 1 }
fn main() {
let _: Option<Checked<{not_one}>> = None;
let _: Checked<{not_one}> = Checked::<{not_one}>;
let _: Checked<{not_one}> = Checked::<{not_two}>; //~ mismatched types
let _ = Checked::<{generic_arg}>;
let _ = Checked::<{generic_arg::<usize>}>;
let _ = Checked::<{generic_arg::<u32>}>; //~ mismatched types
let _ = Checked::<{generic}>; //~ type annotations needed
let _ = Checked::<{generic::<u16>}>;
let _: Checked<{generic::<u16>}> = Checked::<{generic::<u16>}>;
let _: Checked<{generic::<u32>}> = Checked::<{generic::<u16>}>; //~ mismatched types
}

View file

@ -0,0 +1,45 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/fn-const-param-infer.rs:1:12
|
LL | #![feature(const_generics, const_compare_raw_pointers)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default
error[E0308]: mismatched types
--> $DIR/fn-const-param-infer.rs:16:33
|
LL | let _: Checked<{not_one}> = Checked::<{not_two}>;
| ^^^^^^^^^^^^^^^^^^^^ expected `not_one`, found `not_two`
|
= note: expected type `Checked<not_one>`
found type `Checked<not_two>`
error[E0308]: mismatched types
--> $DIR/fn-const-param-infer.rs:20:24
|
LL | let _ = Checked::<{generic_arg::<u32>}>;
| ^^^^^^^^^^^^^^^^^^ expected usize, found u32
|
= note: expected type `fn(usize) -> bool`
found type `fn(u32) -> bool {generic_arg::<u32>}`
error[E0282]: type annotations needed
--> $DIR/fn-const-param-infer.rs:22:24
|
LL | let _ = Checked::<{generic}>;
| ^^^^^^^ cannot infer type for `T`
error[E0308]: mismatched types
--> $DIR/fn-const-param-infer.rs:25:40
|
LL | let _: Checked<{generic::<u32>}> = Checked::<{generic::<u16>}>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `generic::<u32>`, found `generic::<u16>`
|
= note: expected type `Checked<generic::<u32>>`
found type `Checked<generic::<u16>>`
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0282, E0308.
For more information about an error, try `rustc --explain E0282`.

View file

@ -0,0 +1,19 @@
// run-pass
#![feature(const_generics, const_compare_raw_pointers)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
const A: u32 = 3;
struct Const<const P: *const u32>;
impl<const P: *const u32> Const<{P}> {
fn get() -> u32 {
unsafe {
*P
}
}
}
fn main() {
assert_eq!(Const::<{&A as *const _}>::get(), 3)
}

View file

@ -0,0 +1,8 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/raw-ptr-const-param-deref.rs:2:12
|
LL | #![feature(const_generics, const_compare_raw_pointers)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

View file

@ -0,0 +1,9 @@
#![feature(const_generics, const_compare_raw_pointers)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
struct Const<const P: *const u32>;
fn main() {
let _: Const<{15 as *const _}> = Const::<{10 as *const _}>; //~ mismatched types
let _: Const<{10 as *const _}> = Const::<{10 as *const _}>;
}

View file

@ -0,0 +1,20 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/raw-ptr-const-param.rs:1:12
|
LL | #![feature(const_generics, const_compare_raw_pointers)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default
error[E0308]: mismatched types
--> $DIR/raw-ptr-const-param.rs:7:38
|
LL | let _: Const<{15 as *const _}> = Const::<{10 as *const _}>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{pointer}`, found `{pointer}`
|
= note: expected type `Const<{pointer}>`
found type `Const<{pointer}>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.

View file

@ -0,0 +1,9 @@
struct ConstFn<const F: fn()>;
//~^ ERROR const generics are unstable
//~^^ ERROR using function pointers as const generic parameters is unstable
struct ConstPtr<const P: *const u32>;
//~^ ERROR const generics are unstable
//~^^ ERROR using raw pointers as const generic parameters is unstable
fn main() {}

View file

@ -0,0 +1,39 @@
error[E0658]: const generics are unstable
--> $DIR/feature-gate-const_generics-ptr.rs:1:22
|
LL | struct ConstFn<const F: fn()>;
| ^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
= help: add `#![feature(const_generics)]` to the crate attributes to enable
error[E0658]: const generics are unstable
--> $DIR/feature-gate-const_generics-ptr.rs:5:23
|
LL | struct ConstPtr<const P: *const u32>;
| ^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
= help: add `#![feature(const_generics)]` to the crate attributes to enable
error[E0658]: using function pointers as const generic parameters is unstable
--> $DIR/feature-gate-const_generics-ptr.rs:1:25
|
LL | struct ConstFn<const F: fn()>;
| ^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/53020
= help: add `#![feature(const_compare_raw_pointers)]` to the crate attributes to enable
error[E0658]: using raw pointers as const generic parameters is unstable
--> $DIR/feature-gate-const_generics-ptr.rs:5:26
|
LL | struct ConstPtr<const P: *const u32>;
| ^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/53020
= help: add `#![feature(const_compare_raw_pointers)]` to the crate attributes to enable
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0658`.