-
-
Notifications
You must be signed in to change notification settings - Fork 369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add spec for the @disable new(); allocator relic #2846
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,6 @@ $(SPEC_S Deprecated Features, | |
| $(THEAD Feature, Spec, Dep, Error, Gone) | ||
| $(TROW $(DEPLINK body keyword), 2.075, , , ) | ||
| $(TROW $(DEPLINK Hexstring literals), 2.079, 2.079, 2.086, ) | ||
| $(TROW $(DEPLINK Class allocators and deallocators), , 2.080, 2.087, ) | ||
| $(TROW $(DEPLINK Implicit comparison of different enums), 2.075, 2.075, 2.081, ) | ||
| $(TROW $(DEPLINK Implicit string concatenation), 2.072, 2.072, 2.081, ) | ||
| $(TROW $(DEPLINK Using the result of a comma expression), 2.072, 2.072, 2.079, ) | ||
|
|
@@ -116,56 +115,6 @@ $(H4 Rationale) | |
| $(P Hexstrings are used so seldom that they don't warrant a language feature. | ||
| ) | ||
|
|
||
| $(H3 $(DEPNAME Class allocators and deallocators)) | ||
| $(P D classes can have members customizing the (de)allocation strategy. | ||
| --- | ||
| class Foo | ||
| { | ||
| new(uint size, ...) | ||
| { | ||
| return malloc(size); | ||
| } | ||
|
|
||
| delete(void* obj) | ||
| { | ||
| free(obj); | ||
| } | ||
| } | ||
|
|
||
| Foo foo = new(...) Foo(); | ||
| delete foo; | ||
| --- | ||
| ) | ||
| $(H4 Corrective Action) | ||
| $(P Move the (de)allocation strategy out of the class | ||
| --- | ||
| class Foo | ||
| { | ||
| } | ||
|
|
||
| T make(T, Args...)(auto ref Args args) if (is(T == Foo)) | ||
| { | ||
| enum size = __traits(classInstanceSize, T); | ||
| void* mem = malloc(size); | ||
| scope (failure) free(mem); | ||
| return mem !is null ? emplace!T(mem[0..size], args) : null; | ||
| } | ||
|
|
||
| void dispose(T)(T obj) | ||
| { | ||
| auto mem = cast(void*) obj; | ||
| scope (exit) free(mem); | ||
| destroy(obj); | ||
| } | ||
|
|
||
| Foo foo = make!Foo(); | ||
| if (foo !is null) dispose(foo); | ||
| --- | ||
| ) | ||
| $(H4 Rationale) | ||
| $(P Classes should not be responsible for their own (de)allocation strategy. | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No this needs to be kept. The entire point of this document is to provide historic rationals. |
||
|
|
||
| $(H3 $(DEPNAME Implicit comparison of different enums)) | ||
| $(P Comparison of different enumerated type was allowed: | ||
| --- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,8 +19,6 @@ $(GNAME DeclDef): | |
| $(GLINK2 class, Constructor) | ||
| $(GLINK2 class, Destructor) | ||
| $(GLINK2 struct, Postblit) | ||
| $(GLINK2 class, Allocator) | ||
| $(GLINK2 class, Deallocator) | ||
| $(GLINK2 class, ClassInvariant) | ||
| $(GLINK2 struct, StructInvariant) | ||
| $(GLINK2 unittest, UnitTest) | ||
|
|
@@ -37,6 +35,7 @@ $(GNAME DeclDef): | |
| $(GLINK2 template-mixin, TemplateMixinDeclaration) | ||
| $(GLINK2 template-mixin, TemplateMixin) | ||
| $(GLINK MixinDeclaration) | ||
| $(GLINK AllocatorRelic) | ||
| $(D ;) | ||
| ) | ||
|
|
||
|
|
@@ -634,6 +633,27 @@ $(GNAME MixinDeclaration): | |
| $(GLINK DeclDefs), and is compiled as such. | ||
| ) | ||
|
|
||
| $(H2 $(LEGACY_LNAME2 AllocatorRelic, allocator-relic, Allocator Relic)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see there any need to call it a relic, either in the grammar or documentation, unless There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry but I really like this rule name. from the Cambridge dictionnary relic
noun
/ˈrelik/
something left from a past timeThat matches perfectly to what it is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By calling it a relic, to me this is implicitly saying "don't use", or at the very least discourages use (why would you use a relic feature in your codebase?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A relic is not something you drop. It's cultural inheritage. It's something you keep because it tells something about history and culture. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A relic conjures up that it is an artefact, vestige, or an archaism in the language. But as far as I can tell, there is no reason to tell people that they should not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest me another rule name. Discussing about semantics will lead to nothing, although, to be clear, I really liked my proposal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's a cultural/language thing but I would read "relic" (in the context of software docs) as a soft deprecation. Maybe use a neutral name based on the actual effect of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I'm sorry, but I agree that relic is a terrible name for the reasons stated above. Why not name it what it actually is. A few examples: "NewClassDisabler", "ClassAllocationOptOut", "DefaultClassAllocationRule". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think the following:
For instance (re-using some of the existing documentation):
I assume that |
||
|
|
||
| $(GRAMMAR | ||
| $(GNAME AllocatorRelic): | ||
| $(D new) $(D $(LPAREN)) $(D $(RPAREN)) $(D ;) | ||
| ) | ||
|
|
||
| $(P This is a relic of the old D1-style class and struct custom $(D new) operator. | ||
| Its sole purpose is to be prefixed with $(D @disable) so that $(D new) operator | ||
| is not permited for the aggregate that contains the allocator relic, for example | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. permitted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "NewClassDisabler" does not work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's go for |
||
| to enforce the usage of $(D std.allocators.make()) on classes or encourage | ||
| stack allocation for structs.) | ||
|
|
||
| --------- | ||
| class NewNotAllowed | ||
| { | ||
| @disable new(); | ||
| } | ||
|
|
||
| with (new NewNotAllowed()) {} // Error: the `new` operator is disabled for type `main.NewNotAllowed` | ||
| --------- | ||
|
|
||
| $(H2 $(LEGACY_LNAME2 PackageModule, package-module, Package Module)) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just need to update the gone column, no need to remove the documented deprecation.