From f5753689e4097cdff3b3ba1c67a8f8ec1541df5f Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 6 Nov 2026 19:44:31 +0000 Subject: [PATCH] An asserted reference (>ref, ref as Reference) reinterprets again #775 made a reference to a number cast to one laid out otherwise an error, in cast(), which explicit assertions reach too. The default library's HashHelpers.hashCodeGeneral reads a number key's bits through `>Ref(key)`, so every Map/Set with number keys failed to compile (map_get_number, map_negative_keys, map_size_delete under every model). Found by the pre-release default-library check before tagging. An assertion between two reference types is a plain reinterpreting cast again, as before #775; the layout check stays for implicit conversions (a call's argument, an assignment). Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenExpressions.cpp | 22 +++++++++++++++++++ tslang/lib/TypeScript/MLIRGenImpl.h | 2 ++ .../tester/tests/00tslang_issues_24_26.ts | 8 +++++++ 3 files changed, 32 insertions(+) diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index fc130f9e1..40b9535fd 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -324,10 +324,27 @@ namespace mlirgen EXIT_IF_FAILED_OR_NO_VALUE(result) auto exprValue = V(result); + if (auto reinterpreted = reinterpretReference(location, typeInfo, exprValue)) + { + return reinterpreted; + } + CAST_A(castedValue, location, typeInfo, exprValue, genContext); return castedValue; } + // `>ref` asserted on a reference to something else reads its bytes as i32s: meant + // (hashing a number's bits), so the layout check an implicit conversion gets (cast) is not made + mlir::Value MLIRGenImpl::reinterpretReference(mlir::Location location, mlir::Type type, mlir::Value value) + { + if (isa(type) && isa(value.getType()) && type != value.getType()) + { + return builder.create(location, type, value); + } + + return mlir::Value(); + } + ValueOrLogicalResult MLIRGenImpl::mlirGen(AsExpression asExpressionAST, const GenContext &genContext) { auto location = loc(asExpressionAST); @@ -346,6 +363,11 @@ namespace mlirgen EXIT_IF_FAILED_OR_NO_VALUE(result) auto exprValue = V(result); + if (auto reinterpreted = reinterpretReference(location, typeInfo, exprValue)) + { + return reinterpreted; + } + CAST_A(castedValue, location, typeInfo, exprValue, genContext); return castedValue; } diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 266e157bc..e9ca4621f 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -4053,6 +4053,8 @@ class MLIRGenImpl ValueOrLogicalResult mlirGen(TypeAssertion typeAssertionAST, const GenContext &genContext); + mlir::Value reinterpretReference(mlir::Location location, mlir::Type type, mlir::Value value); + ValueOrLogicalResult mlirGen(AsExpression asExpressionAST, const GenContext &genContext); ValueOrLogicalResult mlirGen(ComputedPropertyName computedPropertyNameAST, const GenContext &genContext); diff --git a/tslang/test/tester/tests/00tslang_issues_24_26.ts b/tslang/test/tester/tests/00tslang_issues_24_26.ts index 7fe09ea7e..298108597 100644 --- a/tslang/test/tester/tests/00tslang_issues_24_26.ts +++ b/tslang/test/tester/tests/00tslang_issues_24_26.ts @@ -68,5 +68,13 @@ function main() { const ints: int[] = [1, 2, 3]; assert(fillF32(Ref(floats[0])) == 1 && fillInt(Ref(ints[1])) == 2, "references laid out alike"); + // asserted, a reference reads what it refers to as another type: the default library hashes a + // number's bits so. 1.5 is 0x3FF8000000000000 + let d: number = 1.5; + const halves = >Ref(d); + assert(Deref(halves[0]) == 0 && Deref(halves[1]) == 0x3ff80000, "> of a number"); + const low = Ref(d) as Reference; + assert(Deref(low) == 0, "as Reference"); + print("done."); }