================
@@ -1061,15 +1061,36 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef,
   SmallVector<DeducedTemplateArgument> DeduceResults(
       F->getTemplateParameters()->size());
 
+  // We don't have to deduce against the alias template specialization,
+  // if the source template is a synthesized alias deduction guide. This allows
+  // us to utilize the default template arguments from alias declaration.
+  //
+  //  template <class T>
+  //  using Foo = A<A<T>>;
+  //
+  //  template <class U = int>
+  //  using Bar = Foo<U>;
+  //
+  // In terms of Bar, we want U to appear in the synthesized deduction guide,
+  // but U would remain undeduced if we deduce against A<T> instead of T.
+  // Also note that since the deduced results are only used for synthesizing
+  // template parameters, they should not introduce unintended behavior in
+  // theory.
+  ArrayRef<TemplateArgument> Ps = FReturnType->template_arguments();
+  if (auto *DG = dyn_cast<CXXDeductionGuideDecl>(F->getTemplatedDecl());
+      DG && DG->getSourceDeductionGuideKind() ==
+                CXXDeductionGuideDecl::SourceDeductionGuideKind::Alias)
+    Ps = F->getInjectedTemplateArgs(Context);
----------------
zyn0217 wrote:

> Also, wouldn't we always be wanting to deduce against the injected template 
> parameters of F anyway, so you could instead just change 
> FReturnType->template_arguments() below to 
> F->getInjectedTemplateArgs(Context)?

Ugh actually I ran into an assertion failure even without your suggestion

```cpp
template<typename T, typename...Us>
struct A{
  template<typename V> requires __is_same(V, int)
  A(V);
};

template<typename...TS>
using AA = A<int, TS...>;

template<typename...TS>
using BB = AA<TS...>;

BB a{0};
```

The (synthesized) template parameters are not guaranteed to be syntactially 
correct, so we ended up having
```cpp
template <typename... Us, typename V>
AA(V) -> A<int, Us...>;
```
for AA, and using `<Us..., V>` instead of `<int, Us...>` would violate the 
assumption in `hasTemplateArgumentForDeduction`: `Pack not at the end of 
argument list?`


https://github.com/llvm/llvm-project/pull/147675
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to