Bug 18543 – Comments inside string mixin silently short-circuit code

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2018-03-01T13:55:14Z
Last change time
2018-03-02T12:24:08Z
Assigned to
No Owner
Creator
Eduard Staniloiu

Comments

Comment #0 by edi33416 — 2018-03-01T13:55:14Z
I have recently stumbled upon the following wrong behaviour inside string mixins. If you use a single line style comment (//) inside a string mixin, this will silently short-circuit the code, leading to unexpected behaviour, as you can see in the example bellow: import std.stdio; void main(string[] args) { mixin("" ~"writeln(\"Hello\");" ~"// Some comment" ~"assert(0);" ); } The assert will never get triggered. If you use multi-line comments (/* */) those are correctly recognized and everything works as expected.
Comment #1 by trikkuz — 2018-03-01T13:58:02Z
Just because you're mixin a single line. Adding \n will solve the problem import std.stdio; void main(string[] args) { mixin("" ~"writeln(\"Hello\");" ~"// Some comment\n" ~"assert(0);" ); } your code sounds like: void main() { writeln("hello"); // Some comment assert(0) }
Comment #2 by issues.dlang — 2018-03-01T19:39:14Z
Yeah, remember that string mixins are basically like pasting code in place. So, it's going to act like you pasted that code into your editor, and adding a comment with // is therefore going to have exactly the same effect as it would if you pasted code after a //, which means that it's going to be part of the comment.
Comment #3 by edi33416 — 2018-03-02T12:24:08Z
Thank you both for explaining my mistake. I got confused by my own code. Cheers, Eduard