From 9bb38b7117b569f5ee14013f25714b55758bf0d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Thu, 16 Nov 2023 02:39:58 +0100 Subject: [PATCH] Fixed a crash when inferring return type of an accessor with errors in its return statement (#56258) --- src/compiler/checker.ts | 2 +- ...eturnTypeErrorInReturnStatement.errors.txt | 16 +++++++++++ ...nferredReturnTypeErrorInReturnStatement.js | 27 +++++++++++++++++++ ...edReturnTypeErrorInReturnStatement.symbols | 19 +++++++++++++ ...rredReturnTypeErrorInReturnStatement.types | 26 ++++++++++++++++++ ...nferredReturnTypeErrorInReturnStatement.ts | 9 +++++++ 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.errors.txt create mode 100644 tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.js create mode 100644 tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.symbols create mode 100644 tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.types create mode 100644 tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 99c2fa4d50969..c28a21f9ce60f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7312,7 +7312,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (propertySymbol.flags & SymbolFlags.Accessor) { const writeType = getWriteTypeOfSymbol(propertySymbol); - if (propertyType !== writeType) { + if (propertyType !== writeType && !isErrorType(propertyType) && !isErrorType(writeType)) { const getterDeclaration = getDeclarationOfKind(propertySymbol, SyntaxKind.GetAccessor)!; const getterSignature = getSignatureFromDeclaration(getterDeclaration); typeElements.push( diff --git a/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.errors.txt b/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.errors.txt new file mode 100644 index 0000000000000..1f3c97970e28b --- /dev/null +++ b/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.errors.txt @@ -0,0 +1,16 @@ +accessorInferredReturnTypeErrorInReturnStatement.ts(2,7): error TS7023: 'primaryPath' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions. +accessorInferredReturnTypeErrorInReturnStatement.ts(4,18): error TS2339: Property 'collection' does not exist on type '{ readonly primaryPath: any; }'. + + +==== accessorInferredReturnTypeErrorInReturnStatement.ts (2 errors) ==== + export var basePrototype = { + get primaryPath() { + ~~~~~~~~~~~ +!!! error TS7023: 'primaryPath' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions. + var _this = this; + return _this.collection.schema.primaryPath; + ~~~~~~~~~~ +!!! error TS2339: Property 'collection' does not exist on type '{ readonly primaryPath: any; }'. + }, + }; + \ No newline at end of file diff --git a/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.js b/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.js new file mode 100644 index 0000000000000..0d1bd77500ce7 --- /dev/null +++ b/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.js @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts] //// + +//// [accessorInferredReturnTypeErrorInReturnStatement.ts] +export var basePrototype = { + get primaryPath() { + var _this = this; + return _this.collection.schema.primaryPath; + }, +}; + + +//// [accessorInferredReturnTypeErrorInReturnStatement.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.basePrototype = void 0; +exports.basePrototype = { + get primaryPath() { + var _this = this; + return _this.collection.schema.primaryPath; + }, +}; + + +//// [accessorInferredReturnTypeErrorInReturnStatement.d.ts] +export declare var basePrototype: { + readonly primaryPath: any; +}; diff --git a/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.symbols b/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.symbols new file mode 100644 index 0000000000000..a2525fe0e7237 --- /dev/null +++ b/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.symbols @@ -0,0 +1,19 @@ +//// [tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts] //// + +=== accessorInferredReturnTypeErrorInReturnStatement.ts === +export var basePrototype = { +>basePrototype : Symbol(basePrototype, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 0, 10)) + + get primaryPath() { +>primaryPath : Symbol(primaryPath, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 0, 28)) + + var _this = this; +>_this : Symbol(_this, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 2, 7)) +>this : Symbol(basePrototype, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 0, 26)) + + return _this.collection.schema.primaryPath; +>_this : Symbol(_this, Decl(accessorInferredReturnTypeErrorInReturnStatement.ts, 2, 7)) + + }, +}; + diff --git a/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.types b/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.types new file mode 100644 index 0000000000000..5c48b09abd26d --- /dev/null +++ b/tests/baselines/reference/accessorInferredReturnTypeErrorInReturnStatement.types @@ -0,0 +1,26 @@ +//// [tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts] //// + +=== accessorInferredReturnTypeErrorInReturnStatement.ts === +export var basePrototype = { +>basePrototype : { readonly primaryPath: any; } +>{ get primaryPath() { var _this = this; return _this.collection.schema.primaryPath; }, } : { readonly primaryPath: any; } + + get primaryPath() { +>primaryPath : any + + var _this = this; +>_this : { readonly primaryPath: any; } +>this : { readonly primaryPath: any; } + + return _this.collection.schema.primaryPath; +>_this.collection.schema.primaryPath : any +>_this.collection.schema : any +>_this.collection : any +>_this : { readonly primaryPath: any; } +>collection : any +>schema : any +>primaryPath : any + + }, +}; + diff --git a/tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts b/tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts new file mode 100644 index 0000000000000..1bc5ff1eac9c8 --- /dev/null +++ b/tests/cases/compiler/accessorInferredReturnTypeErrorInReturnStatement.ts @@ -0,0 +1,9 @@ +// @strict: true +// @declaration: true + +export var basePrototype = { + get primaryPath() { + var _this = this; + return _this.collection.schema.primaryPath; + }, +};