Skip to content

Commit d74f363

Browse files
authored
Merge pull request #4151 from WalterBright/__rvalue
add RvalueExpressions
2 parents ca060ac + c78b572 commit d74f363

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

spec/expression.dd

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,7 @@ $(GNAME PrimaryExpression):
20082008
$(GLINK IsExpression)
20092009
$(D $(LPAREN)) $(GLINK Expression) $(D $(RPAREN))
20102010
$(GLINK SpecialKeyword)
2011+
$(GLINK RvalueExpression)
20112012
$(GLINK2 traits, TraitsExpression)
20122013

20132014
$(GNAME LiteralExpression):
@@ -3534,6 +3535,65 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
35343535
)
35353536

35363537

3538+
$(H3 $(LNAME2 RvalueExpression, Rvalue Expression))
3539+
3540+
$(GRAMMAR
3541+
$(GNAME RvalueExpression):
3542+
$(D __rvalue $(LPAREN)) $(GLINK AssignExpression) $(D $(RPAREN))
3543+
)
3544+
3545+
$(P The $(I RvalueExpression) causes the embedded $(I AssignExpression) to be
3546+
treated as an rvalue whether it is an rvalue or an lvalue.)
3547+
3548+
$(H4 Overloading)
3549+
3550+
$(P If both ref and non-ref parameter overloads are present, an rvalue is preferably matched to the
3551+
non-ref parameters, and an lvalue is preferably matched to the ref parameter.
3552+
An $(I RvalueExpression) will preferably match with the non-ref parameter.)
3553+
3554+
$(H4 Semantics of Arguments Matched to Rvalue Parameters)
3555+
3556+
$(P An rvalue argument is owned by the function called. Hence, if an lvalue is matched
3557+
to the rvalue argument, a copy is made of the lvalue to be passed to the function. The function
3558+
will then call the destructor (if any) on the parameter at the conclusion of the function.
3559+
An rvalue argument is not copied, as it is assumed to already be unique, and is also destroyed at
3560+
the conclusion of the function.)
3561+
3562+
$(P The called function's semantics are the same whether a parameter originated as an rvalue
3563+
or is a copy of an lvalue.
3564+
This means that an $(I RvalueExpression) argument destroys the expression upon function return.
3565+
Attempts to continue to use the lvalue expression are invalid. The compiler won't always be able
3566+
to detect a use after being passed to the function, which means that the destructor for the object
3567+
must reset the object's contents to its initial value, or at least a benign value that can
3568+
be destructed more than once.
3569+
)
3570+
3571+
---
3572+
struct S
3573+
{
3574+
ubyte* p;
3575+
~this()
3576+
{
3577+
free(p);
3578+
// add `p = null;` here to prevent double free
3579+
}
3580+
}
3581+
3582+
void aggh(S s)
3583+
{
3584+
}
3585+
3586+
void oops()
3587+
{
3588+
S s;
3589+
s.p = cast(ubyte*)malloc(10);
3590+
aggh(__rvalue(s));
3591+
// destructor of s called at end of scope, double-freeing s.p
3592+
}
3593+
---
3594+
3595+
$(P $(I RvalueExpression)s enable the use of $(I move constructor)s and $(I move assignment)s.)
3596+
35373597
$(H3 $(LNAME2 specialkeywords, Special Keywords))
35383598

35393599
$(GRAMMAR

spec/lex.dd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,15 +1135,16 @@ $(MULTICOLS 4,
11351135

11361136
$(LINK2 expression.html#specialkeywords, $(D __FILE__))
11371137
$(LINK2 expression.html#specialkeywords, $(D __FILE_FULL_PATH__))
1138-
$(LINK2 expression.html#specialkeywords, $(D __MODULE__))
1139-
$(LINK2 expression.html#specialkeywords, $(D __LINE__))
11401138
$(LINK2 expression.html#specialkeywords, $(D __FUNCTION__))
1139+
$(LINK2 expression.html#specialkeywords, $(D __LINE__))
1140+
$(LINK2 expression.html#specialkeywords, $(D __MODULE__))
11411141
$(LINK2 expression.html#specialkeywords, $(D __PRETTY_FUNCTION__))
11421142

11431143
$(LINK2 attribute.html#gshared, $(D __gshared))
1144+
$(LINK2 expression.html#IsExpression, $(D __parameters))
1145+
$(LINK2 expression.html#RvalueExpression, $(D __rvalue))
11441146
$(LINK2 traits.html, $(D __traits))
11451147
$(LINK2 $(ROOT_DIR)phobos/core_simd.html#.Vector, $(D __vector))
1146-
$(LINK2 expression.html#IsExpression, $(D __parameters))
11471148
)
11481149
)
11491150

0 commit comments

Comments
 (0)