dart-lang / dart-lang/language

Don't allow cascades on expressions with infix operators.

Open
#608 8 comments 1 reaction 0 assignees View on GitHub
request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

The grammar currently contains the production `expression : conditionalExpression cascadeSection*`
This allows such atrocities as:
```dart
test ? foo : bar..baz() // meaning (test ? foo : bar)..baz()
e1 ?? e2..baz() // meaning (e1 ?? e2)..baz()
```
etc, all down the expression hierarchy to binary multiplicative and unary prefix operators
```
a * b..bar() // meaning (a * b)..bar()
await foo()..bar() // meaning (await foo())..bar()
~foo..bar() // meaning (~foo)..bar())
```
Only the prefix operators look reasonable here, but it might still be confusing because it differs in behavior from `~foo.bar()`. Arguably that's a common issue in itself when `-2.toString()` fails to compile.)

We should not allow cascades direction on expressions which contain infix or prefix operators. The code reads completely contrary to how it actually works.
I suggest changing the grammar from:
```antlr
expression
: (formalParameterPart functionExpressionBodyPrefix) =>
functionExpression
| throwExpression
| (assignableExpression assignmentOperator) =>
assignableExpression assignmentOperator expression
| conditionalExpression cascadeSection*
;
```
to
```antlr
expression
: (formalParameterPart functionExpressionBodyPrefix) =>
functionExpression
| throwExpression
| (assignableExpression assignmentOperator) =>
assignableExpression assignmentOperator expression
| cascadeExpression
;
cascadeExpression
: prefixExpression cascadeSection+
| conditionalExpression
```
(and change all other occurrences of `conditionalExpression cascadeSection*` to `cascadeExpression` too).

(Edit: Changed from `postfixExpression cascadeSection+` to `prefixExpression cascadeSection+` to allow, e.g., `-x..foo()`).

Contributor guide

Open the contributing guide

Research direction

Start with the language grammar production `expression : conditionalExpression cascadeSection*` and inspect the other occurrences of `conditionalExpression cascadeSection*` mentioned in the issue. Define the grammar change so cascades cannot follow expressions containing infix operators while valid prefix cascades such as `-x..foo()` remain accepted, then verify the affected grammar examples and diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.