External Publication
Visit Post

Idea: Borrowck Transparent Function Calls

Rust Internals [Unofficial] March 20, 2026
Source

josh:

we could support writing something like &mut{_} self to get a compiler error with a suggestion

I was kinda hoping we could write nothing, actually, and have it get filled in.

Like today (example inspired from the other thread) if you have https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=b3fc0ce0ebc6c302bd2befd4f35b8522

fn foo(a: &i32, b: &i32, c: &i32) -> &i32 {
    if *c > 0 { a } else { b }
}

Then the error you get is

  = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `a`, `b`, or `c`
help: consider introducing a named lifetime parameter
  |
1 | fn foo<'a>(a: &'a i32, b: &'a i32, c: &'a i32) -> &'a i32 {
  |       ++++     ++          ++          ++          ++

but ideally we could use the MIR borrow-check results to suggest

fn foo<'a>(a: &'a i32, b: &'a i32, c: &i32) -> &'a i32 {

with a note that that's what the body is currently doing.


Though having typed all that I guess that's largely irrelevant to the "view type" part, which yeah, might need a way to say "intentionally wrong; please fix".

That might just be an empty list, though. &mut{} self is probably not actually going to compile, and the error message can tell you which things to put in there that the current implementation needs, with the structured suggestion.

Discussion in the ATmosphere

Loading comments...