[Issue 23668] New: Can't stable sort structs with disabled default constructor.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Feb 3 13:26:59 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=23668

          Issue ID: 23668
           Summary: Can't stable sort structs with disabled default
                    constructor.
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: minor
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: default_357-line at yahoo.de

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.

--


More information about the Digitalmars-d-bugs mailing list