Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fn's can be used as const generics #84238

Closed
DutchGhost opened this issue Apr 16, 2021 · 6 comments · Fixed by #127243
Closed

fn's can be used as const generics #84238

DutchGhost opened this issue Apr 16, 2021 · 6 comments · Fixed by #127243
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-adt_const_params `#![feature(adt_const_params)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@DutchGhost
Copy link
Contributor

DutchGhost commented Apr 16, 2021

The following code fails to compile, because using function pointers as const generics is forbidden:

#![feature(const_generics)]

struct A<const F: fn()>;

However, if the function pointer is inside an array, it suddenly compiles:

#![feature(const_generics)]

struct B<const F: [fn(); 1]>;

Is this intentional, or should it also be forbidden?

@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 16, 2021
@RalfJung
Copy link
Member

RalfJung commented Apr 18, 2021

The first is definitely intentional. fn() have an unstable notion of equality, in particular if they point to instances of generic functions: two entirely independent functions might have "equal" function pointers if they happen to compile to the same assembly code, and two identical instances of the same generic function in different crates (or even just different codegen units) might compare "inequal".

@RalfJung
Copy link
Member

RalfJung commented Apr 18, 2021

The second is a bug, good catch! I misread the issue description at first. This should not work in arrays -- I assume this would be fixed by properly moving to valtrees.
Cc @rust-lang/wg-const-eval

@BoxyUwU BoxyUwU added F-adt_const_params `#![feature(adt_const_params)]` and removed F-const_generics `#![feature(const_generics)]` labels Jun 28, 2022
@lcnr
Copy link
Contributor

lcnr commented Jun 28, 2022

#![feature(adt_const_params)]

struct B<const F: [fn(); 1]>;

still compiles, the issue is that function pointers are currently considered to be structural match. I intend to work on structural equality in the near (not that near) future, going to assign this to myself.

@rustbot claim

@DutchGhost
Copy link
Contributor Author

DutchGhost commented Jul 2, 2022

Same issue with raw pointers (both *const _ and *mut _):

#![feature(adt_const_params)]

struct Works<const F: [*mut (); 1]>; // Should not work

struct DoesNotWork<const F: *mut ()>;

@BoxyUwU BoxyUwU unassigned lcnr Feb 3, 2023
@RalfJung
Copy link
Member

RalfJung commented Jan 9, 2024

This no longer works:

#![feature(adt_const_params)]

struct Works<const F: [*mut (); 1]>; // Should not work

Looks like the issue got fixed? We should make sure we have a test though.

@lcnr lcnr added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jan 9, 2024
@lcnr
Copy link
Contributor

lcnr commented Jan 9, 2024

this happens as const generics now relies on ConsParamTy instead of simply using structural match.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 2, 2024
…_test, r=compiler-errors

Add test for adt_const_params

Fixes rust-lang#84238

r? `@compiler-errors`
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 2, 2024
Rollup merge of rust-lang#127243 - BoxyUwU:new_invalid_const_param_ty_test, r=compiler-errors

Add test for adt_const_params

Fixes rust-lang#84238

r? `@compiler-errors`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-adt_const_params `#![feature(adt_const_params)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants