5110 – Excess attribute propagation of structs and classes

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5110 - Excess attribute propagation of structs and classes
Summary: Excess attribute propagation of structs and classes
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: patch, spec
: 3598 4211 (view as issue list)
Depends on:
Blocks:
Reported: 2010年10月24日 02:02 UTC by Shin Fujishiro
Modified: 2010年12月05日 13:03 UTC (History)
3 users (show)

See Also:


Attachments
Testcases (6.46 KB, application/octet-stream)
2010年10月24日 02:09 UTC, Shin Fujishiro
Details
Patch against dmd r727, passed dmd/druntime/phobos tests (3.45 KB, patch)
2010年10月24日 02:18 UTC, Shin Fujishiro
Details | Diff
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Shin Fujishiro 2010年10月24日 02:02:08 UTC
The override attribute is unnecessarily propagated to a nested class declaration and causes errors:
--------------------
class C
{
 override:
 string toString() { return ""; }
 class Nested
 { // (7)
 void gun() {} // (8)
 }
}
--------------------
test.d(8): Error: function test.C.Nested.gun does not override any function
test.d(7): Error: variable test.C.Nested.this override cannot be applied to variable
--------------------
Another case. The const attribute is propagated to a static member:
--------------------
const struct S
{
 static int value;
}
static assert(is(typeof(S.value) == int)); // (5)
--------------------
test.d(5): Error: static assert (is(const(int) == int)) is false
--------------------
Though the spec allows this behavior, I think it's more natural if the static member variable S.value is typed as mutable int.
Comment 1 Shin Fujishiro 2010年10月24日 02:05:22 UTC
The current ClassDeclaration uses the following black list for masking attributes to propagate over its members:
--------------------
sc->stc &= ~(STCfinal | STCauto | STCscope | STCstatic |
 STCabstract | STCdeprecated | STC_TYPECTOR | STCtls | STCgshared);
sc->stc |= storage_class & STC_TYPECTOR;
--------------------
The following STCs pass the black list:
 STCextern, STCparameter, STCfield, STCoverride, STCsynchronized,
 STCin, STCout, STClazy, STCforeach, STCcomdat, STCvariadic,
 STCctorinit, STCtemplateparameter, STCref, STCinit, STCmanifest,
 STCnodtor, STCnothrow, STCpure, STCalias, STCproperty, STCsafe,
 STCtrusted, STCsystem, STCctfe, STCdisable, STCresult.
Currently, the STCs above are propagated inside a class. Some STCs such as STCparameter and STCout cannot be applied to class declarations; removing such insignificant STCs, I got the following list:
 STCextern, STCoverride, STCsynchronized, STCref, STCnothrow,
 STCpure, STCproperty, STCsafe, STCtrusted, STCsystem, STCdisable.
Among them, the only STCs with which propagation makes sense are:
 STCsynchronized, STCnothrow, STCpure, STCsafe, STCtrusted,
 STCsystem, STCdisable.
Other STCs such as STCoverride should not be propagated (the reported problem).
Comment 2 Shin Fujishiro 2010年10月24日 02:09:16 UTC
Created attachment 792 [details] 
Testcases
Since nothrow, pure and @disable are one-way, non-revertible attributes, the language should not force them to be propagated IMO. So, structs and classes should really propagate only the following STCs:
 STCimmutable, STCconst, STCshared, STCsynchronized,
 STCsafe, STCtrusted, STCsystem.
Note that the first four STCs must not be applied to static members, including nested types:
--------------------
const synchronized class C
{
 static int value; // NO const
 enum E { a, b, c } // NO const
 class N {} // NO const synchronized
}
--------------------
The nested class N itself isn't necessarily const nor synchronized, since its declaration is effectively the same as the following one. Whether N should be const/synchronized or not is independent of C.
--------------------
class N
{
 C outer; // C is already const synchronized
}
--------------------
To sum up, the rule allows these attribute propagations:
 - const, shared, immutable and synchronized over non-static members
 - @safe, @trusted and @system over all members
Attached test cases.
Comment 3 Shin Fujishiro 2010年10月24日 02:18:33 UTC
Created attachment 793 [details] 
Patch against dmd r727, passed dmd/druntime/phobos tests
The proposed patch implements the said rule.
The patch limits STC propagation via Scope's storage class (sc->stc) only to @safe/@trusted/@system. Other STCs such as const and synchronized are 'pulled' by need of each member out of parent AggregateDeclaration.
This patch also fixes bug 3598 and bug 4211.
Comment 4 Walter Bright 2010年12月05日 12:56:06 UTC
http://www.dsource.org/projects/dmd/changeset/781 
Comment 5 Walter Bright 2010年12月05日 13:00:05 UTC
*** Issue 3598 has been marked as a duplicate of this issue. ***
Comment 6 Walter Bright 2010年12月05日 13:03:18 UTC
*** Issue 4211 has been marked as a duplicate of this issue. ***


AltStyle によって変換されたページ (->オリジナル) /