Skip to content

Commit fcb40da

Browse files
committed
Add more examples to the specification runner
1 parent 8fc54e0 commit fcb40da

File tree

3 files changed

+120
-21
lines changed

3 files changed

+120
-21
lines changed

spec/attribute.dd

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ $(GNAME AlignAttribute):
214214
sets it to the default, which matches the default member alignment
215215
of the companion C compiler.)
216216

217+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
217218
--------
218219
struct S
219220
{
@@ -222,8 +223,9 @@ struct S
222223
int b; // placed at offset 4
223224
long c; // placed at offset 8
224225
}
225-
auto sz = S.sizeof; // 16
226+
static assert(S.sizeof == 16);
226227
--------
228+
)
227229

228230
$(P $(I AssignExpression) specifies the alignment
229231
which matches the behavior of the companion C compiler when non-default
@@ -234,6 +236,7 @@ auto sz = S.sizeof; // 16
234236
fields are packed together.
235237
)
236238

239+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
237240
--------
238241
struct S
239242
{
@@ -242,8 +245,9 @@ struct S
242245
int b; // placed at offset 1
243246
long c; // placed at offset 5
244247
}
245-
auto sz = S.sizeof; // 16
248+
static assert(S.sizeof == 13);
246249
--------
250+
)
247251

248252
$(P The alignment for the fields of an aggregate does not affect the alignment
249253
of the aggregate itself - that is affected by the alignment setting outside
@@ -257,7 +261,7 @@ align (2) struct S
257261
int b; // placed at offset 1
258262
long c; // placed at offset 5
259263
}
260-
auto sz = S.sizeof; // 14
264+
static assert(S.sizeof == 14);
261265
--------
262266

263267
$(P Setting the alignment of a field aligns it to that power of 2, regardless
@@ -271,7 +275,7 @@ struct S
271275
byte b; // placed at offset 4
272276
short c; // placed at offset 8
273277
}
274-
auto sz = S.sizeof; // 12
278+
static assert(S.sizeof == 12);
275279
--------
276280

277281

@@ -306,14 +310,19 @@ $(GNAME DeprecatedAttribute):
306310
if any code refers to deprecated declarations:
307311
)
308312

313+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
309314
---------------
310-
deprecated
315+
struct Foo
311316
{
312-
void oldFoo();
317+
deprecated
318+
{
319+
void oldFoo();
320+
}
313321
}
314322

315-
oldFoo(); // Deprecated: function test.oldFoo is deprecated
323+
Foo().oldFoo(); // Deprecated: function test.oldFoo is deprecated
316324
---------------
325+
)
317326

318327
$(P Optionally a string literal or manifest constant can be used
319328
to provide additional information in the deprecation message.
@@ -327,16 +336,23 @@ $(GNAME DeprecatedAttribute):
327336
$(P Calling CTFE-able functions or using manifest constants is also possible.
328337
)
329338

339+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
330340
---------------
331341
import std.format;
332342
enum Message = format("%s and all its members are obsolete", Foobar.stringof);
333343
deprecated(Message) class Foobar {}
334-
auto f = new Foobar(); // Deprecated: class test.Foobar is deprecated - Foobar
335-
// and all its members are obsolete
344+
336345
deprecated(format("%s is also obsolete", "This class")) class BarFoo {}
337-
auto bf = new BarFoo(); // Deprecated: class test.BarFoo is deprecated - This
338-
// class is also obsolete
346+
347+
void main()
348+
{
349+
auto f = new Foobar(); // Deprecated: class test.Foobar is deprecated - Foobar
350+
// and all its members are obsolete
351+
auto bf = new BarFoo(); // Deprecated: class test.BarFoo is deprecated - This
352+
// class is also obsolete
353+
}
339354
---------------
355+
)
340356

341357
$(P $(D Implementation Note:) The compiler should have a switch
342358
specifying if $(D deprecated) should be ignored, cause a warning, or cause an error during compilation.
@@ -463,6 +479,7 @@ $(H2 $(LNAME2 gshared, $(D __gshared) Attribute))
463479
except that the variable is shared by all threads rather than being
464480
thread local.)
465481

482+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
466483
---
467484
class Foo
468485
{
@@ -475,6 +492,7 @@ $(H2 $(LNAME2 gshared, $(D __gshared) Attribute))
475492
return bar++; // Not thread safe.
476493
}
477494
---
495+
)
478496

479497
$(P Unlike the $(D shared) attribute, $(D __gshared) provides no
480498
safe-guards against data races or other multi-threaded synchronization
@@ -490,12 +508,13 @@ causes a compile time error. This can be used to explicitly disallow certain
490508
operations or overloads at compile time rather than relying on generating a
491509
runtime error.)
492510

511+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
493512
---
494513
@disable void foo() { }
495-
---
496-
---
514+
497515
void main() { foo(); /* error, foo is disabled */ }
498516
---
517+
)
499518

500519
$(P $(DDSUBLINK spec/struct, Struct-Constructor, Disabling struct no-arg constructor)
501520
disallows default construction of the struct.
@@ -543,6 +562,7 @@ $(H2 $(LNAME2 override, $(D override) Attribute))
543562
their overriding functions updated.
544563
)
545564

565+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
546566
---------------
547567
class Foo
548568
{
@@ -559,6 +579,7 @@ class Foo2 : Foo
559579
}
560580
}
561581
---------------
582+
)
562583

563584
$(H2 $(LNAME2 static, $(D static) Attribute))
564585

@@ -569,6 +590,7 @@ $(H2 $(LNAME2 static, $(D static) Attribute))
569590
$(D static) is ignored when applied to other declarations.
570591
)
571592

593+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
572594
---------------
573595
class Foo
574596
{
@@ -584,6 +606,7 @@ Foo.foobar(); // error, no instance of Foo
584606
f.bar(); // produces 6;
585607
f.foobar(); // produces 7;
586608
---------------
609+
)
587610

588611
$(P
589612
Static functions are never virtual.
@@ -695,9 +718,13 @@ $(GNAME UserDefinedAttribute):
695718

696719
A user-defined attribute looks like:
697720

721+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
698722
---
699723
@(3) int a;
700724
---
725+
)
726+
727+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
701728
---
702729
@("string", 7) int b;
703730

@@ -711,6 +738,7 @@ struct Bar
711738

712739
@Bar(3) int d;
713740
---
741+
)
714742

715743
$(P
716744
If there are multiple UDAs in scope for a declaration, they are concatenated:
@@ -728,10 +756,12 @@ struct Bar
728756
UDA's can be extracted into an expression tuple using $(D __traits):
729757
)
730758

759+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
731760
---
732761
@('c') string s;
733762
pragma(msg, __traits(getAttributes, s)); // prints tuple('c')
734763
---
764+
)
735765

736766
$(P
737767
If there are no user-defined attributes for the symbol, an empty tuple is returned.

0 commit comments

Comments
 (0)