Bug 47778 - reading two arrays of structures from namelist fails
Summary: reading two arrays of structures from namelist fails
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.6.0
: P3 major
Target Milestone: ---
Assignee: Jerry DeLisle
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2011-02-17 09:22 UTC by Arnold Moene
Modified: 2011-03-07 02:16 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-02-19 15:52:35


Attachments
This code fails (407 bytes, text/x-fortran)
2011-02-17 09:22 UTC, Arnold Moene
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Arnold Moene 2011-02-17 09:22:21 UTC
Created attachment 23374 [details]
This code fails

Reading a namelist that contains two variable that are an array of structures
fails. It is not even the assignment that fails, but the parsing already gives problems.

Attached the code that illustrates the problem.

This may be a follow-up of Bug 42901 (at least it is for me).
Comment 1 Tobias Burnus 2011-02-17 10:40:18 UTC
At line 27 of file test.f90 (unit = 10, file = '/tmp/gfortrantmp2WKkUW')
Fortran runtime error: Cannot match namelist object name 2

I get the same error with gfortran 4.1.2 20080704 (Red Hat 4.1.2-48)
Thus, it does not seem to be a regression. It works with ifort 11.1, pgf90 10.1-0 and pathscale 3.2.99.

* * *

type field_descr
  integer number
end type
type fsetup
  type (field_descr) :: vel(1)
  type (field_descr) :: scal(1)
end type

&nl_setup"
 field_setup%vel(1)%number=  3,
 field_setup%scal(1)%number=  2 /


For this variant, the error message for the second line of the namelist is some uninitialized garbage:

At line 23 of file foobar.f90 (unit = 10, file = '/tmp/gfortrantmpMxI6TY')
Fortran runtime error: L�7�


The namelist reading works, if "scal" is a scalar instead of an array.
However, if "vel" is a scalar and "scal" an array, it fails - unless one swaps the "vel" and "scal" lines in the input file.
Comment 2 Jerry DeLisle 2011-02-17 19:22:31 UTC
I will try to look at this one this weekend.
Comment 3 Jerry DeLisle 2011-02-19 15:52:35 UTC
A reduced test case:

program test_nml
type field_descr
  integer number
end type
type fsetup
  type (field_descr), dimension(3) :: vel
  type (field_descr), dimension(3) :: scal
end type
type (fsetup) field_setup
namelist /nl_setup/ field_setup
field_setup%vel%number = 0
field_setup%scal%number = 0
open(10, status="scratch")
write(10,'(a)') "&nl_setup"
write(10,'(a)') " field_setup%scal(1)%number=  2,"
write(10,'(a)') " field_setup%scal(2)%number=  6,"
write(10,'(a)') "/"
rewind(10)
read(10,nml=nl_setup)
write(*,nml=nl_setup)
end program test_nml
Comment 4 Jerry DeLisle 2011-02-21 14:35:20 UTC
Status update. I have more or less isolated the problem in list-read.c.  I do not have an exact solution yet, but I am able to get the test case to work. I just need now to find the right place to make the tweak.
Comment 5 Jerry DeLisle 2011-02-27 07:39:37 UTC
Probable Patch, Regression testing:

Index: list_read.c
===================================================================
--- list_read.c	(revision 170492)
+++ list_read.c	(working copy)
@@ -3058,6 +3058,8 @@ find_nml_name:
 	    goto nml_err_ret;
 	  generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
         }
+      if (prev_nl && prev_nl->var_rank == 0)
+	prev_nl = NULL;
     }
 
   free_saved (dtp);
Comment 6 Jerry DeLisle 2011-02-27 20:06:13 UTC
Author: jvdelisle
Date: Sun Feb 27 20:06:10 2011
New Revision: 170548

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=170548
Log:
2011-02-27  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libgfortran/47778
	* io/list_read.c (namelist_read): Intialize the error string buffere.
	If pprev_nl was used during the previous namelist read and the rank
	was zero, reset the pointer to NULL for the next namelist read.

Modified:
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/io/list_read.c
Comment 7 Jerry DeLisle 2011-02-27 20:08:47 UTC
Author: jvdelisle
Date: Sun Feb 27 20:08:44 2011
New Revision: 170549

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=170549
Log:
2011-02-27  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libgfortran/47778
	* gfortran.dg/namelist_71.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/namelist_71.f90
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 8 Jerry DeLisle 2011-02-27 20:10:30 UTC
Fixed on trunk.  I will leave this PR open until I get the back ports done.  I want to give this change a few days on trunk first.
Comment 9 Jerry DeLisle 2011-03-07 02:06:32 UTC
Author: jvdelisle
Date: Mon Mar  7 02:06:27 2011
New Revision: 170726

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=170726
Log:
2011-03-06  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libgfortran/47778
	* io/list_read.c (namelist_read): Intialize the error string buffere.
	If pprev_nl was used during the previous namelist read and the rank
	was zero, reset the pointer to NULL for the next namelist read.

Modified:
    branches/gcc-4_5-branch/libgfortran/ChangeLog
    branches/gcc-4_5-branch/libgfortran/io/list_read.c
Comment 10 Jerry DeLisle 2011-03-07 02:12:01 UTC
Author: jvdelisle
Date: Mon Mar  7 02:11:56 2011
New Revision: 170727

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=170727
Log:
2011-03-06  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	Backport from mainline
	PR libgfortran/47778
	* gfortran.dg/namelist_71.f90: New test.

Added:
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/namelist_71.f90
Modified:
    branches/gcc-4_5-branch/gcc/testsuite/ChangeLog
Comment 11 Jerry DeLisle 2011-03-07 02:16:16 UTC
Fixed on 4.5 and 4.6.  Closing