Skip to content

Commit 7a729a4

Browse files
erinzmoorecopybara-github
authored andcommitted
Support nested lambdas.
PiperOrigin-RevId: 893702803
1 parent e670644 commit 7a729a4

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

xls/dslx/frontend/semantics_analysis.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ class SideEffectExpressionFinder : public AstNodeVisitorWithDefault {
257257
class CollectNameRefs : public AstNodeVisitorWithDefault {
258258
public:
259259
absl::Status HandleNameRef(const NameRef* node) override {
260+
if (node->IsBuiltin()) {
261+
return DefaultHandler(node);
262+
}
260263
if (node->GetDefiner() == nullptr ||
261264
(node->GetDefiner()->kind() != AstNodeKind::kFunction &&
262265
node->GetDefiner()->kind() != AstNodeKind::kImport)) {
@@ -352,6 +355,7 @@ class ReplaceLambdaWithInvocation : public AstNodeVisitorWithDefault {
352355
// map(arr, lambda_capture{x: x}.call)
353356
// }
354357
absl::Status HandleLambda(const Lambda* node) override {
358+
XLS_RETURN_IF_ERROR(DefaultHandler(node));
355359
Module* module = node->owner();
356360
Function* original_fn = node->function();
357361
Span span = node->span();
@@ -471,6 +475,9 @@ class ReplaceLambdaWithInvocation : public AstNodeVisitorWithDefault {
471475
-> std::optional<AstNode*> {
472476
if (node->kind() == AstNodeKind::kNameRef) {
473477
const NameRef* name_ref = absl::down_cast<const NameRef*>(node);
478+
if (name_ref->IsBuiltin()) {
479+
return std::nullopt;
480+
}
474481
const auto* name_def = std::get<const NameDef*>(name_ref->name_def());
475482
if (name_def != nullptr && seen.contains(name_def)) {
476483
NameRef* self_nr = node->owner()->Make<NameRef>(

xls/dslx/type_system_v2/typecheck_module_v2_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9679,6 +9679,31 @@ const_assert!(ARR[1] == u16:2);
96799679
TypecheckSucceeds(HasNodeWithType("ARR", "uN[16][6]")));
96809680
}
96819681

9682+
TEST(TypecheckV2Test, NestedLambdas) {
9683+
EXPECT_THAT(
9684+
R"(
9685+
fn main() -> u32[4][5] {
9686+
let z = zero!<u32[4][5]>();
9687+
map(enumerate(z), | tup | {
9688+
let i = tup.0;
9689+
let arr = tup.1;
9690+
map(enumerate(arr), | tup2 | {
9691+
let j = tup2.0;
9692+
i + j
9693+
})
9694+
})
9695+
}
9696+
9697+
const_assert!(main() == [[u32:0, 1, 2, 3],
9698+
[u32:1, 2, 3, 4],
9699+
[u32:2, 3, 4, 5],
9700+
[u32:3, 4, 5, 6],
9701+
[u32:4, 5, 6, 7]]);
9702+
9703+
)",
9704+
TypecheckSucceeds(HasNodeWithType("main", "() -> uN[32][4][5]")));
9705+
}
9706+
96829707
TEST(TypecheckV2Test, LambdaWithImplicitReturn) {
96839708
EXPECT_THAT(
96849709
R"(

0 commit comments

Comments
 (0)