Skip to content

make returning a reference bound to a temporary an error in C++26 (P2748), fix for [GH #106] - #388

Open
mohitmishra786 wants to merge 2 commits into
edgcpp:mainfrom
mohitmishra786:gh-106-return-ref-to-temporary
Open

mohitmishra786 wants to merge 2 commits into
edgcpp:mainfrom
mohitmishra786:gh-106-return-ref-to-temporary

Conversation

@mohitmishra786

@mohitmishra786 mohitmishra786 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #106.

A return statement that binds the returned reference to a temporary is ill-formed in C++26 (P2748). The front end already detected this in check_for_returning_reference_to_local_entity, but only issued the "returning reference to local temporary" warning.

In C++26 that diagnostic is now an error. When emulating GCC before 14 or Clang before 19 it stays a warning, as in those compilers. GCC 14 and 15 with -std=c++26 error on the same cases (prvalue, converted value, member of a temporary, rvalue reference) and still only warn about returning a reference to a local variable, which is unchanged here.

Known gap, not changed here: in standard (non-GNU, non-Clang) modes, return S().x; is not detected as binding to a temporary at all (no warning before this change either), while GNU and Clang modes do catch it. A broader local regression run is in progress; I will follow up if it finds anything.

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes
    • In C++26, returning a reference bound to a temporary produces an error with GCC 14 or later and Clang 19 or later. With older compiler versions, it remains a warning.
    • Returning a reference to a local variable continues to produce a warning.
  • Tests
    • Added coverage for temporary-reference diagnostics across C++23 and C++26.

@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e3c505e3-86e2-4f86-aa17-e904984544ec
Commits

Reviewing files that changed from the base of the PR and between 4a84f78 and e5e4700.

Files ignored due to path filters: 1
  • tests/expectations/edg_x86_64/imported.log is excluded by !**/*.log
Files selected for processing: 9
  • src/overload.c
  • tests/tests/changes/.GH106.rto/default.2.1.txt
  • tests/tests/changes/.GH106.rto/default.3.1.txt
  • tests/tests/changes/.GH106_a.rto/default.2.1.txt
  • tests/tests/changes/.GH106_a.rto/default.3.1.txt
  • tests/tests/changes/GH106.sft.cpp
  • tests/tests/changes/GH106_a.sft.cpp
  • tests/tests/cwg/.p2748R5.rto/default.1.1.txt
  • tests/tests/imported/clang/cpp/CXX/stmt.stmt/stmt.return/.p6.rto/default.1.1.txt

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

Walkthrough

In C++26 mode, references returned to temporaries produce errors when the source is not a similar-type lvalue, subject to the specified older GCC and Clang compatibility exceptions. References returned to local variables remain warnings. Tests cover these diagnostic cases across language modes and compiler-version settings.

Changes: Returned reference diagnostics

Layer / File(s) Summary
Diagnostic rule and regression coverage
src/overload.c, tests/tests/changes/GH106*.sft.cpp, tests/tests/changes/.GH106*.rto/*, tests/tests/cwg/.p2748R5.rto/default.1.1.txt, tests/tests/imported/clang/cpp/CXX/stmt.stmt/stmt.return/.p6.rto/default.1.1.txt, src/pending_changes/gh-106-return-ref-to-temporary.txt
The diagnostic uses whether the initializer was a similar-type lvalue to distinguish temporary-bound references. Tests and expected output cover C++23 and C++26 modes, compiler-version settings, temporary expressions, and local-variable references. The pending note describes the diagnostic behavior.

Priority: Normal

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

Change: Bug fix

Possibly related PRs

  • edgcpp/compiler#389: It changes classification of member access on class temporaries, which affects the temporary-member cases covered by this PR.

Suggested reviewers: nv-cmeerw, darkarc

Merge Risk: Minimal · up to e5e47

This change makes returning a reference bound to a temporary an error in C++26 mode. Older GCC and Clang emulation modes keep a warning, and returns of references to local variables still warn. No remaining merge-blocking issue was found.

Pre-merge checks: Passed 3 · Failed 1 · Inconclusive 1

Failed Failed checks: 1 warning · 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Warning Issue #106 requires P2748R5. The implementation changes the temporary-reference diagnostic to an error in C++26 and preserves warnings for the specified older GCC and Clang emulation modes. The tests … Make standard C++26 mode diagnose return S().x; as an error. Update the related expected output and verify the GNU, Clang, and pre-C++26 mode expectations.
Docstring Coverage Inconclusive Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 12 functions across 2 files. (7 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 3 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the primary change: returning a reference bound to a temporary becomes an error in C++26 (P2748), and it references the linked issue GH #106. The raw summary confirms the src/overload.…
Out of Scope Changes check Passed The source change, diagnostic note, tests, and expected outputs support issue #106. They cover the requested C++26 severity change and the specified compiler-version compatibility behavior. The local-…
Full details: Title check

Explanation

The title names the primary change: returning a reference bound to a temporary becomes an error in C++26 (P2748), and it references the linked issue GH #106. The raw summary confirms the src/overload.c change, the C++26 error behavior, and the GCC-before-14 and Clang-before-19 warning retention. The objectives describe the same change, so the title matches the changeset. The title is long (92 characters) but still specific and accurate.

Full details: Out of Scope Changes check

Explanation

The source change, diagnostic note, tests, and expected outputs support issue #106. They cover the requested C++26 severity change and the specified compiler-version compatibility behavior. The local-variable warning remains unchanged. No unrelated behavior is demonstrated.

Full details: Linked Issues check

Explanation

Issue #106 requires P2748R5. The implementation changes the temporary-reference diagnostic to an error in C++26 and preserves warnings for the specified older GCC and Clang emulation modes. The tests cover direct temporaries, converted values, temporary members, rvalue references, and local variables. However, the PR description states that standard non-GNU, non-Clang mode does not diagnose return S().x;. The default C++26 expected output also records no error for that case. This leaves a concrete P2748 case unmet.

Resolution

Make standard C++26 mode diagnose return S().x; as an error. Update the related expected output and verify the GNU, Clang, and pre-C++26 mode expectations.

Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 12 functions across 2 files. (7 skipped: 6 unsupported, 1 too large.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 4a84f78dfed0:

@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/overload.c 66.66% 0 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
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.

[GNATS 27152] C++26: Disallow Binding a Returned Glvalue to a Temporary (P2748R5)

2 participants