Calling a function with wrong number of arguments and a while
loop or its equivalence yields weird diagnostics
#98894
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
C-bug
Category: This is a bug.
D-confusing
Diagnostics: Confusing error or lint that should be reworked.
D-incorrect
Diagnostics: A diagnostic that is giving misleading or incorrect information.
Explanation / examples
This perfectly wrong statement causes compiler to mention
if
s,!
and()
even though nothing like this is written in the code:(here and after normal errors about argument number/type mismatch are not shown as they are not the problem)
Originally I've found this while editing some async code and passing
String
instead of&str
. I was very confused and thought that it somehow relates toasync
/impl Trait
/etc but after some minification I was left with the snippet above.This error becomes less confusing after desugaring
while
loop by hand:Note that using
break;
is required to trigger this bug, usingbreak
doesn't trigger it for some reason.In fact, you don't even need a
loop
:So in the end, this can be triggered with an
if
/else
one of branches on which is diverging expression with;
that is being passed to a function with wrong number of arguments.Note about confusion / applicability to real programs
As I previously said, I've found this while working on async code and I think this is the main way how this error can be seen (let's be serious, nobody calls functions with
while
loop arguments). The reason why it's easier to find this error with async is thatasync{}
blocks are commonly passed to functions (liketokio::spawn
) and can containwhile
loops inside them.In my case the code looked something like this:
Which caused an error like this:
Note that "expected because of this" points to the last statement in the
while{}
block even though it has nothing to do, really, with the error.Bisection
searched nightlies: from nightly-2022-04-10 to nightly-2022-05-09
regressed nightly: nightly-2022-04-17
searched commit range: 3f391b8...878c783
regressed commit: 07bb916
bisected with cargo-bisect-rustc v0.6.3
Host triple: x86_64-unknown-linux-gnu
Reproduce with:
src/main.rs
:test.sh
:Cause
Bisection shows that this issue was introduced by #92364 which aligns with other observations. This is likely caused by the same underlying problem as #96335: diagnostic code calls
check_expr
a second time which confuses different parts of the compiler.cc @compiler-errors as they've worked on fixing issues caused by #92364 before
Meta
This is reproducible with
1.63.0-beta.3
(2022-07-02 59f577d) and with most nightly-es afternightly-2022-04-17
(except for those that ICE).@rustbot label +A-diagnostics +D-confusing +D-incorrect
The text was updated successfully, but these errors were encountered: