Bug 22514 – Invalid duplicate case error when the switched value has errors
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-11-16T15:46:21Z
Last change time
2021-12-14T10:34:24Z
Keywords
pull
Assigned to
No Owner
Creator
moonlightsentinel
Comments
Comment #0 by moonlightsentinel — 2021-11-16T15:46:21Z
The following code causes invalid `duplicate case X` errors when compiled with dmd > 2.094:
void main()
{
switch (doesNotExist)
{
case 1: break;
case 2: break;
case 3: break;
}
}
Apparently introduced / revealed by https://github.com/dlang/dmd/pull/11467.
Comment #1 by dlang-bot — 2021-11-16T16:45:14Z
@MoonlightSentinel created dlang/dmd pull request #13309 "Fix 22514 - Don't raise invalid duplicate case errors when the switch..." fixing this issue:
- Fix 22514 - Don't raise invalid duplicate case errors when the switch...
... has other errors.
The duplicate case checks triggered because #11467 made `ErrorExp` into
a singleton and hence caused `ErrorExp` == `ErrorExp` to yield true even
if those errors were caused by different expressions.
This situation can arise from two different user errors:
1. the case is faulty, e.g. an unknown variable
2. the switch'ed value is faulty, causing `implicitCastTo` to generate
an `ErrorExp`.
This commit fixes (1) by simply skipping the ducplicated case check for
`ErrorExp` because it doesn't have enough information. (2) requires the
case expression to not be coerced into the switched type s.t. we can
at least detect some errors while also keeping the actual case
expression to offer helpful diagnostics.
https://github.com/dlang/dmd/pull/13309
Comment #3 by moonlightsentinel — 2021-11-16T16:50:03Z
*** Issue 21389 has been marked as a duplicate of this issue. ***
Comment #4 by dlang-bot — 2021-11-16T17:57:18Z
@MoonlightSentinel created dlang/dmd pull request #13310 "[IGNORE] Check if other code relyies on invalid ErrorExp identity" fixing this issue:
- Fix 22514 - Don't raise invalid duplicate case errors when the switch...
... has other errors.
The duplicate case checks triggered because #11467 made `ErrorExp` into
a singleton and hence caused `ErrorExp` == `ErrorExp` to yield true even
if those errors were caused by different expressions.
This situation can arise from two different user errors:
1. the case is faulty, e.g. an unknown variable
2. the switch'ed value is faulty, causing `implicitCastTo` to generate
an `ErrorExp`.
This commit fixes (1) by simply skipping the ducplicated case check for
`ErrorExp` because it doesn't have enough information. (2) requires the
case expression to not be coerced into the switched type s.t. we can
at least detect some errors while also keeping the actual case
expression to offer helpful diagnostics.
https://github.com/dlang/dmd/pull/13310
Comment #5 by dlang-bot — 2021-11-18T18:36:18Z
dlang/dmd pull request #13309 "Fix 22514 - Don't raise invalid duplicate case errors when the switch..." was merged into stable:
- dcdfb7959e2e1036fbbc7de10b2e663d80b80d3f by MoonlightSentinel:
Fix 22514 - Don't raise invalid duplicate case errors when the switch...
... has other errors.
The duplicate case checks triggered because #11467 made `ErrorExp` into
a singleton and hence caused `ErrorExp` == `ErrorExp` to yield true even
if those errors were caused by different expressions.
This situation can arise from two different user errors:
1. the case is faulty, e.g. an unknown variable
2. the switch'ed value is faulty, causing `implicitCastTo` to generate
an `ErrorExp`.
This commit fixes (1) by simply skipping the ducplicated case check for
`ErrorExp` because it doesn't have enough information. (2) requires the
case expression to not be coerced into the switched type s.t. we can
at least detect some errors while also keeping the actual case
expression to offer helpful diagnostics.
https://github.com/dlang/dmd/pull/13309
Comment #6 by dlang-bot — 2021-12-14T10:34:24Z
dlang/dmd pull request #13421 "merge stable" was merged into master:
- d14ed1e9c2183a533b6f275be2c6692e9aad4e9e by MoonlightSentinel:
Fix 22514 - Don't raise invalid duplicate case errors when the switch...
... has other errors.
The duplicate case checks triggered because #11467 made `ErrorExp` into
a singleton and hence caused `ErrorExp` == `ErrorExp` to yield true even
if those errors were caused by different expressions.
This situation can arise from two different user errors:
1. the case is faulty, e.g. an unknown variable
2. the switch'ed value is faulty, causing `implicitCastTo` to generate
an `ErrorExp`.
This commit fixes (1) by simply skipping the ducplicated case check for
`ErrorExp` because it doesn't have enough information. (2) requires the
case expression to not be coerced into the switched type s.t. we can
at least detect some errors while also keeping the actual case
expression to offer helpful diagnostics.
https://github.com/dlang/dmd/pull/13421