]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: add -Wsfinae-incomplete
authorJason Merrill <jason@redhat.com>
Thu, 12 Jun 2025 15:19:19 +0000 (11:19 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 16 Jun 2025 15:34:21 +0000 (11:34 -0400)
commit117782e0c2a81a4b8170f87f0fe7190ee22548e2
tree6463754b08d79c5b1961dc340135d52d9a553d77
parentad909d5c0ce7fcd0bcbacd0ee20c15bf479fd990
c++: add -Wsfinae-incomplete

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.
16 files changed:
gcc/c-family/c.opt
gcc/c-family/c.opt.urls
gcc/cp/class.cc
gcc/cp/constraint.cc
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/pt.cc
gcc/cp/typeck.cc
gcc/doc/invoke.texi
gcc/testsuite/g++.dg/cpp2a/concepts-complete1.C
gcc/testsuite/g++.dg/cpp2a/concepts-complete2.C
gcc/testsuite/g++.dg/cpp2a/concepts-complete3.C
gcc/testsuite/g++.dg/cpp2a/concepts-complete4.C
gcc/testsuite/g++.dg/template/friend87.C
libstdc++-v3/testsuite/20_util/is_complete_or_unbounded/memoization.cc
libstdc++-v3/testsuite/20_util/is_complete_or_unbounded/memoization_neg.cc