Errors for if
and match
in a const
are not descriptive
#66125
Labels
A-const-eval
Area: Constant evaluation, covers all const contexts (static, const fn, ...)
A-diagnostics
Area: Messages for errors, warnings, and lints
D-papercut
Diagnostics: An error or lint that needs small tweaks.
D-verbose
Diagnostics: Too much output caused by a single piece of incorrect code.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Consider the following code (Playground):
This is not valid today, since
if
andmatch
are 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
const
has two errors associated with it. One is triggered by theFakeRead
emitted for the borrow checker and points to the value being branched on (1
andtrue
) respectively. The other is triggered by theSwitchInt
terminator itself. While the span of this error is good for theif
(it points to the wholeif-else
construct), it is very confusing for thematch
, where it points to the pattern in the first match arm despite theSwitchInt
handling 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
FakeRead
to reject branchy code in aconst
;SwitchInt
seems like the natural candidate (maybe with a fallback toFakeRead
to detect single-armmatch
statements). However, I don't know how to improve the span for theSwitchInt
for 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
SwitchInt
in the MIR to make sure the high-level check didn't miss anything. Is this feasible?The text was updated successfully, but these errors were encountered: