Skip to content

Commit b833ba5

Browse files
wilzbachthewilsonator
authored andcommitted
Add more examples to the specification runner
1 parent 104d354 commit b833ba5

File tree

3 files changed

+121
-21
lines changed

3 files changed

+121
-21
lines changed

spec/attribute.dd

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

216+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
216217
--------
217218
struct S
218219
{
@@ -221,8 +222,9 @@ struct S
221222
int b; // placed at offset 4
222223
long c; // placed at offset 8
223224
}
224-
auto sz = S.sizeof; // 16
225+
static assert(S.sizeof == 16);
225226
--------
227+
)
226228

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

238+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
236239
--------
237240
struct S
238241
{
@@ -241,8 +244,9 @@ struct S
241244
int b; // placed at offset 1
242245
long c; // placed at offset 5
243246
}
244-
auto sz = S.sizeof; // 16
247+
static assert(S.sizeof == 13);
245248
--------
249+
)
246250

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

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

276280

@@ -307,14 +311,19 @@ $(GNAME DeprecatedAttribute):
307311
if any code refers to deprecated declarations:
308312
)
309313

314+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
310315
---------------
311-
deprecated
316+
struct Foo
312317
{
313-
void oldFoo();
318+
deprecated
319+
{
320+
void oldFoo();
321+
}
314322
}
315323

316-
oldFoo(); // Deprecated: function test.oldFoo is deprecated
324+
Foo().oldFoo(); // Deprecated: function test.oldFoo is deprecated
317325
---------------
326+
)
318327

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

340+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
331341
---------------
332342
import std.format;
333343
enum Message = format("%s and all its members are obsolete", Foobar.stringof);
334344
deprecated(Message) class Foobar {}
335-
auto f = new Foobar(); // Deprecated: class test.Foobar is deprecated - Foobar
336-
// and all its members are obsolete
345+
337346
deprecated(format("%s is also obsolete", "This class")) class BarFoo {}
338-
auto bf = new BarFoo(); // Deprecated: class test.BarFoo is deprecated - This
339-
// class is also obsolete
347+
348+
void main()
349+
{
350+
auto f = new Foobar(); // Deprecated: class test.Foobar is deprecated - Foobar
351+
// and all its members are obsolete
352+
auto bf = new BarFoo(); // Deprecated: class test.BarFoo is deprecated - This
353+
// class is also obsolete
354+
}
340355
---------------
356+
)
341357

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

483+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
467484
---
468485
class Foo
469486
{
@@ -476,6 +493,7 @@ $(H2 $(LNAME2 gshared, $(D __gshared) Attribute))
476493
return bar++; // Not thread safe.
477494
}
478495
---
496+
)
479497

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

512+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
494513
---
495514
@disable void foo() { }
496-
---
497-
---
515+
498516
void main() { foo(); /* error, foo is disabled */ }
499517
---
518+
)
500519

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

566+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
547567
---------------
548568
class Foo
549569
{
@@ -560,6 +580,7 @@ class Foo2 : Foo
560580
}
561581
}
562582
---------------
583+
)
563584

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

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

594+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
573595
---------------
574596
class Foo
575597
{
@@ -585,6 +607,7 @@ Foo.foobar(); // error, no instance of Foo
585607
f.bar(); // produces 6;
586608
f.foobar(); // produces 7;
587609
---------------
610+
)
588611

589612
$(P
590613
Static functions are never virtual.
@@ -696,9 +719,13 @@ $(GNAME UserDefinedAttribute):
696719

697720
A user-defined attribute looks like:
698721

722+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
699723
---
700724
@(3) int a;
701725
---
726+
)
727+
728+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
702729
---
703730
@("string", 7) int b;
704731

@@ -712,6 +739,7 @@ struct Bar
712739

713740
@Bar(3) int d;
714741
---
742+
)
715743

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

760+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
732761
---
733762
@('c') string s;
734763
pragma(msg, __traits(getAttributes, s)); // prints tuple('c')
735764
---
765+
)
736766

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

0 commit comments

Comments
 (0)