c++: Implement -Wctad-maybe-unsupported.
authorMarek Polacek <polacek@redhat.com>
Sat, 19 Sep 2020 20:17:42 +0000 (16:17 -0400)
committerMarek Polacek <polacek@redhat.com>
Mon, 21 Sep 2020 21:48:11 +0000 (17:48 -0400)
commit7029dfa38b663d20e0de40395fcd45a2845e2f71
tree8e17199e922c860dc4b9227e1a01b94d56e4a8c9
parent68402af1c68301c6bc852ddba6c63966ed706178
c++: Implement -Wctad-maybe-unsupported.

I noticed that clang++ has this CTAD warning and thought that it might
be useful to have it.  From clang++: "Some style guides want to allow
using CTAD only on types that "opt-in"; i.e. on types that are designed
to support it and not just types that *happen* to work with it."

So this warning warns when CTAD deduced a type, but the type does not
define any deduction guides.  In that case CTAD worked only because the
compiler synthesized the implicit deduction guides.  That might not be
intended.

It can be suppressed by adding a deduction guide that will never be
considered:

  struct allow_ctad_t;
  template <typename T> struct S { S(T) {} };
  S(allow_ctad_t) -> S<void>;

This warning is off by default.  It doesn't warn when the type comes
from a system header unless -Wsystem-headers.

gcc/c-family/ChangeLog:

* c.opt (Wctad-maybe-unsupported): New option.

gcc/cp/ChangeLog:

* pt.c (deduction_guides_for): Add a bool parameter.  Set it.
(do_class_deduction): Warn when CTAD succeeds but the type doesn't
have any explicit deduction guides.

gcc/ChangeLog:

* doc/invoke.texi: Document -Wctad-maybe-unsupported.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wctad-maybe-unsupported.C: New test.
* g++.dg/warn/Wctad-maybe-unsupported2.C: New test.
* g++.dg/warn/Wctad-maybe-unsupported3.C: New test.
* g++.dg/warn/Wctad-maybe-unsupported.h: New file.
gcc/c-family/c.opt
gcc/cp/pt.c
gcc/doc/invoke.texi
gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported.h [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported3.C [new file with mode: 0644]