Bug 95614 - ICE in build_field, at fortran/trans-common.c:301
Summary: ICE in build_field, at fortran/trans-common.c:301
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 11.0
: P4 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2020-06-09 17:35 UTC by G. Steinmetz
Modified: 2020-10-14 13:15 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-06-09 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description G. Steinmetz 2020-06-09 17:35:45 UTC
A name clash affects versions down to at least r5 :


$ cat z1.f90
module m
common m
end


$ cat z2.f90
module m
common /xc/ m
end


$ cat z0.f90
module m
integer m
end


$ gfortran-11-20200607 -c z0.f90
z0.f90:2:9:

    2 | integer m
      |         1
Error: Symbol 'm' at (1) cannot have a type


$ gfortran-11-20200607 -c z1.f90
f951: internal compiler error: Segmentation fault
0xd5a35f crash_signal
        ../../gcc/toplev.c:328
0x9c8ecb contains_struct_check(tree_node*, tree_node_structure_enum, char const*, int, char const*)
        ../../gcc/tree.h:3409
0x9c8ecb size_binop_loc(unsigned int, tree_code, tree_node*, tree_node*)
        ../../gcc/fold-const.c:1906
0x763a2a build_field
        ../../gcc/fortran/trans-common.c:301
0x763a2a create_common
        ../../gcc/fortran/trans-common.c:646
0x765cce translate_common
        ../../gcc/fortran/trans-common.c:1265
0x765e86 gfc_trans_common(gfc_namespace*)
        ../../gcc/fortran/trans-common.c:1356
0x778c27 gfc_generate_module_vars(gfc_namespace*)
        ../../gcc/fortran/trans-decl.c:5796
0x746891 gfc_generate_module_code(gfc_namespace*)
        ../../gcc/fortran/trans.c:2238
0x6f1141 translate_all_program_units
        ../../gcc/fortran/parse.c:6293
0x6f1141 gfc_parse_file()
        ../../gcc/fortran/parse.c:6545
0x73daef gfc_be_parse_file
        ../../gcc/fortran/f95-lang.c:212
Comment 1 Dominique d'Humieres 2020-06-09 17:44:33 UTC
Confirmed.
Comment 2 kargls 2020-06-09 21:41:45 UTC
Index: gcc/fortran/decl.c
===================================================================
--- gcc/fortran/decl.c	(revision 280157)
+++ gcc/fortran/decl.c	(working copy)
@@ -5924,7 +5924,7 @@ get_bind_c_idents (void)
       found_id = MATCH_YES;
       gfc_get_ha_symbol (name, &tmp_sym);
     }
-  else if (match_common_name (name) == MATCH_YES)
+  else if (gfc_match_common_name (name) == MATCH_YES)
     {
       found_id = MATCH_YES;
       com_block = gfc_get_common (name, 0);
@@ -5969,7 +5969,7 @@ get_bind_c_idents (void)
 	      found_id = MATCH_YES;
 	      gfc_get_ha_symbol (name, &tmp_sym);
 	    }
