Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clang's error on non-pack parameter of alias template in a dependent type #84220

Closed
jonathanpoelen opened this issue Mar 6, 2024 · 5 comments · Fixed by #87768
Closed

Clang's error on non-pack parameter of alias template in a dependent type #84220

jonathanpoelen opened this issue Mar 6, 2024 · 5 comments · Fixed by #87768
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@jonathanpoelen
Copy link

It seems that clang does not consider sizeof...(xs) to be a dependent expression when the call to foo is made with a known number of arguments. The expression must absolutely contain xs....

The following code compiles with gcc, but not clang (https://godbolt.org/z/TYzvjTePT):

template<int>
struct foo_impl {
  template<class>
  using f = int;
};

template<class... xs> constexpr int sizeof_pack = sizeof...(xs);

template<class... xs>
using foo = typename foo_impl< // error: pack expansion used as argument for non-pack parameter of alias template
  sizeof...(xs) // does not work
  // sizeof_pack<xs...>  // ok
>::template f<xs...>; // f is dependent on xs

template<class... xs>
using test = list<foo<xs>...>;

// template<class... xs>
// using test = foo<xs...>; // ok with pack...

test<int> a; // ko
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Mar 6, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang Clang issues not falling into any other category labels Mar 6, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 6, 2024

@llvm/issue-subscribers-clang-frontend

Author: Jonathan Poelen (jonathanpoelen)

It seems that clang does not consider `sizeof...(xs)` to be a dependent expression when the call to foo is made with a known number of arguments. The expression must absolutely contain `xs...`.

The following code compiles with gcc, but not clang (https://godbolt.org/z/TYzvjTePT):

template&lt;int&gt;
struct foo_impl {
  template&lt;class&gt;
  using f = int;
};

template&lt;class... xs&gt; constexpr int sizeof_pack = sizeof...(xs);

template&lt;class... xs&gt;
using foo = typename foo_impl&lt; // error: pack expansion used as argument for non-pack parameter of alias template
  sizeof...(xs) // does not work
  // sizeof_pack&lt;xs...&gt;  // ok
&gt;::template f&lt;xs...&gt;; // f is dependent on xs

template&lt;class... xs&gt;
using test = list&lt;foo&lt;xs&gt;...&gt;;

// template&lt;class... xs&gt;
// using test = foo&lt;xs...&gt;; // ok with pack...

test&lt;int&gt; a; // ko

@shafik
Copy link
Collaborator

shafik commented Mar 6, 2024

Not sure who is correct: https://godbolt.org/z/6fM5do836

clang/MSVC reject
gcc/edg accept

@shafik
Copy link
Collaborator

shafik commented Mar 7, 2024

Maybe this is cwg1430

Similar gcc bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59701

Not totally sure.

CC @zygoloid @erichkeane

@zygoloid
Copy link
Collaborator

zygoloid commented Mar 7, 2024

While canonicalizing the RHS of test, we form foo<xs>, for which sizeof...(xs) is 1. This gives

using foo<xs> = typename foo_impl<1>::template f<{xs}...>;

... which I suppose we reject per #1430. But I don't think we should: at this point, {xs}... should have already been expanded into the single argument xs, which is not itself a pack expansion, so #1430 shouldn't apply.

@zyn0217
Copy link
Contributor

zyn0217 commented Apr 3, 2024

The issue arises in RebuildDependentTemplateSpecializationType, where the working case using test = foo<xs...> causes name template f to be considered dependent (and thus the diagnosis in CheckTemplateArgumentList gets bypassed) while the other using test = list<foo<xs>...> resolves the name template f in some way, resulting in the diagnosis in CheckTemplateArgumentList.

zyn0217 added a commit to zyn0217/llvm-project that referenced this issue Apr 7, 2024
…pandedSize

There has been an optimization for SizeOfPackExprs since c5452ed, in which
we overlooked a case where the template arguments were not yet
formed into a PackExpansionType at the token annotation stage. This
led to a problem in that a template involving such expressions may
lose its nature of being dependent, causing some false-positive
diagnostics.

Fixes llvm#84220
zyn0217 added a commit that referenced this issue Apr 10, 2024
…pandedSize (#87768)

There has been an optimization for `SizeOfPackExprs` since c5452ed, in
which
we overlooked a case where the template arguments were not yet
formed into a `PackExpansionType` at the token annotation stage. This
led to a problem in that a template involving such expressions may
lose its nature of being dependent, causing some false-positive
diagnostics.

Fixes #84220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants