Message ID | 20210204231000.1099414-1-polacek@redhat.com |
---|---|
State | New |
Headers | show |
Series | c++: Fix ICE with invalid using enum [PR96462] | expand |
On 2/4/21 6:10 PM, Marek Polacek wrote: > Here we ICE in finish_nonmember_using_decl -> lookup_using_decl -> > ... -> find_namespace_slot because "name" is not an IDENTIFIER_NODE. > It is a BIT_NOT_EXPR because this broken test uses > > using E::~E; // SCOPE::NAME > > A using-decl can't refer to a destructor, and lookup_using_decl already > checks that in the class member case. But in C++17, we do the "enum > scope is the enclosing scope" block, and so scope gets set to ::, and > we go into the NAMESPACE_DECL block. In C++20 we don't do it, we go > to the ENUMERAL_TYPE block. > > I resorted to hoisting the check along with a diagnostic tweak: we > don't want to print "::::~E names destructor". > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK. > gcc/cp/ChangeLog: > > PR c++/96462 > * name-lookup.c (lookup_using_decl): Hoist the destructor check. > > gcc/testsuite/ChangeLog: > > PR c++/96462 > * g++.dg/cpp2a/using-enum-8.C: New test. > --- > gcc/cp/name-lookup.c | 15 ++++++++------- > gcc/testsuite/g++.dg/cpp2a/using-enum-8.C | 5 +++++ > 2 files changed, 13 insertions(+), 7 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/using-enum-8.C > > diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c > index 52e4a630e25..1f4a7ac1d0c 100644 > --- a/gcc/cp/name-lookup.c > +++ b/gcc/cp/name-lookup.c > @@ -5700,6 +5700,14 @@ lookup_using_decl (tree scope, name_lookup &lookup) > scope = ctx; > } > > + /* You cannot using-decl a destructor. */ > + if (TREE_CODE (lookup.name) == BIT_NOT_EXPR) > + { > + error ("%<%T%s%D%> names destructor", scope, > + &"::"[scope == global_namespace ? 2 : 0], lookup.name); > + return NULL_TREE; > + } > + > if (TREE_CODE (scope) == NAMESPACE_DECL) > { > /* Naming a namespace member. */ > @@ -5739,13 +5747,6 @@ lookup_using_decl (tree scope, name_lookup &lookup) > return NULL_TREE; > } > > - /* You cannot using-decl a destructor. */ > - if (TREE_CODE (lookup.name) == BIT_NOT_EXPR) > - { > - error ("%<%T::%D%> names destructor", scope, lookup.name); > - return NULL_TREE; > - } > - > /* Using T::T declares inheriting ctors, even if T is a typedef. */ > if (lookup.name == TYPE_IDENTIFIER (npscope) > || constructor_name_p (lookup.name, npscope)) > diff --git a/gcc/testsuite/g++.dg/cpp2a/using-enum-8.C b/gcc/testsuite/g++.dg/cpp2a/using-enum-8.C > new file mode 100644 > index 00000000000..9a743a98399 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/using-enum-8.C > @@ -0,0 +1,5 @@ > +// PR c++/96462 > +// { dg-do compile { target c++11 } } > + > +enum E {}; > +using E::~E; // { dg-error "names destructor" } > > base-commit: ce5720447c69286599b96bae53ae854b1bbe41fa >
diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index 52e4a630e25..1f4a7ac1d0c 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -5700,6 +5700,14 @@ lookup_using_decl (tree scope, name_lookup &lookup) scope = ctx; } + /* You cannot using-decl a destructor. */ + if (TREE_CODE (lookup.name) == BIT_NOT_EXPR) + { + error ("%<%T%s%D%> names destructor", scope, + &"::"[scope == global_namespace ? 2 : 0], lookup.name); + return NULL_TREE; + } + if (TREE_CODE (scope) == NAMESPACE_DECL) { /* Naming a namespace member. */ @@ -5739,13 +5747,6 @@ lookup_using_decl (tree scope, name_lookup &lookup) return NULL_TREE; } - /* You cannot using-decl a destructor. */ - if (TREE_CODE (lookup.name) == BIT_NOT_EXPR) - { - error ("%<%T::%D%> names destructor", scope, lookup.name); - return NULL_TREE; - } - /* Using T::T declares inheriting ctors, even if T is a typedef. */ if (lookup.name == TYPE_IDENTIFIER (npscope) || constructor_name_p (lookup.name, npscope)) diff --git a/gcc/testsuite/g++.dg/cpp2a/using-enum-8.C b/gcc/testsuite/g++.dg/cpp2a/using-enum-8.C new file mode 100644 index 00000000000..9a743a98399 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/using-enum-8.C @@ -0,0 +1,5 @@ +// PR c++/96462 +// { dg-do compile { target c++11 } } + +enum E {}; +using E::~E; // { dg-error "names destructor" }