Skip to content

Clarify spec for "+=" and friends #1429

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
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
30 changes: 29 additions & 1 deletion spec/expression.dd
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,38 @@ $(H4 Assignment Operator Expressions)
$(P except that:)

$(UL
$(LI operand $(D a) is only evaluated once)
$(LI operand $(D a) is only evaluated once, see below.)
$(LI overloading $(I op) uses a different function than overloading $(I op)= does)
$(LI the left operand of $(D >>>=) does not undergo integral promotions before shifting)
)

$(P The expression evaluates according to

--------------
void doOpAssignment(ref int x, int y)
{
x = x op y;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a cast missing in there. I did not put it in the forum thread as I used int explicitly, but it is needed in the general case. For instance :

byte a = 3;
a += 3; // This is ok because of the cast, but won't work because of int promotion without).

}
doOpAssignment(a, b);
Copy link
Member

@andralex andralex Jul 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description needs to include the cast. I'd say a correct formulation goes as follows:

The expression `a @= b`, where `@` stands for any of the binary operators, 
is evaluated as if it were lowered as follows:

(function ref(ref x, y) { return x = cast(typeof(x)) (x @ y); })(a, b)

See also (sadly) https://issues.dlang.org/show_bug.cgi?id=16271 which blocks this. Other than that this definition is really solid because defines everything by "redirecting" to function calls.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--------------

thus the following unittest passes

--------------
int sum;
int add7tosum_return1() { sum += 7; return 1; }
unittest
{
sum = 0;
sum += add7tosum_return1();
assert(sum == 8); // "eight" !

sum = 0;
sum = cast(typeof(sum))(sum + add7tosum_return1());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the cast necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't. But I made it look exactly the same as the given "semantic equivalent" above.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary if all isn't int.

assert(sum == 1); // "one" !
}
--------------
)

$(H3 Conditional Expressions)

Expand Down