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."); }