Skip to content

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

@ghost

Description

Hi,

try to compile the following test.js code:

/**
 * @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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions