Bug 32707 - Warn on mismatched character lengths in array
Summary: Warn on mismatched character lengths in array
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: 4.4.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid, diagnostic
Depends on:
Blocks:
 
Reported: 2007-07-09 19:02 UTC by Vivek Rao
Modified: 2009-01-03 21:12 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.4.0
Known to fail: 4.3.2
Last reconfirmed: 2007-08-12 10:24:31


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vivek Rao 2007-07-09 19:02:17 UTC
For the following invalid code

program xtrim_array
implicit none
character (len=4) :: xx(2)
xx = [trim("boy "),trim("girl")] ! invalid -- elements have different lengths
print*,"xx = '" // xx(1) // "' '" // xx(2) // "'" 
end program xtrim_array

compiling and running with gfortran gives

gfortran -v -O0 -Wall -fbounds-check -static -g xtrim_array.f90
Driving: gfortran -v -O0 -Wall -fbounds-check -static -g xtrim_array.f90 -lgfortranbegin -lgfortran
Using built-in specs.
Target: i386-pc-mingw32
Configured with: ../trunk/configure --prefix=/mingw --enable-languages=c,fortran --with-gmp=/home/coudert/local --disable-nls --with-ld=/mingw/bin/ld --with-as=/mingw/bin/as --disable-werror --enable-bootstrap --enable-threads --build=i386-pc-mingw32 --disable-shared --enable-libgomp
Thread model: win32
gcc version 4.3.0 20070706 (experimental)
 c:/programs/gfortran/bin/../libexec/gcc/i386-pc-mingw32/4.3.0/f951.exe xtrim_array.f90 -quiet -dumpbase xtrim_array.f90 -mtune=i386 -auxbase xtrim_array -g -O0 -Wall -version -fbounds-check -fintrinsic-modules-path c:/programs/gfortran/bin/../lib/gcc/i386-pc-mingw32/4.3.0/finclude -o C:\DOCUME~1\vrao\LOCALS~1\Temp/ccaETULf.s
GNU F95 version 4.3.0 20070706 (experimental) (i386-pc-mingw32)
	compiled by GNU C version 4.3.0 20070706 (experimental), GMP version 4.2.1, MPFR version 2.2.1.
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
 as -o C:\DOCUME~1\vrao\LOCALS~1\Temp/ccg74eom.o C:\DOCUME~1\vrao\LOCALS~1\Temp/ccaETULf.s
 c:/programs/gfortran/bin/../libexec/gcc/i386-pc-mingw32/4.3.0/collect2.exe -Bstatic c:/programs/g95/lib/crt2.o c:/programs/gfortran/bin/../lib/gcc/i386-pc-mingw32/4.3.0/crtbegin.o -Lc:/programs/gfortran/bin/../lib/gcc/i386-pc-mingw32/4.3.0 -Lc:/programs/gfortran/bin/../lib/gcc -Lc:/programs/g95/lib -Lc:/programs/gfortran/bin/../lib/gcc/i386-pc-mingw32/4.3.0/../../.. C:\DOCUME~1\vrao\LOCALS~1\Temp/ccg74eom.o -lgfortranbegin -lgfortran -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt c:/programs/gfortran/bin/../lib/gcc/i386-pc-mingw32/4.3.0/crtend.o

U:\vrao\fortran>a
 xx = 'boy ' 'girl'

It would be nice if gfortran gave a compile-time or run-time error.

Mingw G95 Jun 30 2007 with the default options says

In file xtrim_array.f90:4

xx = [trim("boy "),trim("girl")] ! invalid -- elements have different lengths
                   1
Error: Element in character array constructor at (1) has length 4 instead of 3
Comment 1 Vivek Rao 2007-07-09 19:05:45 UTC
Gfortran also does not warn about the simpler code

program xtrim_array
! check if compiler catches character array constructor with elements of different sizes
implicit none
character (len=4) :: xx(2)
xx = ["boy","girl"]
print*,"xx = '" // xx(1) // "' '" // xx(2) // "'" 
end program xtrim_array
Comment 2 Daniel Franke 2007-07-10 08:16:46 UTC
See also: PR29267.
Comment 3 Daniel Franke 2009-01-03 21:12:43 UTC
Fixed in trunk. Both testcases give:
$> gfortran-svn pr32707.f90
pr32707.f90:5.12:

xx = ["boy","girl"]
            1
Error: Different CHARACTER lengths (3/4) in array constructor at (1)

Testcases are already present in the testsuite. Closing.