Bug 9232 – Parsing error on some templated methods calls
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-12-28T04:20:00Z
Last change time
2015-06-17T21:03:37Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
dransic
Comments
Comment #0 by dransic — 2012-12-28T04:20:19Z
I think this should compile:
---
struct Foo {
void bar(T)() {}
void baz() {}
}
void main() {
Foo foo;
(foo).bar!int(); // Error: found '!' when expecting ';' following statement
((foo)).bar!int(); // OK
foo.bar!int(); // OK
(foo).baz(); // OK
}
---
(DMD 2.060 MacOSX)
Comment #1 by k.hara.pg — 2012-12-28T07:03:44Z
This is an enhancement request against language syntax.
---
> (foo).bar!int(); // Error: found '!' when expecting ';' following
In current, "(foo).bar" matches UnaryExpression "(Type) . identifier".
http://dlang.org/expression#UnaryExpression
("foo" is parsed as TypeIdentifier, and in semantic analysis phase, it will be finally analyzed as an expression.)
Then, the remaining portions "!int();" don't match anything in the grammar.
---
To allow it, we should add a case to UnaryExpression like follows:
UnaryExpression:
& UnaryExpression
++ UnaryExpression
-- UnaryExpression
* UnaryExpression
- UnaryExpression
+ UnaryExpression
! UnaryExpression
ComplementExpression
( Type ) . Identifier
( Type ) . TemplateInstance // new!
NewExpression
DeleteExpression
CastExpression
PowExpression
Comment #2 by dransic — 2012-12-28T07:41:13Z
> In current, "(foo).bar" matches UnaryExpression "(Type) . identifier".
> http://dlang.org/expression#UnaryExpression
It should not since foo is not a type but an identifier.
(foo).bar!int should match "PrimaryExpression . TemplateInstance", where PrimaryExpression matches "( Identifier )".
So this is a compiler bug IMO, and should not be classified as an enhancement request.
(In reply to comment #3)
> (In reply to comment #2)
> > > In current, "(foo).bar" matches UnaryExpression "(Type) . identifier".
> > > http://dlang.org/expression#UnaryExpression
> >
> > It should not since foo is not a type but an identifier.
> >
> > (foo).bar!int should match "PrimaryExpression . TemplateInstance", where
> > PrimaryExpression matches "( Identifier )".
> >
> > So this is a compiler bug IMO, and should not be classified as an enhancement
> > request.
>
> OK. I was convinced that it is a grammar bug, rather than an enhancement.
>
> https://github.com/D-Programming-Language/dmd/pull/1422
> https://github.com/D-Programming-Language/d-programming-language.org/pull/223
It works fine with this pull request. And by studying it, I now understand how it works.
Thanks.
Comment #5 by github-bugzilla — 2013-03-03T22:47:10Z