End the deprecation period of using the result of a comma expression.#7813
End the deprecation period of using the result of a comma expression.#7813dlang-bot merged 1 commit intodlang:masterfrom
Conversation
|
I wonder if there's a way to use 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! |
changelog/comma-deprecation-error.dd
Outdated
| @@ -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. | |||
src/dmd/expressionsem.d
Outdated
| 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"); |
src/dmd/expressionsem.d
Outdated
| */ | ||
| 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"); |
There was a problem hiding this comment.
Well cant is the existing DMD style:
> ag "cant" | wc -l
466
> ag "can't" | wc -l
128
There was a problem hiding this comment.
Existing bad grammar should not be justification for adding more bad grammar!
There was a problem hiding this comment.
I have seen @WalterBright repeatedly use "cant" - even in recent PRs. That was my motivation to use "cant".
There was a problem hiding this comment.
There was a problem hiding this comment.
"cant" is not a word. I believe @WalterBright made a mistake in those PRs.
There was a problem hiding this comment.
Fair enough, I changed it to can't.
There was a problem hiding this comment.
@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
test/fail_compilation/commaexp.d
Outdated
| 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 |
test/fail_compilation/diag10862.d
Outdated
| 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 |
test/fail_compilation/ice10949.d
Outdated
| 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 |
| { | ||
| auto k = (containsAsm(), containsAsm()); | ||
| (containsAsm(), containsAsm()); | ||
| auto k = containsAsm(); |
There was a problem hiding this comment.
Huh? Shouldn't this be:
containsAsm();
auto k = containsAsm();
instead? This change doesn't make sense to me.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ah, I see why now. Nevermind, carry on. :-P
|
Thanks for your pull request, @wilzbach! Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. |
I like the idea, but it would be a lot easier for Phobos. For dmd you are basically left with https://github.com/dlang/dmd/pull/7760/files is one of the first PRs to use the deprecated pattern in dmd too. |
544bf0c to
be0edfa
Compare
|
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. |
test/runnable/testscope.d
Outdated
| bar11(p, &i); | ||
|
|
||
| bar11((i,p), &i); // comma expressions are deprecated, but need to test them | ||
| bar11(p, &i); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() | |||
| { | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ok. I copied it to fail compilation. I don't see any reason to remove it here though.
src/dmd/expressionsem.d
Outdated
| */ | ||
| 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"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Hmm it's not deprecated anymore, it's a real error and should be displayed to the user as such.
There was a problem hiding this comment.
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. | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
changelog/comma-deprecation-error.dd
Outdated
| foo((++a, b)); | ||
|
|
||
| synchronized (lockA, lockB) {} | ||
| // isn't currently implemented, but still compiles "thanks" to the comma operator |
There was a problem hiding this comment.
"thanks" sounds a bit condescending. Perhaps replace with due.
There was a problem hiding this comment.
Also submitted for dlang.org: dlang/dlang.org#2152
| a(&s); // Error: Cannot convert &S to const(S*) at compile time | ||
| return true; | ||
| } | ||
| static assert((test2!f9525(), true)); |
There was a problem hiding this comment.
Are the static assert not necessary anymore? Or will the static assert inside the function take care of that?
There was a problem hiding this comment.
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.
| --- | ||
|
|
||
| It's also common to use the comma operator in for-loop increment | ||
| statements to allow multiple expressions. |
There was a problem hiding this comment.
I think it should be clearer that this use case is not affected.
changelog/comma-deprecation-error.dd
Outdated
| ) | ||
| $(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. |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Honestly all of these examples are entirely unconvincing because they could be caught by a "discarding result of side-effect-free subexpression" warning.
There was a problem hiding this comment.
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.
changelog/comma-deprecation-error.dd
Outdated
| // will always be reached | ||
| } | ||
|
|
||
| void foo(int x) {} |
There was a problem hiding this comment.
I don't understand this example. Perhaps it would make sense for void foo(int x, int y=0) ?
src/dmd/expressionsem.d
Outdated
| */ | ||
| 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"); |
There was a problem hiding this comment.
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" ?
|
Addressed all comments except for:
It took them from here: https://dlang.org/deprecate.html#Using%20the%20result%20of%20a%20comma%20expression |
|
@JinShil you still have this on "requested changes". I addressed your comments and copied the files over to |
|
Woohoo!!!! The comma operator is officially dead!!! celebration |
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