www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2521] New: Not possible to return immutable value by ref

reply d-bugmail puremagic.com writes:
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


-- 
Dec 17 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2521


2korden gmail.com changed:

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





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?


-- 
Dec 17 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
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                     |





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;
}


-- 
Dec 17 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2521







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.


-- 
Dec 18 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2521






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...


-- 
Dec 22 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jun 07 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



*** 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: -------
Jun 17 2011
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jun 30 2011