Skip to content

fn's can be used as const generics #84238

Closed
@DutchGhost

Description

@DutchGhost
Contributor

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?

Activity

added
A-const-genericsArea: const generics (parameters and arguments)
C-bugCategory: This is a bug.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Apr 16, 2021
RalfJung

RalfJung commented on Apr 18, 2021

@RalfJung
Member

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

RalfJung commented on Apr 18, 2021

@RalfJung
Member

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

added and removed on Jun 28, 2022
lcnr

lcnr commented on Jun 28, 2022

@lcnr
Contributor
#![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

DutchGhost commented on Jul 2, 2022

@DutchGhost
ContributorAuthor

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 ()>;
RalfJung

RalfJung commented on Jan 9, 2024

@RalfJung
Member

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.

added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Jan 9, 2024
lcnr

lcnr commented on Jan 9, 2024

@lcnr
Contributor

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

4 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.E-needs-testCall 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-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @RalfJung@jonas-schievink@BoxyUwU@DutchGhost@lcnr

      Issue actions

        fn's can be used as const generics · Issue #84238 · rust-lang/rust