www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22586] New: 'immutable field `xs` initialized multiple times'


          Issue ID: 22586
           Summary: 'immutable field `xs` initialized multiple times'
                    triggered by 'foreach'  in constructor
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andy.pj.hanson gmail.com

This file causes a compile error:

struct S {
        immutable int[] xs;
        this(immutable int[] xs_) {
                xs = xs_;
                foreach (ref immutable int x; xs) {}

The error is:
$ dmd a.d
a.d(5): Error: immutable field `xs` initialized multiple times
a.d(4):        Previous initialization is here.

This isn't right because iterating over `xs` should count as reading it, not as
initializing it.
Tested with dmd v2.098.0.

Dec 11 2021