Skip to content

Commit 7f3967c

Browse files
richmckeevercopybara-github
authored andcommitted
Include the span of the argument when InferImplicitParametrics fails on a function argument.
See #3979 The mistake in that example is the wrong order of the intended names in the `let (data, tok) = recv(...)`, which actually leads to the first arg to send being the wrong type. This change adds the span for the `tok` argument of the actual `send` call to the error (that is the point where type inference actually fails). PiperOrigin-RevId: 886977632
1 parent ce5dd34 commit 7f3967c

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

xls/dslx/type_system_v2/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ cc_library(
228228
"//xls/dslx:errors",
229229
"//xls/dslx:import_data",
230230
"//xls/dslx:interp_value",
231+
"//xls/dslx:status_payload_utils",
231232
"//xls/dslx:warning_collector",
232233
"//xls/dslx/frontend:ast",
233234
"//xls/dslx/frontend:ast_cloner",

xls/dslx/type_system_v2/inference_table_converter_impl.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "xls/dslx/frontend/semantics_analysis.h"
5656
#include "xls/dslx/import_data.h"
5757
#include "xls/dslx/interp_value.h"
58+
#include "xls/dslx/status_payload_utils.h"
5859
#include "xls/dslx/type_system/parametric_env.h"
5960
#include "xls/dslx/type_system/type.h"
6061
#include "xls/dslx/type_system/type_info.h"
@@ -2089,8 +2090,11 @@ class InferenceTableConverterImpl : public InferenceTableConverter,
20892090
resolver_->ResolveIndirectTypeAnnotations(
20902091
target_context, actual_args[i], formal_types[i],
20912092
TypeAnnotationFilter::None()));
2092-
return TypeMismatchError(actual_arg_context, direct_actual_arg_type,
2093-
direct_formal_arg_type);
2093+
absl::Status error = TypeMismatchError(
2094+
actual_arg_context, direct_actual_arg_type, direct_formal_arg_type);
2095+
AddSpanToStatusPayload(error, actual_args[i]->span(),
2096+
const_cast<FileTable&>(file_table_));
2097+
return error;
20942098
}
20952099
for (auto& [binding, value_or_type] : *resolved) {
20962100
VLOG(5) << "Inferred implicit parametric value: "

xls/dslx/type_system_v2/typecheck_module_v2_builtin_test.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,36 @@ proc P {
16211621
TypecheckFails(HasTypeMismatch("bool", "u32")));
16221622
}
16231623

1624+
TEST(TypecheckV2Test, SendWithWrongArgumentOrder) {
1625+
EXPECT_THAT(R"(
1626+
struct S {
1627+
x: u32
1628+
}
1629+
1630+
type Foo = S;
1631+
1632+
proc Rep {
1633+
input: chan<Foo> in;
1634+
output: chan<Foo> out;
1635+
1636+
init { () }
1637+
1638+
config(input: chan<Foo> in, output: chan<Foo> out) {
1639+
(input, output)
1640+
}
1641+
1642+
next(a: ()) {
1643+
let (data, tok) = recv(join(), input);
1644+
let tok = send(tok, output, data);
1645+
}
1646+
}
1647+
)",
1648+
TypecheckFailsWithPayload(HasTypeMismatch("S", "token"),
1649+
// The span of the `tok` argument in the
1650+
// `send` call should be one of them.
1651+
HasSpan(21, 19, 21, 22)));
1652+
}
1653+
16241654
TEST(TypecheckV2BuiltinTest, Recv) {
16251655
EXPECT_THAT(
16261656
R"(

0 commit comments

Comments
 (0)