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

Alternative syntax suggestion for tilde keyword (~const): where const #107003

Closed
edward-shen opened this issue Jan 17, 2023 · 5 comments
Closed
Labels
F-const_trait_impl `#![feature(const_trait_impl)]` T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@edward-shen
Copy link
Contributor

edward-shen commented Jan 17, 2023

In #67792 it was suggested to file a new issue regarding any additional discussion, and it looks like the syntax for ~const is not finalized. In rust-lang/lang-team#162 and #102090, it still looks like ~const is still a placeholder keyword. I'd like to propose another syntax: where const

fn triple_add<T>(a: T, b: T, c: T) -> T
where
    T: Add<Output=T>,
where const
    T: const Add<Output=T>,
{
    a + b + c
}

Like with normal trait bounds, we would add another where clause to functions that define when the function is const. In the previous example, there is a type bounds on T that always applies, followed by the declaration that triple_add is const if and only if the Add implementation is const.

While this looks a little verbose, where const allows for different bounds to permit const as well. If there was a ConstAdd trait that was always const, we could express the following instead:

fn triple_add<T>(a: T, b: T, c: T) -> T
where
    T: Add<Output=T>,
where const
    T: ConstAdd<Output=T>,
{
    a + b + c
}

This gives us flexibility in different how different the const bounds are expressed in the future, should we decide to expand or restrict it.

A trait could express const bounds as well in a similar way:

trait Foo<Other>
where const
  Other: const Bar,
{}

I think this form has a lot of benefits over the existing ~const:

  1. where const is more familiar than ~const in my opinion: where adds bounds to the function, so its more intuitive that where const adds const bounds the the function.
  2. It allows us to determine the inline representation later, if one is desired.
  3. It better expresses that const is opt-in, without the const keyword in the front
  4. It allows flexibility in how const bounds are expressed, in the way that other traits could express constness, as shown with the previous snippets.
  5. It also gives a framework for keyword generics, e.g. with async:
fn triple_add<T>(a: T, b: T, c: T) -> T
where
    T: Add<Output=T>,
where async
    T: async Add<Output=T>,
{ /* ... */ }
  1. We can restrict requirements if needed, such as requiring all where const clauses to be T: const Trait where Trait is defined in the where bound, and then later opening up to more free bounds.
  2. It might be easier to implement in terms of rustdoc and rustc implementation, since it would be iterating on the where bounds. That being said, I'm not sure of this either.

To the best of my ability, I haven't seen any suggestion of this. Please let me know if I've missed anything.

@compiler-errors
Copy link
Member

For the record,

fn triple_add<T>(a: T, b: T, c: T) -> T

where: // <<<---- this

    T: Add<Output=T>,

is not currently syntax, since we expect a where-clause bound after where instead of a semicolon. Not sure if you're proposing that also, but just wanted to note it.

@edward-shen
Copy link
Contributor Author

Oops, nope, didn't mean to. Thanks for the heads-up.

@edward-shen edward-shen changed the title Alternative syntax suggestion for tilde keyword (~const): where const: Alternative syntax suggestion for tilde keyword (~const): where const Jan 18, 2023
@jyn514
Copy link
Member

jyn514 commented Apr 26, 2023

Triage: ~const was removed altogether in #110393. Is this issue still valid?

@jyn514 jyn514 added T-lang Relevant to the language team, which will review and decide on the PR/issue. F-const_trait_impl `#![feature(const_trait_impl)]` labels Apr 26, 2023
@compiler-errors
Copy link
Member

The feature still exists, it's just not being used in the standard library, but it's gonna get reworked heavily anyways. I'm gonna close this -- if you want to brainstorm new ideas for conditionally const trait bounds, https://internals.rust-lang.org might be a better idea.

@compiler-errors compiler-errors closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2023
@fee1-dead
Copy link
Member

@edward-shen: You could also join the Zulip stream for const eval and open a thread there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-const_trait_impl `#![feature(const_trait_impl)]` T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants