Skip to content

Commit c49b350

Browse files
authored
Merge pull request #2003 from wilzbach/1040-attributes
Revive #1040 - spec/attributes.d merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
2 parents 5251714 + d5d6582 commit c49b350

File tree

1 file changed

+21
-52
lines changed

1 file changed

+21
-52
lines changed

spec/attribute.dd

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ $(GNAME Property):
3737
$(GLINK UserDefinedAttribute)
3838

3939
$(GNAME PropertyIdentifier):
40-
$(RELATIVE_LINK2 property, $(D property))
41-
$(D safe)
42-
$(D trusted)
43-
$(D system)
40+
$(RELATIVE_LINK2 property, `property`)
41+
$(RELATIVE_LINK2 safe, `@safe`)
42+
$(RELATIVE_LINK2 safe, `@trusted`)
43+
$(RELATIVE_LINK2 safe, `@system`)
4444
$(RELATIVE_LINK2 disable, $(D disable))
45-
$(RELATIVE_LINK2 nogc, $(D nogc))
45+
$(RELATIVE_LINK2 nogc, `nogc`)
46+
$(RELATIVE_LINK2 disable, `@disable`)
4647

4748
$(GNAME DeclarationBlock):
4849
$(GLINK2 module, DeclDef)
@@ -502,45 +503,13 @@ void main() { foo(); /* error, foo is disabled */ }
502503
makes the struct not copyable.
503504
)
504505

505-
$(H2 $(LNAME2 nogc, $(D @nogc) Attribute))
506-
507-
$(P $(D @nogc) applies to functions, and means that that function does not
508-
allocate memory on the GC heap, either directly such as with
509-
$(GLINK2 expression, NewExpression) or
510-
indirectly through functions it may call, or through language features
511-
such as array concatenation and dynamic closures.
512-
)
513-
514-
---
515-
@nogc void foo(char[] a)
516-
{
517-
auto p = new int; // error, operator new allocates
518-
a ~= 'c'; // error, appending to arrays allocates
519-
bar(); // error, bar() may allocate
520-
}
521-
522-
void bar() { }
523-
---
506+
$(H2 $(LNAME2 safe, $(D @safe), $(D @trusted), and $(D @system) Attribute))
524507

525-
$(P $(D @nogc) affects the type of the function. An $(D @nogc) function is covariant
526-
with a non-$(D @nogc) function.
527-
)
528-
529-
---
530-
void function() fp;
531-
void function() @nogc gp; // pointer to @nogc function
508+
$(P See $(DDSUBLINK function, function-safety, Function Safety).)
532509

533-
void foo();
534-
@nogc void bar();
510+
$(H2 $(LNAME2 nogc, $(D @nogc) Attribute))
535511

536-
void test()
537-
{
538-
fp = &foo; // ok
539-
fp = &bar; // ok, it's covariant
540-
gp = &foo; // error, not contravariant
541-
gp = &bar; // ok
542-
}
543-
---
512+
$(P See $(DDSUBLINK function, nogc-functions, No-GC Functions).)
544513

545514
$(H2 $(LNAME2 property, $(D @property) Attribute))
546515

@@ -643,6 +612,10 @@ $(H2 $(LNAME2 auto, $(D auto) Attribute))
643612
auto i = 6.8; // declare i as a double
644613
---
645614

615+
$(P For functions, the `auto` attribute means return type inference.
616+
See $(DDSUBLINK function, auto-functions, Auto Functions).
617+
)
618+
646619
$(H2 $(LNAME2 scope, $(D scope) Attribute))
647620

648621
$(P
@@ -703,16 +676,6 @@ $(P
703676

704677
$(H2 $(LNAME2 uda, User Defined Attributes))
705678

706-
$(P
707-
User Defined Attributes (UDA) are compile time expressions that can be attached
708-
to a declaration. These attributes can then be queried, extracted, and manipulated
709-
at compile time. There is no runtime component to them.
710-
)
711-
712-
$(P
713-
Grammatically, a UDA is a StorageClass:
714-
)
715-
716679
$(GRAMMAR
717680
$(GNAME UserDefinedAttribute):
718681
$(D @ $(LPAREN)) $(GLINK2 expression, ArgumentList) $(D $(RPAREN))
@@ -722,7 +685,13 @@ $(GNAME UserDefinedAttribute):
722685
$(D @) $(GLINK2 template, TemplateInstance) $(D $(LPAREN)) $(GLINK2 expression, ArgumentList)$(OPT) $(D $(RPAREN))
723686
)
724687

725-
And looks like:
688+
$(P
689+
User Defined Attributes (UDA) are compile-time expressions that can be attached
690+
to a declaration. These attributes can then be queried, extracted, and manipulated
691+
at compile time. There is no runtime component to them.
692+
)
693+
694+
A user-defined attribute looks like:
726695

727696
---
728697
@(3) int a;

0 commit comments

Comments
 (0)