Fortran: Missing error with IMPLICIT none (external) [PR100972]

Message ID 20211031172520.3841446-1-rep.dot.nop@gmail.com
State Committed
Commit 1727bb533ede295a3ef2dd494225d27b6d1746aa
Headers
Series Fortran: Missing error with IMPLICIT none (external) [PR100972] |

Commit Message

Bernhard Reutner-Fischer Oct. 31, 2021, 5:25 p.m. UTC
  From: Bernhard Reutner-Fischer <aldot@gcc.gnu.org>

gcc/fortran/ChangeLog:

	PR fortran/100972
	* decl.c (gfc_match_implicit_none): Fix typo in warning.
	* resolve.c (resolve_unknown_f): Reject external procedures
	without explicit EXTERNAL attribute whe IMPLICIT none (external)
	is in effect.

gcc/testsuite/ChangeLog:

	PR fortran/100972
	* gfortran.dg/implicit_14.f90: Adjust error.
	* gfortran.dg/external_implicit_none_3.f08: New test.

---

As Gerhard Steinmetz noticed, gfc_match_implicit_none() had a notify_std
that mentioned IMPORT instead of IMPLICIT. Fix that typo.

IMPLICIT NONE (external) is supposed to require external procedures to
be explicitly declared with EXTERNAL.
We cannot do this when parsing in e.g.
gfc_match_rvalue->gfc_match_varspec because the procedure might live
way down in a CONTAINS like in bind-c-contiguous-3.f90.
Hence diagnose missing EXTERNAL declaraions when resolving.

Bootstrapped and regtested on x86_64-unknown-linux without regressions.
Ok for trunk?
---
 gcc/fortran/decl.c                              |  2 +-
 gcc/fortran/resolve.c                           | 13 +++++++++++++
 .../gfortran.dg/external_implicit_none_3.f08    | 17 +++++++++++++++++
 gcc/testsuite/gfortran.dg/implicit_14.f90       |  2 +-
 4 files changed, 32 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/external_implicit_none_3.f08
  

Comments

Mikael Morin Nov. 5, 2021, 8 p.m. UTC | #1
Le 31/10/2021 à 18:25, Bernhard Reutner-Fischer via Fortran a écrit :
> As Gerhard Steinmetz noticed, gfc_match_implicit_none() had a notify_std
> that mentioned IMPORT instead of IMPLICIT. Fix that typo.
> 
> IMPLICIT NONE (external) is supposed to require external procedures to
> be explicitly declared with EXTERNAL.
> We cannot do this when parsing in e.g.
> gfc_match_rvalue->gfc_match_varspec because the procedure might live
> way down in a CONTAINS like in bind-c-contiguous-3.f90.
> Hence diagnose missing EXTERNAL declaraions when resolving.
> 
> Bootstrapped and regtested on x86_64-unknown-linux without regressions.
> Ok for trunk?

OK, thanks.

Mikael
  

Patch

diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
index e9e23fe1acb..ab88ab5e9c1 100644
--- a/gcc/fortran/decl.c
+++ b/gcc/fortran/decl.c
@@ -4715,7 +4715,7 @@  gfc_match_implicit_none (void)
   if (c == '(')
     {
       (void) gfc_next_ascii_char ();
-      if (!gfc_notify_std (GFC_STD_F2018, "IMPORT NONE with spec list at %C"))
+      if (!gfc_notify_std (GFC_STD_F2018, "IMPLICIT NONE with spec list at %C"))
 	return MATCH_ERROR;
 
       gfc_gobble_whitespace ();
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 21126cba262..1f4abd08720 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -2974,6 +2974,19 @@  resolve_unknown_f (gfc_expr *expr)
       return false;
     }
 
+  /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr.  */
+  /* Intrinsics were handled above, only non-intrinsics left here.  */
+  if (sym->attr.flavor == FL_PROCEDURE
+      && sym->attr.implicit_type
+      && sym->ns
+      && sym->ns->has_implicit_none_export)
+    {
+	  gfc_error ("Missing explicit declaration with EXTERNAL attribute "
+	      "for symbol %qs at %L", sym->name, &sym->declared_at);
+	  sym->error = 1;
+	  return false;
+    }
+
   /* The reference is to an external name.  */
 
   sym->attr.proc = PROC_EXTERNAL;
diff --git a/gcc/testsuite/gfortran.dg/external_implicit_none_3.f08 b/gcc/testsuite/gfortran.dg/external_implicit_none_3.f08
new file mode 100644
index 00000000000..329deedc413
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/external_implicit_none_3.f08
@@ -0,0 +1,17 @@ 
+! { dg-do compile }
+! { dg-options "-std=f2018" }
+! Tests fix for PR100972 - Fails to warn about missing EXTERNAL attribute
+! Contributed by Gerhard Steinmetz
+
+program p
+   implicit none (external)
+   real, external :: f
+   real :: a
+   real :: b
+   integer :: i
+   character :: c
+   a = f() ! OK
+   b = g() ! { dg-error "Missing explicit declaration with EXTERNAL attribute" }
+   i = h() ! { dg-error "Missing explicit declaration with EXTERNAL attribute" }
+   c = j() ! { dg-error "Missing explicit declaration with EXTERNAL attribute" }
+end
diff --git a/gcc/testsuite/gfortran.dg/implicit_14.f90 b/gcc/testsuite/gfortran.dg/implicit_14.f90
index 8282c1f1f86..422d913fd4f 100644
--- a/gcc/testsuite/gfortran.dg/implicit_14.f90
+++ b/gcc/testsuite/gfortran.dg/implicit_14.f90
@@ -4,5 +4,5 @@ 
 ! Support Fortran 2018's IMPLICIT NONE with spec list
 ! (currently implemented as vendor extension)
 
-implicit none (type) ! { dg-error "Fortran 2018: IMPORT NONE with spec list at \\(1\\)" }
+implicit none (type) ! { dg-error "Fortran 2018: IMPLICIT NONE with spec list at \\(1\\)" }
 end