Skip to content
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

End the deprecation period of using the result of a comma expression. #7813

Merged
merged 1 commit into from
Feb 6, 2018

Conversation

wilzbach
Copy link
Member

@wilzbach wilzbach commented Jan 31, 2018

The deprecation period could have ended a lot earlier.
DMD deprecations should really be tracked at a common place, s.t. for every
release cycle old deprecations can be bumped accordingly.

dlang.org: dlang/dlang.org#2151

@quickfur
Copy link
Member

I wonder if there's a way to use libdparse, say, to collect all deprecated symbols into a list somewhere, so that we can review which symbols need to be bumped to the next stage of deprecation.

Depending on what we find in such a list, we might even be able to automatically parse the deprecation dates and output a list of symbols that need to be updated. In the distant (or not-so-distant?) future we could even have a script/bot that automatically submits PRs to this effect. Automation FTW!

@@ -0,0 +1,4 @@
The deprecation period of using the result of comma expression has ended

Comma expressions have proven to be a frequent source of confusion, and bug.
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: bug > bugs.

@@ -5844,7 +5844,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor

e.type = e.e2.type;
if (e.type !is Type.tvoid && !e.allowCommaExp && !e.isGenerated)
e.deprecation("Using the result of a comma expression is deprecated");
e.deprecation("The result of a comma expression cant be used");
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: cant > can't.

@@ -6272,7 +6272,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
/* Rewrite to get rid of the comma from rvalue
*/
if (!(cast(CommaExp)exp.e2).isGenerated)
exp.deprecation("Using the result of a comma expression is deprecated");
exp.error("The result of a comma expression cant be used");
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Member Author

Choose a reason for hiding this comment

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

Well cant is the existing DMD style:

> ag "cant" | wc -l
466 
> ag "can't" | wc -l
128

Copy link
Member

Choose a reason for hiding this comment

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

Existing bad grammar should not be justification for adding more bad grammar!

Copy link
Member Author

Choose a reason for hiding this comment

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

I have seen @WalterBright repeatedly use "cant" - even in recent PRs. That was my motivation to use "cant".

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

"cant" is not a word. I believe @WalterBright made a mistake in those PRs.

Copy link
Member

Choose a reason for hiding this comment

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

Yah let's fix those. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough, I changed it to can't.

Copy link
Member

Choose a reason for hiding this comment

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

@JinShil Actually, "cant" is a word... but it has a different meaning. :-D

@wilzbach The next time Walter submits a PR with the wrong use of "cant", somebody should point it out so that it can be fixed. :-P

fail_compilation/commaexp.d(38): Error: The result of a comma expression cant be used
fail_compilation/commaexp.d(39): Error: The result of a comma expression cant be used
fail_compilation/commaexp.d(41): Error: The result of a comma expression cant be used
fail_compilation/commaexp.d(42): Error: The result of a comma expression cant be used
Copy link
Member

Choose a reason for hiding this comment

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

ditto

@@ -57,7 +57,7 @@ fail_compilation/diag10862.d(77): Error: assignment cannot be used as a conditio
fail_compilation/diag10862.d-mixin-80(80): Error: assignment cannot be used as a condition, perhaps `==` was meant?
fail_compilation/diag10862.d-mixin-81(81): Error: assignment cannot be used as a condition, perhaps `==` was meant?
fail_compilation/diag10862.d-mixin-82(82): Error: assignment cannot be used as a condition, perhaps `==` was meant?
fail_compilation/diag10862.d-mixin-83(83): Deprecation: Using the result of a comma expression is deprecated
fail_compilation/diag10862.d-mixin-83(83): Error: The result of a comma expression cant be used
Copy link
Member

Choose a reason for hiding this comment

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

ditto

@@ -1,7 +1,7 @@
/*
TEST_OUTPUT:
---
fail_compilation/ice10949.d(12): Deprecation: Using the result of a comma expression is deprecated
fail_compilation/ice10949.d(12): Error: the result of a comma expression cant be used
Copy link
Member

Choose a reason for hiding this comment

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

ditto

@@ -136,7 +136,8 @@ static assert(bazra(14)==64);

void moreCommaTests()
{
auto k = (containsAsm(), containsAsm());
(containsAsm(), containsAsm());
auto k = containsAsm();
Copy link
Member

Choose a reason for hiding this comment

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

Huh? Shouldn't this be:

containsAsm();
auto k = containsAsm();

instead? This change doesn't make sense to me.

Copy link
Member Author

@wilzbach wilzbach Jan 31, 2018

Choose a reason for hiding this comment

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

I thought it's nice to leave the use of the comma expression in. Here we can do this as containsAsm() is a no-op.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see why now. Nevermind, carry on. :-P

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @wilzbach!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

@wilzbach
Copy link
Member Author

I wonder if there's a way to use libdparse, say, to collect all deprecated symbols into a list somewhere, so that we can review which symbols need to be bumped to the next stage of deprecation.

I like the idea, but it would be a lot easier for Phobos. For dmd you are basically left with grep for now.
OTOH even for Phobos grep works fine - we even have the @@@DEPRECATED_ pattern for Phobos, which we don't have here.
Hence, my proposal to have a simple file or wiki in which this is being kept track of for dmd - essentially it's the table at https://dlang.org/deprecate.html, but just with more love (not many deprecations actually update this file and sometimes there are compiler bugs).

https://github.com/dlang/dmd/pull/7760/files is one of the first PRs to use the deprecated pattern in dmd too.

@wilzbach
Copy link
Member Author

So this is finally passing the testsuite 🚀 and just in case someone was wondering obviously my motivation is to make this an error with 2.079, s.t. comma expressions can be re-purposed for built-in tuples.
See also: upcoming tuple DIP

@@ -294,7 +294,7 @@ void test11()
static int i;
bar11(p, &i);

bar11((i,p), &i); // comma expressions are deprecated, but need to test them
bar11(p, &i);
Copy link
Contributor

@JinShil JinShil Jan 31, 2018

Choose a reason for hiding this comment

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

This test was specifically testing the comma operator. Now that it produces an error, it should probably be moved to the fail_compilation tests. However, given that the equivalent expression already exists on line 295, it would probably be ok to just delete it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I was very careful not to delete anything, but yeah in this case it does make sense. Thanks!

@@ -7866,7 +7867,7 @@ bool test16022()
{
Copy link
Contributor

Choose a reason for hiding this comment

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

This issue 16022 was very specific to the comma operator. Since the result of a comma expression now emits an error, this test should no longer compile, and should probably be moved to the fail_compilation test suite.

Copy link
Member Author

@wilzbach wilzbach Jan 31, 2018

Choose a reason for hiding this comment

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

Ok. I copied it to fail compilation. I don't see any reason to remove it here though.

@@ -6272,7 +6272,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
/* Rewrite to get rid of the comma from rvalue
*/
if (!(cast(CommaExp)exp.e2).isGenerated)
exp.deprecation("Using the result of a comma expression is deprecated");
exp.error("The result of a comma expression can't be used");
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer this to say "The result of a comma expression is deprecated and can no longer be used". But, it's ok the way it is; I'll pull it regardless.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm it's not deprecated anymore, it's a real error and should be displayed to the user as such.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @wilzbach.

Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to distinguish "can't be used" (due to some technical limitation) from "can't be used" (because we decided to forbid it). How about "Using the result of a comma expression is not allowed" ?

