Skip to content

Allow e: unknown in catch arguments #39015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8124,7 +8124,11 @@ namespace ts {
// Handle catch clause variables
const declaration = symbol.valueDeclaration;
if (isCatchClauseVariableDeclarationOrBindingElement(declaration)) {
return anyType;
const decl = declaration as VariableDeclaration;
if (!decl.type) return anyType;
const type = getTypeOfNode(decl.type);
// an errorType will make `checkTryStatement` issue an error
return isTypeAny(type) || type === unknownType ? type : errorType;
}
// Handle export default expressions
if (isSourceFile(declaration) && isJsonSourceFile(declaration)) {
Expand Down Expand Up @@ -33369,8 +33373,9 @@ namespace ts {
if (catchClause) {
// Grammar checking
if (catchClause.variableDeclaration) {
if (catchClause.variableDeclaration.type) {
grammarErrorOnFirstToken(catchClause.variableDeclaration.type, Diagnostics.Catch_clause_variable_cannot_have_a_type_annotation);
if (catchClause.variableDeclaration.type && getTypeOfNode(catchClause.variableDeclaration) === errorType) {
grammarErrorOnFirstToken(catchClause.variableDeclaration.type,
Diagnostics.Catch_clause_variable_type_annotation_must_be_any_or_unknown_if_specified);
}
else if (catchClause.variableDeclaration.initializer) {
grammarErrorOnFirstToken(catchClause.variableDeclaration.initializer, Diagnostics.Catch_clause_variable_cannot_have_an_initializer);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@
"category": "Error",
"code": 1195
},
"Catch clause variable cannot have a type annotation.": {
"Catch clause variable type annotation must be 'any' or 'unknown' if specified.": {
"category": "Error",
"code": 1196
},
Expand Down
61 changes: 54 additions & 7 deletions tests/baselines/reference/catchClauseWithTypeAnnotation.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
tests/cases/compiler/catchClauseWithTypeAnnotation.ts(2,13): error TS1196: Catch clause variable cannot have a type annotation.
tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(17,36): error TS2339: Property 'foo' does not exist on type 'unknown'.
tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(18,37): error TS2339: Property 'foo' does not exist on type 'unknown'.
tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(19,23): error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified.
tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(20,23): error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified.
tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(29,29): error TS2492: Cannot redeclare identifier 'x' in catch clause.
tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(30,29): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'boolean', but here has type 'string'.


==== tests/cases/compiler/catchClauseWithTypeAnnotation.ts (1 errors) ====
try {
} catch (e: any) {
~~~
!!! error TS1196: Catch clause variable cannot have a type annotation.
}
==== tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts (6 errors) ====
type any1 = any;
type unknown1 = unknown;

function fn(x: boolean) {

// no type annotation allowed other than `any` and `unknown`
try { } catch (x) { } // should be OK
try { } catch (x: any) { } // should be OK
try { } catch (x: any1) { } // should be OK
try { } catch (x: unknown) { } // should be OK
try { } catch (x: unknown1) { } // should be OK
try { } catch (x) { x.foo; } // should be OK
try { } catch (x: any) { x.foo; } // should be OK
try { } catch (x: any1) { x.foo; } // should be OK
try { } catch (x: unknown) { console.log(x); } // should be OK
try { } catch (x: unknown1) { console.log(x); } // should be OK
try { } catch (x: unknown) { x.foo; } // error in the body
~~~
!!! error TS2339: Property 'foo' does not exist on type 'unknown'.
try { } catch (x: unknown1) { x.foo; } // error in the body
~~~
!!! error TS2339: Property 'foo' does not exist on type 'unknown'.
try { } catch (x: Error) { } // error in the type
~~~~~
!!! error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified.
try { } catch (x: object) { } // error in the type
~~~~~~
!!! error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified.

try { console.log(); }
// @ts-ignore
catch (e: number) { // e should not be a `number`
console.log(e.toLowerCase());
}

// minor bug: shows that the `catch` argument is skipped when checking scope
try { } catch (x) { let x: string; }
~
!!! error TS2492: Cannot redeclare identifier 'x' in catch clause.
try { } catch (x) { var x: string; }
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'boolean', but here has type 'string'.
!!! related TS6203 tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts:4:13: 'x' was also declared here.
try { } catch (x) { var x: boolean; }

}

104 changes: 98 additions & 6 deletions tests/baselines/reference/catchClauseWithTypeAnnotation.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,102 @@
//// [catchClauseWithTypeAnnotation.ts]
try {
} catch (e: any) {
}
type any1 = any;
type unknown1 = unknown;

function fn(x: boolean) {

// no type annotation allowed other than `any` and `unknown`
try { } catch (x) { } // should be OK
try { } catch (x: any) { } // should be OK
try { } catch (x: any1) { } // should be OK
try { } catch (x: unknown) { } // should be OK
try { } catch (x: unknown1) { } // should be OK
try { } catch (x) { x.foo; } // should be OK
try { } catch (x: any) { x.foo; } // should be OK
try { } catch (x: any1) { x.foo; } // should be OK
try { } catch (x: unknown) { console.log(x); } // should be OK
try { } catch (x: unknown1) { console.log(x); } // should be OK
try { } catch (x: unknown) { x.foo; } // error in the body
try { } catch (x: unknown1) { x.foo; } // error in the body
try { } catch (x: Error) { } // error in the type
try { } catch (x: object) { } // error in the type

try { console.log(); }
// @ts-ignore
catch (e: number) { // e should not be a `number`
console.log(e.toLowerCase());
}

// minor bug: shows that the `catch` argument is skipped when checking scope
try { } catch (x) { let x: string; }
try { } catch (x) { var x: string; }
try { } catch (x) { var x: boolean; }

}


//// [catchClauseWithTypeAnnotation.js]
try {
}
catch (e) {
function fn(x) {
// no type annotation allowed other than `any` and `unknown`
try { }
catch (x) { } // should be OK
try { }
catch (x) { } // should be OK
try { }
catch (x) { } // should be OK
try { }
catch (x) { } // should be OK
try { }
catch (x) { } // should be OK
try { }
catch (x) {
x.foo;
} // should be OK
try { }
catch (x) {
x.foo;
} // should be OK
try { }
catch (x) {
x.foo;
} // should be OK
try { }
catch (x) {
console.log(x);
} // should be OK
try { }
catch (x) {
console.log(x);
} // should be OK
try { }
catch (x) {
x.foo;
} // error in the body
try { }
catch (x) {
x.foo;
} // error in the body
try { }
catch (x) { } // error in the type
try { }
catch (x) { } // error in the type
try {
console.log();
}
// @ts-ignore
catch (e) { // e should not be a `number`
console.log(e.toLowerCase());
}
// minor bug: shows that the `catch` argument is skipped when checking scope
try { }
catch (x) {
var x_1;
}
try { }
catch (x) {
var x;
}
try { }
catch (x) {
var x;
}
}
107 changes: 103 additions & 4 deletions tests/baselines/reference/catchClauseWithTypeAnnotation.symbols
Original file line number Diff line number Diff line change
@@ -1,5 +1,104 @@
=== tests/cases/compiler/catchClauseWithTypeAnnotation.ts ===
try {
} catch (e: any) {
>e : Symbol(e, Decl(catchClauseWithTypeAnnotation.ts, 1, 9))
=== tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts ===
type any1 = any;
>any1 : Symbol(any1, Decl(catchClauseWithTypeAnnotation.ts, 0, 0))

type unknown1 = unknown;
>unknown1 : Symbol(unknown1, Decl(catchClauseWithTypeAnnotation.ts, 0, 16))

function fn(x: boolean) {
>fn : Symbol(fn, Decl(catchClauseWithTypeAnnotation.ts, 1, 24))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 3, 12), Decl(catchClauseWithTypeAnnotation.ts, 29, 27), Decl(catchClauseWithTypeAnnotation.ts, 30, 27))

// no type annotation allowed other than `any` and `unknown`
try { } catch (x) { } // should be OK
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 6, 19))

try { } catch (x: any) { } // should be OK
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 7, 19))

try { } catch (x: any1) { } // should be OK
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 8, 19))
>any1 : Symbol(any1, Decl(catchClauseWithTypeAnnotation.ts, 0, 0))

try { } catch (x: unknown) { } // should be OK
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 9, 19))

