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

Pack indexing expression is not being instantiated #121242

Open
Eczbek opened this issue Dec 28, 2024 · 3 comments
Open

Pack indexing expression is not being instantiated #121242

Eczbek opened this issue Dec 28, 2024 · 3 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party

Comments

@Eczbek
Copy link

Eczbek commented Dec 28, 2024

Compiler Explorer: https://godbolt.org/z/TG4hMMPv3

template<int... x>
int y = x...[0];

int main() {
	y<0>;
}
<source>:2:5: error: cannot initialize a variable of type 'int' with an lvalue of type '<dependent type>'
    2 | int y = x...[0];
      |     ^   ~~~~~~~
<source>:5:2: warning: expression result unused [-Wunused-value]
    5 |         y<0>;
      |         ^~~~
1 warning and 1 error generated.
Compiler returned: 1
@Sirraide Sirraide added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed new issue labels Dec 28, 2024
@Sirraide
Copy link
Member

CC @cor3ntin

@llvmbot
Copy link
Member

llvmbot commented Dec 28, 2024

@llvm/issue-subscribers-clang-frontend

Author: None (Eczbek)

Compiler Explorer: https://godbolt.org/z/TG4hMMPv3
template&lt;int... x&gt;
int y = x...[0];

int main() {
	y&lt;0&gt;;
}
&lt;source&gt;:2:5: error: cannot initialize a variable of type 'int' with an lvalue of type '&lt;dependent type&gt;'
    2 | int y = x...[0];
      |     ^   ~~~~~~~
&lt;source&gt;:5:2: warning: expression result unused [-Wunused-value]
    5 |         y&lt;0&gt;;
      |         ^~~~
1 warning and 1 error generated.
Compiler returned: 1

@Sirraide Sirraide added the confirmed Verified by a second party label Dec 28, 2024
@zyn0217
Copy link
Contributor

zyn0217 commented Dec 28, 2024

It’s not that we're missing an instantiation for y<0>, but rather that we checked the initialization too early, right after the variable template was constructed. This is because the first PackIndexingExpr was assigned a ValueInstantiation dependency but retained a dependent type, which caused the logic to be skipped.

if (DestType->isDependentType() ||
Expr::hasAnyTypeDependentArguments(Args)) {
SequenceKind = DependentSequence;
return;
}

if (Index && FullySubstituted && !SubstitutedExprs.empty())
Type = SubstitutedExprs[*Index]->getType();
else
Type = Context.DependentTy;

We should probably set the expr type to PackIdExpr->getType() on line 1725.

This also indicates that a workaround is to convert the NTTP to a dependent type, i.e. the following would bypass the initialization check that happens before we see y<0> and its instantiation

template<auto... x>
int y = x...[0];

int main() {
	y<0>;
}

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" confirmed Verified by a second party
Projects
None yet
Development

No branches or pull requests

4 participants