Thread overview
[Issue 2521] New: Not possible to return immutable value by ref
Dec 17, 2008
d-bugmail
Dec 17, 2008
d-bugmail
Dec 18, 2008
d-bugmail
Dec 18, 2008
d-bugmail
Dec 23, 2008
d-bugmail
Jun 07, 2011
yebblies
Jun 17, 2011
Kenji Hara
Jun 30, 2011
Walter Bright
December 17, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2521

           Summary: Not possible to return immutable value by ref
           Product: D
           Version: 2.022
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: aarti@interia.pl


module Test;

immutable int val = 23;

ref int func() {
    return val;
}

void main() {
}

----
Result - compilation error:
quicktest.d(3): Error: cast(int)23 is not an lvalue


-- 

December 17, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2521


2korden@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #1 from 2korden@gmail.com  2008-12-17 17:29 -------
Immutable variables are not lvalues, you can't take address of them because they might not present in a final executable. For example, everywhere you use val, it is replaced with 23. You can't return 23 by reference, can you?

Besides, returning immutable values by mutable reference is disallowed:

func() = 42; //what should this do if func() returns reference to immutable
val?


-- 

December 18, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2521


torhu@yahoo.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |torhu@yahoo.com
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |




------- Comment #2 from torhu@yahoo.com  2008-12-17 18:36 -------
From the docs (Const and Invariant page): "Invariant declarations can appear as lvalues, i.e. they can have their address taken, and occupy storage."

This one works:

immutable(int)* func() {
    return &val;
}

I assume that this is supposed to work too:

ref immutable(int) func() {
    return val;
}


-- 

December 18, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2521





------- Comment #3 from aarti@interia.pl  2008-12-18 02:26 -------
comment to #1:
I was not sure if this was supposed to work. But please notice that it is bug
anyway as the line number in error message and message itself is totally
misleading. It suggest that problem is with variable declaration, instead of
with function.


-- 

December 23, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2521





------- Comment #4 from jason.james.house@gmail.com  2008-12-22 19:35 -------
This is definitely an invalid bug!

Func has a return type of "ref int".  The ref means that what gets returned by the function can be modified (and affect the underlying data used by func).  If the return type of func was int, I would hope the code would compile, but that's a whole other issue...


-- 

June 07, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=2521


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch, rejects-valid
                 CC|                            |yebblies@gmail.com


--- Comment #5 from yebblies <yebblies@gmail.com> 2011-06-07 04:22:52 PDT ---
This bug addresses two issues.

This does not currently work:

-------------------------------

immutable int val = 23;

ref immutable(int) func()
{
    return val;
}


Error: constant 23 is not an lvalue
-------------------------------

And this gives the same terrible error message:

-------------------------------

immutable int val = 23;

ref int func()
{
    return val;
}

Error: constant 23 is not an lvalue
-------------------------------

The proposed fix (dmd pull 92) allows the first case, and changes the error to the following for the second case:

Error: cast(int)val is not an lvalue

The root cause of this bug is the fact that while running semantic on the return expression, the immutable variable's value is known at compile time, and is optimized without checking if the function returns an lvalue.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 17, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=2521


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dsimcha@yahoo.com


--- Comment #6 from Kenji Hara <k.hara.pg@gmail.com> 2011-06-17 02:59:16 PDT ---
*** Issue 2780 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 30, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=2521


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> 2011-06-30 13:23:06 PDT ---
https://github.com/D-Programming-Language/dmd/commit/099ed3c987c8cca5dbb8614e21b2a5de99a49252

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------