Bug 23733 – Can't use template type parameter as type of alias parameter
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-02-22T21:19:14Z
Last change time
2023-10-15T22:23:26Z
Keywords
pull
Assigned to
No Owner
Creator
Paul Backus
Comments
Comment #0 by snarwin+bugzilla — 2023-02-22T21:19:14Z
As of DMD v2.102.1, the following program fails to compile:
---
template foo(T, alias T a) {}
int n;
alias _ = foo!(int, n);
---
The error message is:
---
bug.d(4): Error: template instance `foo!(int, n)` does not match template declaration `foo(T, alias T a)`
---
Comment #1 by razvan.nitu1305 — 2023-02-24T13:08:50Z
I don't think that the feature was thought to work with abstract types (the spec is very tight on that matter: https://dlang.org/spec/template.html#typed_alias_op). Although what this bug report is asking is a logical extension.
Comment #2 by snarwin+bugzilla — 2023-02-24T13:31:36Z
The spec you linked to says:
> Alias parameters can also be typed. These parameters will accept symbols of that type
...which is not "tight" at all--it places no restrictions on the type whatsoever.
The *grammar* says that the type must be a BasicType, but the definition of BasicType is as follows:
> BasicType:
> FundamentalType
> . QualifiedIdentifier
> QualifiedIdentifier
> Typeof
> Typeof . QualifiedIdentifier
> TypeCtor ( Type )
> Vector
> TraitsExpression
> MixinType
Since this includes QualifiedIdentifier, Typeof, and MixinType, it also does not place any restriction on the type used. And in any case, the example given in this issue parses successfully.
It's possible that the type was intended to be restricted in some way, but in that case, this is a bug in the spec. Either way, it is certainly not an enhancement request.
Comment #3 by razvan.nitu1305 — 2023-02-27T11:18:37Z
(In reply to Paul Backus from comment #2)
> The spec you linked to says:
>
> > Alias parameters can also be typed. These parameters will accept symbols of that type
>
> ...which is not "tight" at all--it places no restrictions on the type
> whatsoever.
When I said "tight", I meant "brief, not too explanatory".
>
> The *grammar* says that the type must be a BasicType, but the definition of
> BasicType is as follows:
>
> > BasicType:
> > FundamentalType
> > . QualifiedIdentifier
> > QualifiedIdentifier
> > Typeof
> > Typeof . QualifiedIdentifier
> > TypeCtor ( Type )
> > Vector
> > TraitsExpression
> > MixinType
>
> Since this includes QualifiedIdentifier, Typeof, and MixinType, it also does
> not place any restriction on the type used. And in any case, the example
> given in this issue parses successfully.
>
> It's possible that the type was intended to be restricted in some way, but
> in that case, this is a bug in the spec. Either way, it is certainly not an
> enhancement request.
What I meant is that the way I read the spec, it seems that template alias parameters would work only for concrete types. I don't know if this was intentional or not, but if the design is extended to work for abstract types as well, then that is an enhancement request. If the person that implemented this did intend to exclude abstract types, then indeed it is a "normal" spec bug.
Comment #4 by snarwin+bugzilla — 2023-10-15T19:12:27Z
Worth noting that this error is specific to alias parameters. If the example is changed to use a template value parameter, it compiles successfully:
---
template foo(T, T t) {}
alias _ = foo!(int, 0); // ok
---
So, it seems to me that referring to an earlier template parameter in a later parameter's declaration is something that's intended to work in general, and the fact that it doesn't work for alias parameters is indeed a bug.
Comment #5 by dlang-bot — 2023-10-15T20:59:04Z
@SixthDot created dlang/dmd pull request #15691 "fix issue 23733 - Can't use template type parameter as type of alias …" fixing this issue:
- fix issue 23733 - Can't use template type parameter as type of alias parameter
https://github.com/dlang/dmd/pull/15691
Comment #6 by dlang-bot — 2023-10-15T22:23:26Z
dlang/dmd pull request #15691 "fix issue 23733 - Can't use template type parameter as type of alias …" was merged into master:
- 9064a7acc70a5316b1525d8c6f32eff83b29affe by Basile Burg:
fix issue 23733 - Can't use template type parameter as type of alias parameter
https://github.com/dlang/dmd/pull/15691