Skip to content

Add more examples to the specification runner #2330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions spec/attribute.dd
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ $(GNAME AlignAttribute):
sets it to the default, which matches the default member alignment
of the companion C compiler.)

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
--------
struct S
{
Expand All @@ -221,8 +222,9 @@ struct S
int b; // placed at offset 4
long c; // placed at offset 8
}
auto sz = S.sizeof; // 16
static assert(S.sizeof == 16);
--------
)

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

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
--------
struct S
{
Expand All @@ -241,8 +244,9 @@ struct S
int b; // placed at offset 1
long c; // placed at offset 5
}
auto sz = S.sizeof; // 16
static assert(S.sizeof == 13);
--------
)

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

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


Expand Down Expand Up @@ -307,14 +311,19 @@ $(GNAME DeprecatedAttribute):
if any code refers to deprecated declarations:
)

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
---------------
deprecated
struct Foo
{
void oldFoo();
deprecated
{
void oldFoo();
}
}

oldFoo(); // Deprecated: function test.oldFoo is deprecated
Foo().oldFoo(); // Deprecated: function test.oldFoo is deprecated
---------------
)

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

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
---------------
import std.format;
enum Message = format("%s and all its members are obsolete", Foobar.stringof);
deprecated(Message) class Foobar {}
auto f = new Foobar(); // Deprecated: class test.Foobar is deprecated - Foobar
// and all its members are obsolete

deprecated(format("%s is also obsolete", "This class")) class BarFoo {}
auto bf = new BarFoo(); // Deprecated: class test.BarFoo is deprecated - This
// class is also obsolete

void main()
{
auto f = new Foobar(); // Deprecated: class test.Foobar is deprecated - Foobar
// and all its members are obsolete
auto bf = new BarFoo(); // Deprecated: class test.BarFoo is deprecated - This
// class is also obsolete
}
---------------
)

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

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
---
class Foo
{
Expand All @@ -476,6 +493,7 @@ $(H2 $(LNAME2 gshared, $(D __gshared) Attribute))
return bar++; // Not thread safe.
}
---
)

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

$(SPEC_RUNNABLE_EXAMPLE_FAIL
---
@disable void foo() { }
---
---

void main() { foo(); /* error, foo is disabled */ }
---
)

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

$(SPEC_RUNNABLE_EXAMPLE_FAIL
---------------
class Foo
{
Expand All @@ -560,6 +580,7 @@ class Foo2 : Foo
}
}
---------------
)

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

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

$(SPEC_RUNNABLE_EXAMPLE_FAIL
---------------
class Foo
{
Expand All @@ -585,6 +607,7 @@ Foo.foobar(); // error, no instance of Foo
f.bar(); // produces 6;
f.foobar(); // produces 7;
---------------
)

$(P
Static functions are never virtual.
Expand Down Expand Up @@ -696,9 +719,13 @@ $(GNAME UserDefinedAttribute):

A user-defined attribute looks like:

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
---
@(3) int a;
---
)

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
---
@("string", 7) int b;

Expand All @@ -712,6 +739,7 @@ struct Bar

@Bar(3) int d;
---
)

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

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
---
@('c') string s;
pragma(msg, __traits(getAttributes, s)); // prints tuple('c')
---
)

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