bazelbuild / bazelbuild/rules_closure

reportUnknownTypes can't figure out catch blocks

Open
#181 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
159
Forks
111
PR merge metrics
No merged PRs in 30d

Description

Closure errors out if you try to get `ex.message` from a catch block.

Since reportUnknownTypes is enabled by default and recommended for new code bases with these rules_closure, I'm filing the bug here.

All of the following have errors. However, with --new_type_inf, parseComment1 works.

```js
function parseComment0() {
try {
throw new Error;
} catch (ex) {
return ex.message;
}
}

function parseComment1() {
try {
throw new Error;
} catch (ex) {
if (ex instanceof Error) return ex.message;
}
}

function parseComment2() {
try {
throw new Error;
} catch (ex) {
return /** @type {!Error} */ (ex).message;
}
}

function parseComment3() {
try {
throw new Error;
} catch (ex) {
const error = /** @type {!Error} */ (ex);
return error.message;
}
}
```

Full working repo at https://gist.github.com/jschaf/8614760290a95551a905d07107cfd7e7. Steps to reproduce:

1. git clone https://gist.github.com/jschaf/8614760290a95551a905d07107cfd7e7 closure_catch_bug
2. cd closure_catch_bug
3. bazel build :foo_bin

Related Issues:
https://github.com/google/closure-compiler/issues/427
https://github.com/google/closure-compiler/issues/152

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with the linked repository using `bazel build :foo_bin`, focusing on the `parseComment0` through `parseComment3` catch-block examples and the `reportUnknownTypes` setting. Compare the behavior with `--new_type_inf`; done means valid `ex.message` access in these examples no longer produces errors under the reported configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.