Bug 31346 - wrong values for ubound and size of deferred shape arrays without explicit interface
Summary: wrong values for ubound and size of deferred shape arrays without explicit in...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: 4.5.1
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid, diagnostic
: 36553 (view as bug list)
Depends on:
Blocks: 40011
  Show dependency treegraph
 
Reported: 2007-03-25 13:37 UTC by Mikael Morin
Modified: 2010-07-18 21:13 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.3.0 4.2.0 4.5.0 4.6.0
Last reconfirmed: 2007-03-26 07:19:26


Attachments
program showing the bug (193 bytes, text/plain)
2007-03-25 13:38 UTC, Mikael Morin
Details
the working case (195 bytes, text/plain)
2007-03-26 05:58 UTC, Mikael Morin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mikael Morin 2007-03-25 13:37:14 UTC
size and ubound are lost when passing an array as argument of a procedure
Comment 1 Mikael Morin 2007-03-25 13:38:51 UTC
Created attachment 13283 [details]
program showing the bug
Comment 2 Mikael Morin 2007-03-25 13:41:33 UTC
$ FC=gfortran; $FC -v; $FC -o test test.f; echo; ./test
Lecture des spécification à partir de /usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/specs
Target: x86_64-pc-linux-gnu
Configuré avec: /usr/src/gcc-4.1.2/configure --host=x86_64-pc-linux-gnu --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --enable-threads=posix --with-system-zlib --enable-languages=f95 --disable-multilib
Modèle de thread: posix
version gcc 4.1.2

 size=           5
 size=          25
 ubound=           5
 ubound=           5           5
 size=           0
 size=           0
 ubound=           0
 ubound=     2193121           0


FC=/usr/local/bin/gfortran; $FC -v; $FC -o test test.f; echo; ./test
Utilisation des specs internes.
Target: x86_64-unknown-linux-gnu
Configuré avec: ../gcc-4.3-20070316/configure --enable-languages=fortran --disable-multilib
Modèle de thread: posix
version gcc 4.3.0 20070316 (experimental)

 size=           5
 size=          25
 ubound=           5
 ubound=           5           5
 size=        -250
 size=           0
 ubound=           0
 ubound=     5222132           0


Comment 3 Mikael Morin 2007-03-26 05:58:45 UTC
Created attachment 13286 [details]
the working case

