-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Provide suggestions for const
arguments missing braces
#71592
Conversation
This might be because I am used to the current state, so take this with a grain of salt. I don't feel comfortable with allowing While this is inconsequential rn, things are more difficult to change once they are added IMO. |
@lcnr neither work, they both provide a suggestion. |
Oh, seems like I misread the test cases for
🤔 Afaik the only const generic RFC which was merged is https://github.com/rust-lang/rfcs/blob/master/text/2000-const-generics.md This seems to forbid integer operations https://github.com/rust-lang/rfcs/blame/master/text/2000-const-generics.md#L114-L118
|
Integers (and other literals like I was purposely conservative on what new syntax is added, but the recovery code gives you an idea of what would need to be done if we wanted to be fancy and support things like |
When encountering the start of an expression that cannot be a type parameter, parse it as a const expression. For an expression to be valid they must not contain "greater than" or "shift right" operations. The new behavior only parses expressions that begin with a token that can start an expression but can't start a type. Calling functions and methods will cause a type path parsing error.
It's really nice to be able to provide nice diagnostics here, but we shouldn't be increasing the supported expressions. The only expressions that are permitted as const generic arguments without curly brackets are literals and identifiers/paths.
This isn't the merged const generics RFC, though. The merged RFC forbids complex expressions. Even though we can accept some expressions unambiguously, we certainly can't do so consistently (that is, there are some ambiguous expressions). This will be surprising and frustrating for users, who will have little idea what is and is not supported without brackets. That said, I'm excited to see some big improvements to the diagnostics for const generics! I'll take a closer look soon. |
const
argumentsconst
arguments missing braces
@varkor last commit now forbids the parseable expressions and provides a structured suggestion. |
6e7e583
to
9a5a4b0
Compare
This is a tricky area where it's easy to make language-affecting changes, so it'll take some time and effort to review. |
@petrochenkov let me know which of the two PRs (this and #72273) you think has a better change of being accepted first, so that I can rebase one on top of the other (they are right now "sister" PRs with no common parent). |
☔ The latest upstream changes (presumably #72276) made this pull request unmergeable. Please resolve the merge conflicts. |
This can also be unblocked by removing the #72273 part from this PR. |
Closing due to inactivity. |
…ng-braces, r=petrochenkov Suggest that expressions that look like const generic arguments should be enclosed in brackets I pulled out the changes for const expressions from rust-lang#71592 (without the trait object diagnostic changes) and made some small changes; the implementation is `@estebank's.` We're also going to want to make some changes separately to account for trait objects (they result in poor diagnostics, as is evident from one of the test cases here), such as an adaption of rust-lang#72273. Fixes rust-lang#70753. r? `@petrochenkov`
const
arguments:foo::<100 - BAR>();
let
expressions in const argumentsfoo::<BAR + BAR>()
→foo::<{ BAR + BAR }>()
foo::<BAR + 3>()
→foo::<{ BAR + 3 }>()
foo::<BAR - 3>()
→foo::<{ BAR - 3 }>()
foo::<bar::<i32>() + BAR>()
→foo::<{ bar::<i32>() + BAR }>()
foo::<BAR - bar::<i32>()>();
→foo::<{ BAR - bar::<i32>() }>();
r? @petrochenkov