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

Commit b5d124a

Browse files
authored
Merge pull request #2238 from n8sh/core-hash-19048
Fix Issue 19048 - In core.internal.hash.hashOf reduce template bloat: remove `auto ref` where unneeded and add `const` where possible merged-on-behalf-of: Steven Schveighoffer <schveiguy@users.noreply.github.com>
2 parents 707d703 + c819624 commit b5d124a

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/core/internal/convert.d

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private ubyte[] ctfe_alloc()(size_t n)
3434
}
3535

3636
@trusted pure nothrow @nogc
37-
const(ubyte)[] toUbyte(T)(ref T val) if(is(Unqual!T == float) || is(Unqual!T == double) || is(Unqual!T == real) ||
37+
const(ubyte)[] toUbyte(T)(const ref T val) if(is(Unqual!T == float) || is(Unqual!T == double) || is(Unqual!T == real) ||
3838
is(Unqual!T == ifloat) || is(Unqual!T == idouble) || is(Unqual!T == ireal))
3939
{
4040
static const(ubyte)[] reverse_(const(ubyte)[] arr)
@@ -286,7 +286,7 @@ private ulong shiftrRound(ulong x)
286286
}
287287

288288
@safe pure nothrow @nogc
289-
private uint binLog2(T)(T x)
289+
private uint binLog2(T)(const T x)
290290
{
291291
assert(x > 0);
292292
int max = 2 ^^ (FloatTraits!T.EXPONENT-1)-1;
@@ -499,13 +499,13 @@ template floatFormat(T) if(is(T:real) || is(T:ireal))
499499

500500
// all toUbyte functions must be evaluable at compile time
501501
@trusted pure nothrow @nogc
502-
const(ubyte)[] toUbyte(T)(T[] arr) if (T.sizeof == 1)
502+
const(ubyte)[] toUbyte(T)(const T[] arr) if (T.sizeof == 1)
503503
{
504504
return cast(const(ubyte)[])arr;
505505
}
506506

507507
@trusted pure nothrow @nogc
508-
const(ubyte)[] toUbyte(T)(T[] arr) if ((is(typeof(toUbyte(arr[0])) == const(ubyte)[])) && (T.sizeof > 1))
508+
const(ubyte)[] toUbyte(T)(const T[] arr) if ((is(typeof(toUbyte(arr[0])) == const(ubyte)[])) && (T.sizeof > 1))
509509
{
510510
if (__ctfe)
511511
{
@@ -525,7 +525,7 @@ const(ubyte)[] toUbyte(T)(T[] arr) if ((is(typeof(toUbyte(arr[0])) == const(ubyt
525525
}
526526

527527
@trusted pure nothrow @nogc
528-
const(ubyte)[] toUbyte(T)(ref T val) if (__traits(isIntegral, T) && !is(T == enum))
528+
const(ubyte)[] toUbyte(T)(const ref T val) if (__traits(isIntegral, T) && !is(T == enum))
529529
{
530530
static if (T.sizeof == 1)
531531
{
@@ -561,7 +561,7 @@ const(ubyte)[] toUbyte(T)(ref T val) if (__traits(isIntegral, T) && !is(T == enu
561561
}
562562

563563
@trusted pure nothrow @nogc
564-
const(ubyte)[] toUbyte(T)(ref T val) if (is(Unqual!T == cfloat) || is(Unqual!T == cdouble) ||is(Unqual!T == creal))
564+
const(ubyte)[] toUbyte(T)(const ref T val) if (is(Unqual!T == cfloat) || is(Unqual!T == cdouble) ||is(Unqual!T == creal))
565565
{
566566
if (__ctfe)
567567
{
@@ -581,12 +581,12 @@ const(ubyte)[] toUbyte(T)(ref T val) if (is(Unqual!T == cfloat) || is(Unqual!T =
581581
}
582582

583583
@trusted pure nothrow @nogc
584-
const(ubyte)[] toUbyte(T)(ref T val) if (is(T V == enum) && is(typeof(toUbyte(cast(V)val)) == const(ubyte)[]))
584+
const(ubyte)[] toUbyte(T)(const ref T val) if (is(T V == enum) && is(typeof(toUbyte(cast(const V)val)) == const(ubyte)[]))
585585
{
586586
if (__ctfe)
587587
{
588588
static if (is(T V == enum)){}
589-
return toUbyte(cast(V) val);
589+
return toUbyte(cast(const V) val);
590590
}
591591
else
592592
{
@@ -649,7 +649,7 @@ private bool isNonReferenceStruct(T)() if (is(T == struct) || is(T == union))
649649
}
650650

651651
@trusted pure nothrow @nogc
652-
const(ubyte)[] toUbyte(T)(ref T val) if (is(T == struct) || is(T == union))
652+
const(ubyte)[] toUbyte(T)(const ref T val) if (is(T == struct) || is(T == union))
653653
{
654654
if (__ctfe)
655655
{

src/core/internal/hash.d

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && __traits
5656
}
5757

5858
//dynamic array hash
59-
size_t hashOf(T)(auto ref T val, size_t seed = 0)
59+
size_t hashOf(T)(T val, size_t seed = 0)
6060
if (!is(T == enum) && !is(T : typeof(null)) && is(T S: S[]) && !__traits(isStaticArray, T)
6161
&& !is(T == struct) && !is(T == class) && !is(T == union))
6262
{
@@ -101,13 +101,14 @@ if (!is(T == enum) && !is(T : typeof(null)) && is(T S: S[]) && !__traits(isStati
101101

102102
//arithmetic type hash
103103
@trusted @nogc nothrow pure
104-
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && __traits(isArithmetic, T))
104+
size_t hashOf(T)(scope const T val, size_t seed = 0) if (!is(T == enum) && __traits(isArithmetic, T))
105105
{
106106
static if(__traits(isFloating, val))
107107
{
108108
static if (floatCoalesceZeroes || floatCoalesceNaNs)
109109
{
110-
auto data = val;
110+
import core.internal.traits : Unqual;
111+
Unqual!T data = val;
111112
// Zero coalescing not supported for deprecated complex types.
112113
static if (floatCoalesceZeroes && is(typeof(data = 0)))
113114
if (data == 0) data = 0; // +0.0 and -0.0 become the same.
@@ -170,14 +171,14 @@ size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && __traits
170171

171172
//typeof(null) hash. CTFE supported
172173
@trusted @nogc nothrow pure
173-
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && is(T : typeof(null)))
174+
size_t hashOf(T)(scope const T val, size_t seed = 0) if (!is(T == enum) && is(T : typeof(null)))
174175
{
175176
return hashOf(cast(void*)null, seed);
176177
}
177178

178179
//Pointers hash. CTFE unsupported if not null
179180
@trusted @nogc nothrow pure
180-
size_t hashOf(T)(auto ref T val, size_t seed = 0)
181+
size_t hashOf(T)(scope const T val, size_t seed = 0)
181182
if (!is(T == enum) && is(T V : V*) && !is(T : typeof(null))
182183
&& !is(T == struct) && !is(T == class) && !is(T == union))
183184
{
@@ -256,21 +257,21 @@ nothrow pure @safe unittest // issue 19005
256257

257258
//delegate hash. CTFE unsupported
258259
@trusted @nogc nothrow pure
259-
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && is(T == delegate))
260+
size_t hashOf(T)(scope const T val, size_t seed = 0) if (!is(T == enum) && is(T == delegate))
260261
{
261262
assert(!__ctfe, "unable to compute hash of "~T.stringof);
262263
const(ubyte)[] bytes = (cast(const(ubyte)*)&val)[0 .. T.sizeof];
263264
return bytesHashAlignedBy!T(bytes, seed);
264265
}
265266

266267
//class or interface hash. CTFE depends on toHash
267-
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && is(T == interface) || is(T == class))
268+
size_t hashOf(T)(T val, size_t seed = 0) if (!is(T == enum) && is(T == interface) || is(T == class))
268269
{
269270
return hashOf(val ? (cast(Object)val).toHash() : 0, seed);
270271
}
271272

272273
//associative array hash. CTFE depends on base types
273-
size_t hashOf(T)(auto ref T aa, size_t seed = 0) if (!is(T == enum) && __traits(isAssociativeArray, T))
274+
size_t hashOf(T)(T aa, size_t seed = 0) if (!is(T == enum) && __traits(isAssociativeArray, T))
274275
{
275276
if (!aa.length) return hashOf(0, seed);
276277
size_t h = 0;
@@ -544,7 +545,7 @@ nothrow pure @system unittest // issue 18918
544545

545546
// This overload is for backwards compatibility.
546547
@system pure nothrow @nogc
547-
size_t bytesHash(const(void)* buf, size_t len, size_t seed)
548+
size_t bytesHash(scope const(void)* buf, size_t len, size_t seed)
548549
{
549550
return bytesHashAlignedBy!ubyte((cast(const(ubyte)*) buf)[0 .. len], seed);
550551
}

0 commit comments

Comments
 (0)