_localctx not casted to correct class when using Alternative Labels
@parrt is already working on this.
Since Nov 29, 2014.
- Dominant language
- Java
- Stars
- 19k
- Forks
- 3.5k
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
Here is a parser rule with two different types of alternatives:
- with two numerical arguments `BinaryNumberOp`
- with two logical arguments `BinaryLogicalOp`
``` antlr
logicalExpression returns [ LogicalExpression val ]:
l = number '>' r = number { $val = lt($ctx.r.val, $ctx.l.val); } # BinaryNumberOp
| l = number '>=' r = number { $val = le($ctx.r.val, $ctx.l.val); } # BinaryNumberOp
| l = number '<' r = number { $val = lt($ctx.l.val, $ctx.r.val); } # BinaryNumberOp
| l = number '<=' r = number { $val = le($ctx.l.val, $ctx.r.val); } # BinaryNumberOp
| l = number '==' r = number { $val = eq($ctx.l.val, $ctx.r.val); } # BinaryNumberOp
| l = logicalExpression '&&' r = logicalExpression { $val = and($ctx.l.val, $ctx.r.val); } # BinaryLogicalOp
| l = logicalExpression '||' r = logicalExpression { $val = or($ctx.l.val, $ctx.r.val); } # BinaryLogicalOp
;
```
Here is a fragment of code generated for one of alternatives.
``` java
setState(59);
((BinaryNumberOpContext)_localctx).l = number();
setState(60); match(T__5);
setState(61); ((BinaryNumberOpContext)_localctx).r = number();
((BinaryNumberOpContext)_localctx).val = lt(_localctx.r.val, _localctx.l.val);
}
```
The problem is that `_localctx` in the `lt(...)` function invocation is not converted to current local context type `BinaryNumberOpContext`, so this code can't be compiled because base class (`LogicalExpressionContext`) doesn't contain `.r` and `.l` fields.
I'm not sure if it is a bug, but it seems like in this situation `_localctx` can be casted to current local context class without any problems.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.