diff --git a/changelog/dmd.import-exp-hexstring.dd b/changelog/dmd.import-exp-hexstring.dd new file mode 100644 index 00000000000..b14878a27d5 --- /dev/null +++ b/changelog/dmd.import-exp-hexstring.dd @@ -0,0 +1,12 @@ +Import expressions are now treated as hex strings + +While [Import expressions](https://dlang.org/spec/expression.html#import_expressions) are typed as `string`, they are also used to embed binary files. +By treating them the same as hex strings, they will implicitly convert to arrays of integral types other than `char`. + +--- +// Formerly, a cast was required: +immutable ubyte[] iconImg = cast(immutable ubyte[]) import("icon.png"); + +// Now, it implicitly converts to integral arrays: +immutable ubyte[] iconImg = import("icon.png"); +--- diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index 909fd92c40e..c0ea8de8ed5 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -7668,6 +7668,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor if (auto fmResult = global.fileManager.lookup(fileName)) { se = new StringExp(e.loc, fmResult); + se.hexString = true; } else { diff --git a/compiler/test/compilable/import_exp.d b/compiler/test/compilable/import_exp.d new file mode 100644 index 00000000000..014d0f65f6f --- /dev/null +++ b/compiler/test/compilable/import_exp.d @@ -0,0 +1,36 @@ +// REQUIRED_ARGS: -Jcompilable/imports/ +// EXTRA_FILES: imports/imp16088.d imports/test21227/a..b.txt imports/test21227/a.txt imports/test21227/..foo/a.txt + +// https://issues.dlang.org/show_bug.cgi?id=16088 + +void bar(string x) {} +auto foo() +{ + import("imp16088.d").bar; +} + + +// https://issues.dlang.org/show_bug.cgi?id=21227 + +void test21227() +{ + import("./test21227/a.txt").bar; + import("test21227//a..b.txt").bar; + import("test21227/..foo/a.txt").bar; + + version(Windows) + { + import(r".\test21227\a.txt").bar; + import(r"test21227\\a..b.txt").bar; + import(r"test21227\..foo\a.txt").bar; + } +} + +// Test that it's treated like a hex string, allowing implicit conversion to byte array + +// Can't test whole contents because line endings may vary +enum expectedStart = "module imports.imp16088;"; + +immutable ubyte[] s0 = import("imp16088.d"); + +static assert(s0[0 .. expectedStart.length] == "module imports.imp16088;"); diff --git a/compiler/test/compilable/test16088.d b/compiler/test/compilable/test16088.d deleted file mode 100644 index 97326f1fcd7..00000000000 --- a/compiler/test/compilable/test16088.d +++ /dev/null @@ -1,10 +0,0 @@ -// REQUIRED_ARGS: -Jcompilable/imports/ -// EXTRA_FILES: imports/imp16088.d - -// https://issues.dlang.org/show_bug.cgi?id=16088 - -void bar(string x) {} -auto foo() -{ - import("imp16088.d").bar; -} diff --git a/compiler/test/compilable/test21227.d b/compiler/test/compilable/test21227.d deleted file mode 100644 index 29dd2af29c3..00000000000 --- a/compiler/test/compilable/test21227.d +++ /dev/null @@ -1,19 +0,0 @@ -// REQUIRED_ARGS: -Jcompilable/imports -// EXTRA_FILES: imports/test21227/a..b.txt imports/test21227/a.txt imports/test21227/..foo/a.txt - -// https://issues.dlang.org/show_bug.cgi?id=21227 - -void bar(string x) {} -void test21227() -{ - import("./test21227/a.txt").bar; - import("test21227//a..b.txt").bar; - import("test21227/..foo/a.txt").bar; - - version(Windows) - { - import(r".\test21227\a.txt").bar; - import(r"test21227\\a..b.txt").bar; - import(r"test21227\..foo\a.txt").bar; - } -}