From fbe83e6b4d0308f547ab11de6dc0eda2741cc11f Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Wed, 6 Feb 2019 01:51:29 +0000 Subject: [PATCH] Fix type of extern array declared in inner scope with outer initialization shadowed (PR c/88584). As reported in bug 88584, if you have a file-scope array with external linkage, initialized at file scope, and that array is shadowed at block scope, and is declared again with external linkage and an incomplete type in an inner scope, it is wrongly given a complete type in that inner scope when the correct C semantics give it an incomplete type (only the visible declarations contribute to the type in a given scope). In general, issues with the types of external linkage declarations being different in different scopes were addressed by my fixes for bug 13801, for GCC 4.0. In this case, however, the code in pushdecl dealing with giving declarations the right type in each scope works fine, and the type is subsequently modified by complete_array_type called from finish_decl: finish_decl is trying to complete an array type based on an initializer, but that's only correct for the original initialization at file scope, not for such a declaration in an inner scope (it's harmless but unnecessary in the case where the original declaration is still visible in the inner scope). Thus, this patch changes finish_decl to stop this logic applying for such an external declaration in an inner scope. (An erroneous attempt to include an initializer for an extern variable in an inner scope is diagnosed elsewhere.) This is a regression from GCC 3.4, which properly rejected the code in question (quite likely by accident). Bootstrapped with no regressions on x86_64-pc-linux-gnu. gcc/c: PR c/88584 * c-decl.c (finish_decl): Do not complete array types for arrays with external linkage not at file scope. gcc/testsuite: PR c/88584 * gcc.dg/redecl-18.c: New test. From-SVN: r268571 --- gcc/c/ChangeLog | 6 ++++++ gcc/c/c-decl.c | 9 +++++++-- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/redecl-18.c | 17 +++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/redecl-18.c diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 1450d65601d..9bb8351cc2c 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,9 @@ +2019-02-06 Joseph Myers + + PR c/88584 + * c-decl.c (finish_decl): Do not complete array types for arrays + with external linkage not at file scope. + 2019-02-05 Richard Biener PR c/88606 diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 65aee4d3e04..b658eb155bf 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -5099,10 +5099,15 @@ finish_decl (tree decl, location_t init_loc, tree init, type = TREE_TYPE (decl); - /* Deduce size of array from initialization, if not already known. */ + /* Deduce size of array from initialization, if not already known. + This is only needed for an initialization in the current scope; + it must not be done for a file-scope initialization of a + declaration with external linkage, redeclared in an inner scope + with the outer declaration shadowed in an intermediate scope. */ if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE - && TREE_CODE (decl) != TYPE_DECL) + && TREE_CODE (decl) != TYPE_DECL + && !(TREE_PUBLIC (decl) && current_scope != file_scope)) { bool do_default = (TREE_STATIC (decl) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index ac70c728139..2c6d0e47b5c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-02-06 Joseph Myers + + PR c/88584 + * gcc.dg/redecl-18.c: New test. + 2019-02-05 Jakub Jelinek PR c++/89187 diff --git a/gcc/testsuite/gcc.dg/redecl-18.c b/gcc/testsuite/gcc.dg/redecl-18.c new file mode 100644 index 00000000000..f96bbe750f9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/redecl-18.c @@ -0,0 +1,17 @@ +/* Test redeclaration in an inner scope, with an incomplete type, of a + file-scope initialized array shadowed in an intermediate scope (bug + 88584). */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +int a[1] = { 0 }; + +void +f (void) +{ + int a; + { + extern int a[]; + sizeof (a); /* { dg-error "incomplete" } */ + } +} -- 2.30.2