Skip to content

Init-capture visibility in lambda headers in emulation modes [GH #470] - #513

Merged
daveedvdv-nvidia merged 1 commit into
mainfrom
fix/470
Nov 11, 2026
Merged

daveedvdv-nvidia merged 1 commit into
mainfrom
fix/470

Conversation

@daveedvdv-nvidia

@daveedvdv-nvidia daveedvdv-nvidia commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

P2579 (adopted as a defect report) makes init-captures visible throughout a lambda header: The front end implemented that in all modes. However, GCC before version 16, Clang before version 17, and MSVC before 19.50 (and still in pre-C++20 modes) make init-captures visible only in the lambda body: In the lambda header (a parameter type, an exception specification, or a trailing return type), the name of an init-capture refers to an entity of the enclosing scope instead. This change emulates that in the various corresponding modes.

The fields for the init-captures are declared in the closure class before the lambda header is scanned, so in those modes set_init_capture_visibility_if_needed (in class_decl.c) makes them invisible to lookup while the header is scanned (both when the lambda is first scanned and when the call operator of a generic lambda is rescanned), and visible again for the body.

As an approximation of MSVC behavior, in pre-C++20 Microsoft modes the init-captures are made visible again right after the parameter list of a generic lambda (see end_of_generic_lambda_params, called from declarator.c): MSVC then finds them in the exception specification and trailing return type, though not in the parameter types.

Fixes #470

Summary by CodeRabbit

  • Bug Fixes

    • Improved C++ lambda parsing so init-captures resolve consistently with the selected language mode and compiler compatibility settings, including in lambda parameter lists and other header expressions.
    • Corrected handling for generic lambdas in older Microsoft compatibility modes.
  • Tests

    • Added regression coverage across C++ standards and compiler versions, including lambda parameter types, exception specifications, trailing return types, and lambda bodies.

@daveedvdv-nvidia
daveedvdv-nvidia requested a review from a team November 10, 2026 19:33
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adjusts init-capture visibility while parsing lambda headers in specified compiler compatibility modes. It adds a generic-lambda parameter boundary hook, regression tests across compiler versions and language modes, and a compatibility note.

Changes: Lambda-header visibility

Layer / File(s) Summary
Visibility rules and parameter boundary
src/class_decl.c, src/class_decl.h
A helper adjusts init-capture field visibility for specified GCC, Clang, and Microsoft modes. A new entry point restores visibility after generic-lambda parameters in pre-C++20 Microsoft mode.
Parser integration and regression coverage
src/class_decl.c, src/declarator.c, tests/tests/regressions/.gh-470.rto/*, tests/tests/regressions/gh-470.sft.cpp, src/pending_changes/gh-470-init-capture-lambda-header.txt
Lambda declarator scans are bracketed by visibility changes, and function_declarator invokes the generic-lambda parameter boundary hook under the specified conditions. Regression cases cover name lookup across compiler versions and language modes. The compatibility note describes the behavior and includes an example.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: bug

Suggested reviewers: nv-cmeerw, darkarc

Merge Risk: Minimal · up to cd828

No actionable merge-blocking issue is established by the supplied evidence. Normal validation remains appropriate before merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly identifies the main change: emulating init-capture visibility rules in lambda headers.
Linked Issues check Passed Issue #470 requires older GNU, Clang, and Microsoft modes to resolve init-capture names in lambda headers to enclosing-scope entities, while retaining init-capture visibility in lambda bodies. `set_in…
Out of Scope Changes check Passed The implementation, declaration, pending-change note, and regression tests all support issue #470. The diff contains no demonstrated unrelated changes.
Full details: Linked Issues check

Explanation

Issue #470 requires older GNU, Clang, and Microsoft modes to resolve init-capture names in lambda headers to enclosing-scope entities, while retaining init-capture visibility in lambda bodies. set_init_capture_visibility_if_needed hides init-capture fields during initial header scanning and generic-lambda rescans for the specified version and language-mode conditions, then restores visibility. end_of_generic_lambda_params implements the documented pre-C++20 Microsoft behavior for generic-lambda header parts after the parameter list. The regression tests cover the relevant compiler-version boundaries, header contexts, generic lambdas, and body visibility.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 46ba939 and cd828e3.

Files selected for processing: 14

  • src/class_decl.c
  • src/class_decl.h
  • src/declarator.c
  • src/pending_changes/gh-470-init-capture-lambda-header.txt
  • tests/tests/regressions/.gh-470.rto/default.1.1.txt
  • tests/tests/regressions/.gh-470.rto/default.2.1.txt
  • tests/tests/regressions/.gh-470.rto/default.3.1.txt
  • tests/tests/regressions/.gh-470.rto/default.4.1.txt
  • tests/tests/regressions/.gh-470.rto/default.5.1.txt
  • tests/tests/regressions/.gh-470.rto/default.6.1.txt
  • tests/tests/regressions/.gh-470.rto/default.7.1.txt
  • tests/tests/regressions/.gh-470.rto/default.8.1.txt
  • tests/tests/regressions/.gh-470.rto/default.9.1.txt
  • tests/tests/regressions/gh-470.sft.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for cd828e341663:

CI success.

@daveedvdv-nvidia
daveedvdv-nvidia added this pull request to the merge queue Nov 10, 2026
Merged via the queue into main with commit 26f285a Nov 11, 2026
23 checks passed
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.

Init-capture visibility in various emulation modes

3 participants