@@ -0,0 +1,4 @@
The deprecation period of using the result of comma expression has ended

Comma expressions have proven to be a frequent source of confusion, and bugs.
Copy link
Contributor

@jacob-carlborg jacob-carlborg Jan 31, 2018

Choose a reason for hiding this comment

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

Please include an example that now fails to compile and show how to correct it. Perhaps include an example showing how the comma expression can still be used without using the result.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was hoping I could avoid the redundancies from https://dlang.org/deprecate.html#Using%20the%20result%20of%20a%20comma%20expression, but okay copying doesn't cost us much.
I copied the text I have written for dlang.org a while ago.

foo((++a, b));

synchronized (lockA, lockB) {}
// isn't currently implemented, but still compiles "thanks" to the comma operator
Copy link
Contributor

Choose a reason for hiding this comment

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

"thanks" sounds a bit condescending. Perhaps replace with due.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also submitted for dlang.org: dlang/dlang.org#2152

}
static assert((test2!f9525(), true));
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the static assert not necessary anymore? Or will the static assert inside the function take care of that?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the static assert was only there, s.t. the function got evaluated and the static assert inside of it could trigger, hence I added evalTest1 and evalTest2.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok.

---

It's also common to use the comma operator in for-loop increment
statements to allow multiple expressions.
Copy link
Member

Choose a reason for hiding this comment

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

I think it should be clearer that this use case is not affected.

)
$(H4 Rationale)
$(P The comma operator leads to unintended behavior (see below for a selection)
Moreover it is not commonly used and it blocks the ability to implement tuples.
Copy link
Member

Choose a reason for hiding this comment

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

It blocks implementing tuples as a language feature using commas, not in general

$(P The comma operator leads to unintended behavior (see below for a selection)
Moreover it is not commonly used and it blocks the ability to implement tuples.

A selection of problems through the accidental use of the comma operator:
Copy link
Member

Choose a reason for hiding this comment

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

Honestly all of these examples are entirely unconvincing because they could be caught by a "discarding result of side-effect-free subexpression" warning.

Copy link
Member Author

Choose a reason for hiding this comment

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

It took them from here: https://dlang.org/deprecate.html#Using%20the%20result%20of%20a%20comma%20expression
dlang/dlang.org#1308

If you have better examples, just let me know.

// will always be reached
}

void foo(int x) {}
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand this example. Perhaps it would make sense for void foo(int x, int y=0) ?

@@ -6272,7 +6272,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
/* Rewrite to get rid of the comma from rvalue
*/
if (!(cast(CommaExp)exp.e2).isGenerated)
exp.deprecation("Using the result of a comma expression is deprecated");
exp.error("The result of a comma expression can't be used");
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to distinguish "can't be used" (due to some technical limitation) from "can't be used" (because we decided to forbid it). How about "Using the result of a comma expression is not allowed" ?

@wilzbach
Copy link
Member Author

wilzbach commented Feb 1, 2018

Addressed all comments except for:

Honestly all of these examples are entirely unconvincing because they could be caught by a "discarding result of side-effect-free subexpression" warning.

It took them from here: https://dlang.org/deprecate.html#Using%20the%20result%20of%20a%20comma%20expression
dlang/dlang.org#1308
If you have better examples, just let me know. And we can always add them to changelog later.

@wilzbach
Copy link
Member Author

wilzbach commented Feb 5, 2018

@JinShil you still have this on "requested changes". I addressed your comments and copied the files over to fail_compilation, but kept the modified version in compilation. Are you okay with this?
I would like to move on with this, s.t. we can sip this with 2.079 and can introduce tuples into D as soon as possible ...

@wilzbach wilzbach added this to the 2.079.0 milestone Feb 5, 2018
@dlang-bot dlang-bot merged commit c55c9be into dlang:master Feb 6, 2018
@quickfur
Copy link
Member

quickfur commented Feb 6, 2018

Woohoo!!!! The comma operator is officially dead!!! celebration

@wilzbach wilzbach deleted the error-comma branch March 13, 2018 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants