Treat import("file") as hex string

This commit is contained in:
Dennis Korpel 2024-06-30 12:38:14 +02:00 committed by The Dlang Bot
parent 0c8e5b10a4
commit 25f26e2bdf
5 changed files with 49 additions and 29 deletions

View 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");
---

View file

@ -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
{

View 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;");

View file

@ -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;
}

View file

@ -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;
}
}