Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 7ad33fb

Browse files
authored
Merge pull request #2451 from n8sh/issue-19562
Fix Issue 19562 - core.internal.hash.hashOf array of pointers or delegates should be `@safe` merged-on-behalf-of: unknown
2 parents 1705cc1 + dfccc19 commit 7ad33fb

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/core/internal/convert.d

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,20 @@ nothrow pure @safe unittest
705705
enum ctfe_works = (() => { Month x = Month.jan; return toUbyte(x).length > 0; })();
706706
}
707707

708+
@trusted pure nothrow @nogc
709+
const(ubyte)[] toUbyte(T)(const ref T val) if (is(T == delegate) || is(T : V*, V) && __traits(getAliasThis, T).length == 0)
710+
{
711+
if (__ctfe)
712+
{
713+
if (val !is null) assert(0, "Unable to compute byte representation of non-null pointer at compile time");
714+
return ctfe_alloc(T.sizeof);
715+
}
716+
else
717+
{
718+
return (cast(const(ubyte)*)&val)[0 .. T.sizeof];
719+
}
720+
}
721+
708722
package(core.internal) bool isNonReference(T)()
709723
{
710724
static if (is(T == struct) || is(T == union))

test/hash/src/test_hash.d

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
void main()
22
{
3-
hashOfVoidPtrArray();
3+
issue19562();
44
issue15111();
55
issues16654And16764();
66
issue18918();
@@ -15,11 +15,15 @@ void main()
1515
pr2243();
1616
}
1717

18-
/// Check that `hashOf` can be called on an array of void pointers.
19-
void hashOfVoidPtrArray() @nogc nothrow pure @system
18+
/// Check hashOf an array of void pointers or delegates is @safe.
19+
void issue19562() @nogc nothrow pure @safe
2020
{
21-
void*[] val;
22-
const _ = hashOf(val); // Check a PR doesn't break this.
21+
void*[10] val;
22+
size_t h = hashOf(val[]);
23+
24+
alias D = void delegate();
25+
D[10] ds;
26+
h = hashOf(ds[]);
2327
}
2428

2529
/// hashOf was failing for structs that had an `alias this` to a dynamic array.

0 commit comments

Comments
 (0)