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
33 changes: 17 additions & 16 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2116,15 +2116,17 @@ namespace mlirgen

auto src = ss.str();

{
MLIRLocationGuard vgLoc(overwriteLoc);
overwriteLoc = location;
// The generated function's body is generated when the call below instantiates it, not when
// it is parsed, so the call site has to stay the location until then. Without it a
// diagnostic there took the generated text's offset as a position in the user's file - an
// error reported inside a comment on line 1.
MLIRLocationGuard vgLoc(overwriteLoc);
overwriteLoc = location;

if (mlir::failed(parsePartialStatements(src)))
{
assert(false);
return mlir::failure();
}
if (mlir::failed(parsePartialStatements(src)))
{
assert(false);
return mlir::failure();
}

auto funcResult = resolveIdentifier(location, funcName, genContext);
Expand Down Expand Up @@ -2308,15 +2310,14 @@ namespace mlirgen

auto src = ss.str();

{
MLIRLocationGuard vgLoc(overwriteLoc);
overwriteLoc = location;
// kept until the call below has instantiated the function - see castPrimitiveTypeFromAny
MLIRLocationGuard vgLoc(overwriteLoc);
overwriteLoc = location;

if (mlir::failed(parsePartialStatements(src)))
{
assert(false);
return mlir::failure();
}
if (mlir::failed(parsePartialStatements(src)))
{
assert(false);
return mlir::failure();
}

auto funcResult = resolveIdentifier(location, funcName, genContext);
Expand Down
11 changes: 11 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2613,6 +2613,17 @@ foreach(call_arity_case ${call_arity_cases})
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed")
endforeach()

# 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.
add_test(NAME test-compile-diagnostic-location-generated-cast
COMMAND $<TARGET_FILE:tslang> --emit=obj --no-default-lib -mm=none
"${PROJECT_SOURCE_DIR}/test/tester/diagnostic-location/generated_cast.ts"
-o "${CMAKE_CURRENT_BINARY_DIR}/diagnostic-location-generated-cast.obj")
set_tests_properties(test-compile-diagnostic-location-generated-cast
PROPERTIES PASS_REGULAR_EXPRESSION "generated_cast\\.ts:6:23: error: can't cast from 'null' to 'string'"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed|generated_cast\\.ts:1:")

# A call to the collector's API (GC_gcollect, or the weak-link functions DefaultLib's WeakRef uses)
# is an error under every model but gc, which alone links the collector - it used to reach the
# linker as an unresolved symbol. Under gc the same file compiles.
Expand Down
10 changes: 10 additions & 0 deletions tslang/test/tester/diagnostic-location/generated_cast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// An error inside the cast function the compiler generates for a union was reported at the
// generated text's offset taken as a position in this file - inside this comment - rather than
// where the cast is.

function text(x: number | string | null): string {
const s: string = x;
return s;
}

print(text("a"));
Loading