[Deprecation -> Error] destructor field attributes (#16802)

This commit is contained in:
Nicholas Wilson 2024-08-22 14:35:39 +08:00 committed by GitHub
parent 796f5cee9a
commit df377af9fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 56 deletions

View file

@ -0,0 +1,32 @@
An error is now given for constructors with field destructors with stricter attributes
```
struct HasDtor
{
~this() {}
}
struct Pure
{
HasDtor member;
this(int) pure {} // Error: `this` has stricter attributes than its destructor (`pure`)
}
struct Nothrow
{
HasDtor member;
this(int) nothrow {} // Error: `this` has stricter attributes than its destructor (`nothrow`)
}
struct NoGC
{
HasDtor member;
this(int) @nogc {} // Error: `this` has stricter attributes than its destructor (`@nogc`)
}
struct Safe
{
HasDtor member;
this(int) @safe {} // Error: `this` has stricter attributes than its destructor (`@safe`)
}
```

View file

@ -1452,10 +1452,6 @@ private extern(C++) final class Semantic3Visitor : Visitor
auto sexp = new ExpStatement(ctor.loc, ce);
auto ss = new ScopeStatement(ctor.loc, sexp, ctor.loc);
// @@@DEPRECATED_2.106@@@
// Allow negligible attribute violations to allow for a smooth
// transition. Remove this after the usual deprecation period
// after 2.106.
if (global.params.dtorFields == FeatureState.default_)
{
auto ctf = cast(TypeFunction) ctor.type;
@ -1474,9 +1470,9 @@ private extern(C++) final class Semantic3Visitor : Visitor
(puErr ? STC.pure_ : 0) |
(saErr ? STC.system : 0)
);
ctor.loc.deprecation("`%s` has stricter attributes than its destructor (`%s`)", ctor.toPrettyChars(), ob.peekChars());
ctor.loc.deprecationSupplemental("The destructor will be called if an exception is thrown");
ctor.loc.deprecationSupplemental("Either make the constructor `nothrow` or adjust the field destructors");
ctor.loc.error("`%s` has stricter attributes than its destructor (`%s`)", ctor.toPrettyChars(), ob.peekChars());
ctor.loc.errorSupplemental("The destructor will be called if an exception is thrown");
ctor.loc.errorSupplemental("Either make the constructor `nothrow` or adjust the field destructors");
ce.ignoreAttributes = true;
}

View file

@ -1,49 +0,0 @@
/**
Checks that code still compiles when -preview=dtorfields is enabled by default
but issues an appropriate deprecation message.
Remove this test when the deprecations period ends, see visit(CtorDeclaration)
in semantic3.d
TEST_OUTPUT:
---
compilable/dtorfields_deprecation.d(30): Deprecation: `dtorfields_deprecation.Pure.this` has stricter attributes than its destructor (`pure`)
compilable/dtorfields_deprecation.d(30): The destructor will be called if an exception is thrown
compilable/dtorfields_deprecation.d(30): Either make the constructor `nothrow` or adjust the field destructors
compilable/dtorfields_deprecation.d(42): Deprecation: `dtorfields_deprecation.NoGC.this` has stricter attributes than its destructor (`@nogc`)
compilable/dtorfields_deprecation.d(42): The destructor will be called if an exception is thrown
compilable/dtorfields_deprecation.d(42): Either make the constructor `nothrow` or adjust the field destructors
compilable/dtorfields_deprecation.d(48): Deprecation: `dtorfields_deprecation.Safe.this` has stricter attributes than its destructor (`@system`)
compilable/dtorfields_deprecation.d(48): The destructor will be called if an exception is thrown
compilable/dtorfields_deprecation.d(48): Either make the constructor `nothrow` or adjust the field destructors
---
**/
struct HasDtor
{
~this() {}
}
struct Pure
{
HasDtor member;
this(int) pure {}
}
struct Nothrow
{
HasDtor member;
this(int) nothrow {}
}
struct NoGC
{
HasDtor member;
this(int) @nogc {}
}
struct Safe
{
HasDtor member;
this(int) @safe {}
}