google / google/xls

[enhancement][DSLX] if-expr followed by `as` should give diagnostic about precedence

Open
#1,835 0 comments 1 reaction 0 assignees View on GitHub
dslx enhancement
Dominant language
C++
Stars
1.9k
Forks
283
Avg merge
2d 10h
Merged PRs (30d)
135

Description

### What's hard to do? (limit 100 words)

```
$ ~/opt/xlsynth/latest/dslx_interpreter_main /tmp/if_expr.x
/tmp/if_expr.x:1:56-1:58
0001: fn f(x: bool) -> u64 { if x { u32:42 } else { u32:64 } as u64 }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ ParseError: Expected '}', got 'keyword:as'
```

We should give a nice diagnostic like rust does, via the rust playground:

```
error: expected expression, found `as`
--> src/lib.rs:2:29
|
2 | if b { 42 } else { 64 } as u64
| ^^ expected expression
|
help: parentheses are required to parse this as an expression
|
2 | (if b { 42 } else { 64 }) as u64
| +
```

-- https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6b48b619a2f263d65f6adf674f126e62

### Current best alternative workaround (limit 100 words)

Break into two statements.

### Your view of the "best case XLS enhancement" (limit 100 words)

We add a similar diagnostic (otherwise it's confusing since we use expressions for the purposes of ternaries).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.