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

            Bug ID: 122816
           Summary: Missed simplification of concatenation of constant
                    strings
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: anlauf at gcc dot gnu.org
  Target Milestone: ---

Consider:

  character(1) :: s = "+"
  print *, 'a' // 'b' //  s   ! OK
  print *,  s //  'b' // 'c'  ! missing simplication
  print *,  s // ('b' // 'c') ! OK
end


This gives (-fdump-fortran-original)

  code:
  WRITE UNIT=6 FMT=-1
  TRANSFER (// 'ab' MAIN__:s)
  DT_END
  WRITE UNIT=6 FMT=-1
  TRANSFER (// (// MAIN__:s 'b') 'c')
  DT_END
  WRITE UNIT=6 FMT=-1
  TRANSFER (// MAIN__:s 'bc')
  DT_END

and (-fdump-tree-original) confirms the double concat:

    {
      character(kind=1) str.3[2];
      character(kind=1) str.4[3];

      _gfortran_concat_string (2, (character(kind=1)[1:] *) &str.3, 1, &s, 1,
&"b"[1]{lb: 1 sz: 1});
      _gfortran_concat_string (3, (character(kind=1)[1:] *) &str.4, 2,
(character(kind=1)[1:] *) &str.3, 1, &"c"[1]{lb: 1 sz: 1});
      _gfortran_transfer_character_write (&dt_parm.2, (character(kind=1)[1:] *)
&str.4, 3);
    }
    _gfortran_st_write_done (&dt_parm.2);
  }


Since string concatenation is strictly associative, as only same character
kinds are allowed, we could simplify the second case even without explicit
specification of parentheses.

Reply via email to