[Issue 15086] import doesn't verify module declaration
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Jun 30 20:22:54 UTC 2023
https://issues.dlang.org/show_bug.cgi?id=15086
--- Comment #31 from ag0aep6g <ag0aep6g at gmail.com> ---
I just ran into this one again in the wild.
Step 1: Start with a program that prints some data as an HTML table.
Step 2: Add another module to print the same data in CSV format. Do this by
copying html.d to csv.d and adjust as needed. Make sure you forget updating the
module declaration.
The resulting code (heavily simplified, of course):
--- main.d
module main;
void main()
{
const mode = "html";
if (mode == "csv")
{
import csv;
print();
}
else if (mode == "html")
{
import html;
print();
}
}
--- csv.d
module html; /* oopsie daisy */
void print()
{
import std.stdio;
writeln("foo,bar\nbaz,qux");
}
--- html.d
module html;
void print()
{
import std.stdio;
writeln("<table><tr><td>foo</td><td>bar</td></tr>" ~
"<tr><td>baz</td><td>qux</td></tr></table>");
}
---
Step 3: Run with `dmd -i -run main.d`. Be surprised that it prints CSV even
though `mode` is clearly set to "html". Scratch your head. Stare at the screen.
Question your sanity.
Finally, figure out that you forgot to update the module declaration. Wonder
why the compiler didn't complain. Remember that bug you filed seven(!) years(!)
ago.
--
More information about the Digitalmars-d-bugs
mailing list