Skip to content

Commit 2a069fa

Browse files
authored
[spec/attribute] Make more examples runnable (#3557)
Make C++ namespace example runnable. Make deprecated example runnable. Add 'Scope Values' subheading. Make 1 scope example runnable (the other needs -dip1000 or a compiler update). Make scope new class example runnable. Make 2 UDA examples runnable and add 'Templates' subheading.
1 parent d785b7c commit 2a069fa

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

spec/attribute.dd

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,19 @@ $(H3 C++ $(LNAME2 namespace, Namespaces))
183183

184184
$(P Namespaces create a new named scope that is imported into its enclosing scope.)
185185

186+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
186187
---
187188
extern (C++, N) { void foo(); void bar(); }
188189
extern (C++, M) { void foo(); }
189190

190-
bar(); // ok
191-
foo(); // error - N.foo() or M.foo() ?
192-
M.foo(); // ok
191+
void main()
192+
{
193+
bar(); // ok
194+
//foo(); // error - N.foo() or M.foo() ?
195+
M.foo(); // ok
196+
}
193197
---
198+
)
194199

195200
$(P Multiple identifiers in the $(I QualifiedIdentifier) create nested namespaces:)
196201

@@ -373,16 +378,23 @@ $(GNAME DeprecatedAttribute):
373378
$(P Calling CTFE-able functions or using manifest constants is also possible.
374379
)
375380

381+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
376382
---------------
377383
import std.format;
378-
enum Message = format("%s and all its members are obsolete", Foobar.stringof);
379-
deprecated(Message) class Foobar {}
380-
auto f = new Foobar(); // Deprecated: class test.Foobar is deprecated - Foobar
381-
// and all its members are obsolete
384+
385+
enum message = format("%s and all its members are obsolete", Foobar.stringof);
386+
deprecated(message) class Foobar {}
382387
deprecated(format("%s is also obsolete", "This class")) class BarFoo {}
383-
auto bf = new BarFoo(); // Deprecated: class test.BarFoo is deprecated - This
384-
// class is also obsolete
388+
389+
void main()
390+
{
391+
auto fb = new Foobar(); // Deprecated: class test.Foobar is deprecated - Foobar
392+
// and all its members are obsolete
393+
auto bf = new BarFoo(); // Deprecated: class test.BarFoo is deprecated - This
394+
// class is also obsolete
395+
}
385396
---------------
397+
)
386398

387399
$(P $(D Implementation Note:) The compiler should have a switch
388400
specifying if $(D deprecated) should be ignored, cause a warning, or cause an error during compilation.
@@ -733,7 +745,7 @@ $(P
733745
$(LI Putting a `scope` variable in an array literal)
734746
)
735747
$(P
736-
The `scope` attribute is part of the variable declaration, not of the type, and it only applies to the first level of indirection.
748+
The `scope` attribute is part of the variable declaration, not the type, and it only applies to the first level of indirection.
737749
For example, it is impossible to declare a variable as a dynamic array of scope pointers, because `scope` only applies to the `.ptr`
738750
of the array itself, not its elements. `scope` affects various types as follows:
739751
)
@@ -772,6 +784,8 @@ string escape(scope S s, scope S* sPtr, scope string[2] sarray, scope string[] d
772784
}
773785
---
774786

787+
$(H3 $(LNAME2 scope-values, Scope Values))
788+
775789
$(P
776790
A "`scope` value" is the value of a `scope` variable, or a generated value pointing to stack allocated memory.
777791
Such values are generated by $(DDSUBLINK spec/arrays, slicing, slicing) a static array
@@ -814,6 +828,14 @@ void variadic(int[] a...)
814828
int[] x = a; // inferred `scope int[]`
815829
}
816830

831+
void main()
832+
{
833+
variadic(1, 2, 3);
834+
}
835+
---
836+
837+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
838+
---
817839
struct S
818840
{
819841
int x;
@@ -826,6 +848,7 @@ struct S
826848
}
827849
}
828850
---
851+
)
829852

830853
$(P
831854
$(DDSUBLINK spec/function, scope-parameters, Scope Parameters) are treated the same as scope local variables,
@@ -857,21 +880,29 @@ $(P
857880
proper destruction of the variable.
858881
)
859882

883+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
860884
---
861-
class C {}
885+
import core.stdc.stdio : puts;
886+
887+
class C
888+
{
889+
~this() @nogc { puts(__FUNCTION__); }
890+
}
862891

863892
void main() @nogc
864893
{
865894
{
866895
scope c0 = new C(); // allocated on the stack
867896
scope c1 = new C();
868897

869-
c1 = c0; // not allowed
898+
//c1 = c0; // Error: cannot rebind scope variables
870899

871900
// destructor of `c1` and `c0` are called here in that order
872901
}
902+
puts("bye");
873903
}
874904
---
905+
)
875906

876907

877908
$(H2 $(LNAME2 class-attributes, Class Attributes))
@@ -1025,6 +1056,7 @@ pragma(msg, __traits(getAttributes, s)); // prints tuple('c')
10251056
The expression tuple can be turned into a manipulatable tuple:
10261057
)
10271058

1059+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
10281060
---
10291061
enum EEE = 7;
10301062
@("hello") struct SSS { }
@@ -1034,7 +1066,10 @@ alias TP = __traits(getAttributes, foo);
10341066

10351067
pragma(msg, TP); // prints tuple(3, 4, 7, (SSS))
10361068
pragma(msg, TP[2]); // prints 7
1069+
1070+
void main() {}
10371071
---
1072+
)
10381073

10391074
$(P
10401075
Of course the tuple types can be used to declare things:
@@ -1065,12 +1100,15 @@ pragma(msg, __traits(getAttributes, typeof(a))); // prints tuple("hello")
10651100
interprets them.
10661101
)
10671102

1103+
$(H3 $(LNAME2 uda-templates, Templates))
1104+
10681105
$(P
10691106
If a UDA is attached to a template declaration, then it will be automatically
10701107
attached to all direct members of instances of that template. If any of those
10711108
members are templates themselves, this rule applies recursively:
10721109
)
10731110

1111+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
10741112
---
10751113
@("foo") template Outer(T)
10761114
{
@@ -1098,7 +1136,10 @@ pragma(msg, __traits(getAttributes, Outer!int.Inner));
10981136
// prints tuple("foo", "bar")
10991137
pragma(msg, __traits(getAttributes, Outer!int.Inner!int.z));
11001138
// prints tuple("foo", "bar")
1139+
1140+
void main() {}
11011141
---
1142+
)
11021143

11031144
$(P
11041145
UDAs cannot be attached to template parameters.

0 commit comments

Comments
 (0)