From 33fe3b79e8151cc5d2c3266e9be7ee02384a7bed Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 17 Oct 2026 14:09:46 +0100 Subject: [PATCH] A static field's failed initializer is an error `class A { static s: Ref = null; }` compiled with no diagnostic, while the same initializer on an instance field or a `let` is an error. registerVariable returned no type for the field, and the field was registered anyway: the cast error, postponed like every message, was never shown because the module did not fail. The dynamic-import twin of this code already checked for it. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenClasses.cpp | 7 +++++++ tslang/test/tester/CMakeLists.txt | 8 ++++++++ .../diagnostic-location/static_field_initializer.ts | 7 +++++++ 3 files changed, 22 insertions(+) create mode 100644 tslang/test/tester/diagnostic-location/static_field_initializer.ts diff --git a/tslang/lib/TypeScript/MLIRGenClasses.cpp b/tslang/lib/TypeScript/MLIRGenClasses.cpp index 489e403b0..2caeb893b 100644 --- a/tslang/lib/TypeScript/MLIRGenClasses.cpp +++ b/tslang/lib/TypeScript/MLIRGenClasses.cpp @@ -1540,6 +1540,13 @@ namespace mlirgen }, genContext); + // a failed initializer (e.g. `null` for a `Ref`) leaves no type; going on with + // none dropped its diagnostic, since those are only shown when the module fails + if (!staticFieldType) + { + return mlir::failure(); + } + auto &staticFieldInfos = newClassPtr->staticFields; pushStaticField(staticFieldInfos, fieldId, staticFieldType, fullClassStaticFieldName, -1, accessLevel); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 5feb2e703..bc19af0fc 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -2939,6 +2939,14 @@ add_test(NAME test-compile-diagnostic-location-field-initializer set_tests_properties(test-compile-diagnostic-location-field-initializer PROPERTIES PASS_REGULAR_EXPRESSION "field_initializer\\.ts:6:5: error: can't cast from 'null' to 'Reference'" FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed|field_initializer\\.ts:1:") +# A static field's initializer is checked like an instance field's: it compiled with no error at all. +add_test(NAME test-compile-diagnostic-location-static-field-initializer + COMMAND $ --emit=obj --no-default-lib -mm=none + "${PROJECT_SOURCE_DIR}/test/tester/diagnostic-location/static_field_initializer.ts" + -o "${CMAKE_CURRENT_BINARY_DIR}/diagnostic-location-static-field-initializer.obj") +set_tests_properties(test-compile-diagnostic-location-static-field-initializer + PROPERTIES PASS_REGULAR_EXPRESSION "static_field_initializer\\.ts:6:32: error: can't cast from 'null' to 'Reference' in 'strict null mode'" + FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed|static_field_initializer\\.ts:1:") add_test(NAME test-compile-diagnostic-location-implicit-constructor COMMAND $ --emit=obj --no-default-lib -mm=none "${PROJECT_SOURCE_DIR}/test/tester/diagnostic-location/implicit_constructor.ts" diff --git a/tslang/test/tester/diagnostic-location/static_field_initializer.ts b/tslang/test/tester/diagnostic-location/static_field_initializer.ts new file mode 100644 index 000000000..9ddae4d3b --- /dev/null +++ b/tslang/test/tester/diagnostic-location/static_field_initializer.ts @@ -0,0 +1,7 @@ +// A static field's failed initializer compiled with no diagnostic: the field was registered +// without a type, and the error, postponed like every message, was never shown. + +class Args { + static count = 1; + static argv: Ref = null; +}