The attached patch has be regression tested on x86_64-*-freebsd.

The testcase in the PR explains what the patch does.

% cat z1.f90
subroutine sub (x)
   use iso_fortran_env, only: x => character_kinds
end
%  gfcx -c a.f90
a.f90:1:17:

    1 | subroutine sub (x)
      |                 1
    2 |    use iso_fortran_env, only: x => character_kinds
      |                               2
Error: Symbol 'x' at (1) conflicts with the rename symbol at (2)

OK to commit?

2019-08-15  Steven G. Kargl  <ka...@gcc.gnu.org>

 PR fortran/82992
 * module.c (gfc_match_use):  When renaming a module entity, search
 current namespace for conflicting symbol.

2019-08-15  Steven G. Kargl  <ka...@gcc.gnu.org>

 PR fortran/82992
 * gfortran.dg/pr71649.f90: Adjust error messages.
 * gfortran.dg/use_15.f90: Ditto.
 * gfortran.dg/use_rename_8.f90: Ditto.

-- 
Steve
Index: gcc/fortran/module.c
===================================================================
--- gcc/fortran/module.c	(revision 274495)
+++ gcc/fortran/module.c	(working copy)
@@ -525,6 +525,8 @@ gfc_match_use (void)
   gfc_intrinsic_op op;
   match m;
   gfc_use_list *use_list;
+  gfc_symtree *st;
+  locus loc;
 
   use_list = gfc_get_use_list ();
 
@@ -632,6 +634,8 @@ gfc_match_use (void)
 	case INTERFACE_USER_OP:
 	case INTERFACE_GENERIC:
 	case INTERFACE_DTIO:
+	  loc = gfc_current_locus;
+
 	  m = gfc_match (" =>");
 
 	  if (type == INTERFACE_USER_OP && m == MATCH_YES
@@ -641,6 +645,14 @@ gfc_match_use (void)
 
 	  if (type == INTERFACE_USER_OP)
 	    new_use->op = INTRINSIC_USER;
+
+	  st = gfc_find_symtree (gfc_current_ns->sym_root, name);
+	  if (st)
+	    {
+	      gfc_error ("Symbol %qs at %L conflicts with the rename symbol "
+			 "at %L", name, &st->n.sym->declared_at, &loc);
+	      goto cleanup;
+	    }
 
 	  if (use_list->only_flag)
 	    {
Index: gcc/testsuite/gfortran.dg/pr71649.f90
===================================================================
--- gcc/testsuite/gfortran.dg/pr71649.f90	(revision 274495)
+++ gcc/testsuite/gfortran.dg/pr71649.f90	(working copy)
@@ -1,13 +1,13 @@
 ! { dg-do compile }
 ! PR71649 Internal Compiler Error
-SUBROUTINE Compiler_Options ( Options, Version, WriteOpt )
-   USE ISO_FORTRAN_ENV, ONLY : Compiler_Version, Compiler_Options ! { dg-error "already declared" }
+SUBROUTINE Compiler_Options ( Options, Version, WriteOpt )        ! { dg-error "\(1\)" }
+   USE ISO_FORTRAN_ENV, ONLY : Compiler_Version, Compiler_Options ! { dg-error "conflicts with the rename" }
    IMPLICIT NONE
    CHARACTER (LEN=*), INTENT(OUT) :: Options
    CHARACTER (LEN=*), INTENT(OUT) :: Version
    LOGICAL, INTENT(IN), OPTIONAL  :: WriteOpt
-   Version = Compiler_Version()
-   Options = Compiler_Options() ! { dg-error "Unexpected use of subroutine name" }
+   Version = Compiler_Version()  ! { dg-error "has no IMPLICIT type" }
+   Options = Compiler_Options()  ! { dg-error "Unexpected use of subroutine name" }
    RETURN
 END SUBROUTINE Compiler_Options
 
Index: gcc/testsuite/gfortran.dg/use_15.f90
===================================================================
--- gcc/testsuite/gfortran.dg/use_15.f90	(revision 274495)
+++ gcc/testsuite/gfortran.dg/use_15.f90	(working copy)
@@ -28,8 +28,8 @@ subroutine my_sub2 (a)
 end subroutine
 
 
-subroutine my_sub3 (a)
-  use test_mod2, my_sub3 => my_sub2  ! { dg-error "is also the name of the current program unit" }
+subroutine my_sub3 (a)              ! { dg-error "\(1\)" }
+  use test_mod2, my_sub3 => my_sub2 ! { dg-error "conflicts with the rename" }
   real a
   print *, a
 end subroutine
Index: gcc/testsuite/gfortran.dg/use_rename_8.f90
===================================================================
--- gcc/testsuite/gfortran.dg/use_rename_8.f90	(revision 274495)
+++ gcc/testsuite/gfortran.dg/use_rename_8.f90	(working copy)
@@ -19,8 +19,8 @@ SUBROUTINE T
     USE MOO, ONLY: X => B
 END SUBROUTINE T
 
-SUBROUTINE C
-    USE MOO, ONLY: C  ! { dg-error "is also the name of the current program unit" }
+SUBROUTINE C          ! { dg-error "\(1\)" }
+    USE MOO, ONLY: C  ! { dg-error "conflicts with the rename" }
 END SUBROUTINE C
 
 SUBROUTINE D
@@ -36,15 +36,15 @@ SUBROUTINE F
     USE MOO, ONLY: X => F
 END SUBROUTINE F
 
-SUBROUTINE X
-    USE MOO, ONLY: X => G ! { dg-error "is also the name of the current program unit" }
+SUBROUTINE X              ! { dg-error "\(1\)" }
+    USE MOO, ONLY: X => G ! { dg-error "conflicts with the rename" }
 END SUBROUTINE X
 
-SUBROUTINE Y
-    USE MOO, ONLY: Y => H ! { dg-error "is also the name of the current program unit" }
+SUBROUTINE Y              ! { dg-error "\(1\)" }
+    USE MOO, ONLY: Y => H ! { dg-error "conflicts with the rename" }
 END SUBROUTINE Y
 
-SUBROUTINE Z
-    USE MOO, ONLY: Z => I, Z => I ! { dg-error "is also the name of the current program unit" }
+SUBROUTINE Z                        ! { dg-error "\(1\)" }
+    USE MOO, ONLY: Z => I, Z => I   ! { dg-error "conflicts with the rename" }
 END SUBROUTINE Z
 

Reply via email to