Skip to content

_localctx not casted to correct class when using Alternative Labels #767

Open
@dbolotin

Description

@dbolotin

Hi,

Here is a parser rule with two different types of alternatives:

  • with two numerical arguments BinaryNumberOp
  • with two logical arguments BinaryLogicalOp
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.

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions