From e2b846c1d0979fa49d6cdec25acca1eafe333a30 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 29 Oct 2026 00:26:10 +0000 Subject: [PATCH] bigint: literals are bigint, 64-bit /, % and shifts, wrapper calls convert - A bigint literal is typed `bigint` (it was a plain i64). `x ?? 7n` of an optional bigint failed in LLVM (`bigint | i64`, and an i64 compared with a null pointer), and `5n` given for `bigint | string` was an invalid cast. `??` also takes a bigint for a value that is never null. - A literal that does not fit 64 bits is an error: 12345678901234567890n printed -6101065172474983726. - `/`, `%`, shifts and bitwise operators of bigints stay 64-bit integers: `10n / 3n` was 3.33333 through a number, and `1n << 40n` was 256, shifted by 40 mod 32 through an i32. - `Number(x)`, `BigInt(x)`, `String(x)` and `Boolean(x)` called without `new` convert to the primitive, as in JavaScript: they made the class's object, so `BigInt(42) + 1n` and `Number(x) + 0.5` were invalid casts. Tests: 00bigint_ops (gc, rc, none), diagnostic-location/bigint_literal_out_of_range. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenAccessCall.cpp | 20 +++++++++ tslang/lib/TypeScript/MLIRGenExpressions.cpp | 16 +++++-- tslang/lib/TypeScript/MLIRGenImpl.h | 38 +++++++++++++++- tslang/test/tester/CMakeLists.txt | 6 ++- .../bigint_literal_out_of_range.ts | 5 +++ tslang/test/tester/tests/00bigint_ops.ts | 43 +++++++++++++++++++ 6 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 tslang/test/tester/diagnostic-location/bigint_literal_out_of_range.ts create mode 100644 tslang/test/tester/tests/00bigint_ops.ts diff --git a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp index 308a8e149..4c1b74e44 100644 --- a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp +++ b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp @@ -2551,6 +2551,26 @@ namespace mlirgen } }) .Case([&](auto classType) { + // `Number(x)`, `BigInt(x)`, `String(x)` and `Boolean(x)` called without `new` convert `x` to the + // primitive, as in JavaScript: made the class's object, `BigInt(42) + 1n` was an invalid cast + // of the bigint to the class + if (operands.size() == 1) + { + auto name = classType.getName().getValue(); + mlir::Type primitiveType = name == "Number" ? mlir::Type(getNumberType()) + : name == "BigInt" ? mlir::Type(getBigIntType()) + : name == "String" ? mlir::Type(getStringType()) + : name == "Boolean" ? mlir::Type(getBooleanType()) + : mlir::Type(); + if (primitiveType) + { + value = operands.front().getType() == primitiveType + ? ValueOrLogicalResult(operands.front()) + : cast(location, primitiveType, operands.front(), genContext); + return; + } + } + value = NewClassInstance(location, classType, operands, genContext); }) .Case([&](auto classStorageType) { diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index 405cbcadd..239bb2837 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -2178,11 +2178,21 @@ namespace mlirgen *(bigIntLiteral->text.end() - 1) == S('n') ? bigIntLiteral->text.substr(0, bigIntLiteral->text.length() - 1) : bigIntLiteral->text.c_str())); - // 64-bit whatever the target: a `bigint` literal is modeled as the language's own 64-bit - // integer type; its width is fixed by the language, not by the pointer width. + // 64-bit whatever the target: a `bigint` is the language's own 64-bit integer; its width is fixed + // by the language, not by the pointer width. A literal that does not fit was wrapped silently + // (12345678901234567890n printed -6101065172474983726). + if (newVal.getActiveBits() > 63) + { + emitError(loc(bigIntLiteral)) << "bigint literal '" << convertWideToUTF8(bigIntLiteral->text) + << "' is out of range: a bigint is a 64-bit signed integer"; + return mlir::failure(); + } + + // the literal is a `bigint`: widened, it is one (`x ?? 7n` of an optional bigint), and a union + // with bigint takes it (`5n` given for `bigint | string`); typed by its i64 it was neither auto type = builder.getI64Type(); auto attrVal = mlir::IntegerAttr::get(type, newVal.getExtValue()); - auto literalType = mlir_ts::LiteralType::get(attrVal, type); + auto literalType = mlir_ts::LiteralType::get(attrVal, getBigIntType()); return V(builder.create(loc(bigIntLiteral), literalType, attrVal)); } diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 5a545de2c..e5f0e693a 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -5802,7 +5802,8 @@ class MLIRGenImpl return llvm::all_of(unionType.getTypes(), [&](mlir::Type subType) { return isNeverNullish(subType); }); } - return mth.isValueType(type); + // a bigint is a 64-bit integer: compared with null it was an i64 against a pointer + return mth.isValueType(type) || isa(mth.stripLiteralType(type)); } // the type without null and undefined: what is left once `?? b` did not take `b` @@ -7853,9 +7854,42 @@ class MLIRGenImpl CAST(rightExpressionValue, location, type, rightExpressionValue, genContext); } - // cast step + // A bigint is a 64-bit integer, and so is what `/`, `%`, a shift or a bitwise operator makes of + // two of them: `10n / 3n` is 3n, not 3.33333 through a number, and `1n << 40n` is not shifted by + // 40 mod 32 through an i32. + auto isBigInt = [&](mlir::Value value) { return isa(mth.stripLiteralType(value.getType())); }; + auto bigIntOperation = false; switch (opCode) { + case SyntaxKind::SlashToken: + case SyntaxKind::PercentToken: + case SyntaxKind::LessThanLessThanToken: + case SyntaxKind::GreaterThanGreaterThanToken: + case SyntaxKind::AmpersandToken: + case SyntaxKind::BarToken: + case SyntaxKind::CaretToken: + bigIntOperation = isBigInt(leftExpressionValue) || isBigInt(rightExpressionValue); + break; + default: + break; + } + + if (bigIntOperation) + { + if (leftExpressionValue.getType() != getBigIntType()) + { + CAST(leftExpressionValue, location, getBigIntType(), leftExpressionValue, genContext); + } + + if (rightExpressionValue.getType() != getBigIntType()) + { + CAST(rightExpressionValue, location, getBigIntType(), rightExpressionValue, genContext); + } + } + + // cast step + switch (bigIntOperation ? SyntaxKind::CommaToken : opCode) + { case SyntaxKind::CommaToken: // no cast needed break; diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 78f848e9b..79409a413 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -753,6 +753,7 @@ tslang_add_test(NAME test-compile-00-spread COMMAND test-runner "${PROJECT_SOURC tslang_add_test(NAME test-compile-01-spread COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01spread.ts") tslang_add_test(NAME test-compile-00-types-unknown1 COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_unknown1.ts") tslang_add_test(NAME test-compile-00-any-to-tuple COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_to_tuple.ts") +tslang_add_test(NAME test-compile-00-bigint-ops COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00bigint_ops.ts") tslang_add_test(NAME test-compile-00-any-literal-to-interface COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_literal_to_interface.ts") tslang_add_test(NAME test-compile-00-unknown-round-trip COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00unknown_round_trip.ts") tslang_add_test(NAME test-compile-00-types-indexed-access-type COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_indexedaccesstype.ts") @@ -1377,6 +1378,7 @@ tslang_add_test(NAME test-jit-00-spread COMMAND test-runner -jit "${PROJECT_SOUR tslang_add_test(NAME test-jit-01-spread COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01spread.ts") tslang_add_test(NAME test-jit-00-types-unknown1 COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_unknown1.ts") tslang_add_test(NAME test-jit-00-any-to-tuple COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_to_tuple.ts") +tslang_add_test(NAME test-jit-00-bigint-ops COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00bigint_ops.ts") tslang_add_test(NAME test-jit-00-any-literal-to-interface COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_literal_to_interface.ts") tslang_add_test(NAME test-jit-00-unknown-round-trip COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00unknown_round_trip.ts") tslang_add_test(NAME test-jit-00-types-indexed-access-type COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_indexedaccesstype.ts") @@ -2479,6 +2481,7 @@ set(TSLANG_CORPUS 00template_literal_escapes.ts 00types_unknown1.ts 00any_to_tuple.ts + 00bigint_ops.ts 00any_literal_to_interface.ts 00unknown_round_trip.ts 00types_utility.ts @@ -3768,7 +3771,8 @@ set_tests_properties(test-compile-diagnostic-location-enum-member-initializer set(diagnostic_location_statement_cases "for_of|for_of\\.ts:6:5: error: Can't resolve property 'length' of type s32" "for_in|for_in\\.ts:6:5: error: Can't resolve property 'length' of type s32" - "using|using\\.ts:5:11: error: Can't resolve property 'dispose' of type s32") + "using|using\\.ts:5:11: error: Can't resolve property 'dispose' of type s32" + "bigint_literal_out_of_range|bigint_literal_out_of_range\\.ts:3:15: error: bigint literal '12345678901234567890n' is out of range") foreach(diagnostic_location_case ${diagnostic_location_statement_cases}) string(REPLACE "|" ";" diagnostic_location_parts "${diagnostic_location_case}") list(GET diagnostic_location_parts 0 diagnostic_location_name) diff --git a/tslang/test/tester/diagnostic-location/bigint_literal_out_of_range.ts b/tslang/test/tester/diagnostic-location/bigint_literal_out_of_range.ts new file mode 100644 index 000000000..e4bc9a7d6 --- /dev/null +++ b/tslang/test/tester/diagnostic-location/bigint_literal_out_of_range.ts @@ -0,0 +1,5 @@ +// A bigint is a 64-bit signed integer: a literal that does not fit is an error, not wrapped. +function main() { + const a = 12345678901234567890n; + print(a); +} diff --git a/tslang/test/tester/tests/00bigint_ops.ts b/tslang/test/tester/tests/00bigint_ops.ts new file mode 100644 index 000000000..520345b4e --- /dev/null +++ b/tslang/test/tester/tests/00bigint_ops.ts @@ -0,0 +1,43 @@ +// A bigint is the language's 64-bit integer. Its literal is a `bigint` (it was an i64): widened it is one, +// so `x ?? 7n` of an optional bigint is a bigint (it was `bigint | i64`, and the null test an i64 against a +// pointer), and `5n` given for `bigint | string` is its bigint member (an invalid cast). `/`, `%`, shifts +// and bitwise operators stay 64-bit integers: `10n / 3n` was 3.33333 through a number, and `1n << 40n` +// shifted by 40 mod 32 through an i32. +function orSeven(x?: bigint) { + return x ?? 7n; +} + +function next(u: bigint | string) { + if (typeof u == "bigint") { + return u + 1n; + } + + return 0n; +} + +function main() { + const big = 1234567890123456789n; + assert(`${big}` == "1234567890123456789", "a literal near the top of the range"); + + assert(orSeven() == 7n, "?? of no value"); + assert(orSeven(3n) == 3n, "?? of a value"); + + assert(next(5n) == 6n, "a literal given for bigint | string"); + assert(next("s") == 0n, "a string given for bigint | string"); + + const a = 10n; + const b = 3n; + assert(a / b == 3n, "division truncates"); + assert(-a / b == -3n, "toward zero"); + assert(a % b == 1n && -a % b == -1n, "remainder"); + assert(1n << 40n == 1099511627776n, "a shift past 32 bits"); + assert((1n << 62n) >> 60n == 4n, "a right shift"); + assert((a & 6n) == 2n && (a | 5n) == 15n && (a ^ 3n) == 9n, "bitwise"); + + let c = 100n; + c /= 7n; + c <<= 33n; + assert(c == 120259084288n, "compound assignments"); + + print("done."); +}