mirror of
https://github.com/dlang/dmd.git
synced 2025-04-26 05:00:16 +03:00
Treat import("file")
as hex string
This commit is contained in:
parent
0c8e5b10a4
commit
25f26e2bdf
5 changed files with 49 additions and 29 deletions
12
changelog/dmd.import-exp-hexstring.dd
Normal file
12
changelog/dmd.import-exp-hexstring.dd
Normal file
|
@ -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");
|
||||
---
|
|
@ -7752,6 +7752,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
|
|||
if (auto fmResult = global.fileManager.getFileContents(fileName))
|
||||
{
|
||||
se = new StringExp(e.loc, fmResult);
|
||||
se.hexString = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
36
compiler/test/compilable/import_exp.d
Normal file
36
compiler/test/compilable/import_exp.d
Normal file
|
@ -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;");
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue