aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorHarald Anlauf <anlauf@gmx.de>2025-02-01 19:14:21 +0100
committerHarald Anlauf <anlauf@gmx.de>2025-02-03 22:36:20 +0100
commitf3a41e6cb5d70f0c94cc8273a118b8542fb5c2fa (patch)
tree20a994c4b35d1d6eef69d964f0141d8fb230297c /gcc
parent214224c4973bfb76f73a7efff29c5823eef31194 (diff)
downloadgcc-f3a41e6cb5d70f0c94cc8273a118b8542fb5c2fa.zip
gcc-f3a41e6cb5d70f0c94cc8273a118b8542fb5c2fa.tar.gz
gcc-f3a41e6cb5d70f0c94cc8273a118b8542fb5c2fa.tar.bz2
Fortran: different character lengths in array constructor [PR93289]
PR fortran/93289 gcc/fortran/ChangeLog: * decl.cc (gfc_set_constant_character_len): Downgrade different string lengths in character array constructor to legacy extension. gcc/testsuite/ChangeLog: * gfortran.dg/unlimited_polymorphic_1.f03: Pad element in character array constructor to correct length. * gfortran.dg/char_array_constructor_5.f90: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/fortran/decl.cc20
-rw-r--r--gcc/testsuite/gfortran.dg/char_array_constructor_5.f9013
-rw-r--r--gcc/testsuite/gfortran.dg/unlimited_polymorphic_1.f032
3 files changed, 28 insertions, 7 deletions
diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index 7954a84..5a46658 100644
--- a/gcc/fortran/decl.cc
+++ b/gcc/fortran/decl.cc
@@ -1889,12 +1889,20 @@ gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
/* Apply the standard by 'hand' otherwise it gets cleared for
initializers. */
- if (check_len != -1 && slen != check_len
- && !(gfc_option.allow_std & GFC_STD_GNU))
- gfc_error_now ("The CHARACTER elements of the array constructor "
- "at %L must have the same length (%ld/%ld)",
- &expr->where, (long) slen,
- (long) check_len);
+ if (check_len != -1 && slen != check_len)
+ {
+ if (!(gfc_option.allow_std & GFC_STD_GNU))
+ gfc_error_now ("The CHARACTER elements of the array constructor "
+ "at %L must have the same length (%ld/%ld)",
+ &expr->where, (long) slen,
+ (long) check_len);
+ else
+ gfc_notify_std (GFC_STD_LEGACY,
+ "The CHARACTER elements of the array constructor "
+ "at %L must have the same length (%ld/%ld)",
+ &expr->where, (long) slen,
+ (long) check_len);
+ }
s[len] = '\0';
free (expr->value.character.string);
diff --git a/gcc/testsuite/gfortran.dg/char_array_constructor_5.f90 b/gcc/testsuite/gfortran.dg/char_array_constructor_5.f90
new file mode 100644
index 0000000..0cbe6b1
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/char_array_constructor_5.f90
@@ -0,0 +1,13 @@
+! { dg-do compile }
+! { dg-additional-options "-std=gnu" }
+!
+! PR fortran/93289
+!
+! Contributed by Tobias Burnus
+
+character(len=*), parameter :: str = "abj", str2 = "1234"
+print *, [character(5) :: str, "ab", "hjf333"]
+print *, [character(5) :: str, str2]
+print *, [str, "ab", "hjf333"] ! { dg-warning "must have the same length" }
+print *, [str, str2] ! { dg-warning "must have the same length" }
+end
diff --git a/gcc/testsuite/gfortran.dg/unlimited_polymorphic_1.f03 b/gcc/testsuite/gfortran.dg/unlimited_polymorphic_1.f03
index 8634031..51483ed 100644
--- a/gcc/testsuite/gfortran.dg/unlimited_polymorphic_1.f03
+++ b/gcc/testsuite/gfortran.dg/unlimited_polymorphic_1.f03
@@ -155,7 +155,7 @@ END MODULE
call foo([a(8),a(9)], res)
if (trim (res) .ne. "type(a) array 8 9") STOP 1
- call foo([sun, " & rain"], res)
+ call foo([sun, " & rain "], res)
if (trim (res) .ne. "char( 8, 2)sunshine & rain") STOP 1
call foo([sun//" never happens", " & rain always happens"], res)