Skip to content

Defer expansion statement body instantiation#261

Open
Perdixky wants to merge 2 commits intobloomberg:p2996from
Perdixky:p2996
Open

Defer expansion statement body instantiation#261
Perdixky wants to merge 2 commits intobloomberg:p2996from
Perdixky:p2996

Conversation

@Perdixky
Copy link

@Perdixky Perdixky commented Feb 5, 2026

Compute expansion size and instantiate after expansion to avoid premature body instantiation and spurious diagnostics. Reproducer: https://godbolt.org/z/dxqPxe6ed

Perdixky added 2 commits February 5, 2026 18:58
Compute expansion size and instantiate after expansion to avoid premature body instantiation and spurious diagnostics. Reproducer: https://godbolt.org/z/dxqPxe6ed
- Change ExpansionStmtDecl to store Stmt* instead of CXXExpansionStmt*
  to support expanded CompoundStmt results from non-dependent expansions
- Update ExpansionStmtDecl::Create, constructor, getStmt, and setStmt
  signatures to use Stmt*
- Modify FinishCXXExpansionStmt(Heading, Body) to delegate to the new
  FinishCXXExpansionStmt(Stmt*, UnsignedOrNone) after setting the body
- Remove redundant expansion logic from the old overload
- Update callers in ParseStmt.cpp, SemaTemplateInstantiateDecl.cpp,
  and ASTReaderDecl.cpp to remove unnecessary casts
Copilot AI review requested due to automatic review settings February 5, 2026 12:27
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR defers expansion statement body instantiation to avoid premature instantiation and spurious diagnostics when working with nested template expansions over annotations.

Changes:

  • Refactored ExpansionStmtDecl to store a generic Stmt* instead of CXXExpansionStmt* to support deferred instantiation
  • Added a new FinishCXXExpansionStmt overload that takes UnsignedOrNone Size to enable deferred body instantiation
  • Updated parsing, serialization, and template instantiation code to work with the more general Stmt* type
  • Added test coverage for nested template for loops over annotations and error cases

Reviewed changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
libcxx/test/std/experimental/reflection/p3394-annotations.pass.cpp Added test for nested template for over annotations, plus formatting improvements
clang/test/SemaCXX/cxx2c-expansion-stmts.cpp Added tests for non-constant expansion size and label diagnostics
clang/lib/Sema/SemaExpand.cpp Refactored to support deferred expansion with new FinishCXXExpansionStmt overload
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp Updated to work with Stmt* type, formatting improvements
clang/lib/Serialization/ASTReaderDecl.cpp Updated deserialization to use readStmt(), formatting improvements
clang/lib/Parse/ParseStmt.cpp Updated parser to work with Stmt* type, formatting improvements
clang/lib/AST/DeclTemplate.cpp Updated ExpansionStmtDecl implementation, formatting improvements
clang/lib/Sema/SemaReflect.cpp Removed redundant comment
clang/include/clang/Sema/Sema.h Added new FinishCXXExpansionStmt signature
clang/include/clang/AST/DeclTemplate.h Changed ExpansionStmtDecl to use Stmt* instead of CXXExpansionStmt*

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants