dmd icon indicating copy to clipboard operation
dmd copied to clipboard

Remove complex and imaginary types from language in Edition v2024

Open ibuclaw opened this issue 4 years ago • 20 comments

Add -preview=complex, an option that removes cfloat, cdouble, and creal from the D language.

For C/C++ compatibility, new special enums have been added - and will likely be used for a std.complex.Complex!(T).toNative() function.

Example implementation:

// ABI layout of native complex
struct _Complex(T) { T re; T im; }

// Special enum definitions.
version (Posix)
{
    align(float.alignof)  enum __c_complex_float : _Complex!float;
    align(double.alignof) enum __c_complex_double : _Complex!double;
    align(real.alignof)   enum __c_complex_long_double : _Complex!real;
}
else
{
    align(float.sizeof * 2)  enum __c_complex_float : _Complex!float;
    align(double.sizeof * 2) enum __c_complex_double : _Complex!double;
    align(real.alignof)      enum __c_complex_long_double : _Complex!real;
}

// Backwards compatibility aliases
alias cfloat = __c_complex_float;
alias ifloat = float;
alias cdouble = __c_complex_double;
alias idouble = double;
alias creal = __c_complex_long_double;
alias ireal = real;

// Test can still call extern C++ functions.
extern(C++) creal cadd1(creal a);

int main()
{
    creal a = creal(1, 0);
    auto b = cadd1(a);
    version (DigitalMars)  // ??? DMD bug with native complex numbers.
        assert(b.re == 0 && b.im == 2);
    else
        assert(b.re == 2 && b.im == 0);
    return 0;
}

ibuclaw avatar Dec 29 '20 19:12 ibuclaw

If I understand correctly, when this switch is enabled, code that uses cfloat, cdouble and creal will instead treat them as float, double, real?

RazvanN7 avatar Jan 08 '21 10:01 RazvanN7

If I understand correctly, when this switch is enabled, code that uses cfloat, cdouble and creal will instead treat them as float, double, real?

No, cfloat, cdouble, and creal will be gone from the language.

However, because some people still need to be able to interface to extern C libraries, some magic enums have been added in order to treat struct { float, float } as a cfloat type for ABI purposes.

When testing this, I have ran into many bugs in DMD though. Which I could infer to mean that nobody can be using complex types in D if there's this many problems with them.

ibuclaw avatar Jan 08 '21 13:01 ibuclaw

Then why use a preview flag? Preview flags usually add a new feature, whereas for removing features we do a deprecation cycle. Is there something preventing us from going down that path?

RazvanN7 avatar Jan 08 '21 14:01 RazvanN7

Removing a feature is still a new feature... :-)

If complex numbers get removed outright, then the -preview=complex will turn into a -revert=complex for another couple years.

ibuclaw avatar Jan 08 '21 17:01 ibuclaw

Which I could infer to mean that nobody can be using complex types in D if there's this many problems with them.

Complex native types were a bad idea, and were deprecated long ago so there was no point to fixing bugs in them. Except that they were never actually deprecated.

WalterBright avatar Jan 14 '21 08:01 WalterBright

Is it close to being done?

12345swordy avatar Feb 20 '21 15:02 12345swordy

Is it close to being done?

The implementation is "done", just slowly rolling out the scaffolding for the library support. It would be good to get -transition=complex being enabled by default before this as well, so that complex types are in full deprecation mode.

ibuclaw avatar Feb 20 '21 16:02 ibuclaw

Which I could infer to mean that nobody can be using complex types in D if there's this many problems with them.

Present in 7 commercial products. We are still using it, and thanks to provide a proper deprecation flag. For us the removal of complex number is not as trivial as people here think it is!

Then why use a preview flag? Preview flags usually add a new feature, whereas for removing features we do a deprecation cycle. Is there something preventing us from going down that path?

We need that preview flag please.

p0nce avatar Feb 20 '21 16:02 p0nce

It would be good to get -transition=complex being enabled by default before this as well, so that complex types are in full deprecation mode.

It would be nice to have preview flag become default in more than just 2 months? We. are. using. this.

Preview flags not exactly simple with DUB.

p0nce avatar Feb 20 '21 16:02 p0nce

It would be good to get -transition=complex being enabled by default before this as well, so that complex types are in full deprecation mode.

It would be nice to have preview flag become default in more than just 2 months? We. are. using. this.

The transition flag has been in the compiler for a few years now. Phobos is built with it, but druntime isn't because there's still a couple of places still using complex types (rt.typeinfo and rt.memset).

Honestly, even before you chimed in my timeline was approximately around in two years time as per DIP 1013.

ibuclaw avatar Feb 20 '21 16:02 ibuclaw

Which I could infer to mean that nobody can be using complex types in D if there's this many problems with them.

Present in 7 commercial products. We are still using it, and thanks to provide a proper deprecation flag. For us the removal of complex number is not as trivial as people here think it is!

In case you missed it, you can still pull out native types for ABI purposes (#12167), these magic enums will land in druntime core.stdc.config soon.

And no, it is not trivial, otherwise it would have been done 3 years ago when the process was started. :-)

ibuclaw avatar Feb 20 '21 16:02 ibuclaw

@ibuclaw status of this PR?

12345swordy avatar Feb 20 '21 17:02 12345swordy

OK I didn't know about the transition flag.

p0nce avatar Feb 20 '21 17:02 p0nce

Rebased, I'll write up a changelog and bring this out of draft phase when #12390 is done.

ibuclaw avatar Apr 06 '21 17:04 ibuclaw

I don't really understand the point of this. Once we're deprecating imaginary / complex, we wait the 10 releases, then remove them. So why an extra -preview ?

Geod24 avatar Apr 06 '21 23:04 Geod24

I don't really understand the point of this. Once we're deprecating imaginary / complex, we wait the 10 releases, then remove them. So why an extra -preview ?

Allows introducing new library code that was previously impeded by creal being a keyword ahead of time and verify that it's working.

There is no rush, however, so I'm fine just leaving this pr for 10 releases to remind what needs to be done to initially remove them. There'll be many follow-up PRs to clean up the now dead parts of the AST.

ibuclaw avatar Apr 07 '21 06:04 ibuclaw

Thanks for your pull request, @ibuclaw!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#12069"

dlang-bot avatar Feb 17 '22 00:02 dlang-bot

Rebased, let's see where we're at.

ibuclaw avatar Feb 17 '22 00:02 ibuclaw

Looking good!

RazvanN7 avatar Feb 17 '22 09:02 RazvanN7

Original implementation using -preview= has now been replaced with Editions.

FYI @dkorpel

ibuclaw avatar Apr 19 '24 16:04 ibuclaw