character (len=4), s1 character (len=20), pointer :: p1 s1 = 'abcd' p1 => s1(2:3) should be rejected at compile time as len(s1(2:3)) == 2 but p1 has the length 20. This is not detected because primary.c's match_varspec contains: if (substring) primary->ts.cl = NULL; If one removes this check, the proper error: Error: Different character lengths in pointer assignment at (1) is shown. The solution is to resolve ts.cl->length for substrings if possible and only set it to NULL if it is not known at compile time.
This looks suspiciously like the problem we are hunting down in pr31197. Can you have a look and see if its related, if not the same bug?
See also: http://gcc.gnu.org/ml/fortran/2007-05/msg00072.html One also needs a run-time test for: character (len=4), target :: s1 character (len=2), pointer :: p1 s1 = 'abcd' p1 => s1(1:2) if(s1 /= 'abcd') call abort() if(p1 /= 'ab') call abort() end There might be already such a check, though.
There seems to be a consensus on this - confirmed. Paul
The error message is wrong if the if (substring) primary->ts.cl = NULL; part is removed: ig25@linux-fd1f:~/Krempel/Char> cat aa.f90 character (len=4), pointer:: s1 character (len=20), pointer :: p1 s1 = 'abcd' p1 => s1(2:3) end ig25@linux-fd1f:~/Krempel/Char> gfortran aa.f90 aa.f90:4.2: p1 => s1(2:3) 1 Fehler: Unequal character lengths (20/4) in pointer assignment at (1) The length of the substring is wrongly taken to be 4, instead of 2.
gfc_check_same_strlen does not check for references. Ouch.
Working on a patch.
Created attachment 22724 [details] patch which causes regressions
The patch in comment#7 causes a regression in program gfcbug33 character(12) :: a(2) a(1) = "abcdefghijkl" a(2) = "mnopqrstuvwx" call foo ((a(2:1:-1)(6:))) contains subroutine foo (chr) character(7) :: chr(:) print *,'X',chr(1),'Y' print *,'A',chr(2),'B' if (chr(1)//chr(2) .ne. "rstuvwxfghijkl") call abort () end subroutine foo end program gfcbug33 ig25@linux-fd1f:~/Krempel/Char> gfortran short_1.f90 ig25@linux-fd1f:~/Krempel/Char> ./a.out XrstuvwxY A%�2�fgB Abgebrochen which is a shortened version of actual_array_substr_1.f90. The part of the patch - if (substring) - primary->ts.u.cl = NULL; - opens a can of worms of wrong-code and rejects-valid bugs...
Created attachment 22746 [details] As far as I got... This is as far as I got. Offsets are still calculated wrongly, but without a clue as to where to start looking, I think I'll give up for now.
Created attachment 22838 [details] Patch that has a good chance of working (In reply to comment #8) > > The part of the patch > > - if (substring) > - primary->ts.u.cl = NULL; > - > is part of the precipitate. This should be better.
Author: tkoenig Date: Fri Dec 24 08:42:04 2010 New Revision: 168224 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=168224 Log: 2010-12-24 Thomas Koenig <tkoenig@gcc.gnu.org> PR fortran/31821 * check.c (gfc_var_strlen): New function, also including substring references. (gfc_check_same_strlen): Use gfc_var_strlen. 2010-12-24 Thomas Koenig <tkoenig@gcc.gnu.org> PR fortran/31821 * gfortran.dg/char_pointer_assign_6.f90: New test. Added: trunk/gcc/testsuite/gfortran.dg/char_pointer_assign_6.f90 Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/check.c trunk/gcc/testsuite/ChangeLog
Fixed, closing.