Skip to content

Commit bb19d7f

Browse files
Fix cross-module interface indexer printer gap and add coverage
cross-module-class-indexer-shared-gap-fix.md flagged interfaces as likely sharing the same latent DeclarationPrinter gap as classes did for index signatures, untested because no cross-module interface-indexer test existed. It did: DeclarationPrinter::print(InterfaceInfo::TypePtr) never re-emitted an interface's index signature ([x: T]: U;) for -shared reimport, so a reimporting module's InterfaceInfo::indexes stayed empty and casting to the interface then indexing it failed with "indexer is not declared" / "Interface member '.index' can't be found". Printing it uncovered a second, narrower bug: an interface's index signature FunctionType carries a leading opaque `this` input (added via getInterfaceMethodNameAndType's funcGenContext.thisType, unlike a class's plain (arg)->result index signature), so naively reusing input(0) as the index-argument type printed the wrong type and crashed an unrelated assert in getIndexSignatureArgumentAndResultTypes on reimport. Fixed by picking input(1) when the signature has more than one input, matching that helper's own "first parameter is Opaque" branch. Unlike the class case (PR #283), no isDynamicImport fallback was needed here - interface method dispatch (including the indexer's synthesized get/set) already goes through vtable slots rather than statically-linked symbols, so AOT -shared linking was never an issue once the printer emitted the signature. New export_interface_indexer.ts/import_interface_indexer.ts cross-module test pair (compile, -shared compile, -jit -shared tiers). 799/799 green. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 44761ad commit bb19d7f

4 files changed

Lines changed: 62 additions & 0 deletions

File tree

tslang/lib/TypeScript/DeclarationPrinter.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,27 @@ namespace typescript
669669
newline();
670670
}
671671

672+
// index signature ([x: T]: U;) - mirrors the class printer's fix
673+
// (see cross-module-class-indexer-shared-gap-fix memory): without
674+
// this, a reimporting module's InterfaceInfo::indexes stays empty
675+
// and `obj[i]` through the interface fails with "indexer is not
676+
// declared" / "Interface member '.index' can't be found".
677+
//
678+
// Unlike a class's index signature (a plain (arg)->result FunctionType),
679+
// an interface's is built via getInterfaceMethodNameAndType with
680+
// funcGenContext.thisType set, which prepends an opaque `this` input -
681+
// so the real index-argument type is input(1), not input(0) (matching
682+
// getIndexSignatureArgumentAndResultTypes's "first parameter is Opaque"
683+
// branch in MLIRTypeHelper.h).
684+
for (auto indexInfo : interfaceType->indexes)
685+
{
686+
if (!indexInfo.indexSignature || indexInfo.indexSignature.getNumResults() == 0)
687+
continue;
688+
689+
auto argIndex = indexInfo.indexSignature.getNumInputs() > 1 ? 1 : 0;
690+
printIndexer(indexInfo.indexSignature.getInput(argIndex), indexInfo.indexSignature.getResult(0));
691+
}
692+
672693
// methods (including static)
673694
auto opaqueType = mlir_ts::OpaqueType::get(interfaceType->interfaceType.getContext());
674695
for (auto method : interfaceType->methods)

tslang/test/tester/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ add_test(NAME test-compile-export-import-class-static COMMAND test-runner "${PRO
886886
add_test(NAME test-compile-export-import-class-generic COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_generic.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_generic.ts")
887887
add_test(NAME test-compile-export-import-class-accessor COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_accessor.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_accessor.ts")
888888
add_test(NAME test-compile-export-import-class-indexer COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_indexer.ts")
889+
add_test(NAME test-compile-export-import-interface-indexer COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_interface_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_interface_indexer.ts")
889890
add_test(NAME test-compile-export-import-class-implements-interface-multilevel COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_implements_interface_multilevel.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_implements_interface_multilevel.ts")
890891
add_test(NAME test-compile-export-import-class-implements-interface-optional COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_implements_interface_optional.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_implements_interface_optional.ts")
891892
add_test(NAME test-compile-export-import-class-implements-interface-abstract COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_implements_interface_abstract.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_implements_interface_abstract.ts")
@@ -961,6 +962,7 @@ add_test(NAME test-compile-shared-export-import-class-generic COMMAND test-runne
961962
# in DeclarationPrinter.cpp.
962963
add_test(NAME test-compile-shared-export-import-class-accessor COMMAND test-runner -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_accessor.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_accessor.ts")
963964
add_test(NAME test-compile-shared-export-import-class-indexer COMMAND test-runner -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_indexer.ts")
965+
add_test(NAME test-compile-shared-export-import-interface-indexer COMMAND test-runner -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_interface_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_interface_indexer.ts")
964966
add_test(NAME test-compile-shared-export-import-object-literal-with-interface COMMAND test-runner -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_with_interface.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_with_interface.ts")
965967
add_test(NAME test-compile-shared-export-import-object-literal-untyped COMMAND test-runner -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_untyped.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_untyped.ts")
966968
add_test(NAME test-compile-shared-export-import-object-literal-untyped-multi-method COMMAND test-runner -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_untyped_multi_method.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_untyped_multi_method.ts")
@@ -1002,6 +1004,7 @@ add_test(NAME test-jit-shared-export-import-class-generic COMMAND test-runner -j
10021004
# FIXED: see the matching test-compile-shared-export-import-class-accessor comment above (2026-07-22).
10031005
add_test(NAME test-jit-shared-export-import-class-accessor COMMAND test-runner -jit -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_accessor.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_accessor.ts")
10041006
add_test(NAME test-jit-shared-export-import-class-indexer COMMAND test-runner -jit -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_indexer.ts")
1007+
add_test(NAME test-jit-shared-export-import-interface-indexer COMMAND test-runner -jit -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_interface_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_interface_indexer.ts")
10051008
add_test(NAME test-jit-shared-export-import-object-literal-with-interface COMMAND test-runner -jit -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_with_interface.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_with_interface.ts")
10061009
add_test(NAME test-jit-shared-export-import-object-literal-untyped COMMAND test-runner -jit -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_untyped.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_untyped.ts")
10071010
add_test(NAME test-jit-shared-export-import-object-literal-untyped-multi-method COMMAND test-runner -jit -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_untyped_multi_method.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_untyped_multi_method.ts")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace M {
2+
3+
// Cross-module counterpart to 00interface_indexer.ts - an exported
4+
// interface with an index signature, cast-to from an importer-side class
5+
// implementation. See cross-module-class-indexer-shared-gap-fix memory:
6+
// DeclarationPrinter never re-emits a class's index signature for
7+
// -shared reimport; interfaces may share the same gap since their
8+
// print() function also never mentions `indexes`/InterfaceIndexInfo.
9+
10+
export interface Storage {
11+
[index: number]: string;
12+
}
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import './export_interface_indexer'
2+
3+
class Impl {
4+
#val: string = "";
5+
6+
[index: number]: string;
7+
8+
get(index: number): string {
9+
return this.#val;
10+
}
11+
12+
set(index: number, value: string) {
13+
this.#val = value;
14+
}
15+
}
16+
17+
function main() {
18+
const impl = new Impl();
19+
const s: M.Storage = impl;
20+
21+
s[0] = "hi";
22+
assert(s[0] == "hi");
23+
24+
print("done.");
25+
}

0 commit comments

Comments
 (0)