diff options
author | Marek Polacek <polacek@redhat.com> | 2024-08-30 14:12:22 -0400 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2024-09-05 11:07:03 -0400 |
commit | d44cae2d9310660e3e47f15202e86e4f73f15b37 (patch) | |
tree | 8a73f52b31f6e1a839559e3b44e46e9cd9d8c014 /gcc/cp | |
parent | ae88e91938af364ef5613e5461b12b484b578bc5 (diff) | |
download | gcc-d44cae2d9310660e3e47f15202e86e4f73f15b37.zip gcc-d44cae2d9310660e3e47f15202e86e4f73f15b37.tar.gz gcc-d44cae2d9310660e3e47f15202e86e4f73f15b37.tar.bz2 |
c++: fn redecl in fn scope wrongly accepted [PR116239]
Redeclaration such as
void f(void);
consteval void f(void);
is invalid. In a namespace scope, we detect the collision in
validate_constexpr_redeclaration, but not when one declaration is
at block scope.
When we have
void f(void);
void g() { consteval void f(void); }
we call pushdecl on the second f and call push_local_extern_decl_alias.
It finds the namespace-scope f:
for (ovl_iterator iter (binding); iter; ++iter)
if (decls_match (decl, *iter, /*record_versions*/false))
{
alias = *iter;
break;
}
but decls_match says they match so we just set DECL_LOCAL_DECL_ALIAS
(and do not call another pushdecl leading to duplicate_decls which
would detect mismatching return types, for example). I don't think
we want to change decls_match, so a simple fix is to detect the
problem in push_local_extern_decl_alias.
PR c++/116239
gcc/cp/ChangeLog:
* cp-tree.h (validate_constexpr_redeclaration): Declare.
* decl.cc (validate_constexpr_redeclaration): No longer static.
* name-lookup.cc (push_local_extern_decl_alias): Call
validate_constexpr_redeclaration.
gcc/testsuite/ChangeLog:
* g++.dg/diagnostic/redeclaration-6.C: New test.
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/cp-tree.h | 1 | ||||
-rw-r--r-- | gcc/cp/decl.cc | 2 | ||||
-rw-r--r-- | gcc/cp/name-lookup.cc | 2 |
3 files changed, 4 insertions, 1 deletions
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 2eeb5e3..1a763b6 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6992,6 +6992,7 @@ extern bool member_like_constrained_friend_p (tree); extern bool fns_correspond (tree, tree); extern int decls_match (tree, tree, bool = true); extern bool maybe_version_functions (tree, tree, bool); +extern bool validate_constexpr_redeclaration (tree, tree); extern bool merge_default_template_args (tree, tree, bool); extern tree duplicate_decls (tree, tree, bool hiding = false, diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 7bad304..f4128db 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -1412,7 +1412,7 @@ check_redeclaration_exception_specification (tree new_decl, /* Return true if OLD_DECL and NEW_DECL agree on constexprness. Otherwise issue diagnostics. */ -static bool +bool validate_constexpr_redeclaration (tree old_decl, tree new_decl) { old_decl = STRIP_TEMPLATE (old_decl); diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 7a6cc24..cd3947c 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -3637,6 +3637,8 @@ push_local_extern_decl_alias (tree decl) if (decls_match (decl, *iter, /*record_versions*/false)) { alias = *iter; + if (!validate_constexpr_redeclaration (alias, decl)) + return; break; } |