-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Errors for if and match in a const are not descriptive #66125
Copy link
Copy link
Closed
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.Diagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.Diagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Consider the following code (Playground):
This is not valid today, since
ifandmatchare forbidden in constants. That restriction can be lifted soon on nightly, so we would like to emit good errors that point the user to the offending code and suggest a feature gate if it would help.However, the errors emitted by the nightly compiler are not great:
At present, each
consthas two errors associated with it. One is triggered by theFakeReademitted for the borrow checker and points to the value being branched on (1andtrue) respectively. The other is triggered by theSwitchIntterminator itself. While the span of this error is good for theif(it points to the wholeif-elseconstruct), it is very confusing for thematch, where it points to the pattern in the first match arm despite theSwitchInthandling all arms simultaneously.I've thought a bit about how to improve this, but I don't have a great solution. I'm hoping someone can help. I don't think we want to rely only on the presence of
FakeReadto reject branchy code in aconst;SwitchIntseems like the natural candidate (maybe with a fallback toFakeReadto detect single-armmatchstatements). However, I don't know how to improve the span for theSwitchIntfor thematch, or if doing so would cause other diagnostics to regress.I think the ideal approach would be to check for this at the HIR or AST level. This would give us very precise spans, and we could continue checking for
SwitchIntin the MIR to make sure the high-level check didn't miss anything. Is this feasible?