Bug 23668 – Can't stable sort structs with disabled default constructor.

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-02-03T13:26:59Z
Last change time
2023-02-13T03:17:39Z
Keywords
pull
Assigned to
No Owner
Creator
FeepingCreature

Comments

Comment #0 by default_357-line — 2023-02-03T13:26:59Z
Consider the following code: ``` import std; struct S { int a; this(int a) { this.a = a; } int opCmp(S other) { return a > other.a ? 1 : a < other.a ? -1 : 0; } @disable this(); } void main() { S[] foo = [S(3), S(2)]; foo.sort!"a > b"; foo.sort!("a < b", SwapStrategy.stable); } ``` This will error with ``` /dlang/dmd-nightly/linux/bin64/../../src/phobos/std/algorithm/sorting.d(2602): Error: struct `onlineapp.S` default construction is disabled ``` This happens because TimSort (the stable sort) tries to run a simpler codepath if called during CTFE which does `array.length = ...`, which calls the default constructors. Even though we're not in CTFE, this codepath still has to be semantically valid (because `__ctfe` is a runtime variable), so it errors. The CTFE optimization should only be done if the struct has a default constructor.
Comment #1 by dlang-bot — 2023-02-03T13:43:30Z
@FeepingCreature created dlang/phobos pull request #8676 "Fix issue 23668: TimSort must avoid default initialization if the type being sorted has it disabled." fixing this issue: - Fix issue 23668: TimSort must avoid default initialization if the type being sorted has it disabled. https://github.com/dlang/phobos/pull/8676
Comment #2 by dlang-bot — 2023-02-04T08:51:34Z
dlang/phobos pull request #8676 "Fix issue 23668: TimSort must avoid default initialization if the type being sorted has it disabled." was merged into stable: - 01b99cb6c39ee1876a2dceaa7be54adb62e460e6 by Mathis Beer: Fix issue 23668: TimSort must avoid default initialization if the type being sorted has it disabled. https://github.com/dlang/phobos/pull/8676
Comment #3 by dlang-bot — 2023-02-13T03:17:39Z
dlang/phobos pull request #8683 "Fix issue 23668: TimSort must avoid default initialization if the type being sorted has it disabled." was merged into master: - c06e3171f1b6d9cc38493959ab5b34d80f8d3d51 by Mathis Beer: Fix issue 23668: TimSort must avoid default initialization if the type being sorted has it disabled. https://github.com/dlang/phobos/pull/8683