google / google/closure-compiler

switch with default goog.asserts.fail should guarantee any other case

Open
#614 2 comments 0 reactions 0 assignees View on GitHub
enhancement
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.