Skip to content

Consider redefining the places ~const bounds are allowed #102497

Open

Description

@fee1-dead

At #102409 (comment), it was pointed out that associated functions can have ~const bounds even though they may be non-const. We need to figure out and define what places ~const should be allowed. Note that since ~const only applies more restrictions on callers that are const, putting them on non-const fns has no effect.

Activity

  1. added
    F-const_trait_impl
    on Sep 30, 2022
  2. oli-obk commented on Sep 30, 2022

    @oli-obk
    Contributor

    Why can't we just forbid it on non const assoc fns?

  3. fee1-dead commented on Sep 30, 2022

    @fee1-dead
    MemberAuthor

    Yes we can. I was also thinking about impl<T: ~const Tr> SomeType {} so that all const fns inherit this bound.

  4. oli-obk commented on Sep 30, 2022

    @oli-obk
    Contributor

    Ah yea, seems ok as long as we have an unused ~const lint for the case where nothing uses it

  5. onestacked commented on Oct 4, 2022

    @onestacked
    Contributor

    Yes we can. I was also thinking about impl<T: ~const Tr> SomeType {} so that all const fns inherit this bound.

    I think that would make a lot of const code a bit cleaner.

  6. oli-obk commented on Jan 11, 2023

    @oli-obk
    Contributor

    I tried implementing this with effects, but it's a bit messy. Right now, we desugar

    impl SomeType {
        const fn foo<T: ~const Trait>() {}
    }

    to

    impl SomeType {
        fn foo<effect C: constness, T: Trait<C>>() {}
    }

    but in order to allow

    impl<T: ~const Tr> SomeType {
        const fn foo() {}
    }

    to use the <T as ~const Tr> information, we'd need an effect available on the impl level, so

    impl<effect C: constness, T: Tr<C>> SomeType {
        const fn foo() {}
        fn bar() {}
    }

    but then non-const fns like bar now suddenly have const effects in their generics. This all seems resolvable, but I'm going to mark the existing test as // known-bug pointing to this issue and look into this again once the rest works reliably

  7. fee1-dead commented on Jan 11, 2023

    @fee1-dead
    MemberAuthor

    Can we ignore the constness effect in non-const contexts?

  8. oli-obk commented on Jan 11, 2023

    @oli-obk
    Contributor

    Can we ignore the constness effect in non-const contexts?

    The current design treats the presence of a constness effect as the source of truth for whether a function is const fn. I tried going back to the existing constness/is_const_fn_raw checks, but that turned out to cause lots of problems in other situations so I stopped pursuing this for now.

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

    F-const_trait_impl

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions