digitalmars.D - Should this be flagged as a warning?
- Bernard Helyer (13/13) Sep 25 2012 I tried to post this last night, but the NG wasn't having any of
- bearophile (5/12) Sep 25 2012 The top level operation is the ==, that is a pure expression, so
- Bernard Helyer (2/4) Sep 25 2012 Yeah, I thought so but I'm not sure. Hence me asking.
- Bernard Helyer (4/15) Sep 25 2012 Err, the bug is that I wrote '==' instead of '='. That's what
- Don Clugston (14/24) Sep 26 2012 The "must have an effect" rule only applies to statements, not
- Timon Gehr (8/37) Sep 26 2012 I'd rather restrict this to expressions whose value is unused.
- Don Clugston (6/47) Sep 26 2012 I wouldn't give it special treatment. I think it should be allowed if
- bearophile (4/7) Sep 26 2012 Already tried that :-(
- Ben Davis (6/31) Sep 26 2012 What's a realistic use case for allowing foo() + foo(), even if it is
I tried to post this last night, but the NG wasn't having any of it. I found myself writing a bug that looked like this match(ts, TokenType.Is); match(ts, TokenType.OpenParen); isExp.type == parseType(ts); The bug being of course, that a type is parsed and ts is modified, so the expression has side effects so it's not flagged as a useless expression. But the comparison still has no effect, so should this be flagged by DMD? -Bernard.
Sep 25 2012
Bernard Helyer:match(ts, TokenType.Is); match(ts, TokenType.OpenParen); isExp.type == parseType(ts); The bug being of course, that a type is parsed and ts is modified, so the expression has side effects so it's not flagged as a useless expression. But the comparison still has no effect, so should this be flagged by DMD?The top level operation is the ==, that is a pure expression, so maybe dmd should warn on this. Bye, bearophile
Sep 25 2012
On Tuesday, 25 September 2012 at 21:15:24 UTC, bearophile wrote:The top level operation is the ==, that is a pure expression, so maybe dmd should warn on this.Yeah, I thought so but I'm not sure. Hence me asking.
Sep 25 2012
On Tuesday, 25 September 2012 at 19:29:26 UTC, Bernard Helyer wrote:I tried to post this last night, but the NG wasn't having any of it. I found myself writing a bug that looked like this match(ts, TokenType.Is); match(ts, TokenType.OpenParen); isExp.type == parseType(ts); The bug being of course, that a type is parsed and ts is modified, so the expression has side effects so it's not flagged as a useless expression.Err, the bug is that I wrote '==' instead of '='. That's what I get for posting first thing in the morning, I guess.
Sep 25 2012
On 25/09/12 21:30, Bernard Helyer wrote:I tried to post this last night, but the NG wasn't having any of it. I found myself writing a bug that looked like this match(ts, TokenType.Is); match(ts, TokenType.OpenParen); isExp.type == parseType(ts); The bug being of course, that a type is parsed and ts is modified, so the expression has side effects so it's not flagged as a useless expression. But the comparison still has no effect, so should this be flagged by DMD? -Bernard.The "must have an effect" rule only applies to statements, not expressions, so this is according to the spec. It's not a bug. This is a bit like the more extreme case I recently posted about: int x, y, z; x == y, ++z; doesn't generate an error message even though x == y has no side-effects, because comma is an expression, not a statement. IMHO, every expression should be required to have an effect. For example foo() + foo(); shouldn't compile, unless + is an overloaded operator with side-effects. It would be really interesting to add this check, and then compile existing code (such as Phobos) to see if it breaks valid code, or reveals bugs.
Sep 26 2012
On 09/26/2012 11:45 AM, Don Clugston wrote:On 25/09/12 21:30, Bernard Helyer wrote:I'd rather restrict this to expressions whose value is unused. Otherwise 'a = b is c;' will be illegal, because 'b is c' does not have an effect.I tried to post this last night, but the NG wasn't having any of it. I found myself writing a bug that looked like this match(ts, TokenType.Is); match(ts, TokenType.OpenParen); isExp.type == parseType(ts); The bug being of course, that a type is parsed and ts is modified, so the expression has side effects so it's not flagged as a useless expression. But the comparison still has no effect, so should this be flagged by DMD? -Bernard.The "must have an effect" rule only applies to statements, not expressions, so this is according to the spec. It's not a bug. This is a bit like the more extreme case I recently posted about: int x, y, z; x == y, ++z; doesn't generate an error message even though x == y has no side-effects, because comma is an expression, not a statement. IMHO, every expression should be required to have an effect.For example foo() + foo(); shouldn't compile, unless + is an overloaded operatorYes.with side-effects.Not sure about that. In generic code, it is possible and likely (as pure is inferred for lambdas and template functions) that a pure function ends up being called for potential side effects.It would be really interesting to add this check, and then compile existing code (such as Phobos) to see if it breaks valid code, or reveals bugs.
Sep 26 2012
On 26/09/12 14:19, Timon Gehr wrote:On 09/26/2012 11:45 AM, Don Clugston wrote:If the value is used, it has an effect.On 25/09/12 21:30, Bernard Helyer wrote:I'd rather restrict this to expressions whose value is unused.I tried to post this last night, but the NG wasn't having any of it. I found myself writing a bug that looked like this match(ts, TokenType.Is); match(ts, TokenType.OpenParen); isExp.type == parseType(ts); The bug being of course, that a type is parsed and ts is modified, so the expression has side effects so it's not flagged as a useless expression. But the comparison still has no effect, so should this be flagged by DMD? -Bernard.The "must have an effect" rule only applies to statements, not expressions, so this is according to the spec. It's not a bug. This is a bit like the more extreme case I recently posted about: int x, y, z; x == y, ++z; doesn't generate an error message even though x == y has no side-effects, because comma is an expression, not a statement. IMHO, every expression should be required to have an effect.Otherwise 'a = b is c;' will be illegal, because 'b is c' does not have an effect.I wouldn't give it special treatment. I think it should be allowed if and only if plus(foo(), foo()); compiles, when plus is pure nothrow.For example foo() + foo(); shouldn't compile, unless + is an overloaded operatorYes.with side-effects.Not sure about that. In generic code, it is possible and likely (as pure is inferred for lambdas and template functions) that a pure function ends up being called for potential side effects.
Sep 26 2012
Timon Gehr:In generic code, it is possible and likely (as pure is inferred for lambdas and template functions) that a pure function ends up being called for potential side effects.Already tried that :-( Bye, bearophile
Sep 26 2012
On 26/09/2012 10:45, Don Clugston wrote:On 25/09/12 21:30, Bernard Helyer wrote:What's a realistic use case for allowing foo() + foo(), even if it is overloaded to have side-effects? It looks like an expression with no side-effects. Surely if anyone actually intends it to have side-effects, then they've made a fundamental design mistake in their API? I know C++ has << used this way, but that's a bit horrible.I tried to post this last night, but the NG wasn't having any of it. I found myself writing a bug that looked like this match(ts, TokenType.Is); match(ts, TokenType.OpenParen); isExp.type == parseType(ts); The bug being of course, that a type is parsed and ts is modified, so the expression has side effects so it's not flagged as a useless expression. But the comparison still has no effect, so should this be flagged by DMD? -Bernard.The "must have an effect" rule only applies to statements, not expressions, so this is according to the spec. It's not a bug. This is a bit like the more extreme case I recently posted about: int x, y, z; x == y, ++z; doesn't generate an error message even though x == y has no side-effects, because comma is an expression, not a statement. IMHO, every expression should be required to have an effect. For example foo() + foo(); shouldn't compile, unless + is an overloaded operator with side-effects.
Sep 26 2012