try { } catch (x: unknown1) { } // should be OK
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 10, 19))
>unknown1 : Symbol(unknown1, Decl(catchClauseWithTypeAnnotation.ts, 0, 16))

try { } catch (x) { x.foo; } // should be OK
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 11, 19))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 11, 19))

try { } catch (x: any) { x.foo; } // should be OK
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 12, 19))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 12, 19))

try { } catch (x: any1) { x.foo; } // should be OK
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 13, 19))
>any1 : Symbol(any1, Decl(catchClauseWithTypeAnnotation.ts, 0, 0))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 13, 19))

try { } catch (x: unknown) { console.log(x); } // should be OK
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 14, 19))
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 14, 19))

try { } catch (x: unknown1) { console.log(x); } // should be OK
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 15, 19))
>unknown1 : Symbol(unknown1, Decl(catchClauseWithTypeAnnotation.ts, 0, 16))
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 15, 19))

try { } catch (x: unknown) { x.foo; } // error in the body
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 16, 19))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 16, 19))

try { } catch (x: unknown1) { x.foo; } // error in the body
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 17, 19))
>unknown1 : Symbol(unknown1, Decl(catchClauseWithTypeAnnotation.ts, 0, 16))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 17, 19))

try { } catch (x: Error) { } // error in the type
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 18, 19))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

try { } catch (x: object) { } // error in the type
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 19, 19))

try { console.log(); }
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))

// @ts-ignore
catch (e: number) { // e should not be a `number`
>e : Symbol(e, Decl(catchClauseWithTypeAnnotation.ts, 23, 11))

console.log(e.toLowerCase());
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>e : Symbol(e, Decl(catchClauseWithTypeAnnotation.ts, 23, 11))
}

// minor bug: shows that the `catch` argument is skipped when checking scope
try { } catch (x) { let x: string; }
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 28, 19))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 28, 27))

try { } catch (x) { var x: string; }
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 29, 19))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 3, 12), Decl(catchClauseWithTypeAnnotation.ts, 29, 27), Decl(catchClauseWithTypeAnnotation.ts, 30, 27))

try { } catch (x) { var x: boolean; }
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 30, 19))
>x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 3, 12), Decl(catchClauseWithTypeAnnotation.ts, 29, 27), Decl(catchClauseWithTypeAnnotation.ts, 30, 27))

}

Loading