https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90958
Bug ID: 90958 Summary: Recent versions of GCC raise CONSTRAINT_ERROR at runtime for iterator loops with nested constrained records. Product: gcc Version: 9.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: ada Assignee: unassigned at gcc dot gnu.org Reporter: lburke at labprogramming dot net Target Milestone: --- Here is source code that shows the problem: procedure GNAT.IO.T is type Variant (Has : Boolean := False) is record case Has is when True => I : Integer; when False => F : Float; end case; end record; type VArray is array (Positive range <>) of Variant; type VRec (Size : Natural) is record Arr : VArray (1 .. Size); end record; type VRec2 (Size : Natural) is record Underlying : VRec (Size); end record; procedure Test (Input : in out VRec2) is begin for X of Input.Underlying.Arr loop if X.Has then X.I := X.I + 1; end if; end loop; end Test; Data : VArray (1 .. 3) := ( 1 => (Has => True, I => 10), 2 => (Has => False, F => 10.0), 3 => (Has => True, I => 20)); Rec : VRec := (Size => 3, Arr => Data); Rec2 : VRec2 := (Size => 3, Underlying => Rec); begin Test (Rec2); Put_Line (Rec2.Underlying.Arr (3).I'Img); end GNAT.IO.T; Result (from compilation to run): [ ~/test/ada/fresh ] louis@Wolvog $ gcc -v -save-temps -c test.adb Using built-in specs. COLLECT_GCC=gcc Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp --enable-cet=auto Thread model: posix gcc version 9.1.0 (GCC) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/gnat1 -quiet -dumpbase test.adb -auxbase test -mtune=generic -march=x86-64 test.adb -o test.s test.adb:1:18: warning: file name does not match unit name, should be "g-io-t.adb" COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-mtune=generic' '-march=x86-64' as -v --64 -o test.o test.s GNU assembler version 2.32 (x86_64-pc-linux-gnu) using BFD version (GNU Binutils) 2.32 COMPILER_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/ LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-mtune=generic' '-march=x86-64' [ ~/test/ada/fresh ] louis@Wolvog $ ls test.adb test.ali test.o test.s [ ~/test/ada/fresh ] louis@Wolvog $ gnatbind -v -x test.ali gnatbind -v -x test.ali GNATBIND 9.1.0 Copyright (C) 1995-2019, Free Software Foundation, Inc. Binding: test.ali No errors [ ~/test/ada/fresh ] louis@Wolvog $ gnatlink -v test.ali -o test GNATLINK 9.1.0 Copyright (C) 1995-2019, Free Software Foundation, Inc. gnatlink: warning: executable name "test" may conflict with shell command gcc -c -mtune=generic -march=x86-64 -gnatA -gnatWb -gnatiw -gnatws /home/louis/test/ada/fresh/b~test.adb /usr/bin/gcc b~test.o ./test.o -o test -L./ -L/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/adalib/ /usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/adalib/libgnat.a -ldl -static-libgcc [ ~/test/ada/fresh ] louis@Wolvog $ ./test raised CONSTRAINT_ERROR : test.adb:24 discriminant check failed Here is code that should be functionally similar, but doesn't raise the error: procedure GNAT.IO.T is type Variant (Has : Boolean := False) is record case Has is when True => I : Integer; when False => F : Float; end case; end record; type VArray is array (Positive range <>) of Variant; type VRec (Size : Natural) is record Arr : VArray (1 .. Size); end record; type VRec2 (Size : Natural) is record Underlying : VRec (Size); end record; procedure Test (Input : in out VRec2) is begin for I in Input.Underlying.Arr'Range loop declare X : Variant renames Input.Underlying.Arr (I); begin if X.Has then X.I := X.I + 1; end if; end; end loop; end Test; Data : VArray (1 .. 3) := ( 1 => (Has => True, I => 10), 2 => (Has => False, F => 10.0), 3 => (Has => True, I => 20)); Rec : VRec := (Size => 3, Arr => Data); Rec2 : VRec2 := (Size => 3, Underlying => Rec); begin Test (Rec2); Put_Line (Rec2.Underlying.Arr (3).I'Img); end GNAT.IO.T; Result (from compilation to run): [ ~/test/ada/fresh ] louis@Wolvog $ gcc -v -save-temps -c test.adb Using built-in specs. COLLECT_GCC=gcc Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp --enable-cet=auto Thread model: posix gcc version 9.1.0 (GCC) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/gnat1 -quiet -dumpbase test.adb -auxbase test -mtune=generic -march=x86-64 test.adb -o test.s test.adb:1:18: warning: file name does not match unit name, should be "g-io-t.adb" COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-mtune=generic' '-march=x86-64' as -v --64 -o test.o test.s GNU assembler version 2.32 (x86_64-pc-linux-gnu) using BFD version (GNU Binutils) 2.32 COMPILER_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/ LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-mtune=generic' '-march=x86-64' [ ~/test/ada/fresh ] louis@Wolvog $ ls test.adb test.ali test.o test.s [ ~/test/ada/fresh ] louis@Wolvog $ gnatbind -v -x test.ali gnatbind -v -x test.ali GNATBIND 9.1.0 Copyright (C) 1995-2019, Free Software Foundation, Inc. Binding: test.ali No errors [ ~/test/ada/fresh ] louis@Wolvog $ gnatlink -v test.ali -o test GNATLINK 9.1.0 Copyright (C) 1995-2019, Free Software Foundation, Inc. gnatlink: warning: executable name "test" may conflict with shell command gcc -c -mtune=generic -march=x86-64 -gnatA -gnatWb -gnatiw -gnatws /home/louis/test/ada/fresh/b~test.adb /usr/bin/gcc b~test.o ./test.o -o test -L./ -L/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/adalib/ /usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/adalib/libgnat.a -ldl -static-libgcc [ ~/test/ada/fresh ] louis@Wolvog $ ./test 21 I have also tested this with 8.3.1 at tio.run: https://tio.run/##jVPBbtNAEL33K@aGLdJV49waWqkIpVhCpYIU5YYGe2xWcnej9boofDxmZmwnIUDVXDxrv3lv9r0JlnheO4x9vw2@oLILBLd3N2uTfzRrsO0ZQNxtCb5gsOgiJO@xhUt4631D6ODyClbYtJQOUIBAhQ@llgAFtgTSMH6U34/v5GAdOoKra8iZKneRagrLPxHKKpAVQ1aNxzgByJVKPJzlNGguz/az3oSAOxYF1CK5962N9okgoKsJ3lyn4KvpSkd9n6iA5LP9Sax5h7EL2Pz7YizAkFEnmYMxIG3pMzMxd/Yy8gdXUmh21tWisZ/pb/IBLhKH7NbUcki523aRu60Dz4WKj2LfqLZOmSofYCNGKNocZI1cr/F@Ow5kK9gYiTFyMvuYNiaX@OXxGubH6djqMKrQyElqmU0deYcRT/1bpMKWaONcgtdN46fsyow3hcv5RTpTQHYM0FWZ8aIowkyYxX9IsotUrRRjR39FWJPhz4uZpsuVTDkhsxGanWKPwuIXjOCOyeIhC/VeaO67@PWDdTS8OvU7WaQmf5U/1owVs/Z/wmXf/yqqBuu2P3/6DQ This bug does not appear to be present in version 4.8.5. I did not check every version in between.