Bug 17701 – DMD does not properly gag errors when passing `typeof` to a template inside __traits(compiles)
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-07-29T00:45:00Z
Last change time
2017-07-29T02:18:35Z
Assigned to
nobody
Creator
monkeyworks12
Comments
Comment #0 by monkeyworks12 — 2017-07-29T00:45:44Z
import std.meta: AliasSeq, staticMap;
alias typesAndValues = AliasSeq!(int, false, char, "test");
//Error: found ',' when expecting '('
//Error: found ';' when expecting ')' following template argument list
//Error: semicolon expected to close alias declaration
assert(!__traits(compiles, { alias justTypes = staticMap!(typeof, typeAndValues); }));
Tested with DMD 2.075.0. I'm not sure if this is a regression or not.
Comment #1 by dhasenan — 2017-07-29T01:02:14Z
`typeof` is a keyword with specific syntax. In order for the compiler to get to the point where it runs `__traits(complies, ...)`, it has to successfully parse your code. If you violate syntax rules, as in your example, it can't successfully parse your code.
This is similar to the rule that code inside inactive `version` blocks must be parseable.
In order to handle cases like this, you can use string mixins:
static assert(!__traits(compiles, {mixin(q{alias justTypes = ...;});}));