Chromium Code Reviews
DescriptionChange switch to give warnings when cases don't follow the newest syntax.
The current implementation still falls back on if-else-if dispatch if
the case expressions are not compile-time constants. It also falls back
if the expressions fail to follow the specification for any other reason.
The specification requires that case expressions:
- Are compile-time constants.
- All have the same type and are instances of the same class.
- Do not override Object.operator==.
For the last one, I've made exceptions for int/double/String.
Function constants are rejected due to overriding operator==.
I'm not sure they do yet, but they will eventually.
Map and List are accepted. All constant Map/List instances are
expected to be instances of the same class. If we later decide to make
them override operator==, we'll have to reject them too. If we decide
to have a special class for some List/Map constants, we'll let it
override runtimeType to hide it.
Type equality is only checked as far as the constants know their type.
Type parameters are not included in the test since I don't think they
are recorded in the compile-time constant (I could be wrong).
The new switch semantics are still on "hold", so this change retains
the old version too (which accepted any expression). When we want to
remove that, it's a matter of making the warnings into errors and
remove the fallback (the current content of visitSwitchStatement after
the call to tryBuildConstantSwitch).
The validation of switch cases should really happen during resolution
if possible. I'll try to figure out how to move it there in a later
CL.
Committed: https://code.google.com/p/dart/source/detail?r=12189
Patch Set 1 #
Total comments: 16
Patch Set 2 : Address review comments #Patch Set 3 : Merge to head #Patch Set 4 : use .compileType on elements. #
Total comments: 3
Messages
Total messages: 13 (0 generated)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||