google / google/closure-compiler
switch with default goog.asserts.fail should guarantee any other case
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
Hi,
try to compile the following `test.js` code:
``` javascript
/**
* @param {number} y
* @return {!Date}
*/
var a = function(y) {
var x;
switch(y) {
case 0: x = new Date(); break;
default: goog.asserts.fail('x would be undefined if we go beyond this line');
}
return x;
};
```
with
```
java -jar /home/user/bin/closure-compiler/build/compiler.jar \
--compilation_level ADVANCED_OPTIMIZATIONS --jscomp_error=checkTypes \
test.js \
closure-library/closure/goog/asserts/asserts.js \
lib/closure-library/closure/goog/debug/error.js \
lib/closure-library/closure/goog/string/string.js \
lib/closure-library/closure/goog/dom/nodetype.js \
lib/closure-library/closure/goog/base.js
```
No error should arise, since the default case is a fail. But the compiler insists:
```
test.js:11: ERROR - inconsistent return type
found : (Date|undefined)
required: Date
return x;
^
1 error(s), 0 warning(s), 95.7% typed
```
Contributor guide
Assessment
This issue has not been assessed yet.