-	  else if (match_common_name (name) == MATCH_YES)
+	  else if (gfc_match_common_name (name) == MATCH_YES)
 	    {
 	      found_id = MATCH_YES;
 	      com_block = gfc_get_common (name, 0);
 
Index: gcc/fortran/match.c
===================================================================
--- gcc/fortran/match.c	(revision 280157)
+++ gcc/fortran/match.c	(working copy)
@@ -5122,7 +5122,8 @@ gfc_get_common (const char *name, int from_module)
 
 /* Match a common block name.  */
 
-match match_common_name (char *name)
+match
+gfc_match_common_name (char *name)
 {
   match m;
 
@@ -5174,7 +5175,7 @@ gfc_match_common (void)
 
   for (;;)
     {
-      m = match_common_name (name);
+      m = gfc_match_common_name (name);
       if (m == MATCH_ERROR)
 	goto cleanup;
 
Index: gcc/fortran/match.h
===================================================================
--- gcc/fortran/match.h	(revision 280157)
+++ gcc/fortran/match.h	(working copy)
@@ -103,11 +103,8 @@ match gfc_match_call (void);
 
 /* We want to use this function to check for a common-block-name
    that can exist in a bind statement, so removed the "static"
-   declaration of the function in match.c.
- 
-   TODO: should probably rename this now that it'll be globally seen to
-   gfc_match_common_name.  */
-match match_common_name (char *name);
+   declaration of the function in match.c.  */
+match gfc_match_common_name (char *name);
 
 match gfc_match_common (void);
 match gfc_match_block_data (void);
Index: gcc/fortran/resolve.c
===================================================================
--- gcc/fortran/resolve.c	(revision 280157)
+++ gcc/fortran/resolve.c	(working copy)
@@ -936,9 +936,16 @@ static void
 resolve_common_vars (gfc_common_head *common_block, bool named_common)
 {
   gfc_symbol *csym = common_block->head;
+  gfc_gsymbol *gsym;
 
   for (; csym; csym = csym->common_next)
     {
+      gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
+      if (gsym && gsym->type != GSYM_UNKNOWN)
+	gfc_error_now ("Global entity %qs at %L cannot appear in a "
+			"COMMON block at %L", gsym->name,
+			&gsym->where, &csym->common_block->where);
+
       /* gfc_add_in_common may have been called before, but the reported errors
 	 have been ignored to continue parsing.
 	 We do the checks again here.  */
Comment 3 Ev Drikos 2020-06-22 09:10:25 UTC
Hello,

Perhaps, an additional check in file resolve.c might be necessary, or
one would have to adjust one-two "common*.f" test cases.

Hope this helps,
Ev. Drikos

----------------------------------------------------------------------

       if (gsym && gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
+      gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
+      if (gsym && gsym->type != GSYM_UNKNOWN)
+	gfc_error_now ("Global entity %qs at %L cannot appear in a "
+			"COMMON block at %L", gsym->name,
+			&gsym->where, &csym->common_block->where);
+
Comment 4 Steve Kargl 2020-06-22 15:53:20 UTC
On Mon, Jun 22, 2020 at 09:10:25AM +0000, drikosev at gmail dot com wrote:
> 
> --- Comment #3 from Ev Drikos <drikosev at gmail dot com> ---
> 
> Hello,
> 
> Perhaps, an additional check in file resolve.c might be necessary, or
> one would have to adjust one-two "common*.f" test cases.
> 
> Hope this helps,
> Ev. Drikos

Yes, indeed.  Some testcases may need to be updated to
account for the new error message or one may needs to
use the old testcase to adjust the conditional to not
trigger for the older testcase.  Also, note that I've
found invalid Fortran in testcases as I've fixed a 
bug.

When parsing a file, gfortran runs a series of matchers.
If a failure is detected in a matcher an error message
is queued, and then other matchers are run.  If no matcher
succeeds in matching a statement, the last queued error
message(s) is(are) emitted and gfortran will exit.  If a
matcher does match a statement, then the error queue is
cleared and the next statement is parsed.  Sometimes a
new patch will find invalid Fortran in the testsuite.
Comment 5 GCC Commits 2020-09-27 12:12:20 UTC
The master branch has been updated by Mark Eggleston <markeggleston@gcc.gnu.org>:

https://gcc.gnu.org/g:e5a76af3a2f3324efc60b4b2778ffb29d5c377bc

commit r11-3487-ge5a76af3a2f3324efc60b4b2778ffb29d5c377bc
Author: Mark Eggleston <markeggleston@gcc.gnu.org>
Date:   Thu Jun 11 14:33:51 2020 +0100

    Fortran  :  ICE in build_field PR95614
    
    Local identifiers can not be the same as a module name.  Original
    patch by Steve Kargl resulted in name clashes between common block
    names and local identifiers.  A local identifier can be the same as
    a global identier if that identifier represents a common.  The patch
    was modified to allow global identifiers that represent a common
    block.
    
    2020-09-27  Steven G. Kargl  <kargl@gcc.gnu.org>
                Mark Eggleston  <markeggleston@gcc.gnu.org>
    
    gcc/fortran/
    
            PR fortran/95614
            * decl.c (gfc_get_common): Use gfc_match_common_name instead
            of match_common_name.
            * decl.c (gfc_bind_idents): Use gfc_match_common_name instead
            of match_common_name.
            * match.c : Rename match_common_name to gfc_match_common_name.
            * match.c (gfc_match_common): Use gfc_match_common_name instead
            of match_common_name.
            * match.h : Rename match_common_name to gfc_match_common_name.
            * resolve.c (resolve_common_vars): Check each symbol in a
            common block has a global symbol.  If there is a global symbol
            issue an error if the symbol type is known as is not a common
            block name.
    
    2020-09-27  Mark Eggleston  <markeggleston@gcc.gnu.org>
    
    gcc/testsuite/
    
            PR fortran/95614
            * gfortran.dg/pr95614_1.f90: New test.
            * gfortran.dg/pr95614_2.f90: New test.
Comment 6 GCC Commits 2020-09-27 13:50:15 UTC
The releases/gcc-10 branch has been updated by Mark Eggleston <markeggleston@gcc.gnu.org>:

https://gcc.gnu.org/g:4a67941a956003dcce8866604ba25f5a0bfd16cf

commit r10-8803-g4a67941a956003dcce8866604ba25f5a0bfd16cf
Author: Mark Eggleston <markeggleston@gcc.gnu.org>
Date:   Thu Jun 11 14:33:51 2020 +0100

    Fortran  :  ICE in build_field PR95614
    
    Local identifiers can not be the same as a module name.  Original
    patch by Steve Kargl resulted in name clashes between common block
    names and local identifiers.  A local identifier can be the same as
    a global identier if that identifier represents a common.  The patch
    was modified to allow global identifiers that represent a common
    block.
    
    2020-09-27  Steven G. Kargl  <kargl@gcc.gnu.org>
                Mark Eggleston  <markeggleston@gcc.gnu.org>
    
    gcc/fortran/
    
            PR fortran/95614
            * decl.c (gfc_get_common): Use gfc_match_common_name instead
            of match_common_name.
            * decl.c (gfc_bind_idents): Use gfc_match_common_name instead
            of match_common_name.
            * match.c : Rename match_common_name to gfc_match_common_name.
            * match.c (gfc_match_common): Use gfc_match_common_name instead
            of match_common_name.
            * match.h : Rename match_common_name to gfc_match_common_name.
            * resolve.c (resolve_common_vars): Check each symbol in a
            common block has a global symbol.  If there is a global symbol
            issue an error if the symbol type is known as is not a common
            block name.
    
    2020-09-27  Mark Eggleston  <markeggleston@gcc.gnu.org>
    
    gcc/testsuite/
    
            PR fortran/95614
            * gfortran.dg/pr95614_1.f90: New test.
            * gfortran.dg/pr95614_2.f90: New test.
    
    (cherry picked from commit e5a76af3a2f3324efc60b4b2778ffb29d5c377bc)
Comment 7 GCC Commits 2020-09-27 16:30:39 UTC
The releases/gcc-9 branch has been updated by Mark Eggleston <markeggleston@gcc.gnu.org>:

https://gcc.gnu.org/g:7a8c40bca889785fc4664e5e11c430e31d297696

commit r9-8942-g7a8c40bca889785fc4664e5e11c430e31d297696
Author: Mark Eggleston <markeggleston@gcc.gnu.org>
Date:   Thu Jun 11 14:33:51 2020 +0100

    Fortran  :  ICE in build_field PR95614
    
    Local identifiers can not be the same as a module name.  Original
    patch by Steve Kargl resulted in name clashes between common block
    names and local identifiers.  A local identifier can be the same as
    a global identier if that identifier represents a common.  The patch
    was modified to allow global identifiers that represent a common
    block.
    
    2020-09-27  Steven G. Kargl  <kargl@gcc.gnu.org>
                Mark Eggleston  <markeggleston@gcc.gnu.org>
    
    gcc/fortran/
    
            PR fortran/95614
            * decl.c (gfc_get_common): Use gfc_match_common_name instead
            of match_common_name.
            * decl.c (gfc_bind_idents): Use gfc_match_common_name instead
            of match_common_name.
            * match.c : Rename match_common_name to gfc_match_common_name.
            * match.c (gfc_match_common): Use gfc_match_common_name instead
            of match_common_name.
            * match.h : Rename match_common_name to gfc_match_common_name.
            * resolve.c (resolve_common_vars): Check each symbol in a
            common block has a global symbol.  If there is a global symbol
            issue an error if the symbol type is known as is not a common
            block name.
    
    2020-09-27  Mark Eggleston  <markeggleston@gcc.gnu.org>
    
    gcc/testsuite/
    
            PR fortran/95614
            * gfortran.dg/pr95614_1.f90: New test.
            * gfortran.dg/pr95614_2.f90: New test.
    
    (cherry picked from commit e5a76af3a2f3324efc60b4b2778ffb29d5c377bc)
Comment 8 GCC Commits 2020-09-27 17:43:09 UTC
The releases/gcc-8 branch has been updated by Mark Eggleston <markeggleston@gcc.gnu.org>:

https://gcc.gnu.org/g:e28cc38ac34cb4de31b983f817c6e5f7dde55e2c

commit r8-10539-ge28cc38ac34cb4de31b983f817c6e5f7dde55e2c
Author: Mark Eggleston <markeggleston@gcc.gnu.org>
Date:   Thu Jun 11 14:33:51 2020 +0100

    Fortran  :  ICE in build_field PR95614
    
    Local identifiers can not be the same as a module name.  Original
    patch by Steve Kargl resulted in name clashes between common block
    names and local identifiers.  A local identifier can be the same as
    a global identier if that identifier represents a common.  The patch
    was modified to allow global identifiers that represent a common
    block.
    
    2020-09-27  Steven G. Kargl  <kargl@gcc.gnu.org>
                Mark Eggleston  <markeggleston@gcc.gnu.org>
    
    gcc/fortran/
    
            PR fortran/95614
            * decl.c (gfc_get_common): Use gfc_match_common_name instead
            of match_common_name.
            * decl.c (gfc_bind_idents): Use gfc_match_common_name instead
            of match_common_name.
            * match.c : Rename match_common_name to gfc_match_common_name.
            * match.c (gfc_match_common): Use gfc_match_common_name instead
            of match_common_name.
            * match.h : Rename match_common_name to gfc_match_common_name.
            * resolve.c (resolve_common_vars): Check each symbol in a
            common block has a global symbol.  If there is a global symbol
            issue an error if the symbol type is known as is not a common
            block name.
    
    2020-09-27  Mark Eggleston  <markeggleston@gcc.gnu.org>
    
    gcc/testsuite/
    
            PR fortran/95614
            * gfortran.dg/pr95614_1.f90: New test.
            * gfortran.dg/pr95614_2.f90: New test.
    
    (cherry picked from commit e5a76af3a2f3324efc60b4b2778ffb29d5c377bc)
Comment 9 markeggleston 2020-09-28 06:29:18 UTC
Committed to master and backported.
Comment 10 markeggleston 2020-09-28 10:14:01 UTC
See 97224
Comment 11 GCC Commits 2020-10-14 10:08:30 UTC
The master branch has been updated by Mark Eggleston <markeggleston@gcc.gnu.org>:

https://gcc.gnu.org/g:4d2a56a0f7135469587feacef44cf00e08f71d09

commit r11-3866-g4d2a56a0f7135469587feacef44cf00e08f71d09
Author: Mark Eggleston <markeggleston@gcc.gnu.org>
Date:   Thu Jun 11 14:33:51 2020 +0100

    Fortran  :  ICE in build_field PR95614
    
    Local identifiers can not be the same as a module name.  Original
    patch by Steve Kargl resulted in name clashes between common block
    names and local identifiers.  A local identifier can be the same as
    a global identier if that identifier is not a module or a program.
    The original patch was modified to reject global identifiers that
    represent a module or a program.
    
    2020-10-14  Steven G. Kargl  <kargl@gcc.gnu.org>
                Mark Eggleston  <markeggleston@gcc.gnu.org>
    
    gcc/fortran/ChangeLog:
    
            PR fortran/95614
            * decl.c (gfc_get_common): Use gfc_match_common_name instead
            of match_common_name.
            * decl.c (gfc_bind_idents): Use gfc_match_common_name instead
            of match_common_name.
            * match.c : Rename match_common_name to gfc_match_common_name.
            * match.c (gfc_match_common): Use gfc_match_common_name instead
            of match_common_name.
            * match.h : Rename match_common_name to gfc_match_common_name.
            * resolve.c (resolve_common_vars): Check each symbol in a
            common block has a global symbol.  If there is a global symbol
            issue an error if the symbol type is a module or a program.
    
    2020-10-14  Mark Eggleston  <markeggleston@gcc.gnu.org>
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/95614
            * gfortran.dg/pr95614_1.f90: New test.
            * gfortran.dg/pr95614_2.f90: New test.
            * gfortran.dg/pr95614_3.f90: New test.
            * gfortran.dg/pr95614_4.f90: New test.
Comment 12 markeggleston 2020-10-14 13:15:43 UTC
Committed to master.