google / google/xls

fn and lambda should support destructuring

Open
#4,637 3 comments 0 reactions 0 assignees View on GitHub
bug dslx
Dominant language
C++
Stars
1.9k
Forks
283
Avg merge
2d 10h
Merged PRs (30d)
135

Description

**Describe the bug**
Currently when passing an array of tuple to a `map` with lambda, we can't destructuring the element into a tuple.

**To Reproduce**
```
fn muladd(a: u8, b: u8, c: u8) -> u8 {
let p = [(a, b, c)];
map(p, |(a, b, c)| {
a * b + c
})[0]
}
```
0003: fn muladd(a: u8, b: u8, c: u8) -> u8 {
0004: let p = [(a, b, c)];
0005: map(p, |(a, b, c)| {
~~~~~~~~~~~~~~~~~~^ ParseError: Expected 'identifier', got '(': Expected name (definition)
0006: a * b + c
0007: })
```

**Workaround**

Deconstruct the tuple explicitly within the lambda body:
```
fn muladd(a: u8, b: u8, c: u8) -> u8 {
let p = [(a, b, c)];
map(p, |t| {
let (a, b, c) = t;
a * b + c
})[0]
}
```

**Expected behavior**
Tuple can be deconstructed within lambda parameter declaration.

Contributor guide

Open the contributing guide

Research direction

Start with the reported reproducer and its parser diagnostic, then trace how lambda parameter declarations and tuple destructuring are parsed. Verify the expected behavior by compiling the example and confirming it produces the same result as the explicit-deconstruction workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.