www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12478] New: Current element in foreach statement is implicitly casted to const

https://d.puremagic.com/issues/show_bug.cgi?id=12478

           Summary: Current element in foreach statement is implicitly
                    casted to const
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: neuranuz gmail.com


--- Comment #0 from Uranuz <neuranuz gmail.com> 2014-03-27 00:33:40 PDT ---
When iterating over aggregate with foreach statement current element is
imlicitly casted to const although source aggregate is not surely const. The
following code illustrates it.

struct S
{    string str;
    int num;
}

class C
{    
private:
    S[] _items;

public:
    inout(S)* getItemPtr(string str) inout
    {    foreach( ref s; _items )
        {    
            if( s.str == str )
            {    auto itemPtr = &s;
                 pragma(msg, typeof(itemPtr));
                 //Expected to be inout(s)*. Not inout(const(S))* (in DMD
2.065) or const(S)* (in DMD 2.064)
                 static assert( is( typeof(itemPtr) == inout(S)* ) ); 
                return itemPtr; //Error: not implicitly convertible to
inout(S)*
            }
        }
         return null;
    }

}

When using inout method in this case result item pointer can address const or
mutable data because result is inout(S)*. But operation &s returns pointer of
type inout(const(S))* (in 2.064, or const(S)* in previous versions of DMD). So
it points to not mutable data in any case. But it expected to be inout(S)*,
because method is inout and *this* reference is inout (and all of it's fields
without modifiers). So foreach statement iterating over *inout* array should
give us inout reference to data, but not const or inout(const).

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