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
18 changes: 18 additions & 0 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,24 @@ namespace mlirgen
{
return castObjectToInterface(location, value, objectType, interfaceType, genContext);
}

// A number, string, boolean or array has no fields an interface's members could be read
// from: passed for an `{ kind: number }` it compiled, without a word, to a cast that
// lowering could not make, and the callee read garbage. An interface without members
// takes any value, as in TypeScript.
auto baseValueType = mth.stripLiteralType(valueType);
if (baseValueType.isIntOrIndexOrFloat()
|| isa<mlir_ts::NumberType, mlir_ts::StringType, mlir_ts::BooleanType, mlir_ts::CharType,
mlir_ts::BigIntType, mlir_ts::ArrayType, mlir_ts::ConstArrayType>(baseValueType))
{
auto interfaceInfo = getInterfaceInfoByFullName(interfaceType.getName().getValue());
if (interfaceInfo && interfaceInfo->getVTableSize() > 0)
{
emitError(location) << "type '" << to_print(baseValueType) << "' can't be cast to interface '"
<< to_print(interfaceType) << "': it has none of the interface's members";
return mlir::failure();
}
}
}

return std::nullopt;
Expand Down
19 changes: 19 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,25 @@ foreach(call_arity_case ${call_arity_cases})
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed")
endforeach()

# 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.
foreach(interface_cast_name number string boolean array)
add_test(NAME test-compile-interface-cast-${interface_cast_name}
COMMAND $<TARGET_FILE:tslang> --emit=obj --no-default-lib -mm=none
"${PROJECT_SOURCE_DIR}/test/tester/interface-cast/${interface_cast_name}.ts"
-o "${CMAKE_CURRENT_BINARY_DIR}/interface-cast-${interface_cast_name}.obj")
set_tests_properties(test-compile-interface-cast-${interface_cast_name}
PROPERTIES PASS_REGULAR_EXPRESSION "can't be cast to interface 'IKind': it has none of the interface's members"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed")
endforeach()
add_test(NAME test-compile-interface-cast-empty
COMMAND $<TARGET_FILE:tslang> --emit=obj --no-default-lib -mm=none
"${PROJECT_SOURCE_DIR}/test/tester/interface-cast/empty.ts"
-o "${CMAKE_CURRENT_BINARY_DIR}/interface-cast-empty.obj")
set_tests_properties(test-compile-interface-cast-empty
PROPERTIES FAIL_REGULAR_EXPRESSION "error:|Stack dump|Assertion failed")

# An error inside a cast function the compiler generates (here: null in a union cast to string) is
# reported where the cast is - line 6 - and not at the generated text's offset taken as a position
# in the user's file, which put it inside the comment on line 1.
Expand Down
11 changes: 11 additions & 0 deletions tslang/test/tester/interface-cast/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// A value with no fields of its own passed where an interface with members is expected compiled
// without a word, and the callee read garbage. It is an error, as in TypeScript.
interface IKind {
kind: number;
}

function f(i: IKind) {
return i.kind;
}

print(f([1, 2]));
11 changes: 11 additions & 0 deletions tslang/test/tester/interface-cast/boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// A value with no fields of its own passed where an interface with members is expected compiled
// without a word, and the callee read garbage. It is an error, as in TypeScript.
interface IKind {
kind: number;
}

function f(i: IKind) {
return i.kind;
}

print(f(true));
9 changes: 9 additions & 0 deletions tslang/test/tester/interface-cast/empty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// An interface without members takes any value, as in TypeScript: this compiles.
interface IEmpty {
}

function f(i: IEmpty) {
return 1;
}

print(f([1, 2]) + f(5) + f("text"));
11 changes: 11 additions & 0 deletions tslang/test/tester/interface-cast/number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// A value with no fields of its own passed where an interface with members is expected compiled
// without a word, and the callee read garbage. It is an error, as in TypeScript.
interface IKind {
kind: number;
}

function f(i: IKind) {
return i.kind;
}

print(f(5));
11 changes: 11 additions & 0 deletions tslang/test/tester/interface-cast/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// A value with no fields of its own passed where an interface with members is expected compiled
// without a word, and the callee read garbage. It is an error, as in TypeScript.
interface IKind {
kind: number;
}

function f(i: IKind) {
return i.kind;
}

print(f("text"));
Loading