Bug 11488 – Disallow chaining of - operator

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-09T12:21:15Z
Last change time
2024-12-13T18:13:51Z
Assigned to
No Owner
Creator
Manfred Nowak
Moved to GitHub: dmd#18711 →

Comments

Comment #0 by svv1999 — 2013-11-09T12:21:15Z
... may have reached D. According to http://dlang.org/expression.html#UnaryExpression both "--" and "-" are legal prefices and both can be chained. This means that a statement like "x= --y;" stays legal when wrongly inserted whitespace turns it into "x= - -y;". This can be prevented when the alternative " - UnaryExpression" is changed into " - PostfixExpression" thereby disallowing to chain negation operators.
Comment #1 by smjg — 2015-01-30T19:47:15Z
This would undesirably prevent - from being being chained with any unary operator, not just another -. I can't see such an error as being likely to occur. If you mean the -- operator, how would a space between the two minuses creep in by accident?
Comment #2 by svv1999 — 2015-02-01T07:35:35Z
(In reply to Stewart Gordon from comment #1) If chaining of "-", "--". "+" and "++" is used heavily, then disallowing to shoot oneself is okay. Hint: "- -- -- -- - --- -- - -- ---- - -- - --x" is currently a legal expression and I doubt that one can _see_ in general the positions at which a significant space is wrongly placed or deleted. Disallowing chaining requires to use paired paranthesis in such cases, which doubles the number of wrongly placed characters needed to make an error slip through. BTW: allowing such chaining makes D similar to an esoteric language. If chaining of those operators will never be used heavily, then there is no ratio for allowing the chaining.
Comment #3 by smjg — 2015-02-01T14:25:42Z
(In reply to Manfred Nowak from comment #2) > If chaining of "-", "--". "+" and "++" is used heavily, then disallowing to > shoot oneself is okay. Hint: "- -- -- -- - --- -- - -- ---- - -- - --x" is > currently a legal expression and I doubt that one can _see_ in general the > positions at which a significant space is wrongly placed or deleted. But how would such an expression find its way into real-world code in the first place? > If chaining of those operators will never be used heavily, then there is no > ratio for allowing the chaining. In my mind, this is all the more reason that it isn't worth disallowing it and thereby (a) adding extra complexity to the parser (b) precluding legitimate, non-error-prone use cases. Also, what does `FORTRAN "FORI=" Error` mean? I used Fortran for a few years, and never came across the term. Probably a lot of people here have never used Fortran, and have even less idea what it means. As such, I'm changing the summary line to make sense to D programmers.
Comment #4 by svv1999 — 2015-02-01T15:16:00Z
(In reply to Stewart Gordon from comment #3) > But how would such an expression find its way into real-world code in the > first place? I did show by example, that chaining "-" and "--" is not tolerable. An analog example holds for "+" and "++". Its the burdon of the language designer to give the ratio for allowing such constructs. > In my mind, I agree, that our thinking differs. > Also, what does `FORTRAN "FORI=" Error` mean? Maybe its an urban legend that made it into a university. According to that university talk in a fortran-program the space between the `for' and the index-variable `i' was lost. Thereby declaring and assigning the variable `fori' instead of introducing a loop---with fatal consequences for a space-ship.
Comment #5 by smjg — 2015-02-01T15:46:09Z
(In reply to Manfred Nowak from comment #4) > I did show by example, that chaining "-" and "--" is not tolerable. An > analog example holds for "+" and "++". Its the burdon of the language > designer to give the ratio for allowing such constructs. What do you mean by "the ratio for allowing such constructs"? >> Also, what does `FORTRAN "FORI=" Error` mean? > Maybe its an urban legend that made it into a university. According to that > university talk in a fortran-program the space between the `for' and the > index-variable `i' was lost. Thereby declaring and assigning the variable > `fori' instead of introducing a loop---with fatal consequences for a > space-ship. I see. But does Fortran have the FOR keyword? I've just had a look and been reminded that Fortran uses DO for for-loops - none of the online sources I've found mention FOR as alternative syntax. But it seems to me that the cause of this was a failure to test the software properly - something that is needed regardless of what language it is written in.
Comment #6 by ibuclaw — 2015-02-01T15:57:29Z
It looks like the grammar does allow this, but fails semantic because in the following expression, -x is not an lvalue (probably an implementation detail though). -- -x; More complex examples expose artificial variables in errors, so that is definitely a bug.
Comment #7 by smjg — 2015-02-01T21:44:47Z
(In reply to Iain Buclaw from comment #6) > It looks like the grammar does allow this, but fails semantic because in the > following expression, -x is not an lvalue (probably an implementation detail > though). > > -- -x; For primitive types, this should never pass. However, it's potentially possible for it to work on struct/class objects that have this overloaded. But even then, such a long-winded example as Manfred posted is highly unlikely to find its way into real-world code.
Comment #8 by robert.schadek — 2024-12-13T18:13:51Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18711 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB