[clang] [clang] Implement P2582R1: CTAD from inherited constructors (PR #98788)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 6 14:26:40 PST 2024


================
@@ -931,12 +935,73 @@ getRHSTemplateDeclAndArgs(Sema &SemaRef, TypeAliasTemplateDecl *AliasTemplate) {
   return {Template, AliasRhsTemplateArgs};
 }
 
+struct InheritedConstructorDeductionInfo {
+  // Class template for which we are declaring deduction guides
+  // This is `C` in the standard wording
+  TemplateDecl *DerivedClassTemplate;
+
+  // `template<typename> CC` in the standard wording
+  // This is the type of template that is substituted in the deduction guide
+  // return type `CC<R>`
+  TypeSourceInfo *CCType;
+};
+
+// Build the type for a deduction guide generated from an inherited constructor
+// C++23 [over.match.class.deduct]p1.10:
+// ... the set contains the guides of A with the return type R
+// of each guide replaced with `typename CC<R>::type` ...
+TypeSourceInfo *buildInheritedConstructorDeductionGuideType(
+    Sema &SemaRef, const InheritedConstructorDeductionInfo &Info,
+    TypeSourceInfo *SourceGuideTSI) {
+  auto &Context = SemaRef.Context;
+  const auto *FPT = SourceGuideTSI->getType()->getAs<FunctionProtoType>();
+  assert(FPT && "Source Guide type should be a FunctionProtoType");
+
+  // This substitution can fail in cases where the source return type
+  // is not dependent and the derived class is not deducible
+  Sema::SFINAETrap Trap(SemaRef);
----------------
antangelo wrote:

If we don't suppress diagnostics here, then these substitution failure errors would appear at the time that implicit deduction guides are declared (regardless of whether or not the failing deduction guide was actually used).

Right now any deduction guide that fails substitution here will be skipped, and not inherited by the base. Unfortunately this means that we don't get diagnostics for them, but there isn't a great way to propagate the error through to the point where deduction of the derived class fails. We don't have the return type to create a valid deduction guide AST node, like we do in other cases where we know deduction will fail up front (e.g. with default parameters not appearing in the base), so some other mechanism is needed to preserve this information until deduction of the derived class has failed and the candidate notes are being printed.

https://github.com/llvm/llvm-project/pull/98788


More information about the cfe-commits mailing list