Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5303,6 +5303,17 @@ class MLIRGenImpl
EXIT_IF_FAILED_OR_NO_VALUE(resultRight)
auto rightExpressionValue = V(resultRight);

// a number, a boolean or a bigint has no fields to look up (a string's `length` is an
// extension: tests/00in_method_names.ts)
auto rightBaseType = mth.getBaseType(rightExpressionValue.getType());
if (mth.isNumericType(rightBaseType) || isa<mlir_ts::BooleanType>(rightBaseType)
|| isa<mlir_ts::BigIntType>(rightBaseType))
{
emitError(loc(binaryExpressionAST->right), "The right-hand side of an 'in' expression must not be a primitive: '")
<< to_print(rightExpressionValue.getType()) << "'";
return mlir::failure();
}

if (isa<mlir_ts::UnionType>(rightExpressionValue.getType()))
{
emitError(loc(binaryExpressionAST->right), "not supported");
Expand Down
10 changes: 10 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,16 @@ set_tests_properties(test-compile-ref-index-store
PROPERTIES PASS_REGULAR_EXPRESSION "store\\.ts:8:5: error: indexing a 'Ref<T>' gives a reference to the element; store through it with 'Deref\\(p\\[i\\]\\) = value'"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed")

# `in` with a number, a boolean or a bigint on the right is an error: it was quietly false. A
# string on the right stays an extension (its `length`).
add_test(NAME test-compile-diagnostic-location-in-primitive
COMMAND $<TARGET_FILE:tslang> --emit=obj --no-default-lib -mm=none
"${PROJECT_SOURCE_DIR}/test/tester/diagnostic-location/in_primitive.ts"
-o "${CMAKE_CURRENT_BINARY_DIR}/diagnostic-location-in-primitive.obj")
set_tests_properties(test-compile-diagnostic-location-in-primitive
PROPERTIES PASS_REGULAR_EXPRESSION "in_primitive\\.ts:7:18: error: The right-hand side of an 'in' expression must not be a primitive: '5'"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed|in_primitive\\.ts:6:")

# A number, string, boolean or array passed where an interface with members is expected is an
# error; it compiled to a cast lowering could not make, and the callee read garbage. An interface
# without members still takes any value.
Expand Down
8 changes: 8 additions & 0 deletions tslang/test/tester/diagnostic-location/in_primitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// The right-hand side of `in` that is a number had no fields to look in, and the test was
// quietly false.

function main() {
const s = "hello";
print("length" in s);
print("a" in 5);
}
Loading