The problem arise when the subroutine is not defined in a contains statement in the main program. 
I haven't seen anything in the Fortran 2003 standard forcing the subroutines to be in the contains statement. 
But even if it is the case, the compiler should report an error.
Comment 4 Paul Thomas 2007-03-26 07:19:25 UTC
(In reply to comment #3)

> I haven't seen anything in the Fortran 2003 standard forcing the subroutines to
> be in the contains statement.

In this case the interface is said to be implicit and the standard does not require the programme to do the right thing. Use an INTERFACE, as below, or put the subroutine in a module.  In either case the interface will be made explicit.

> But even if it is the case, the compiler should report an error. 

This is not a requirement of the standard but is a long standing regression, relative to g77.

Confirmed and marked as "diagnostic"

Nous t'en remercions

Cordialement

Paul

      program test

      interface
        subroutine test1(x,y)

        real(kind=8), dimension(:) :: x
        real(kind=8), dimension(:,:) :: y

        end subroutine
      end interface

      real(kind=8), dimension(5) :: test_array
      real(kind=8), dimension(5,5) :: test_array2

      real(kind=8) :: taille


      write(6,*) 'size=', size(test_array)
      write(6,*) 'size=', size(test_array2)
      write(6,*) 'ubound=', ubound(test_array)
      write(6,*) 'ubound=', ubound(test_array2)
      

      call test1(test_array, test_array2)


      end program

> 

Comment 5 Tobias Schlüter 2007-04-07 17:04:01 UTC
(In reply to comment #4)
> (In reply to comment #3)
> > But even if it is the case, the compiler should report an error. 
> 
> This is not a requirement of the standard but is a long standing regression,
> relative to g77.

Really?  From how I read the standard (F2K draft), UBOUND(ARRAY, DIM) has (in this case) "a value equal to the upper bound for subscript DIM of ARRAY".  I can't see it allowing returning random values such as 2193121.
Comment 6 Francois-Xavier Coudert 2007-10-02 11:27:14 UTC
(In reply to comment #5)
> Really?  From how I read the standard (F2K draft), UBOUND(ARRAY, DIM) has (in
> this case) "a value equal to the upper bound for subscript DIM of ARRAY".  I
> can't see it allowing returning random values such as 2193121.

The program shown is not valid Fortran, the processor can do whatever it wants. A most sensible choice would be to reject it, of course: that's what g95 does (and g77 also, according to Paul) but others have chosen to segfault (Intel, Sun).
Comment 7 Francois-Xavier Coudert 2007-11-12 01:14:10 UTC
I don't see how this can be a regression wrt g77, which doesn't have assumed-shape arrays or even UBOUND!
Comment 8 Thomas Koenig 2009-12-20 20:10:40 UTC
We fail to catch the invalid code

$ cat whole.f90
program main
  real, dimension(2) :: a
  call foo(a)
end program main

subroutine foo(a)
  real, dimension(:) :: a
end subroutine foo
$ gfortran -fwhole-file -O3 whole.f90
$ 
Comment 9 Daniel Franke 2010-05-23 22:34:17 UTC
*** Bug 36553 has been marked as a duplicate of this bug. ***
Comment 10 Daniel Franke 2010-05-23 22:35:47 UTC
The dupe had accepts-invalid, adding it here. Pushing back from enhancement to normal.
Comment 11 Daniel Franke 2010-05-25 18:10:19 UTC
Subject: Bug 31346

Author: dfranke
Date: Tue May 25 18:10:01 2010
New Revision: 159838

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=159838
Log:
gcc/fortran/:
2010-05-25  Daniel Franke  <franke.daniel@gmail.com>

	PR fortran/30668
	PR fortran/31346
	PR fortran/34260
	* resolve.c (resolve_global_procedure): Add check for global
	procedures with implicit interfaces and assumed-shape or optional
	dummy arguments. Verify that function return type, kind and string
	lengths match.

gcc/testsuite/:
2010-05-25  Daniel Franke  <franke.daniel@gmail.com>

	PR fortran/30668
	PR fortran/31346
	PR fortran/34260
	* gfortran.dg/pr40999.f: Fix function type.
	* gfortran.dg/whole_file_5.f90: Likewise.
	* gfortran.dg/whole_file_6.f90: Likewise.
	* gfortran.dg/whole_file_16.f90: New.
	* gfortran.dg/whole_file_17.f90: New.
	* gfortran.dg/whole_file_18.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/whole_file_16.f90
    trunk/gcc/testsuite/gfortran.dg/whole_file_17.f90
    trunk/gcc/testsuite/gfortran.dg/whole_file_18.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/resolve.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/pr40999.f
    trunk/gcc/testsuite/gfortran.dg/whole_file_5.f90
    trunk/gcc/testsuite/gfortran.dg/whole_file_6.f90

Comment 12 Daniel Franke 2010-07-18 20:49:52 UTC
Subject: Bug 31346

Author: dfranke
Date: Sun Jul 18 20:49:30 2010
New Revision: 162287

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=162287
Log:
gcc/fortran/:
2010-07-18  Daniel Franke  <franke.daniel@gmail.com>
	    Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/30668
	PR fortran/31346
	PR fortran/34260
        * resolve.c (resolve_global_procedure): Improved checking if an
        explicit interface is required.

	PR fortran/40011
	* resolve.c (resolve_global_procedure): Resolve the gsymbol's
	namespace before trying to reorder the gsymbols.

gcc/testsuite/:
2010-07-18  Daniel Franke  <franke.daniel@gmail.com>
	    Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/30668
	PR fortran/31346
	PR fortran/34260
	PR fortran/40011
	* gfortran.dg/pr40999.f: Fix function type.
	* gfortran.dg/whole_file_5.f90: Likewise.
	* gfortran.dg/whole_file_6.f90: Likewise.
	* gfortran.dg/whole_file_16.f90: New.
	* gfortran.dg/whole_file_17.f90: New.
	* gfortran.dg/whole_file_18.f90: New.
	* gfortran.dg/whole_file_19.f90: New.


Added:
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/whole_file_16.f90
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/whole_file_17.f90
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/whole_file_18.f90
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/whole_file_19.f90
Modified:
    branches/gcc-4_5-branch/gcc/fortran/ChangeLog
    branches/gcc-4_5-branch/gcc/fortran/resolve.c
    branches/gcc-4_5-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/pr40999.f
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/whole_file_5.f90
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/whole_file_6.f90

Comment 13 Daniel Franke 2010-07-18 21:13:23 UTC
Fixed in trunk and 4.5. Closing.