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




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


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




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


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


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


-- 
Dec 22 2008
prev 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


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