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

Recover and suggest wrapping in block in const generic value application #61175

Closed
Centril opened this issue May 25, 2019 · 3 comments
Closed
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. const-generics-bad-diagnostics An error is correctly emitted, but is confusing, for `min_const_generics`. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Centril
Copy link
Contributor

Centril commented May 25, 2019

cc @varkor @rust-lang/wg-diagnostics

Say we have:

#![feature(const_generics)]

fn foo<const N: usize>() {}

fn bar() {
    foo::<1 + 1>();
}

the parser now greets you with:

error: expected one of `,` or `>`, found `+`
 --> src/lib.rs:6:13
  |
6 |     foo::<1 + 1>();
  |             ^ expected one of `,` or `>` here

It would be good if you got:

error: expected one of `,` or `>`, found `+`
 --> src/lib.rs:6:13
  |
6 |     foo::<1 + 1>();
  |             ^ expected one of `,` or `>` here
  |
  | help: wrap the expression in a block:
6 |     foo::<{1 + 1}>();

or some such.

@Centril Centril added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-const-generics Area: const generics (parameters and arguments) labels May 25, 2019
@oli-obk
Copy link
Contributor

oli-obk commented May 25, 2019

Won't attempting to fix this issue hit exactly the same issues that just allowing foo::<1 + 1>(); hits?

@estebank
Copy link
Contributor

@oli-obk allowing blockless const expressions result in ambiguous grammar for the general case, but there's a subset that is unambiguous, and we get away with best effort attempts for the sake of diagnostics. Being able to parse almost any const expression with the exception of those involving greater than is great for error recovery, but would be terrible for the feature/grammar.

@Centril Centril added F-const_generics `#![feature(const_generics)]` C-enhancement Category: An issue proposing an enhancement or a PR with one. requires-nightly This issue requires a nightly compiler in some way. labels Aug 6, 2019
estebank added a commit to estebank/rust that referenced this issue Sep 23, 2019
When encountering `foo::<1 + 1>()`, suggest `foo::<{ 1 + 1 }>()`.

Fix rust-lang#61175.
estebank added a commit to estebank/rust that referenced this issue Sep 30, 2019
When encountering `foo::<1 + 1>()`, suggest `foo::<{ 1 + 1 }>()`.

Fix rust-lang#61175.
@varkor varkor added the const-generics-bad-diagnostics An error is correctly emitted, but is confusing, for `min_const_generics`. label Sep 13, 2020
@varkor
Copy link
Member

varkor commented Oct 2, 2020

#70753 is a duplicate of this issue, but I'm going to close this one because it has less discussion.

@varkor varkor closed this as completed Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. const-generics-bad-diagnostics An error is correctly emitted, but is confusing, for `min_const_generics`. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants