aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/pt.cc
AgeCommit message (Collapse)AuthorFilesLines
29 hoursc++: bogus error with union in qualified name [PR83469]Marek Polacek1-8/+17
While working on Reflection I noticed that we reject: union U { int i; }; constexpr auto r = ^^typename ::U; which is due to PR83469. Andrew P. posted a patch in 2021: https://gcc.gnu.org/pipermail/gcc-patches/2021-December/586344.html for which I had some comments but an updated patch never came. ~~ There are a few issues here with typenames and unions (and even struct keywords with unions). First in cp_parser_check_class_key, we need to allow typenames to name union types and union key to be able to use with typenames. The next issue is we need to record if we had a union key, right now we just record it was a struct/class/typename one which is wrong. ~~ This patch is an updated and cleaned up version; I've also addressed a missing bit in pt.cc. PR c++/83469 PR c++/93809 gcc/cp/ChangeLog: * cp-tree.h (UNION_TYPE_P): Define. (TYPENAME_IS_UNION_P): Define. * decl.cc (struct typename_info): Add union_p field. (struct typename_hasher::equal): Compare union_p field. (build_typename_type): Use ti.union_p for union_type. Set TYPENAME_IS_UNION_P. * error.cc (dump_type) <case TYPENAME_TYPE>: Handle TYPENAME_IS_UNION_P. * module.cc (trees_out::type_node): Likewise. * parser.cc (cp_parser_check_class_key): Allow typename key for union types and allow union keyword for typename types. * pt.cc (tsubst) <case TYPENAME_TYPE>: Don't conflate unions with class_type. For TYPENAME_IS_CLASS_P, check NON_UNION_CLASS_TYPE_P rather than CLASS_TYPE_P. Add TYPENAME_IS_UNION_P handling. gcc/testsuite/ChangeLog: * g++.dg/template/error45.C: Adjust dg-error. * g++.dg/warn/Wredundant-tags-3.C: Remove xfail. * g++.dg/parse/union1.C: New test. * g++.dg/parse/union2.C: New test. * g++.dg/parse/union3.C: New test. * g++.dg/parse/union4.C: New test. * g++.dg/parse/union5.C: New test. * g++.dg/parse/union6.C: New test. Co-authored-by: Andrew Pinski <quic_apinski@quicinc.com> Reviewed-by: Jason Merrill <jason@redhat.com>
11 daysc++/modules: Ensure type of partial spec VAR_DECL is consistent with its ↵Nathaniel Shead1-0/+4
template [PR120644] We were erroring because the TEMPLATE_DECL of the existing partial specialisation has an undeduced return type, but the imported declaration did not. The root cause is similar to what was fixed in r13-2744-g4fac53d6522189, where modules streaming code assumes that a TEMPLATE_DECL and its DECL_TEMPLATE_RESULT will always have the same TREE_TYPE. That commit fixed the issue by ensuring that when the type of a variable is deduced the TEMPLATE_DECL is updated as well, but missed handling partial specialisations. This patch ensures that the same adjustment is made there as well. PR c++/120644 gcc/cp/ChangeLog: * decl.cc (cp_finish_decl): Also propagate type to partial templates. * module.cc (trees_out::decl_value): Add assertion that the TREE_TYPE of a streamed template decl matches its inner. (trees_in::is_matching_decl): Clarify function return type deduction should only occur for non-TEMPLATE_DECL. * pt.cc (template_for_substitution): Handle partial specs. gcc/testsuite/ChangeLog: * g++.dg/modules/auto-7.h: New test. * g++.dg/modules/auto-7_a.H: New test. * g++.dg/modules/auto-7_b.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Jason Merrill <jason@redhat.com> Reviewed-by: Patrick Palka <ppalka@redhat.com>
12 daysc++: fix ICE with [[deprecated]] [PR120756]Marek Polacek1-1/+2
Here we end up with "error reporting routines re-entered" because resolve_nondeduced_context isn't passing complain to mark_used. PR c++/120756 gcc/cp/ChangeLog: * pt.cc (resolve_nondeduced_context): Pass complain to mark_used. gcc/testsuite/ChangeLog: * g++.dg/warn/deprecated-22.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-06-16c++: add -Wsfinae-incompleteJason Merrill1-1/+0
We already error about a type or function definition causing a concept check to change value, but it would be useful to diagnose this for other SFINAE contexts as well; the memoization problem also affects templates. So -Wsfinae-incomplete remembers if we've failed a requirement for a complete type/deduced return type in a non-tf_error context, and later warns if the type/function becomes complete. This warning is enabled by default; I think the signal-to-noise ratio is high enough to warrant that, and it catches things that are likely to make the program "ill-formed, no diagnostic required". friend87.C is an interesting case; this could be considered a false positive because it is using friend injection to define the auto function to implement a compile-time counter. I think this is sufficiently pathological that it's fine to expect people who want to play this sort of game to suppress the warning. The data for this warning uses GTY((cache)) to persist through GC, but allow entries to be discarded if the key is not otherwise marked. I don't think it's desirable to export/import this information in modules, it makes sense for it to be local to a single TU. -Wsfinae-incomplete=2 adds a warning at the point of failure, which is primarily intended to help with debugging warnings from the default mode. gcc/ChangeLog: * doc/invoke.texi: Document -Wsfinae-incomplete. gcc/c-family/ChangeLog: * c.opt: Add -Wsfinae-incomplete. * c.opt.urls: Regenerate. gcc/cp/ChangeLog: * constraint.cc (failed_completions_map): New. (note_failed_type_completion): Rename from note_failed_type_completion_for_satisfaction. Add -Wsfinae-incomplete handling. (failed_completion_location): New. * class.cc (finish_struct_1): Add -Wsfinae-incomplete warning. * decl.cc (require_deduced_type): Adjust. (finish_function): Add -Wsfinae-incomplete warning. * typeck.cc (complete_type_or_maybe_complain): Adjust. (cxx_sizeof_or_alignof_type): Call note_failed_type_completion. * pt.cc (dependent_template_arg_p): No longer static. * cp-tree.h: Adjust. libstdc++-v3/ChangeLog: * testsuite/20_util/is_complete_or_unbounded/memoization.cc * testsuite/20_util/is_complete_or_unbounded/memoization_neg.cc: Expect -Wsfinae-incomplete. gcc/testsuite/ChangeLog: * g++.dg/template/friend87.C * g++.dg/cpp2a/concepts-complete1.C * g++.dg/cpp2a/concepts-complete2.C * g++.dg/cpp2a/concepts-complete3.C * g++.dg/cpp2a/concepts-complete4.C: Expect -Wsfinae-incomplete.
2025-06-09c++: Fix template class lookup [PR120495, PR115605].Iain Sandoe1-11/+19
In the reported issues, using lookup_template_class () directly on (for example) the coroutine_handle identifier fails because a class-local TYPE_DECL is found. This is because, in the existing code, lookup is called with default parameters which means that class contexts are examined first. Fix this, when a context is provided by the caller, by doing lookup in namespace provided. PR c++/120495 PR c++/115605 gcc/cp/ChangeLog: * pt.cc (lookup_template_class): Honour provided namespace contexts when looking up class templates. gcc/testsuite/ChangeLog: * g++.dg/coroutines/pr120495.C: New test. * g++.dg/pr115605.C: New test. Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
2025-06-05c++: substituting fn parm redeclared with dep alias tmpl [PR120224]Patrick Palka1-2/+12
Here we declare f twice, the second time around using a dependent alias template. Due to alias template transparency these are logically the same overload. But now the function type of f (produced from the first declaration) diverges from the type of its formal parameter (produced from the subsequent redefinition) in that substituting T=int succeeds for the function type but not for the formal parameter type. This eventually causes us to produce an undiagnosed error_mark_node in the AST of the function call, leading to failure of the sanity check check added in r14-6343-g0c018a74eb1aff. Before r14-6343 we would still go on to reject the testcase later at instantiation time, from regenerate_decl_from_template, making this a regression. To fix this, it seems we just need to propagate error_mark_node upon substitution failure into the type of a PARM_DECL. PR c++/120224 gcc/cp/ChangeLog: * pt.cc (tsubst_function_decl): Return error_mark_node if substituting into the formal parameter list failed. (tsubst_decl) <case PARM_DECL>: Return error_mark_node upon TREE_TYPE substitution failure, when in a SFINAE context. Return error_mark_node upon DECL_CHAIN substitution failure. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/alias-decl-80.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-06-02OpenMP: Handle more cases in user/condition selectorSandra Loosemore1-7/+23
Tobias had noted that the C front end was not treating C23 constexprs as constant in the user/condition selector property, which led to missed opportunities to resolve metadirectives at parse time. Additionally neither C nor C++ was permitting the expression to have pointer or floating-point type -- the former being a common idiom in other C/C++ conditional expressions. By using the existing front-end hooks for the implicit conversion to bool in conditional expressions, we also get free support for using a C++ class object that has a bool conversion operator in the user/condition selector. gcc/c/ChangeLog * c-parser.cc (c_parser_omp_context_selector): Call convert_lvalue_to_rvalue and c_objc_common_truthvalue_conversion on the expression for OMP_TRAIT_PROPERTY_BOOL_EXPR. gcc/cp/ChangeLog * cp-tree.h (maybe_convert_cond): Declare. * parser.cc (cp_parser_omp_context_selector): Call maybe_convert_cond and fold_build_cleanup_point_expr on the expression for OMP_TRAIT_PROPERTY_BOOL_EXPR. * pt.cc (tsubst_omp_context_selector): Likewise. * semantics.cc (maybe_convert_cond): Remove static declaration. gcc/testsuite/ChangeLog * c-c++-common/gomp/declare-variant-2.c: Update expected output. * c-c++-common/gomp/metadirective-condition-constexpr.c: New. * c-c++-common/gomp/metadirective-condition.c: New. * c-c++-common/gomp/metadirective-error-recovery.c: Update expected output. * g++.dg/gomp/metadirective-condition-class.C: New. * g++.dg/gomp/metadirective-condition-template.C: New.
2025-05-30OpenMP: C++ "declare mapper" supportJulian Brown1-3/+50
This patch adds support for OpenMP 5.0 "declare mapper" functionality for C++. I've merged it to og13 based on the last version posted upstream, with some minor changes due to the newly-added 'present' map modifier support. There's also a fix to splay-tree traversal in gimplify.cc:omp_instantiate_implicit_mappers, and this patch omits the rearrangement of gimplify.cc:gimplify_{scan,adjust}_omp_clauses that I separated out into its own patch and applied (to og13) already. gcc/c-family/ * c-common.h (c_omp_region_type): Add C_ORT_DECLARE_MAPPER and C_ORT_OMP_DECLARE_MAPPER codes. (omp_mapper_list): Add forward declaration. (c_omp_find_nested_mappers, c_omp_instantiate_mappers): Add prototypes. * c-omp.cc (c_omp_find_nested_mappers): New function. (remap_mapper_decl_info): New struct. (remap_mapper_decl_1, omp_instantiate_mapper, c_omp_instantiate_mappers): New functions. gcc/cp/ * constexpr.cc (reduced_constant_expression_p): Add OMP_DECLARE_MAPPER case. (cxx_eval_constant_expression, potential_constant_expression_1): Likewise. * cp-gimplify.cc (cxx_omp_finish_mapper_clauses): New function. * cp-objcp-common.h (LANG_HOOKS_OMP_FINISH_MAPPER_CLAUSES, LANG_HOOKS_OMP_MAPPER_LOOKUP, LANG_HOOKS_OMP_EXTRACT_MAPPER_DIRECTIVE, LANG_HOOKS_OMP_MAP_ARRAY_SECTION): Define langhooks. * cp-tree.h (lang_decl_base): Add omp_declare_mapper_p field. Recount spare bits comment. (DECL_OMP_DECLARE_MAPPER_P): New macro. (omp_mapper_id): Add prototype. (cp_check_omp_declare_mapper): Add prototype. (omp_instantiate_mappers): Add prototype. (cxx_omp_finish_mapper_clauses): Add prototype. (cxx_omp_mapper_lookup): Add prototype. (cxx_omp_extract_mapper_directive): Add prototype. (cxx_omp_map_array_section): Add prototype. * decl.cc (check_initializer): Add OpenMP declare mapper support. (cp_finish_decl): Set DECL_INITIAL for OpenMP declare mapper var decls as appropriate. * decl2.cc (mark_used): Instantiate OpenMP "declare mapper" magic var decls. * error.cc (dump_omp_declare_mapper): New function. (dump_simple_decl): Use above. * parser.cc (cp_parser_omp_clause_map): Add KIND parameter. Support "mapper" modifier. (cp_parser_omp_all_clauses): Add KIND argument to cp_parser_omp_clause_map call. (cp_parser_omp_target): Call omp_instantiate_mappers before finish_omp_clauses. (cp_parser_omp_declare_mapper): New function. (cp_parser_omp_declare): Add "declare mapper" support. * pt.cc (tsubst_decl): Adjust name of "declare mapper" magic var decls once we know their type. (tsubst_omp_clauses): Call omp_instantiate_mappers before finish_omp_clauses, for target regions. (tsubst_expr): Support OMP_DECLARE_MAPPER nodes. (instantiate_decl): Instantiate initialiser (i.e definition) for OpenMP declare mappers. * semantics.cc (gimplify.h): Include. (omp_mapper_id, omp_mapper_lookup, omp_extract_mapper_directive, cxx_omp_map_array_section, cp_check_omp_declare_mapper): New functions. (finish_omp_clauses): Delete GOMP_MAP_PUSH_MAPPER_NAME and GOMP_MAP_POP_MAPPER_NAME artificial clauses. (omp_target_walk_data): Add MAPPERS field. (finish_omp_target_clauses_r): Scan for uses of struct/union/class type variables. (finish_omp_target_clauses): Create artificial mapper binding clauses for used structs/unions/classes in offload region. gcc/fortran/ * parse.cc (tree.h, fold-const.h, tree-hash-traits.h): Add includes (for additions to omp-general.h). gcc/ * gimplify.cc (gimplify_omp_ctx): Add IMPLICIT_MAPPERS field. (new_omp_context): Initialise IMPLICIT_MAPPERS hash map. (delete_omp_context): Delete IMPLICIT_MAPPERS hash map. (instantiate_mapper_info): New structs. (remap_mapper_decl_1, omp_mapper_copy_decl, omp_instantiate_mapper, omp_instantiate_implicit_mappers): New functions. (gimplify_scan_omp_clauses): Handle MAPPER_BINDING clauses. (gimplify_adjust_omp_clauses): Instantiate implicit declared mappers. (gimplify_omp_declare_mapper): New function. (gimplify_expr): Call above function. * langhooks-def.h (lhd_omp_mapper_lookup, lhd_omp_extract_mapper_directive, lhd_omp_map_array_section): Add prototypes. (LANG_HOOKS_OMP_FINISH_MAPPER_CLAUSES, LANG_HOOKS_OMP_MAPPER_LOOKUP, LANG_HOOKS_OMP_EXTRACT_MAPPER_DIRECTIVE, LANG_HOOKS_OMP_MAP_ARRAY_SECTION): Define macros. (LANG_HOOK_DECLS): Add above macros. * langhooks.cc (lhd_omp_mapper_lookup, lhd_omp_extract_mapper_directive, lhd_omp_map_array_section): New dummy functions. * langhooks.h (lang_hooks_for_decls): Add OMP_FINISH_MAPPER_CLAUSES, OMP_MAPPER_LOOKUP, OMP_EXTRACT_MAPPER_DIRECTIVE, OMP_MAP_ARRAY_SECTION hooks. * omp-general.h (omp_name_type<T>): Add templatized struct, hash type traits (for omp_name_type<tree> specialization). (omp_mapper_list<T>): Add struct. * tree-core.h (omp_clause_code): Add OMP_CLAUSE__MAPPER_BINDING_. * tree-pretty-print.cc (dump_omp_clause): Support GOMP_MAP_UNSET, GOMP_MAP_PUSH_MAPPER_NAME, GOMP_MAP_POP_MAPPER_NAME artificial mapping clauses. Support OMP_CLAUSE__MAPPER_BINDING_ and OMP_DECLARE_MAPPER. * tree.cc (omp_clause_num_ops, omp_clause_code_name): Add OMP_CLAUSE__MAPPER_BINDING_. * tree.def (OMP_DECLARE_MAPPER): New tree code. * tree.h (OMP_DECLARE_MAPPER_ID, OMP_DECLARE_MAPPER_DECL, OMP_DECLARE_MAPPER_CLAUSES): New defines. (OMP_CLAUSE__MAPPER_BINDING__ID, OMP_CLAUSE__MAPPER_BINDING__DECL, OMP_CLAUSE__MAPPER_BINDING__MAPPER): New defines. include/ * gomp-constants.h (gomp_map_kind): Add GOMP_MAP_UNSET, GOMP_MAP_PUSH_MAPPER_NAME, GOMP_MAP_POP_MAPPER_NAME artificial mapping clause types. gcc/testsuite/ * c-c++-common/gomp/map-6.c: Update error scan output. * c-c++-common/gomp/declare-mapper-3.c: New test (only enabled for C++ for now). * c-c++-common/gomp/declare-mapper-4.c: Likewise. * c-c++-common/gomp/declare-mapper-5.c: Likewise. * c-c++-common/gomp/declare-mapper-6.c: Likewise. * c-c++-common/gomp/declare-mapper-7.c: Likewise. * c-c++-common/gomp/declare-mapper-8.c: Likewise. * c-c++-common/gomp/declare-mapper-9.c: Likewise. * c-c++-common/gomp/declare-mapper-10.c: Likewise. * c-c++-common/gomp/declare-mapper-12.c: Likewise. * g++.dg/gomp/declare-mapper-1.C: New test. * g++.dg/gomp/declare-mapper-2.C: New test. * g++.dg/gomp/declare-mapper-3.C: New test. libgomp/ * testsuite/libgomp.c++/declare-mapper-1.C: New test. * testsuite/libgomp.c++/declare-mapper-2.C: New test. * testsuite/libgomp.c++/declare-mapper-3.C: New test. * testsuite/libgomp.c++/declare-mapper-4.C: New test. * testsuite/libgomp.c++/declare-mapper-5.C: New test. * testsuite/libgomp.c++/declare-mapper-6.C: New test. * testsuite/libgomp.c++/declare-mapper-7.C: New test. * testsuite/libgomp.c++/declare-mapper-8.C: New test. * testsuite/libgomp.c-c++-common/declare-mapper-9.c: New test (only enabled for C++ for now). * testsuite/libgomp.c-c++-common/declare-mapper-10.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-11.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-12.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-13.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-14.c: Likewise. Co-authored-by: Tobias Burnus <tburnus@baylibre.com>
2025-05-26c++: add -fdump-lang-tinstJason Merrill1-17/+94
This patch adds a dump with a trace of template instantiations, indented based on the depth of recursive instantiation. -lineno adds the location that triggered the instantiation, -details adds non-instantiation sbustitutions. The instantiate_pending_templates change is to avoid a bunch of entries for reopening tinst scopes that we then don't instantiate anything with; it also seems a bit cleaner this way. gcc/cp/ChangeLog: * cp-tree.h: Declare tinst_dump_id. * cp-objcp-common.cc (cp_register_dumps): Set it. * pt.cc (push_tinst_level_loc): Dump it. (reopen_tinst_level): Here too. (tinst_complete_p): New. (instantiate_pending_templates): Don't reopen_tinst_level for already-complete instantiations. gcc/ChangeLog: * doc/invoke.texi: Move C++ -fdump-lang to C++ section. Add -fdump-lang-tinst.
2025-05-15c++: unifying specializations of non-primary tmpls [PR120161]Patrick Palka1-3/+3
Here unification of P=Wrap<int>::type, A=Wrap<long>::type wrongly succeeds ever since r14-4112 which made the RECORD_TYPE case of unify no longer recurse into template arguments for non-primary templates (since they're a non-deduced context) and so the int/long mismatch that makes the two types distinct goes unnoticed. In the case of (comparing specializations of) a non-primary template, unify should still go on to compare the types directly before returning success. PR c++/120161 gcc/cp/ChangeLog: * pt.cc (unify) <case RECORD_TYPE>: When comparing specializations of a non-primary template, still perform a type comparison. gcc/testsuite/ChangeLog: * g++.dg/template/unify13.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-05-15c++: one more PR99599 tweakJason Merrill1-2/+2
Patrick pointed out that if the parm/arg types aren't complete yet at this point, it would affect the type_has_converting_constructor and TYPE_HAS_CONVERSION tests. I don't have a testcase, but it makes sense for safety. PR c++/99599 gcc/cp/ChangeLog: * pt.cc (conversion_may_instantiate_p): Make sure classes are complete.
2025-05-09c++: recursive instantiation diagnostic [PR120204]Jason Merrill1-4/+12
Here tsubst_baselink was returning error_mark_node silently despite tf_error; we need to actually give an error. PR c++/120204 gcc/cp/ChangeLog: * pt.cc (tsubst_baselink): Always error if lookup fails. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-recursion3.C: New test.
2025-05-09c++: CWG2369 workaround and ... [PR120185]Jason Merrill1-1/+1
My r16-479 adjustment to the PR99599 workaround broke on a class with a varargs constructor. It also occurred to me that we don't need to do non-dep conversion checking in two phases when concepts aren't supported. PR c++/99599 PR c++/120185 gcc/cp/ChangeLog: * class.cc (type_has_converting_constructor): Handle null parm. * pt.cc (fn_type_unification): Skip early non-dep checking if no concepts. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-nondep6.C: New test.
2025-05-08c++: adjust PR99599/CWG2369 workaroundJason Merrill1-29/+11
This tweak to CWG2369 has gotten more discussion lately in CWG, including in P3606. In those discussions, it occurred to me that having the check depend on whether a class has been instantiated yet is unstable, that it should only check for user-defined conversions. Also, one commenter was surprised that adding an explicitly-declared default constructor to a class changed things, so this patch also changes the aggregate check to more narrowly checking for one-argument constructors other than the copy/move constructors. As a result, this early filter resembles how LOOKUP_DEFAULTED rejects any candidate that would need a UDC: in both cases we want to avoid considering arbitrary UDCs. But here, rather than rejecting, we want the early filter to let the candidate past without considering the conversion. PR c++/99599 gcc/cp/ChangeLog: * cp-tree.h (type_has_converting_constructor): Declare. * class.cc (type_has_converting_constructor): New. * pt.cc (conversion_may_instantiate_p): Don't check completeness. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-recursive-sat4.C: Adjust again. * g++.dg/cpp2a/concepts-nondep5.C: New test.
2025-05-01c++: more overeager use of deleted function before ADL [PR119034]Patrick Palka1-7/+1
The PR68942 fix used the tf_conv flag to disable mark_used when substituting a FUNCTION_DECL callee of an ADL-enabled call. In this slightly more elaborate testcase, we end up prematurely calling mark_used anyway on the FUNCTION_DECL directly from the CALL_EXPR case of tsubst_expr during partial instantiation, leading to a bogus "use of deleted function" error. This patch fixes the general problem in a more robust way by ensuring the callee of an ADL-enabled call is wrapped in an OVERLOAD, so that tsubst_expr leaves it alone. PR c++/119034 PR c++/68942 gcc/cp/ChangeLog: * pt.cc (tsubst_expr) <case CALL_EXPR>: Revert PR68942 fix. * semantics.cc (finish_call_expr): Ensure the callee of an ADL-enabled call is wrapped in an OVERLOAD. gcc/testsuite/ChangeLog: * g++.dg/template/koenig13.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-05-01c++/modules: Fix imported CNTTPs being considered non-constant [PR119938]Nathaniel Shead1-1/+6
When importing a CNTTP object, since r15-3031-g0b7904e274fbd6 we shortcut the processing of the generated NTTP so that we don't attempt to recursively load pendings. However, due to an oversight we do not properly set TREE_CONSTANT or DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P on the decl, which confuses later processing. This patch ensures that this happens correctly. PR c++/119938 gcc/cp/ChangeLog: * pt.cc (get_template_parm_object): When !check_init, add assert that expr really is constant and mark decl as such. gcc/testsuite/ChangeLog: * g++.dg/modules/tpl-nttp-2_a.H: New test. * g++.dg/modules/tpl-nttp-2_b.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Jason Merrill <jason@redhat.com>
2025-04-30c++: UNBOUND_CLASS_TEMPLATE context substitution [PR119981]Patrick Palka1-7/+13
In r15-123 and r14-11434 we unconditionally set processing_template_decl when substituting the context of an UNBOUND_CLASS_TEMPLATE, in order to handle instantiation of the dependently scoped friend declaration template<int N> template<class T> friend class A<N>::B; where the scope A<N> remains dependent after instantiation. But this turns out to misbehave for the UNBOUND_CLASS_TEMPLATE in the below testcase representing g<[]{}>::template fn since with the flag set substituting the args of test3 into the lambda causes us to defer the substitution and yield a lambda that still looks dependent, which in turn makes g<[]{}> still dependent and not suitable for qualified name lookup. This patch restricts setting processing_template_decl during UNBOUND_CLASS_TEMPLATE substitution to the case where there are multiple levels of introduced template parameters, as in the friend declaration. (This means we need to substitute the template parameter list(s) first, which makes sense since they lexically appear first.) PR c++/119981 PR c++/119378 gcc/cp/ChangeLog: * pt.cc (tsubst) <case UNBOUND_CLASS_TEMPLATE>: Substitute into template parameter list first. When substituting the context, only set processing_template_decl if there's more than one level of introduced template parameters. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-targ15.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-04-25c++: bad pending_template recursionJason Merrill1-2/+8
limit_bad_template_recursion currently avoids immediate instantiation of templates from uses in an already ill-formed instantiation, but we still can get unnecessary recursive instantiation in pending_templates if the instantiation was queued before the error. Initially this regressed several libstdc++ tests which seemed to rely on a static_assert in a function called from another that is separately ill-formed. For instance, in the 48101_neg.cc tests, we first got an error in find(), then later instantiate _S_key() (called from find) and got the static_assert error from there. r16-131-g876d1a22dfaf87 and r16-132-g901900bc37566c changed the library code (and tests) to make the expected static_assert errors happen earlier. gcc/cp/ChangeLog: * cp-tree.h (struct tinst_level): Add had_errors bit. * pt.cc (push_tinst_level_loc): Clear it. (pop_tinst_level): Set it. (reopen_tinst_level): Check it. (instantiate_pending_templates): Call limit_bad_template_recursion. gcc/testsuite/ChangeLog: * g++.dg/template/recurse5.C: New test.
2025-04-16c++: ill-formed constexpr function [PR113360]Jason Merrill1-0/+5
If we already gave an error while parsing a function, we don't also need to try to explain what's wrong with it when we later try to use it in a constant-expression. In the new testcase explain_invalid_constexpr_fn couldn't find anything still in the function to complain about, so it said because: followed by nothing. We still try to constant-evaluate it to reduce error cascades, but we shouldn't complain if it doesn't work very well. This flag is similar to CLASSTYPE_ERRONEOUS that I added a while back. PR c++/113360 gcc/cp/ChangeLog: * cp-tree.h (struct language_function): Add erroneous bit. * constexpr.cc (explain_invalid_constexpr_fn): Return if set. (cxx_eval_call_expression): Quiet if set. * parser.cc (cp_parser_function_definition_after_declarator) * pt.cc (instantiate_body): Set it. gcc/testsuite/ChangeLog: * g++.dg/cpp23/constexpr-nonlit18.C: Remove redundant message. * g++.dg/cpp1y/constexpr-diag2.C: New test. * g++.dg/cpp1y/pr63996.C: Adjust expected errors. * g++.dg/template/explicit-args6.C: Likewise. * g++.dg/cpp0x/constexpr-ice21.C: Likewise.
2025-04-16c++: templates, attributes, #pragma target [PR114772]Jason Merrill1-0/+2
Since r12-5426 apply_late_template_attributes suppresses various global state to avoid applying active pragmas to earlier declarations; we also need to override target_option_current_node. PR c++/114772 PR c++/101180 gcc/cp/ChangeLog: * pt.cc (apply_late_template_attributes): Also override target_option_current_node. gcc/testsuite/ChangeLog: * g++.dg/ext/pragma-target2.C: New test.
2025-04-15c++: prev declared hidden tmpl friend inst, cont [PR119807]Patrick Palka1-0/+4
When remapping existing specializations of a hidden template friend from a previous declaration to the new definition, we must remap only those specializations that match this new definition, but currently we remap all specializations (since they all appear in the same DECL_TEMPLATE_INSTANTIATIONS list of the most general template). Concretely, in the first testcase below, we form two specializations of the friend A::f, one with arguments {{0},{bool}} and another with arguments {{1},{bool}}. Later when instantiating B, we need to remap these specializations. During the B<0> instantiation we only want to remap the first specialization, and during the B<1> instantiation only the second specialization, but currently we remap both specializations twice. tsubst_friend_function needs to determine if an existing specialization matches the shape of the new definition, which is tricky in general, e.g. if the outer template parameters may not match up. Fortunately we don't have to reinvent the wheel here since is_specialization_of_friend seems to do exactly what we need. We can check this unconditionally, but I think it's only necessary when dealing with specializations formed from a class template scope previous declaration, hence the TMPL_ARGS_HAVE_MULTIPLE_LEVELS check. PR c++/119807 PR c++/112288 gcc/cp/ChangeLog: * pt.cc (tsubst_friend_function): Skip remapping an existing specialization if it doesn't match the shape of the new friend definition. gcc/testsuite/ChangeLog: * g++.dg/template/friend86.C: New test. * g++.dg/template/friend87.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-04-10c++: nested lambda capture pack [PR119345]Jason Merrill1-0/+2
tsubst_stmt already registers a local capture proxy as a local_specialization of both an outer capture proxy and the captured variable; we also need to do that in add_extra_args. PR c++/119345 gcc/cp/ChangeLog: * pt.cc (add_extra_args): Also register a specialization of the captured variable. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-targ14.C: New test.
2025-04-10c++: alias_ctad_tweaks ICE w/ inherited CTAD [PR119687]Patrick Palka1-2/+1
With inherited CTAD the set of guides may be a two-dimensional overload set (i.e. OVERLOADs of OVERLOADs) so alias_ctad_tweaks (which also does the inherited CTAD transformation) needs to use the 2D-aware lkp_iterator instead of ovl_iterator, or better yet use the more idiomatic lkp_range. PR c++/119687 gcc/cp/ChangeLog: * pt.cc (alias_ctad_tweaks): Use lkp_range / lkp_iterator instead of ovl_iterator. gcc/testsuite/ChangeLog: * g++.dg/cpp23/class-deduction-inherited8.C: New test. Reviewed-by: Jason Merill <jason@redhat.com>
2025-04-09c++: ICE with nested default targ lambdas [PR119574]Patrick Palka1-1/+0
Here we substitute into the inner lambda twice, first during default argument substitution for the outer template parameters, then during that for the inner template parameters. For the second testcase (which is easier to follow/debug), the first substitution into the inner lambda is with the template arguments {0, NULL_TREE}, which we defer because it's an incremental substitution. For the second and final substitution we have the template arguments {1, NULL_TREE}, which we try combining via add_extra_args and ICE on the checking assert because TREE_STATIC isn't set on the deferred arguments but the template arguments are considered dependent. The template arguments aren't dependent however -- they're just incomplete because when we deferred them we were in the middle of deduction, and we consider a NULL_TREE template argument as dependent. If we remove this checking assert, we go on to correctly merge the template arguments into {{0, NULL_TREE}, {1, NULL_TREE}}. So this patch just removes this imprecise assert. PR c++/119574 gcc/cp/ChangeLog: * pt.cc (add_extra_args): Remove checking assert. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-targ13.C: New test. * g++.dg/cpp2a/lambda-targ13a.C: New test. * g++.dg/cpp2a/lambda-targ13b.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-04-08c++: lambda in concept [PR118698]Jason Merrill1-0/+12
When normalizing is_foo for <T>, we get to normalizing callable<decltype(...),T> for <T,foo>, which means substituting <T,foo> into <decltype(...),T>. Since r14-9938, because in_template_context is false we return the lambda unchanged, just with LAMBDA_EXPR_EXTRA_ARGS set, so the closure type still refers to the is_specialization_of tparms in its CLASSTYPE_TEMPLATE_INFO. So then in normalize_atom caching find_template_parameters walks over the parameter mapping; any_template_parm_r walks into the TREE_TYPE of a LAMBDA_EXPR without considering EXTRA_ARGS and finds a template parm from the wrong parameter list. But since r15-3530 we expect to set tf_partial when substituting with dependent arguments, so we should set that when normalizing. And then tf_partial causes TREE_STATIC to be set on the EXTRA_ARGS, meaning that those args will replace all the template parms in the rest of the lambda, so we can walk just the EXTRA_ARGS and ignore the rest. PR c++/118698 gcc/cp/ChangeLog: * constraint.cc (struct norm_info): Add tf_partial. * pt.cc (any_template_parm_r): Handle LAMBDA_EXPR_EXTRA_ARGS. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-lambda22.C: New test.
2025-04-08c++: self-dependent alias template [PR117530]Jason Merrill1-6/+8
Here, instantiating B<short> means instantiating A<short>, which means instantiating B<short>. And then when we go to register the initial instantiation, it conflicts with the inner one. Fixed by checking after tsubst whether there's already something in the hash table. We already did something much like this in tsubst_decl, but that doesn't handle this case. While I was here, I noticed that we had a pop_deferring_access_checks on one early exit but not another, and since I wanted to add yet another I switched to using deferring_access_check_sentinel. PR c++/117530 gcc/cp/ChangeLog: * pt.cc (instantiate_template): Check retrieve_specialization after tsubst. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-uneval27.C: New test.
2025-04-05c++: maybe_dependent_member_ref and typenames [PR118626]Patrick Palka1-1/+2
Here during maybe_dependent_member_ref for accepted_type<_Up>, we correctly don't strip the typedef because it's a complex one (its defaulted template parameter isn't used in its definition) and so we recurse to consider its corresponding TYPE_DECL. We then incorrectly decide to not rewrite this use because of the TYPENAME_TYPE shortcut. But I don't think this shortcut should apply to a typedef TYPE_DECL. PR c++/118626 gcc/cp/ChangeLog: * pt.cc (maybe_dependent_member_ref): Restrict TYPENAME_TYPE shortcut to non-typedef TYPE_DECL. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/class-deduction-alias25a.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-04-05c++: maybe_dependent_member_ref and stripped alias [PR118626]Patrick Palka1-3/+27
Here during maybe_dependent_member_ref (as part of CTAD rewriting for the variant constructor) for __accepted_type<_Up> we strip this alias all the way to type _Nth_type<__accepted_index<_Up>>, for which we return NULL since _Nth_type is at namespace scope and so no longer needs rewriting. Note that however the template argument __accepted_index<_Up> of this stripped type _does_ need rewriting (since it specializes a variable template from the current instantiation). We end up not rewriting this variable template reference at any point however because upon returning NULL, the caller (tsubst) proceeds to substitute the original form of the type __accepted_type<_Up>, which doesn't directly refer to __accepted_index. This later leads to an ICE during subsequent alias CTAD rewriting of this guide that contains a non-rewritten reference to __accepted_index. So when maybe_dependent_member_ref decides to not rewrite a class-scope alias that's been stripped, the caller needs to commit to substituting the stripped type rather than the original type. This patch essentially implements that by making maybe_dependent_member_ref call tsubst itself in that case. PR c++/118626 gcc/cp/ChangeLog: * pt.cc (maybe_dependent_member_ref): Substitute and return the stripped type if we decided to not rewrite it directly. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/class-deduction-alias25.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-03-31c++: lambda in function template signature [PR119401]Jason Merrill1-0/+13
Here we instantiate the lambda three times in producing A<0>::f: 1) in tsubst_function_type, substituting the type of A<>::f 2) in tsubst_function_decl, substituting the parameters of A<>::f 3) in regenerate_decl_from_template when instantiating A<>::f The first one gets thrown away by maybe_rebuild_function_decl_type. Before r15-7202, we happily built all of them and mangled the result wrongly as lambda #3. After r15-7202, we try to mangle #3 as #1, which breaks because #1 is already mangled as #1. This patch avoids building #3 by suppressing regenerate_decl_from_template if the template signature includes a lambda, fixing the ICE. We now mangle the lambda as #2, which is still wrong. Addressing that should involve not calling tsubst_function_type from tsubst_function_decl, and building the type from the parms types in the first place rather than fixing it up in maybe_rebuild_function_decl_type. PR c++/119401 gcc/cp/ChangeLog: * pt.cc (regenerate_decl_from_template): Don't regenerate if the signature involves a lambda. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-targ11.C: New test.
2025-03-29c++/modules: Fix modules and LTO with header units [PR118961]Nathaniel Shead1-9/+18
This patch makes some adjustments required to get a simple modules testcase working with LTO. There are two main issues fixed. Firstly, modules only streams the maybe-in-charge constructor, and any clones are recreated on stream-in. These clones are copied from the existing function decl and then adjusted. This caused issues because the clones were getting incorrectly marked as abstract, since after clones have been created (in the imported file) the maybe-in-charge decl gets marked as abstract. So this patch just ensures that clones are always created as non-abstract. The second issue is that we need to explicitly tell cgraph that explicit instantiations need to be emitted, otherwise LTO will elide them (as they don't necessarily appear to be used directly) and cause link errors. Additionally, expand_or_defer_fn doesn't setup comdat groups for explicit instantiations, so we need to do that here as well. Currently this is all handled in 'mark_decl_instantiated'; this patch splits out the linkage handling into a separate function that we can call from modules code, maybe in GCC16 we could move this somewhere more central. PR c++/118961 gcc/cp/ChangeLog: * class.cc (copy_fndecl_with_name): Mark clones as non-abstract. * cp-tree.h (setup_explicit_instantiation_definition_linkage): Declare new function. * module.cc (trees_in::read_var_def): Use it. (module_state::read_cluster): Likewise. * pt.cc (setup_explicit_instantiation_definition_linkage): New function. (mark_decl_instantiated): Use it. gcc/testsuite/ChangeLog: * g++.dg/modules/lto-1.h: New test. * g++.dg/modules/lto-1_a.H: New test. * g++.dg/modules/lto-1_b.C: New test. * g++.dg/modules/lto-1_c.C: New test. * g++.dg/modules/lto-2_a.H: New test. * g++.dg/modules/lto-2_b.C: New test. * g++.dg/modules/lto-3_a.H: New test. * g++.dg/modules/lto-3_b.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Jason Merrill <jason@redhat.com>
2025-03-27OpenMP: Fix C++ template handling with append_args' prefer_type modifierTobias Burnus1-2/+7
It is possible but not very sensible to use C++ templates with in the prefer_type modifier to the 'append_args' clause of 'declare variant'. The commit r15-6336-g12dd892b1a3ad7 added substitution support in pt.cc, but missed to update afterward the actual data in decl.cc. As gimplification support was only added in r15-8898-gf016ee89955ab4, this could not be tested back then. The latter commit added a sorry for it gimplify.cc and the existing testcase, which this commit now removes. gcc/cp/ChangeLog: * cp-tree.h (cp_finish_omp_init_prefer_type): Add. * decl.cc (omp_declare_variant_finalize_one): Call it. * pt.cc (tsubst_attribute): Minor rebustification for OpenMP append_args handling. * semantics.cc (cp_omp_init_prefer_type_update): Rename to ... (cp_finish_omp_init_prefer_type): ... this; remove static attribute and return modified tree. Move clause handling to ... (finish_omp_clauses): ... the caller. gcc/ChangeLog: * gimplify.cc (modify_call_for_omp_dispatch): Remove sorry. gcc/testsuite/ChangeLog: * g++.dg/gomp/append-args-1.C: Remove expected dg-sorry. * g++.dg/gomp/append-args-8.C: New test.
2025-03-22c++: structural equality and partially inst typedef [PR119379]Patrick Palka1-0/+5
Complex alias templates (and their dependent specializations) always use structural equality because we need to treat them as transparent in some contexts but not others. Structural-ness however wasn't being preserved during partial instantiation, which for the below testcase leads to the checking ICE same canonical type node for different types 'S<int>::P<U>' and 'pair<int, U>' when comparing those two types with comparing_dependent_aliases set (from alias_ctad_tweaks). This patch fixes this by making us preserve structural-ness for partially instantiated typedefs in general. PR c++/119379 gcc/cp/ChangeLog: * pt.cc (tsubst_decl) <case TYPE_DECL>: Preserve structural-ness of a partially instantiated typedef. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/class-deduction-alias24.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-03-18c++: ICE when substituting packs into type aliases [PR118104]Marek Polacek1-1/+10
r12-1094 mentions that adding the assert didn't lead to any regressions in the testsuite, but this test case demonstrates that we can reach it with valid code. Here we arrive in use_pack_expansion_extra_args_p with t which is an expansion whose pattern is void(Ts, Us) and tparm packs are {Us, Ts}, and parm_packs is { Ts -> <int, int>, Us -> <A, P...> }. We want to expand the pack into void(int, A) and void(int, P...). We compare int to A, which is fine, but then int to P... which crashes. But the code is valid so this patch removes the assert. PR c++/118104 gcc/cp/ChangeLog: * pt.cc (use_pack_expansion_extra_args_p): Remove an assert. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/alias-decl-variadic3.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-03-18c++: memfn pointer as NTTP argument considered unused [PR119233]Patrick Palka1-0/+6
This is just the member function pointer version of PR c++/105848, in which our non-dependent call pruning may cause us to not mark an otherwise unused function pointer template argument as used. PR c++/119233 gcc/cp/ChangeLog: * pt.cc (mark_template_arguments_used): Also handle member function pointers. gcc/testsuite/ChangeLog: * g++.dg/template/fn-ptr5.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-03-12c++: ICE with lambda in fold expression in requires [PR119134]Marek Polacek1-0/+1
The r12-8258 fix assumes that DECL_CONTEXT of 'pack' in check_for_bare_parameter_packs is going to be an operator() but as this test shows, it can be empty. PR c++/119134 gcc/cp/ChangeLog: * pt.cc (check_for_bare_parameter_packs): Check DECL_CONTEXT. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-uneval24.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-03-08c++/modules: purview of explicit instantiations [PR114630]Jason Merrill1-1/+16
When calling instantiate_pending_templates at end of parsing, any new functions that are instantiated from this point have their module purview set based on the current value of module_kind. This is unideal, however, as the modules code will then treat these instantiations as reachable and cause large swathes of the GMF to be emitted into the module CMI, despite no code in the actual module purview referencing it. This patch fixes this by setting DECL_MODULE_PURVIEW_P as appropriate when we see an explicit instantiation, and adjusting module_kind accordingly during deferred instantiation, meaning that GMF entities won't be counted as reachable unless referenced by an actually reachable entity. Note that purviewness and attachment etc. is generally only determined by the base template: this is purely for determining whether an explicit instantiation is in the module purview and hence whether it should be streamed out. See the comment on 'set_instantiating_module'. Incidentally, since the "xtreme" testcases are deliberately large (and this commit adds another one), let's make sure we only run them once. PR c++/114630 PR c++/114795 gcc/cp/ChangeLog: * pt.cc (reopen_tinst_level): Set or clear MK_PURVIEW. (mark_decl_instantiated): Call set_instantiating_module. (instantiate_pending_templates): Save and restore module_kind so it isn't affected by reopen_tinst_level. gcc/testsuite/ChangeLog: * g++.dg/modules/modules.exp: Run xtreme tests once. * g++.dg/modules/gmf-3.C: New test. * g++.dg/modules/gmf-4.C: New test. * g++.dg/modules/gmf-xtreme.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Co-authored-by: Nathaniel Shead <nathanieloshead@gmail.com>
2025-03-07c++: Handle TU_LOCAL_ENTITY in tsubst_expr and potential_constant_expressionNathaniel Shead1-66/+14
This cleans up the TU_LOCAL_ENTITY handling to avoid unnecessary tree walks and make the logic more robust. gcc/cp/ChangeLog: * constexpr.cc (potential_constant_expression_1): Handle TU_LOCAL_ENTITY. * pt.cc (expr_contains_tu_local_entity): Remove. (function_contains_tu_local_entity): Remove. (dependent_operand_p): Remove special handling for TU_LOCAL_ENTITY. (tsubst_expr): Handle TU_LOCAL_ENTITY when tsubsting OVERLOADs; remove now-unnecessary extra handling. (type_dependent_expression_p): Handle TU_LOCAL_ENTITY. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Patrick Palka <ppalka@redhat.com> Reviewed-by: Jason Merrill <jason@redhat.com>
2025-03-06c++: Fix up instantiation of pointer/reference/array types with attributes ↵Jakub Jelinek1-3/+6
[PR119138] My r15-7822 PR118787 change unfortunately broke build on x86_64-w64-mingw32. The reduced testcase below shows what is going on. va_list on this target is char * with extra (non-dependent) attributes on it. Before my r15-7822 change, instantiation of such type used the fast path and just returned t, but as it has non-NULL TYPE_ATTRIBUTES, it now falls through, builds a pointer type and then calls apply_late_template_attributes. And in there triggers a bug, that function has been written for types with RECORD_TYPE/UNION_TYPE (or ENUMERAL_TYPE?) in mind, where we call apply_late_template_attributes with ATTR_FLAG_TYPE_IN_PLACE and can just apply the non-dependent attributes directly to TYPE_ATTRIBUTES. That is wrong for shared types like {POINTER,REFERENCE,ARRAY}_TYPE etc., we should just force cp_build_type_attribute_variant to build a variant type for the non-dependent attributes and then process dependent attributes (which given attr_flag will DTRT already). The second change in the patch is an optimization, we can actually return back to returning t even when TYPE_ATTRIBUTES is non-NULL, as long as it is non-dependent (dependent attributes are stored first, so it is enough to check the first attribute). 2025-03-06 Jakub Jelinek <jakub@redhat.com> PR c++/119138 * pt.cc (apply_late_template_attributes): Set p to NULL if ATTR_FLAG_TYPE_IN_PLACE is not set in attr_flags. (tsubst) <case POINTER_TYPE, case REFERENCE_TYPE, case ARRAY_TYPE>: Reuse original type even if TYPE_ATTRIBUTES is non-NULL, but all the attributes are non-dependent. * g++.dg/template/pr119138.C: New test.
2025-03-05c++: Apply/diagnose attributes when instatiating ↵Jakub Jelinek1-5/+19
ARRAY/POINTER/REFERENCE_TYPE [PR118787] The following testcase IMO in violation of the P2552R3 paper doesn't pedwarn on alignas applying to dependent types or alignas with dependent argument. tsubst was just ignoring TYPE_ATTRIBUTES. The following patch fixes it for the POINTER/REFERENCE_TYPE and ARRAY_TYPE cases, but perhaps we need to do the same also for other types (INTEGER_TYPE/REAL_TYPE and the like). I guess I'll need to construct more testcases. 2025-03-05 Jakub Jelinek <jakub@redhat.com> PR c++/118787 * pt.cc (tsubst) <case ARRAY_TYPE>: Use return t; only if it doesn't have any TYPE_ATTRIBUTES. Call apply_late_template_attributes. <case POINTER_TYPE, case REFERENCE_TYPE>: Likewise. Formatting fix. * g++.dg/cpp0x/alignas22.C: New test.
2025-02-14c++: assign the result of force_paren_exprMarek Polacek1-1/+1
gcc/cp/ChangeLog: * pt.cc (tsubst_expr) <COMPONENT_REF>: Assign the result of force_paren_expr.
2025-02-14c++: fix propagating REF_PARENTHESIZED_P [PR116379]Marek Polacek1-2/+2
Here we have: template<typename T> struct X{ T val; decltype(auto) value(){ return (val); } }; where the return type of value should be 'int &' since '(val)' is an expression, not a name, and decltype(auto) performs the type deduction using the decltype rules. The problem is that we weren't propagating REF_PARENTHESIZED_P correctly: the return value of finish_non_static_data_member in this test was a REFERENCE_REF_P, so we didn't set the flag. We should use force_paren_expr like below. PR c++/116379 gcc/cp/ChangeLog: * pt.cc (tsubst_expr) <COMPONENT_REF>: Use force_paren_expr to set REF_PARENTHESIZED_P. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/decltype-auto9.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-02-11c++: change implementation of -frange-for-ext-temps [PR118574]Jason Merrill1-22/+0
The implementation in r15-3840 used a novel technique of wrapping the entire range-for loop in a CLEANUP_POINT_EXPR, which confused the coroutines transformation. Instead let's use the existing extend_ref_init_temps mechanism. This does not revert all of r15-3840, only the parts that change how CLEANUP_POINT_EXPRs are applied to range-for declarations. PR c++/118574 PR c++/107637 gcc/cp/ChangeLog: * call.cc (struct extend_temps_data): New. (extend_temps_r, extend_all_temps): New. (set_up_extended_ref_temp): Handle tree walk case. (extend_ref_init_temps): Cal extend_all_temps. * decl.cc (initialize_local_var): Revert ext-temps change. * parser.cc (cp_convert_range_for): Likewise. (cp_parser_omp_loop_nest): Likewise. * pt.cc (tsubst_stmt): Likewise. * semantics.cc (finish_for_stmt): Likewise. gcc/testsuite/ChangeLog: * g++.dg/coroutines/range-for1.C: New test.
2025-02-11c++: Fix use-after-free of replaced friend instantiation [PR118807]Nathaniel Shead1-0/+11
When instantiating a friend function, we call register_specialization which adds it to the DECL_TEMPLATE_INSTANTIATIONS of the template. However, in some circumstances we might immediately call pushdecl and find an existing specialisation. In this case, when reregistering the specialisation we also need to update the DECL_TEMPLATE_INSTANTIATIONS list so that we don't try to access the freed spec again later. PR c++/118807 gcc/cp/ChangeLog: * pt.cc (reregister_specialization): Remove spec from DECL_TEMPLATE_INSTANTIATIONS. gcc/testsuite/ChangeLog: * g++.dg/modules/pr118807.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Jason Merrill <jason@redhat.com>
2025-02-07c++: ICE with unparsed noexcept [PR117106]Marek Polacek1-5/+11
In a member-specification of a class, a noexcept-specifier is a complete-class context. Thus we delay parsing until the end of the class via our DEFERRED_PARSE mechanism; see cp_parser_save_noexcept and cp_parser_late_noexcept_specifier. We also attempt to defer instantiation of noexcept-specifiers in order to reduce the number of instantiations; this is done via DEFERRED_NOEXCEPT. We can even have both, as in noexcept65.C: a DEFERRED_PARSE wrapped in DEFERRED_NOEXCEPT, which uses the DEFPARSE_INSTANTIATIONS mechanism. noexcept65.C works, because when we really need the noexcept, which is when parsing the body of S::A::A(), the noexcept will have been parsed already; noexcepts are parsed before bodies of member function. But in this test we have: struct A { int x; template<class> void foo() noexcept(noexcept(x)) {} auto bar() -> decltype(foo<int>()) {} // #1 }; and I think the decltype in #1 needs the unparsed noexcept before it could have been parsed. clang++ rejects the test and I suppose we should reject it as well, rather than crashing on a DEFERRED_PARSE in tsubst_expr. PR c++/117106 PR c++/118190 gcc/cp/ChangeLog: * pt.cc (maybe_instantiate_noexcept): Give an error if the noexcept hasn't been parsed yet. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/noexcept89.C: New test. * g++.dg/cpp0x/noexcept90.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2025-02-03c++: find A pack from B in <typename...A,Class<A>...B> [PR118265]A J Ryan Solutions Ltd1-0/+5
For non-type parameter packs when unifying the arguments in unify_pack_expansion it iterates over the associated packs of a param so that when it recursively unifies the param with the arguments it knows which targs have been populated with parameter pack arguments that it can then collect up. This change adds a tree walk so that in the example above it reaches ...A and adds it to the associated packs for ...B and therefore knows it will have been set in targs in unify_pack_expansion and processes it as per other pack arguments. PR c++/118265 gcc/cp/ChangeLog: * pt.cc (find_parameter_packs_r) <case TEMPLATE_PARM_INDEX>: Walk into the type of a parameter pack. Signed-off-by: Adam J Ryan <gcc.gnu.org@ajryansolutions.co.uk>
2025-01-30OpenMP: append_args clause fixes + Fortran supportTobias Burnus1-7/+21
This fixes a large number of smaller and larger issues with the append_args clause to 'declare variant' and adds Fortran support for it; it also contains a larger number of testcases. In particular, for Fortran, it also handles passing allocatable, pointer, optional arguments to an interop dummy argument with or without value attribute. And it changes the internal representation such that dumping the tree does not lead to an ICE. gcc/c/ChangeLog: * c-parser.cc (c_finish_omp_declare_variant): Modify how append_args is saved internally. gcc/cp/ChangeLog: * parser.cc (cp_finish_omp_declare_variant): Modify how append_args is saved internally. * pt.cc (tsubst_attribute): Likewise. (tsubst_omp_clauses): Remove C_ORT_OMP_DECLARE_SIMD from interop handling as no longer called for it. * decl.cc (omp_declare_variant_finalize_one): Update append_args changes; fixes for ADL input. gcc/fortran/ChangeLog: * gfortran.h (gfc_omp_declare_variant): Add append_args_list. * openmp.cc (gfc_parser_omp_clause_init_modifiers): New; splitt of from ... (gfc_match_omp_init): ... here; call it. (gfc_match_omp_declare_variant): Update to handle append_args clause; some syntax handling fixes. * trans-openmp.cc (gfc_trans_omp_declare_variant): Handle append_args clause; add some diagnostic. gcc/ChangeLog: * gimplify.cc (gimplify_call_expr): For OpenMP's append_args clause processed by 'omp dispatch', update for internal-representation changes; fix handling of hidden arguments, add some comments and handle Fortran's value dummy and optional/pointer/allocatable actual args. libgomp/ChangeLog: * libgomp.texi (Impl. Status): Update for accumpulated changes related to 'dispatch' and interop. gcc/testsuite/ChangeLog: * c-c++-common/gomp/append-args-1.c: Update dg-*. * c-c++-common/gomp/append-args-3.c: Likewise. * g++.dg/gomp/append-args-1.C: Likewise. * gfortran.dg/gomp/adjust-args-1.f90: Likewise. * gfortran.dg/gomp/adjust-args-3.f90: Likewise. * gfortran.dg/gomp/declare-variant-2.f90: Likewise. * c-c++-common/gomp/append-args-6.c: New test. * c-c++-common/gomp/append-args-7.c: New test. * c-c++-common/gomp/append-args-8.c: New test. * c-c++-common/gomp/append-args-9.c: New test. * g++.dg/gomp/append-args-4.C: New test. * g++.dg/gomp/append-args-5.C: New test. * g++.dg/gomp/append-args-6.C: New test. * g++.dg/gomp/append-args-7.C: New test. * gcc.dg/gomp/append-args-1.c: New test. * gfortran.dg/gomp/append_args-1.f90: New test. * gfortran.dg/gomp/append_args-2.f90: New test. * gfortran.dg/gomp/append_args-3.f90: New test. * gfortran.dg/gomp/append_args-4.f90: New test.
2025-01-27c++: only strip conversions for deduction [PR118632]Jason Merrill1-14/+10
In r15-2761 I changed unify to always strip IMPLICIT_CONV_EXPR from PARM. In this testcase that leads to comparing nullptr to (int*)0, and failing because they aren't the same. Let's only strip conversions if we're actually going to try to deduce template arguments. While we're at it, let's move this after the early exits. And with this adjustment we can remove the workaround for mangle57.C. PR c++/118632 gcc/cp/ChangeLog: * pt.cc (unify): Only strip conversion if deducible_expression. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/nontype7.C: New test.
2025-01-27c++: Don't prune constant capture proxies only used in array dimensions ↵Simon Martin1-5/+0
[PR114292] We currently ICE upon the following valid (under -Wno-vla) code === cut here === void f(int c) { constexpr int r = 4; [&](auto) { int t[r * c]; }(0); } === cut here === When parsing the lambda body, and more specifically the multiplication, we mark the lambda as LAMBDA_EXPR_CAPTURE_OPTIMIZED, which indicates to prune_lambda_captures that it might be possible to optimize out some captures. The problem is that prune_lambda_captures then misses the use of the r capture (because neither walk_tree_1 nor cp_walk_subtrees walks the dimensions of array types - here "r * c"), hence believes the capture can be pruned... and we trip on an assert when instantiating the lambda. This patch changes cp_walk_subtrees so that (1) when walking a DECL_EXPR, it also walks the DECL's type, and (2) when walking an INTEGER_TYPE, it also walks its TYPE_{MIN,MAX}_VALUE. Note that #2 makes a <case INTEGER_TYPE> redundant in for_each_template_parm_r, and removes it. PR c++/114292 gcc/cp/ChangeLog: * pt.cc (for_each_template_parm_r) <INTEGER_TYPE>: Remove case now handled by cp_walk_subtrees. * tree.cc (cp_walk_subtrees): Walk the type of DECL_EXPR declarations, as well as the TYPE_{MIN,MAX}_VALUE of INTEGER_TYPEs. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/lambda-ice4.C: New test.
2025-01-25c++/modules: Diagnose TU-local lambdas, give mangling scope to lambdas in ↵Nathaniel Shead1-6/+15
concepts This fills in a hole left in r15-6378-g9016c5ac94c557 with regards to detection of TU-local lambdas. Now that LAMBDA_EXPR_EXTRA_SCOPE is properly set for most lambdas we can use it to detect lambdas that are TU-local. CWG2988 suggests that lambdas in concept definitions should not be considered TU-local, since they are always unevaluated and should never be emitted. This patch gives these lambdas a mangling scope (though it will never be actually used in name mangling). PR c++/116568 gcc/cp/ChangeLog: * cp-tree.h (finish_concept_definition): Adjust parameters. (start_concept_definition): Declare. * module.cc (depset::hash::is_tu_local_entity): Use LAMBDA_EXPR_EXTRA_SCOPE to detect TU-local lambdas. * parser.cc (cp_parser_concept_definition): Start a lambda scope for concept definitions. * pt.cc (tsubst_lambda_expr): Namespace-scope lambdas may now have extra scope. (finish_concept_definition): Split into... (start_concept_definition): ...this new function. gcc/testsuite/ChangeLog: * g++.dg/modules/internal-4_b.C: Remove XFAIL, add lambda alias testcase. * g++.dg/modules/lambda-9.h: New test. * g++.dg/modules/lambda-9_a.H: New test. * g++.dg/modules/lambda-9_b.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Jason Merrill <jason@redhat.com>
2025-01-25c++: Fix mangling of otherwise unattached class-scope lambdas [PR118245]Nathaniel Shead1-0/+6
This is a step closer to implementing the suggested changes for https://github.com/itanium-cxx-abi/cxx-abi/pull/85. Most lambdas defined within a class should have an extra scope of that class so that uses across different TUs are properly merged by the linker. This also needs to happen during template instantiation. While I was working on this I found some other cases where the mangling of lambdas was incorrect and causing issues, notably the testcase lambda-ctx3.C which currently emits the same mangling for the base class and member lambdas, causing mysterious assembler errors since r14-9232. One notable case not handled either here or in the ABI is what is supposed to happen with such unattached lambdas declared in member templates; see lambda-uneval22. I believe that by the C++ standard, such lambdas should also dedup across TUs, but this isn't currently implemented, and it's not clear exactly how such lambdas should mangle. Since this should only affect usage of lambdas in unevaluated contexts (a C++20 feature) this patch does not add an ABI flag to control this behaviour. PR c++/118245 gcc/cp/ChangeLog: * cp-tree.h (LAMBDA_EXPR_EXTRA_SCOPE): Adjust comment. * parser.cc (cp_parser_class_head): Start (and do not finish) lambda scope for all valid types. (cp_parser_class_specifier): Finish lambda scope after parsing members instead. * pt.cc (instantiate_class_template): Add lambda scoping. gcc/testsuite/ChangeLog: * g++.dg/abi/lambda-ctx3.C: New test. * g++.dg/cpp2a/lambda-uneval22.C: New test. * g++.dg/cpp2a/lambda-uneval23.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Jason Merrill <jason@redhat.com>