Difference between revisions of "DIP60"

From D Wiki
Jump to: navigation, search
(Description)
(Added a note regarding static allocations.)
Line 7: Line 7:
 
|-
 
|-
 
|Version:
 
|Version:
|1
+
|1.1
 
|-
 
|-
 
|Status:
 
|Status:
Line 13: Line 13:
 
|-
 
|-
 
|Created:
 
|Created:
|2014-14-15
+
|2014-4-15
 
|-
 
|-
 
|Last Modified:
 
|Last Modified:
|2014-14-15
+
|2014-4-16
 
|-
 
|-
 
|Author:
 
|Author:
Line 47: Line 47:
 
@nogc int func(int a) { ... }
 
@nogc int func(int a) { ... }
  
 +
== Static allocations should be ignored ==
 +
 +
This code (and its mutable __gshared variants) should work since the beginning:
 +
 +
  void foo() @nogc nothrow {
 +
      static const err = new Error("error");
 +
      throw err;
 +
  }
 +
 +
The situation is similar to this code, that is allowed (text is not nothrow, but here it's called at compile-time):
 +
 +
    void foo() nothrow {
 +
        import std.conv;
 +
        enum msg = text(10);
 +
    }
  
 
== Copyright ==
 
== Copyright ==

Revision as of 20:40, 16 April 2014

Title: DIP Add @nogc Function Attribute
DIP: 60
Version: 1.1
Status: Draft
Created: 2014-4-15
Last Modified: 2014-4-16
Author: Walter Bright
Links: DIP60/Archivepull request
- forum discussion

Abstract

The @nogc function attribute will mark a function as not making any allocations using the GC.

Rationale

Many users want to be able to guarantee that code will not allocate using the GC.

Description

@nogc goes in the same way that the nothrow attribute does, and is quite similar in behavior. It affects inheritance in that it is covariant. The name mangling for it will be "Ni". @nogc will be inferred for template functions in the same manner as nothrow is. @nogc will be transitive, in that all functions called by an @nogc function must also be @nogc. GC allocations in a @nogc function will be disallowed, and that means calls to operator new, closures that allocate on the GC, array concatenation, array appends, and some array literals.

No functions in the GC implementation will be marked @nogc.

Usage

@nogc int func(int a) { ... }

Static allocations should be ignored

This code (and its mutable __gshared variants) should work since the beginning:

 void foo() @nogc nothrow {
     static const err = new Error("error");
     throw err;
 }

The situation is similar to this code, that is allowed (text is not nothrow, but here it's called at compile-time):

   void foo() nothrow {
       import std.conv;
       enum msg = text(10);
   }

Copyright

This document has been placed in the Public Domain.