Bug 71147 - [6 Regression] Flexible array member wrongly rejected in template
Summary: [6 Regression] Flexible array member wrongly rejected in template
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.1.1
: P3 normal
Target Milestone: 6.2
Assignee: Martin Sebor
URL:
Keywords: rejects-valid
Depends on:
Blocks: flexmembers
  Show dependency treegraph
 
Reported: 2016-05-16 12:00 UTC by Avi Kivity
Modified: 2016-07-21 15:29 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work: 5.3.0
Known to fail: 6.1.0
Last reconfirmed: 2016-05-18 00:00:00


Attachments
Reproducer (197 bytes, text/plain)
2016-05-16 12:00 UTC, Avi Kivity
Details
Slightly simpler reproducer (no std::atomic) (177 bytes, text/plain)
2016-05-16 12:06 UTC, Avi Kivity
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Avi Kivity 2016-05-16 12:00:52 UTC
Created attachment 38497 [details]
Reproducer

gcc (GCC) 6.1.1 20160510 (Red Hat 6.1.1-2)

The attached code does not compile under g++ 6.  It works fine under 5.3 (with -std=gnu++14).

Removing "template <typename T>, or converting the array to be an array of unsigned (instead of array of struct) hides the problem.
Comment 1 Avi Kivity 2016-05-16 12:06:06 UTC
Created attachment 38498 [details]
Slightly simpler reproducer (no std::atomic)
Comment 2 Jakub Jelinek 2016-05-18 08:00:10 UTC
I believe this started with r233126.
Comment 3 Martin Sebor 2016-05-18 15:48:40 UTC
There is code to handle this case in instantiate_class_template_1 but it's not quite correct.  It assumes that the the array element type has been completed which in this case isn't a safe assumption.  Let me fix this.

A slightly simpler test case is below:

$ cat xx.cpp && gcc -S -Wall -Wextra -Wpedantic xx.cpp
template <typename>
struct container
{
  struct elem {
    unsigned u;
  };
  struct incomplete {
    int x;
    elem array[];
  };
};

unsigned f (container<void>::incomplete* i)
{
  return i->array [0].u;
}
xx.cpp: In instantiation of ‘struct container<void>::incomplete’:
xx.cpp:15:11:   required from here
xx.cpp:9:16: error: ‘container< <template-parameter-1-1> >::incomplete::array’ has incomplete type
     elem array[];
                ^
xx.cpp:9:16: error: invalid use of array with unspecified bounds
Comment 4 Martin Sebor 2016-05-19 01:42:01 UTC
Patch posted for review:
https://gcc.gnu.org/ml/gcc-patches/2016-05/msg01427.html
Comment 5 Martin Sebor 2016-05-24 20:30:08 UTC
Author: msebor
Date: Tue May 24 20:29:36 2016
New Revision: 236664

URL: https://gcc.gnu.org/viewcvs?rev=236664&root=gcc&view=rev
Log:
PR c++/71147 - [6 Regression] Flexible array member wrongly rejected in template

gcc/ChangeLog:
2016-05-24  Martin Sebor  <msebor@redhat.com>

	PR c++/71147
	* gcc/tree.h (complete_or_array_type_p): New inline function.

gcc/testsuite/ChangeLog:
2016-05-24  Martin Sebor  <msebor@redhat.com>

	PR c++/71147
	* g++.dg/ext/flexary16.C: New test.

gcc/cp/ChangeLog:
2016-05-24  Martin Sebor  <msebor@redhat.com>

	PR c++/71147
	* decl.c (layout_var_decl, grokdeclarator): Use complete_or_array_type_p.
	* pt.c (instantiate_class_template_1): Try to complete the element
	type of a flexible array member.
	(can_complete_type_without_circularity): Handle arrays of unknown bound.
	* typeck.c (complete_type): Also complete the type of the elements of
	arrays with an unspecified bound.

Added:
    trunk/gcc/testsuite/g++.dg/ext/flexary16.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree.h
Comment 6 Martin Sebor 2016-05-25 16:01:56 UTC
Author: msebor
Date: Wed May 25 16:01:24 2016
New Revision: 236729

URL: https://gcc.gnu.org/viewcvs?rev=236729&root=gcc&view=rev
Log:
PR c++/71147 - [6 Regression] Flexible array member wrongly rejected in template

gcc/ChangeLog:
2016-05-24  Martin Sebor  <msebor@redhat.com>

	PR c++/71147
	* gcc/tree.h (complete_or_array_type_p): New inline function.

gcc/testsuite/ChangeLog:
2016-05-24  Martin Sebor  <msebor@redhat.com>

	PR c++/71147
	* g++.dg/ext/flexary16.C: New test.

gcc/cp/ChangeLog:
2016-05-24  Martin Sebor  <msebor@redhat.com>

	PR c++/71147
	* decl.c (layout_var_decl, grokdeclarator): Use complete_or_array_type_p.
	* pt.c (instantiate_class_template_1): Try to complete the element
	type of a flexible array member.
	(can_complete_type_without_circularity): Handle arrays of unknown bound.
	* typeck.c (complete_type): Also complete the type of the elements of
	arrays with an unspecified bound.

Added:
    branches/gcc-6-branch/gcc/testsuite/g++.dg/ext/flexary16.C
Modified:
    branches/gcc-6-branch/gcc/ChangeLog
    branches/gcc-6-branch/gcc/cp/ChangeLog
    branches/gcc-6-branch/gcc/cp/decl.c
    branches/gcc-6-branch/gcc/cp/pt.c
    branches/gcc-6-branch/gcc/cp/typeck.c
    branches/gcc-6-branch/gcc/testsuite/ChangeLog
    branches/gcc-6-branch/gcc/tree.h
Comment 7 Martin Sebor 2016-05-25 16:03:58 UTC
Fixed on trunk (r236664) and 6-branch (r236729).
Comment 8 Paul Wankadia 2016-07-21 15:02:33 UTC
(In reply to Martin Sebor from comment #7)
> Fixed on trunk (r236664) and 6-branch (r236729).

https://github.com/google/re2/issues/102 reported this for g++ (GCC) 6.1.1 20160707. The 6-20160707 snapshot was taken at gcc-6-branch revision 238150, so I guess that it would have included your fix. Is it possible that the bug is not completely fixed yet? (My apologies in advance if I'm barking up the wrong tree!)

P.S. Thank you for redirecting me here from bug 70932. :)
Comment 9 Martin Sebor 2016-07-21 15:29:42 UTC
The test case from comment 4 on bug 70932 fails due to this bug.  But looking at the error in comment 3 more closely I see "unbekannte Feldgröße in »delete«" which with some help from Google Translate does look like the same error as in bug 70932.  Sorry for the confusion!