From dfa2f821c18b7e926b5f5d6e394a0c915937db5e Mon Sep 17 00:00:00 2001 From: Jerry DeLisle Date: Fri, 19 Feb 2021 12:47:54 -0800 Subject: [PATCH] fortran: Object types should be declared before use in NAMELIST. gcc/fortran/ChangeLog: PR fortran/98686 * match.c (gfc_match_namelist): If BT_UNKNOWN, check for IMPLICIT NONE and and issue an error, otherwise set the type to its IMPLICIT type so that any subsequent use of objects will will confirm their types. gcc/testsuite/ChangeLog: PR fortran/98686 * gfortran.dg/namelist_4.f90: Modify. * gfortran.dg/namelist_98.f90: New test. --- gcc/fortran/match.c | 18 ++++++++++++++++++ gcc/testsuite/gfortran.dg/namelist_4.f90 | 7 ++++--- gcc/testsuite/gfortran.dg/namelist_98.f90 | 11 +++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/namelist_98.f90 diff --git a/gcc/fortran/match.c b/gcc/fortran/match.c index 2df6191d7e6..4d5890fd523 100644 --- a/gcc/fortran/match.c +++ b/gcc/fortran/match.c @@ -5536,6 +5536,24 @@ gfc_match_namelist (void) if (m == MATCH_ERROR) goto error; + if (sym->ts.type == BT_UNKNOWN) + { + if (gfc_current_ns->seen_implicit_none) + { + /* It is required that members of a namelist be declared + before the namelist. We check this by checking if the + symbol has a defined type for IMPLICIT NONE. */ + gfc_error ("Symbol %qs in namelist %qs at %C must be " + "declared before the namelist is declared.", + sym->name, group_name->name); + gfc_error_check (); + } + else + /* If the type is not set already, we set it here to the + implicit default type. It is not allowed to set it + later to any other type. */ + gfc_set_default_type (sym, 0, gfc_current_ns); + } if (sym->attr.in_namelist == 0 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL)) goto error; diff --git a/gcc/testsuite/gfortran.dg/namelist_4.f90 b/gcc/testsuite/gfortran.dg/namelist_4.f90 index 538bceaa4b6..e6681fd3b55 100644 --- a/gcc/testsuite/gfortran.dg/namelist_4.f90 +++ b/gcc/testsuite/gfortran.dg/namelist_4.f90 @@ -23,12 +23,13 @@ CONTAINS G3=1 END FUNCTION END module M1 - + program P1 +implicit none CONTAINS ! This has the additional wrinkle of a reference to the object. INTEGER FUNCTION F1() - NAMELIST /NML3/ F2 ! { dg-error "PROCEDURE attribute conflicts" } + NAMELIST /NML3/ F2 ! { dg-error "PROCEDURE attribute conflicts" } ! Used to ICE here f2 = 1 ! { dg-error "is not a VALUE" } F1=1 @@ -36,5 +37,5 @@ CONTAINS INTEGER FUNCTION F2() F2=1 END FUNCTION -END +END diff --git a/gcc/testsuite/gfortran.dg/namelist_98.f90 b/gcc/testsuite/gfortran.dg/namelist_98.f90 new file mode 100644 index 00000000000..19a7e869f92 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/namelist_98.f90 @@ -0,0 +1,11 @@ +! { dg-do compile } +! pr98686 + implicit none + real :: x, m + namelist /NML/ x, m, q ! { dg-error "must be declared before the namelist*" } + integer :: q + x = 1.0 + m = 2.0 + q = 3 + write(*, nml=NML) +end -- 2.30.2