D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10496 - Initialization in lazy function parameter allows immutable member not to be initialized
Summary: Initialization in lazy function parameter allows immutable member not to be i...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2013-06-28 09:00 UTC by David Eckardt
Modified: 2024-12-13 18:08 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Eckardt 2013-06-28 09:00:44 UTC
class C
{
	immutable int x;
	
	this()
	{
		this.f(this.x = 5);
	}
	
	void f(lazy int x){}
}

C.int isn't initialized, although this code compiles.
Whether C.int is initialized or not depends on the, in general indeterministic, behavior of f(), this should be a compile time error.
Comment 1 Dicebot 2013-06-28 09:21:47 UTC
I think this is part of a bigger problem:

----------------------------------------
class A
{
    immutable int x;

    void delegate() f;

    this()
    {
        x = 40;
        f = () { x = 42; };
    }
}

void main()
{
    import std.stdio;
    auto a = new A();
    writeln(a.x);
    a.f();
    writeln(a.x);
}
----------------------------------------

40
42
Comment 2 Maxim Fomin 2013-06-28 11:08:00 UTC
The root is that delegate construction does not respect immutability at all.

struct S
{
   void foo() immutable {}
   void baz() 
   {
      //foo();
      (&foo)();
   }
}
Comment 3 dlangBugzillaToGithub 2024-12-13 18:08:47 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18619

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB