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

            Bug ID: 128837
           Summary: [17 Regression] ICE in gfc_get_location_with_offset
                    with -Wundefined-vars for an unused ALLOCATABLE
                    INTENT(OUT) dummy of finalizable type
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: abensonca at gcc dot gnu.org
  Target Milestone: ---

With -Wall (which enables the new -Wundefined-vars), trunk segfaults on a
procedure whose ALLOCATABLE, INTENT(OUT) dummy argument has a type with a
final procedure, if the procedure body never references the dummy:

module m
  implicit none
  type :: t
   contains
     final :: destroy
  end type t
contains
  subroutine destroy(self)
    type(t), intent(inout) :: self
  end subroutine destroy
  subroutine get(x)
    type(t), intent(out), allocatable :: x
  end subroutine get
end module m

$ gfortran -c -Wall ice.f90
f951: internal compiler error: Segmentation fault
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
0x25ea8ff internal_error(char const*, ...)
0x107a55f crash_signal
0x8d3110 gfc_get_location_with_offset(locus*, unsigned int)
0x8d3110 gfc_format_decoder
0x26405c8 format_phase_2
0x26405c8 pretty_printer::format(text_info&)
0x25f19fe pp_format(pretty_printer*, text_info*)
0x25f19fe
diagnostics::context::report_diagnostic(diagnostics::diagnostic_info*)
0x8d2d79 diagnostic_report_diagnostic(diagnostics::context*,
diagnostics::diagnostic_info*)
0x8d2d79 gfc_report_diagnostic
0x8d2d79 gfc_warning
0x8d3666 gfc_warning(int, char const*, ...)
0x96efc1 find_unused_vs_set
0x96efc1 find_unused_vs_set
0x9ae752 do_traverse_symtree
0x971ee5 warn_unused_vs_set
...

The same happens for an array dummy (dimension(:)), and for the dummy of a
separate module procedure's interface body (where there is no executable code
at all), which is how we first hit it: four files of a large code
(Galacticus) fail to compile with -Wall on trunk.

No ICE with -Wno-undefined-vars, without -Wall, if the dummy is not
ALLOCATABLE, if the type has no final procedure, or if the procedure allocates
the dummy. 16.2.1 compiles the test case without complaint.

The crash is in the first check of find_unused_vs_set:

  if (warn_undefined_vars && attr->allocatable && !attr->allocated
      && var_value_is_used (sym))
    {
      if (attr->dummy && attr->intent == INTENT_OUT)
        {
          gfc_warning (OPT_Wundefined_vars, "Unallocated INTENT(OUT) variable "
                       "%qs referenced at %L", sym->name, &sym->other_loc);

var_value_is_used() returns true for any INTENT(OUT) dummy, and the dummy is
marked "referenced" (presumably by the code that deallocates and finalizes an
INTENT(OUT) allocatable dummy on entry). But nothing in the source references
it, so sym->other_loc was never set: in gdb it is {nextc = 0x0, u = {lb =
0x0}}, and gfc_get_location_with_offset dereferences loc->u.lb.

So the warning would be a false positive here even without the crash: the only
"reference" is the compiler's own. A possible fix (untested) is to skip these
warnings when sym->other_loc was never set (other_loc.nextc == NULL), since
there is then no user reference to point to.

Reply via email to