Skip to content

const fn and generics #1

Open
Open
@RalfJung

Description

@RalfJung

Now that I came to like const soundness, of course I had to start thinking about how to achieve const soundness in the presence of generics. The problem is that in a const fn foo<T: Trait>, when we call T::method, how can we know if that method is safe to call in const context? When type-checking the method body, we cannot know if that will later resolve to a const fn or not.

There was plenty of discussion on this that I did not follow closely, but it seems it mostly happened in chat so I don't know of a good summary. @Centril was involved, probably @eddyb and @oli-obk and I am not sure who else?

I propose to discuss this further here so that we have a place to look at. I see two fundamental approaches:

  • Find some way to solve this and obtain a const sound generic type system, i.e., obtain a static guarantee that (safe) const code will not even attempt to call a non-const fn. (By "static" I mean that we detect this before even starting CTFE, just on the type system level. In our context here, "dynamic" means "during CTFE evaluation", i.e. when miri runs).
  • Punt on this. We will detect this a CTFE evaluation time. Our type system would only be const sound for non-generic code. Not very satisfying, but it's worth keeping in mind that this is not a horrible alternative, which puts an upper bound on the amount of complexity we should pile up to avoid this outcome. :D

(I understand there are other open questions around const fn, like avoiding having to manually mark every single function as const. Things like const mod have been proposed. Feel free to open a separate issue for those concerns, but I consider such discussion to be off-topic in this issue.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions