www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4417] New: const-violation in struct error message

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

           Summary: const-violation in struct error message
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: diagnostic
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2010-07-02 05:31:17 PDT ---
This D2 program is wrong, because the rnd field of Foo must change its state,
so it can't be called in a const method bar():


import std.random: MinstdRand, uniform;
struct Foo {
    MinstdRand rnd;
    int bar() const {
        return uniform(0, 10, rnd);
    }
}
void main() {}


But dmd v2.047 shows bad error messages:

...\dmd\src\phobos\std\random.d(684): Error: function
std.random.LinearCongruentialEngine!(uint,48271,0,2147483647).LinearCongruentialEngine.front
() is not callable using argument types ()
...\dmd\src\phobos\std\random.d(685): Error: function
std.random.LinearCongruentialEngine!(uint,48271,0,2147483647).LinearCongruentialEngine.popFront
() is not callable using argument types ()
...\dmd\src\phobos\std\random.d(5): Error: template instance
std.random.uniform!("[)",int,int,const(LinearCongruentialEngine!(uint,48271,0,2147483647)))
error instantiating


A better single error message can just say that uniform(_, _, rnd) can't be
called inside bar() because uniform() tries to mutates rnd while bar is const.

See also bug 4416

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 02 2010
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4417


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com
          Component|DMD                         |Phobos
           Platform|x86                         |All
         OS/Version|Windows                     |All
           Severity|enhancement                 |normal


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-01-20
13:08:30 PST ---
I think this is an issue with Phobos, not the compiler.

The error is now slightly better (first two shown):

Error: mutable method std.random.LinearCongruentialEngine!(uint, 48271, 0,
2147483647).LinearCongruentialEngine.front is not callable using a const object
Error: mutable method std.random.LinearCongruentialEngine!(uint, 48271, 0,
2147483647).LinearCongruentialEngine.popFront is not callable using a const
object

uniform() should probably reject a const random number generator. And this is
probably true for all range-taking functions. If you can't call popFront then
you can't do much with the range AFAICT. Not even .save helps in this case.

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