https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87973

            Bug ID: 87973
           Summary: ICE with pragma Discard_Names and GCC optimisation for
                    x64
           Product: gcc
           Version: 8.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: ada
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cthowie at netzero dot net
  Target Milestone: ---

Created attachment 44985
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44985&action=edit
Test.adb file; see comment for other file 'Test.gpr'

PREFACE: 
1) Compiler: GNAT FSF 8.2.0 with MinGW on Windows 64-bit. I've appended the
detailed output of "gcc -v" to the end of this comment.

2) My GNAT setup is default MSYS2 based GNAT FSF on Windows 64/MinGW64. No
fiddling or manual compiling of GNAT. I have also installed the Win32Ada
library from GitHub, though there are problems even if Win32Ada ignored as I
comment on below.

3) The compiler crash and other problems reported herein do not appear to
affect x86 (32-bit) GNAT, only x64. I ran these steps below on 32-bit Windows
with MSYS2 GNAT x86 and had no problems.

TWO TEST FILES: Test.adb, Test.gpr
Although I've attached Test.adb, you can make these two files by pasting the
following text into the named files (gnatchop won't work):
------------ Test.adb ------------ 
with Ada.Text_IO; use Ada.Text_IO;
procedure Test
is
        pragma Discard_Names;
        type Name is (Alice, Bob, Charles);
        n : Name := Bob;
begin
        Put_Line ("Hello, " & n'Image);
end Test;
------------ Test.gpr ------------ 
with "win32ada";
project Test is
        for Source_Dirs use (".");
        for Object_Dir use "obj/";
        for Main use ("Test.adb");
        package Compiler is
                for Switches ("Ada") use ("-O2");
        end Compiler;
end Test;
------------ end of test files ------ 

DIAGNOSTIC STEPS:
I will describe how to generate the ICE and then some finer-grained diagnostic
steps you can use to remove the ICE but get a compiler error which I'm sure
will help home in on the bug, and then finally how to get the pragma working
even with optimisation switched on.

To generate the ICE, run 'gprbuild' for the simple test and project files I've
submitted:

   gprbuild -p -d -PTest.gpr

and you get this bug box   
        +===========================GNAT BUG
DETECTED==============================+
        | 8.2.0 (x86_64-w64-mingw32) Program_Error EXCEPTION_ACCESS_VIOLATION  
   |
        | Error detected at test.adb:8:32                                      
   |
        | Please submit a bug report; see https://gcc.gnu.org/bugs/ .          
   |
        | Use a subject line meaningful to you and us to track the bug.        
   |
        | Include the entire contents of this bug box in the report.           
   |
        | Include the exact command that you entered.                          
   |
        | Also include sources listed below.                                   
   |
       
+==========================================================================+

The crash relates to a combination of things:
 - specifying Win32Ada in the project file
 - the use of pragma Discard_Names inside a procedure (not at file scope)
 - the use of GCC optimisation
Yet there seems to be a non-crashing bug if we drop the Win32Ada aspect because
the compiler will produce an incorrect message unless we drop optimisation as
well. To explore this further, proceed through the following steps:

1) In the Test.gpr file, comment out or remove the first line that says "with
win32ada;" as this will prevent the compiler from crashing but will give us a
new clue by triggering an incorrect compiler error message. Note that the
Win32Ada library is not actually used in this test, it's just here because I've
stripped down the test files from a bigger project and Win32Ada may help in
diagnosing the problem. Even if you don't have the Win32Ada library installed,
you can still get problems when combining the pragma with optimisation, but the
ICE itself is related to the Win32Ada specification. So, after commenting
out/removing that "with win32ada;" line and running the same command:

   gprbuild -p -d -PTest.gpr

you'll see the bug box has gone but now there's an incorrect compiler error
message (incorrect because the program is legal), and the error message itself
is suspicious in claiming an empty string is undefined:

   test.adb:8:32: "" is undefined

2) Now in the Test.gpr file, comment out (or remove) the line that sets the Ada
compiler switches. The issue appears related to the use of optimisation
(whether GCC level -O1 or -O2, there's a bug somewhere). Running the same
command as before:
   gprbuild -p -d -PTest.gpr
gives a successful compilation and the program runs fine, producing the desired
output:
   Hello, 1

3) We can drop down to a finer diagnostic level by ignoring gprbuild and
running gnatmake directly, without the project file wrapping. Run this command:

   gnatmake Test.adb -cargs -O2

and we get the earlier incorrect compiler message for Test.adb line 8 about ""
being undefined. We can get a successful compilation by turning off
optimisation e.g.,

   gnatmake Test.adb -cargs -O0

or just by dropping optimisation entirely e.g.,

   gnatmake Test.adb

4) Note that we can both remove the ICE and the incorrect compiler error
message, and get a successful compilation at optimisation level 2, if we do as
ff:
  i) restore the Test.gpr file to reenable the optimisation switches for Ada
back to "-O2".
  ii) alter Test.adb to move the pragma Discard_Names to the first line of the
file
  iii) run this command as we did earlier:

   gprbuild -p -d -PTest.gpr

It'll even work if you uncomment the "with win32ada;" line in the Test.gpr
file. Of course, it'll also work if you go back to gnatmake with optimisation
back on:

   gnatmake Test.adb -cargs -O2

So there's clearly an issue when using pragma Discard_Names with some degree of
optimisation and where the pragma appears inside the procedure scope instead of
at the top of the file. Moreover, using "with win32ada;" in the project file
causes an actual compiler crash, not just a bad error message.

COMPILER DETAILS: output of "gcc -v":
Using built-in specs.
COLLECT_GCC=C:\MSYS2\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=C:/MSYS2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-8.2.0/configure --prefix=/mingw64
--with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32
--host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32
--with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include
--libexecdir=/mingw64/lib --enable-bootstrap --with-arch=x86-64
--with-tune=generic --enable-languages=ada,c,lto,c++,objc,obj-c++,fortran
--enable-shared --enable-static --enable-libatomic --enable-threads=posix
--enable-graphite --enable-fully-dynamic-string
--enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes
--disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check
--enable-lto --enable-libgomp --disable-multilib --enable-checking=release
--disable-rpath --disable-win32-registry --disable-nls --disable-werror
--disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64
--with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64
--with-pkgversion='Rev1, Built by MSYS2 project'
--with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as
--with-gnu-ld
Thread model: posix
gcc version 8.2.0 (Rev1, Built by MSYS2 project)

Reply via email to