Thread overview
[Issue 18594] X is not an lvalue should have a better error message
[Issue 18594] constant 1 is not an lvalue should have a better error message
Mar 11, 2018
Seb
Mar 11, 2018
Seb
Mar 12, 2018
Seb
Mar 12, 2018
Seb
Sep 25, 2019
Tobias Pankrath
Feb 22, 2022
duser@neet.fi
Dec 17, 2022
Iain Buclaw
May 09, 2023
RazvanN
March 11, 2018
https://issues.dlang.org/show_bug.cgi?id=18594

--- Comment #1 from Seb <greensunny12@gmail.com> ---
(PR incoming)

--
March 11, 2018
https://issues.dlang.org/show_bug.cgi?id=18594

Seb <greensunny12@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|constant 1 is not an lvalue |X is not an lvalue should
                   |should have a better error  |have a better error message
                   |message                     |

--
March 12, 2018
https://issues.dlang.org/show_bug.cgi?id=18594

--- Comment #2 from Seb <greensunny12@gmail.com> ---
Hmm, just modifying the error message isn't enough. We should probably look up in the tree to check whether we are in if expression, assert, ...

diff --git a/src/dmd/expression.d b/src/dmd/expression.d index d93c1ffda..997423b85 100644
--- a/src/dmd/expression.d
+++ b/src/dmd/expression.d
@@ -1855,7 +1855,7 @@ extern (C++) abstract class Expression : RootObject
                         }
                     }
                 }
-                error("cannot modify `%s` expression `%s`. Did you mean
`==`?", MODtoChars(type.mod), toChars());
+                error("cannot modify `%s` expression `%s`",
MODtoChars(type.mod), toChars());
                 return new ErrorExp();
             }
             else if (!type.isAssignable())
@@ -2604,7 +2604,7 @@ extern (C++) final class IntegerExp : Expression
             e = this;
         else if (!loc.isValid())
             loc = e.loc;
-        e.error("constant `%s` is not an lvalue. Did you mean `==`?",
e.toChars());
+        e.error("constant `%s` is not an lvalue", e.toChars());
         return new ErrorExp();
     }

diff --git a/test/fail_compilation/test18594.d
b/test/fail_compilation/test18594.d
deleted file mode 100644
index 631df3033..000000000
--- a/test/fail_compilation/test18594.d
+++ /dev/null
@@ -1,11 +0,0 @@
-/*TEST_OUTPUT:
----
-fail_compilation/test18594.d(8): Error: constant `1` is not an lvalue. Did you
mean `==`?
-fail_compilation/test18594.d(10): Error: cannot modify `const` expression `a`.
Did you mean `==`?
----
-*/
-void main() {
-    assert(1 = 2);
-    const a = 1;
-    assert(a = 2);
-}

--
March 12, 2018
https://issues.dlang.org/show_bug.cgi?id=18594

--- Comment #3 from Seb <greensunny12@gmail.com> ---
A start: https://github.com/dlang/dmd/pull/8009

--
April 05, 2018
https://issues.dlang.org/show_bug.cgi?id=18594

--- Comment #4 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/ea309c0967e473db3c19e202ffcaf563d9cfff63 Issue 12663, 18594 - Replace lvalue in error messages with 'cannot modify'

https://github.com/dlang/dmd/commit/4574f6891b925c768382ae6dba57b93c4b66bf13 Merge pull request #8009 from wilzbach/fix-18594

Issue 18594 - Replace lvalue in error messages with 'cannot modify' merged-on-behalf-of: Mike Franklin <JinShil@users.noreply.github.com>

--
September 25, 2019
https://issues.dlang.org/show_bug.cgi?id=18594

Tobias Pankrath <tobias@pankrath.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tobias@pankrath.net

--- Comment #5 from Tobias Pankrath <tobias@pankrath.net> ---
---
struct Test(T) {

        @property ref T get() inout { return member; }

private:
   T member;
}


int main(string[] args)
{
   Test!int t;
   t.get = 12;
   return t.get;
}
---

This code has an very similar issue:

> test.d(3): Error: cast(int)this.member is not an lvalue and cannot be modified

The correct definition is:
---
struct Test(T) {

        @property ref inout(T) get() inout { return member; }

private:
   T member;
}
---

but the error message gives no hint in that direction. It confused me quite a bit, since IMO member is a fine lvalue that happens to be const/inout.

https://run.dlang.io/is/IJKZ80

--
February 22, 2022
https://issues.dlang.org/show_bug.cgi?id=18594

duser@neet.fi changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |duser@neet.fi

--- Comment #6 from duser@neet.fi ---
the same error message is given when taking the address of a non-lvalue, but "cannot be modified" doesn't necessarily apply there (and can be confusing)

// Error: `a[0..2]` is not an lvalue and cannot be modified
ubyte[2] a;
const void* p1 = &a[0..2];

// Error: cannot modify constant `1`
const void* p2 = &1;

for these, the message should rather say something like "cannot have its address taken"

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=18594

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--
May 09, 2023
https://issues.dlang.org/show_bug.cgi?id=18594

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |razvan.nitu1305@gmail.com
         Resolution|---                         |FIXED

--- Comment #7 from RazvanN <razvan.nitu1305@gmail.com> ---
This issue has been fixed by: https://github.com/dlang/dmd/pull/8009

--