D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22708 - switch statement with an undefined symbol results in many errors
Summary: switch statement with an undefined symbol results in many errors
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 minor
Assignee: No Owner
URL:
Keywords: diagnostic, pull
Depends on:
Blocks:
 
Reported: 2022-01-27 18:29 UTC by Steven Schveighoffer
Modified: 2022-03-08 16:27 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Steven Schveighoffer 2022-01-27 18:29:35 UTC
Consider this code:

```d
enum Foo
{
   one, two
}

void main()
{
   final switch(foo()) { // oops, forgot to define foo()
      case Foo.one: break;
      case Foo.two: break;
   }
}
```

In addition to the correct error of "foo" not being defined, the compiler will emit nonsense errors based on the case statements.

The error has changed throughout the years.

for 2.097, the error is:

Error: duplicate `case two` in `switch` statement

Note the missing "Foo." prefix, no error about case `Foo.one`, and also, the nonsensical error.

For 2.098.1, the error has changed to:

Error: `case` must be a `string` or an integral constant, not `one`
Error: `case` must be a `string` or an integral constant, not `two`

Such errors should either be omitted, or adjusted to be less confusing.
Comment 1 Dlang Bot 2022-02-07 16:00:58 UTC
@MoonlightSentinel created dlang/dmd pull request #13617 "Fix 22708 - Extends semantic for switch statements with invalid values" fixing this issue:

- Fix 22708 - Extends semantic for switch statements with invalid values
  
  Only omitting the cast allows further semantic to perform the expected
  checks and transformations. This allows for better error detection that
  were not reported before and also avoids invalid errors due to missing
  `optimize` calls.

https://github.com/dlang/dmd/pull/13617
Comment 2 Dlang Bot 2022-03-04 14:30:20 UTC
@Panke updated dlang/dmd pull request #13755 "Fix Issue 21844: makedeps contain non-existing files" fixing this issue:

- Fix 22708 - Extend semantic for switch statements with invalid values (#13617)
  
  Only omitting the cast allows further semantic to perform the expected
  checks and transformations. This allows for better error detection that
  were not reported before and also avoids invalid errors due to missing
  `optimize` calls.

https://github.com/dlang/dmd/pull/13755
Comment 3 Dlang Bot 2022-03-08 15:05:54 UTC
@MartinNowak created dlang/dmd pull request #13785 "merge stable" fixing this issue:

- Fix 22708 - Extend semantic for switch statements with invalid values (#13617)
  
  Only omitting the cast allows further semantic to perform the expected
  checks and transformations. This allows for better error detection that
  were not reported before and also avoids invalid errors due to missing
  `optimize` calls.

https://github.com/dlang/dmd/pull/13785
Comment 4 Dlang Bot 2022-03-08 16:27:12 UTC
dlang/dmd pull request #13785 "merge stable" was merged into master:

- 160e3ebeaed0448a81322b2c19a0ac156445d61e by Florian:
  Fix 22708 - Extend semantic for switch statements with invalid values (#13617)
  
  Only omitting the cast allows further semantic to perform the expected
  checks and transformations. This allows for better error detection that
  were not reported before and also avoids invalid errors due to missing
  `optimize` calls.

https://github.com/dlang/dmd/pull/13785