Bug 24032 – Compiler is parsing string parameters to Templates
Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-07-06T10:29:42Z
Last change time
2023-07-10T11:32:26Z
Assigned to
No Owner
Creator
Puneet Goel
Comments
Comment #0 by puneet — 2023-07-06T10:29:42Z
Works with dmd-2.081.2.
Error with dmd-2.085 and also with current release dmd-2.104
$ dmd /tmp/test.d
/tmp/test.d(3): Error: `0X` isn't a valid integer literal, use `0X0` instead
/tmp/test.d(4): Error: `0B` isn't a valid integer literal, use `0B0` instead
// test.d
class Foo(string str) {}
void main() {
Foo!q{0X} foo;
Foo!q{0B} bar;
}
Comment #1 by default_357-line — 2023-07-06T10:45:13Z
Yes, Q{} is defined in the grammar as a "token string" which must consist of valid D tokens, because it's meant to pass around fragments of source code. "0X" stopped being a valid token in DMD 2.084.0: https://dlang.org/changelog/2.084.0.html#deprecated_binary_literals
Comment #2 by default_357-line — 2023-07-06T10:48:26Z
If you can replace the string with `q{0x0}`, that should work. Alternately, pass a regular `"0x"` string.
Comment #3 by puneet — 2023-07-06T12:36:06Z
I am using q{} to write my own DSL.
For this particular thing, I need to extend binary number strings in a way where I can write stuff like 0b?01?11 where '?' can be used for pattern matching. So, this becomes blocking for me. Is there an easy solution, or do I need to discover an alternate syntax for my need?
BTW, what advantage do we gain by making the compiler parse q{} as a token string?
Comment #4 by puneet — 2023-07-06T12:41:40Z
Even if we have to do token parsing for q{}, the following code should not fail.
class Foo(string str) {}
void main() {
Foo!q{string str = "0X"} foo;
Foo!q{string str = "0B"} bar;
}
The compiler gives the same errors as it gives for q{0B} and q{0X}.
Comment #5 by puneet — 2023-07-06T12:43:47Z
Please ignore the previous comment. There was a mistake at my end.
Comment #6 by default_357-line — 2023-07-06T13:12:41Z
The easy solution would be writing `foo!"0b?01?11"`. `q{}` is intended for fragments of D code.
Comment #7 by dkorpel — 2023-07-10T11:32:26Z
Like FeepingCreature said, q{} is a string of D tokens, typically used for string mixin purposes. dmd's behavior is correct. Please use a different string literal kind for strings with code from other languages.