<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/66562>66562</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] ICE on handling reference to consteval function in initializer
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          groundswellaudio
      </td>
    </tr>
</table>

<pre>
    ```cpp 
namespace ns
{
    consteval int foo(int x) { return 1; }
}

template <class A>
struct T {
    static constexpr auto xx = ns::foo(A{});
};
```

crash with 

```
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++20 <source> -isystem/opt/compiler-explorer/libs/catch2/v3.0.1/src
1.      <source>:9:44: current parser token ';'
2.      <source>:8:1: parsing struct/union/class body 'T'
 #0 0x00000000033cba18 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x33cba18)
 #1 0x00000000033c9b44 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x33c9b44)
 #2 0x0000000003318b68 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007fc4f42c7420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x0000000000ad8e28 clang::Sema::MarkExpressionAsImmediateEscalating(clang::Expr*) (.cold) SemaExpr.cpp:0:0
 #5 0x0000000005f39f3d clang::Sema::PopExpressionEvaluationContext() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5f39f3d)
 #6 0x0000000005a025e4 clang::Parser::ParseCXXMemberInitializer(clang::Decl*, bool, clang::SourceLocation&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5a025e4)
 #7 0x0000000005a15a5f clang::Parser::ParseCXXClassMemberDeclaration(clang::AccessSpecifier, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo const&, clang::ParsingDeclRAIIObject*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5a15a5f)
 #8 0x0000000005a16d34 clang::Parser::ParseCXXClassMemberDeclarationWithPragmas(clang::AccessSpecifier&, clang::ParsedAttributes&, clang::TypeSpecifierType, clang::Decl*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5a16d34)
 #9 0x0000000005a173a6 clang::Parser::ParseCXXMemberSpecification(clang::SourceLocation, clang::SourceLocation, clang::ParsedAttributes&, unsigned int, clang::Decl*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5a173a6)
#10 0x0000000005a19679 clang::Parser::ParseClassSpecifier(clang::tok::TokenKind, clang::SourceLocation, clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, bool, clang::Parser::DeclSpecContext, clang::ParsedAttributes&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5a19679)
#11 0x00000000059e9414 clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*, clang::ImplicitTypenameContext) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x59e9414)
....
```

https://godbolt.org/z/5a8hPo5je

On Clang 16, this used to error out with : 
```
<source>:9:32: error: cannot take address of consteval function 'foo' outside of an immediate invocation
    static constexpr auto xx = ns::foo(A{});
 ^
<source>:4:19: note: declared here
    consteval int foo(int x) { return 1; }
```

I think the problem stems from 
1/ during the parsing of the initializer, the ExpressionEvaluationContext is not set to ConstantEvaluated, so the reference to ns::foo is added to ReferenceToConsteval
2/ When popping the Sema::PopExpressionEvalContext is called, `Context.InImmediateEscalatingFunctionContext` is true, most likely because `ParseCXXMemberInitializer` contains this line : 

```
 Actions.ExprEvalContexts.back().InImmediateEscalatingFunctionContext = true;
```

Which does not makes a lot of sense to me (besides this kind of state mutation would be more suitable on the Sema side? e.g. in Sema::ActOnStartCXXInClassMemberInitializer/Finish).

Finally, the evaluation context should probably be set to ConstantEvaluated when the variable is `constexpr` to not error on the declaration reference. 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0WF9z4rjy_TTKS5cpIxsDD3kgEOqX-u3WpDapmnmbkuUGtMiSS5ITsp_-VsuGmAzLzL2bnZoZjN1unXP6j9QI79XWIN6yyR2brG5EG3bW3W6dbU3lX1Fr0VbK3pS2ertlRdr9lU0DLF2xdGFEjb4REsH47g6b3nUXAADSGh_wRWhQJsDGWsZndHVgfA5segcOQ-sMjFl2B2y6Oro4XsT_A9aNFgGBZUuphfewYNl998wH18oAz3C2rA8iKNmvfmgciDZYOByAZSsCmi1YtujQRLzTFeNzlt29L3-6PlIeApJO-B28qrCD4e0Ptk9ByD1Ubd3QevFWOmLp_NHZrRM1CLdtazSB4ADja9sExtfS1o3S6BI8NNo6dHRPC7NNgmvNnvF1qczxHuN3jN9Bsq1ehdskOSRbSCw5E01DLtvQtGHkIam1fqkhSQ6zIhG-TvybCeLAspUyATUkT5AkWymTYK2WO6EMy1ZXMZGxN6LxOxsg2UirrUsqJbbG-qCkh2RjbBKVOr_tQ8Wyleyg85SC6m3rJLLsHhLl33zA-urSWpWeHokgd5zx9Us2SkdjxtfeyU7nMek8dMyyxZxlizwnrWXrHJoAjXAeHQS7RwOMTynofNp54Bc8zFi2GJMDelGZLXTJx_i6Ncr2QfEeqFbI3_PJGzCepZAe0uOfLJOlGM-AotJlo3_r0_LRKRNi7jw7IZHx2buRE6_frQ8ORc14wfiSyiqWEp_9kwxi_C499JioEk6gxx9Az8s8vwR6qVGYtvlintTWCM34rDWxsVSgLa3yaRAJwRlEfg5xPCuLGSwp7_5AaV_QvXWY_k-YStNKs16zM5ulNQEPYSQbqtY0_j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