# Infered fields for partial borrowing

**URL:** <https://internals.rust-lang.org/t/infered-fields-for-partial-borrowing/18766>\
**Category:** language design\
**Created:** [April 30, 2023, 10:50pm UTC](https://internals.rust-lang.org/t/infered-fields-for-partial-borrowing/18766 "2023-04-30T22:50:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeffBurdges](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jeffburdges/32/2116_2.png) [@JeffBurdges](https://internals.rust-lang.org/u/JeffBurdges)\
**Post date:** [April 30, 2023, 10:50pm UTC](https://internals.rust-lang.org/t/infered-fields-for-partial-borrowing/18766/1 "2023-04-30T22:50:55Z")

</div>

I've first posted this in the fields-in-traits repo since afaik that repo contains the most serious work towards partial borrowing, but maybe a comment here makes sense too.

> <https://github.com/nikomatsakis/fields-in-traits-rfc/issues/20>
>
> We maybe have independent reasons for doing fields-in-traits, but they appear un…necessary for typical "partial borrowing" use cases.
> 
> We de facto loose lifetime elision with any partial borrowing scheme because you'll typically have methods that several fields but for different lifetimes and with different mutability. We should lean into this by expressing only disjointness using only "relative lifetimes". I think formally these resemble:
> \`\`\`
> impl Trait for Type {
> type 'a\<'self\> where 'self: 'a\<'self\> = { field1, field2 };
> }
> \`\`\`
> In other words, relative lifetimes are associated lifetime constructors in the trait, or inherent type, which construct a lifetime that outlives \`'self\` and represents only a subset of the fields of the underlying type.
> 
> We could infer the above complex syntax by simply (a) declaring disjointness for specific named relative lifetimes and (b) explicitly using these relative lifetimes in methods. All this resembles:
> \`\`\`
> trait Trait {
> disjoint 'a, 'b, 'c;
> fn foo(&'a mut 'c self) -\> Foo\<'a\>; 
> fn bar(&'b+'c mut self) -\> &'b mut Bar; 
> }
> \`\`\`
> This says: At least three disjoint sets of fields \`'a\`, \`'b\`, \`'c\` exist, all possibly empty. \`fn foo\` borrows \`'c\` immutably and \`'a\` mutably, but its return only borrows \`'a\` mutably. \`fn bar\` borrows \`'b\` and \`'c\` mutably, but its return only borrows \`'b\` mutably. 
> 
> In other words, \`foo\` and \`bar\` could both be called sequentially since both abandon their overlapping \`'c\` borrow, but neither could be called a second time until you abandon its return. Also, you cannot invoke \`foo\` and \`bar\` from simultaneous unrelated borrows. In particular, you cannot have separate conventional \`FnMut\`s built from \`foo\` and \`bar\`, unless you somehow make those \`FnMut\` traits be sub-traits of \`Trait\`.
> 
> Any \`impl Trait for Type\` causes rustc to infer which fields actually belong under \`'a\`, \`'b\`, \`'c\`, based upon its actual implementations of \`foo\` and \`bar\`, but our above rules for invoking \`foo\` and \`bar\` have become part of the trait. It's possible for \`'c\` to be inferred to empty, but we do not leak this emptiness so you still cannot invoke \`foo\` and \`bar\` from simultaneous unrelated borrows. 
> 
> Advantages? We all love real fields but often you want them abstracted somehow, ala \`RefCell\` vs \`Mutex\` or \`Vec\<T\>\` vs \`\[T; N\]\`. A bunch of disjoint getter etc methods likely captures this better, ala \`fn get\_fieldA(& 'fieldA self) -\> impl Deref\<Target=Whatever\> + 'fieldA\`. It's already lower notation overhead, but you could lower this further by making \`disjoint method\_name, ...;\` be sugar for \`disjoint 'method\_name, ...;\` plus \`fn method\_name(&'method\_name \[mut\] self, ..\`. It also works uniformly in traits and inherent types and likely plays nicer in semver.

In essence, there is seemingly no reason to name individual fields when doing partial borrowing. Instead "relative lifetimes" could more concisely express simultaneity requirements upon trait or inherent methods.

At a formal level, these relative lifetimes are associated lifetime constructors of the form:

```rust
impl Trait for Type {
    type 'a<'self> where 'self: 'a<'self> = { field1, field2 };
    ...
}

```

We do not require this syntax however since rustc could infer the assignment of fields to relative lifetimes. Actual syntax might resemble:

```rust
trait Trait {
    disjoint 'a, 'b, 'c;
    fn borrow_mut_a(&'a mut self) -> &'a mut A;
    fn borrow_mut_b(&'b mut 'a self) -> &'b mut B;
    fn borrow_mut_c(&'c+'a mut self) -> &'c mut C;
}

```

We'd need only some `disjoint` declaration for the relative lifetimes, as well as "algebra" of the relative lifetimes, which expresses what the methods may do.

There is no need to rush this into existence of course. Among the partial borrowing solutions, I do think field inference brings many benefits over named field approaches though, so imho named field based partial borrowing should probably not be too seriously considered.

---

<div class="post-metadata">

**Author:** ![JeffBurdges](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jeffburdges/32/2116_2.png) [@JeffBurdges](https://internals.rust-lang.org/u/JeffBurdges)\
**Post date:** [May 1, 2023, 8:07pm UTC](https://internals.rust-lang.org/t/infered-fields-for-partial-borrowing/18766/2 "2023-05-01T20:07:11Z")

</div>

As I said on github, named field approaches would typically require lifetimes too, which becomes really heavy notation. If we let rustc infer the fields, then we could focus upon methods, like what actually matters.

In fact, one could enter method names as abbreviations for their own lifetimes to recapture some of the lifetime elision:

```rust
pub trait Trait {
    disjoint foo,bar,'baz;
    fn foo(&self) -> &Foo; // sugar for fn foo(&'foo self) -> &'foo Foo;
    fn foo_mut(&'foo mut self) -> &'foo mut Foo; // Explicitly reuses the lifetime defined for fn foo 
    fn bar(&mut self) -> &Bar; // sugar for fn bar(&'bar mut self) -> &'bar Bar;
    fn baz(&'foo mut 'baz self) -> .. // Needs an explicit lifetime since it's doing something less common.
}

```

Again, I do not think partial borrowing should be prioritized per se, but I think field inference gives a preferable corner of that design space.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [July 30, 2023, 8:07pm UTC](https://internals.rust-lang.org/t/infered-fields-for-partial-borrowing/18766/3 "2023-07-30T20:07:51Z")

</div>

Closed on July 30, 2023, 8:07pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
