From 539dc473ba79db0e5e8cf9ee059a5cc137b88bda Mon Sep 17 00:00:00 2001 From: FeepingCreature <540727+FeepingCreature@users.noreply.github.com> Date: Mon, 10 Jun 2024 17:10:18 +0200 Subject: [PATCH] Fix bugzilla issue 24596: std.typecons.Rebindable2: Don't destroy classes! Only destroy structs! (#9012) --- std/algorithm/searching.d | 15 +++++++++++++++ std/typecons.d | 5 ++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/std/algorithm/searching.d b/std/algorithm/searching.d index 2d89dea3f1c..42a9df518c9 100644 --- a/std/algorithm/searching.d +++ b/std/algorithm/searching.d @@ -3873,6 +3873,21 @@ if (isInputRange!Range && !isInfinite!Range && assert([BigInt(2), BigInt(3)].maxElement == BigInt(3)); } +// https://issues.dlang.org/show_bug.cgi?id=24596 +@safe unittest +{ + static class A { + int i; + int getI() @safe => i; + this(int i) @safe { this.i = i; } + } + auto arr = [new A(2), new A(3)]; + + arr.maxElement!(a => a.getI); + + assert(arr[0].getI == 2); +} + // minPos /** Computes a subrange of `range` starting at the first occurrence of `range`'s diff --git a/std/typecons.d b/std/typecons.d index f5b48468a80..3c425c7d7de 100644 --- a/std/typecons.d +++ b/std/typecons.d @@ -3125,7 +3125,10 @@ private: } // call possible struct destructors - .destroy!(No.initialize)(*cast(T*) &this.data); + static if (is(T == struct)) + { + .destroy!(No.initialize)(*cast(T*) &this.data); + } } }