Skip to content

Auto-borrow ref self receivers on parameters declared without own - #1658

Merged
sbillig merged 1 commit into
masterfrom
fix/ref-self-auto-borrow
Oct 1, 2026
Merged

sbillig merged 1 commit into
masterfrom
fix/ref-self-auto-borrow

Conversation

@micahscopes

Copy link
Copy Markdown
Collaborator

A ref self method could not be called directly on a parameter declared without own:

struct S { x: u8 }
impl S { fn get(ref self) -> u8 { self.x } }

fn f(s: S) -> u8 { s.get() }   // error: expected `ref S`, but `S` is given

The same call works on a local, on an own parameter, and as let r = ref s; r.get(). A parameter without own is
a borrowed view of its value (shown as S in the message), and the receiver auto-borrow only accepted an owned value.

The type checker now accepts such a receiver for ref self. Lowering borrows the receiver place, as it does for an
explicit ref. mut self receivers are unchanged.

Tested with a new fe test fixture, fe_test/ref_self_receiver_on_param.fe, which runs the calls. It covers
concrete, generic and trait methods, a field of a parameter, an own parameter and an explicit ref.

This is close to #1643, which improves trait-bound errors on the same kind of borrowed values. The two change
different code and merge cleanly.

A `ref self` method could not be called directly on a parameter
declared without `own`: `fn f(s: S) -> u8 { s.get() }` was rejected with
"expected `ref S`, but `S` is given", while `let r = ref s; r.get()` and
the same call on a local worked. Such a parameter is typed as a view of
its value, and the receiver auto-borrow only accepted an owned value.

Accept a view receiver for a `ref self` borrow in the type checker, and
lower it as a borrow of the receiver place, as an explicit `ref` is
lowered. `mut self` receivers are unchanged.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@micahscopes
micahscopes force-pushed the fix/ref-self-auto-borrow branch from 0e4208b to 55685bc Compare September 30, 2026 18:26
@sbillig

sbillig commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-01T04:00:07.415152Z 55685bc Manual request
🔒 Security Review ✅ Completed 2026-10-01T03:54:17.829262Z 55685bc Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 55685bc172

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 55685bc172

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@sbillig
sbillig merged commit 2e8f3a5 into master Oct 1, 2026
9 checks passed
@sbillig
sbillig deleted the fix/ref-self-auto-borrow branch October 1, 2026 17:16
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