From: Eric Fiselier Date: Thu, 17 Jan 2019 21:44:24 +0000 (+0000) Subject: Add -Wctad-maybe-unsupported to diagnose CTAD on types with no user defined deduction... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8abc38eefae434edc7fc43ab41086bfccb0641e3;p=clang Add -Wctad-maybe-unsupported to diagnose CTAD on types with no user defined deduction guides. Summary: 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. This patch implements the `-Wctad-maybe-unsupported` warning, which is off by default, which warns when CTAD is used on a type that does not define any deduction guides. The following pattern can be used to suppress the warning in cases where the type intentionally doesn't define any deduction guides: ``` struct allow_ctad_t; template struct TestSuppression { TestSuppression(T) {} }; TestSuppression(allow_ctad_t)->TestSuppression; // guides with incomplete parameter types are never considered. ``` Reviewers: rsmith, james.dennett, gromer Reviewed By: rsmith Subscribers: jdennett, Quuxplusone, lebedev.ri, cfe-commits Differential Revision: https://reviews.llvm.org/D56731 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@351484 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td index 19e187cc5d..568fbf1eee 100644 --- a/include/clang/Basic/DiagnosticGroups.td +++ b/include/clang/Basic/DiagnosticGroups.td @@ -1050,3 +1050,5 @@ def NoDeref : DiagGroup<"noderef">; // A group for cross translation unit static analysis related warnings. def CrossTU : DiagGroup<"ctu">; + +def CTADMaybeUnsupported : DiagGroup<"ctad-maybe-unsupported">; diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index b71f65d146..a1029a22eb 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2129,6 +2129,12 @@ def warn_cxx14_compat_class_template_argument_deduction : Warning< "class template argument deduction is incompatible with C++ standards " "before C++17%select{|; for compatibility, use explicit type name %1}0">, InGroup, DefaultIgnore; +def warn_ctad_maybe_unsupported : Warning< + "%0 may not intend to support class template argument deduction">, + InGroup, DefaultIgnore; +def note_suppress_ctad_maybe_unsupported : Note< + "add a deduction guide to suppress this warning">; + // C++14 deduced return types def err_auto_fn_deduction_failure : Error< diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 10c0c6bf33..b5a0a88816 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -9264,9 +9264,14 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer( OverloadCandidateSet Candidates(Kind.getLocation(), OverloadCandidateSet::CSK_Normal); OverloadCandidateSet::iterator Best; + + bool HasAnyDeductionGuide = false; + auto tryToResolveOverload = [&](bool OnlyListConstructors) -> OverloadingResult { Candidates.clear(OverloadCandidateSet::CSK_Normal); + HasAnyDeductionGuide = false; + for (auto I = Guides.begin(), E = Guides.end(); I != E; ++I) { NamedDecl *D = (*I)->getUnderlyingDecl(); if (D->isInvalidDecl()) @@ -9278,6 +9283,9 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer( if (!GD) continue; + if (!GD->isImplicit()) + HasAnyDeductionGuide = true; + // C++ [over.match.ctor]p1: (non-list copy-initialization from non-class) // For copy-initialization, the candidate functions are all the // converting constructors (12.3.1) of that class. @@ -9430,5 +9438,15 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer( Diag(TSInfo->getTypeLoc().getBeginLoc(), diag::warn_cxx14_compat_class_template_argument_deduction) << TSInfo->getTypeLoc().getSourceRange() << 1 << DeducedType; + + // Warn if CTAD was used on a type that does not have any user-defined + // deduction guides. + if (!HasAnyDeductionGuide) { + Diag(TSInfo->getTypeLoc().getBeginLoc(), + diag::warn_ctad_maybe_unsupported) + << TemplateName; + Diag(Template->getLocation(), diag::note_suppress_ctad_maybe_unsupported); + } + return DeducedType; } diff --git a/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp b/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp index c310c65140..29adc8f560 100644 --- a/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp +++ b/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp @@ -409,6 +409,86 @@ B b(0, {}); } +#pragma clang diagnostic push +#pragma clang diagnostic warning "-Wctad-maybe-unsupported" +namespace test_implicit_ctad_warning { + +template +struct Tag {}; + +template +struct NoExplicit { // expected-note {{add a deduction guide to suppress this warning}} + NoExplicit(T) {} + NoExplicit(T, int) {} +}; + +// expected-warning@+1 {{'NoExplicit' may not intend to support class template argument deduction}} +NoExplicit ne(42); + +template +struct HasExplicit { + HasExplicit(U) {} + HasExplicit(U, int) {} +}; +template HasExplicit(U, int) -> HasExplicit>; + +HasExplicit he(42); + +// Motivating examples from (taken from Stephan Lavavej's 2018 Cppcon talk) +template +struct AmateurPair { // expected-note {{add a deduction guide to suppress this warning}} + T first; + U second; + explicit AmateurPair(const T &t, const U &u) {} +}; +// expected-warning@+1 {{'AmateurPair' may not intend to support class template argument deduction}} +AmateurPair p1(42, "hello world"); // deduces to Pair + +template +struct AmateurPair2 { // expected-note {{add a deduction guide to suppress this warning}} + T first; + U second; + explicit AmateurPair2(T t, U u) {} +}; +// expected-warning@+1 {{'AmateurPair2' may not intend to support class template argument deduction}} +AmateurPair2 p2(42, "hello world"); // deduces to Pair2 + +template +struct ProPair { + T first; U second; + explicit ProPair(T const& t, U const& u) {} +}; +template +ProPair(T1, T2) -> ProPair; +ProPair p3(42, "hello world"); // deduces to ProPair +static_assert(__is_same(decltype(p3), ProPair)); + +// Test that user-defined explicit guides suppress the warning even if they +// aren't used as candidates. +template +struct TestExplicitCtor { + TestExplicitCtor(T) {} +}; +template +explicit TestExplicitCtor(TestExplicitCtor const&) -> TestExplicitCtor; +TestExplicitCtor ce1{42}; +TestExplicitCtor ce2 = ce1; +static_assert(__is_same(decltype(ce2), TestExplicitCtor), ""); + +struct allow_ctad_t { + allow_ctad_t() = delete; +}; + +template +struct TestSuppression { + TestSuppression(T) {} +}; +TestSuppression(allow_ctad_t)->TestSuppression; +TestSuppression ta("abc"); +static_assert(__is_same(decltype(ta), TestSuppression), ""); +} +#pragma clang diagnostic pop + #else // expected-no-diagnostics