D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6923 - Not restrictive initialization semantics
Summary: Not restrictive initialization semantics
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 minor
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-09 19:56 UTC by bearophile_hugs
Modified: 2019-08-29 17:40 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 bearophile_hugs 2011-11-09 19:56:08 UTC
This is not exactly a bug report, it's just an example of code that shows a possible design/implementation problem.

Code by Timon Gehr:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=148455

A comment:

> I think the current initialization semantics are not restrictive enough.

-------------------------

immutable(int)* p;

void foo() { // checks if values pointed to by p are really immutable
    static int[immutable(int)*] mem;

    if (p !in mem)
        mem[p] = *p;
    else
        assert(mem[p] == *p); // fail.
}

struct Foo {
    immutable int x;

    this(int y) {
        p = &x;
        foo();
        x = 1;
        foo();
    }
}

void main() {
    Foo(2);
}
Comment 1 Mathias LANG 2019-08-29 17:40:18 UTC
Nowadays this code yields:
```
f1.d(18): Error: immutable field x initialized multiple times
f1.d(16):        Previous initialization is here.
```