www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2486] New: taking address of slice rvalue is valid

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486

           Summary: taking address of slice rvalue is valid
           Product: D
           Version: 1.037
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: spec
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: kamm-removethis incasoftware.de


I was surprised to see that following compiles and generates reasonable code:

int[] a = [1, 2, 3]
auto addr = &a[0..2];

Since the frontend seems to explicitly allow it, it's probably intentional and
we've made LDC behave the same way. Could a description and rationale be added
to the spec?


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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Platform|x86                         |All
            Version|1.037                       |D1 & D2
         OS/Version|Linux                       |All
           Severity|minor                       |normal



This is still valid in D2.

import std.stdio;
void main()
{
    int[] arr = [1,2,3];
    auto p = &(arr[0..2]);  // same as &(arr[0..2])
    writeln(typeof(p).stringof);    // int[]*
    writeln(arr.ptr);
    writeln(&arr);
    writeln(p);
    assert(&arr == p);
}

I think this is bad behavior, because it allows nonsense code like follows.

void main()
{
    int[] arr = [1,2,3];
    foo(arr[0..2]);         // ref parameter can receive lvalue made by slicing
    assert(arr == [1,2,3]); // assertion succeeds...
}
void foo(ref int[] arr)
{
    arr = [4,5,6];          // what is modified?
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 10 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|spec                        |
                 CC|                            |bugzilla digitalmars.com



20:13:33 PST ---
I agree, and will fix the spec. So it's a compiler bug that this code is
accepted. It should only work for const references. Changing to a compiler bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 22 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486




Maybe the documentation fix is:

https://github.com/D-Programming-Language/d-programming-language.org/commit/8681d6fb36bd7a00a395bfc66205d5dbe8b19d88

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 24 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486




I found related bug that returning slice by auto ref causes an error.
----
struct S
{
    int[] a;
    auto ref opSlice(){ return a[]; }  // line 4
}

void main()
{
    S s;
    s[];
}

Output:
----
test.d(4): Error: slice expression this.a[] is not a modifiable lvalue

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid, pull



D2 pull:
https://github.com/D-Programming-Language/dmd/pull/1343
https://github.com/D-Programming-Language/phobos/pull/989

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 02 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486




Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/7062ce191a651ca94fc779ac646a5ef5653b4559
fix Issue 2486 - taking address of slice rvalue is valid

https://github.com/D-Programming-Language/phobos/commit/af4fea78550216647dbf3e4aaa7d89ab1b79f542


Issue 2486 - taking address of slice rvalue is valid

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



*** Issue 9270 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: -------
Jan 06 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486




Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/4573ac12bbe1fde782684ed9042f57ab53b05e90
fix Issue 2486 - taking address of slice rvalue is valid

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com
            Summary|taking address of slice     |taking address of slice
                   |rvalue is valid             |rvalue should not be
                   |                            |allowed



05:52:43 PST ---
Changing title to make it easier to understand in changelog.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 06 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gassa mail.ru



18:50:48 PST ---
*** Issue 9651 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: -------
Mar 07 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486




Commit pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/4ca1b6113444083a4168c4d5027ed6121f89bdba
Enable test lines that had been blocked by issue 2486

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 11 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2486


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies gmail.com
         Resolution|                            |FIXED



Appears to be fixed

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