# Fields in Traits

**URL:** <https://internals.rust-lang.org/t/fields-in-traits/6933>\
**Category:** language design\
**Created:** [March 6, 2018, 11:23pm UTC](https://internals.rust-lang.org/t/fields-in-traits/6933 "2018-03-06T23:23:44Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [March 8, 2018, 11:47am UTC](https://internals.rust-lang.org/t/fields-in-traits/6933/16 "2018-03-08T11:47:58Z")

</div>

> [@HadrienG](#):
>
> I’m a bit worried about how this would interact with the borrow checker. In the current design, I understand that I can have two unrelated traits A and B which both alias the same field in a given struct. But this means that changing the mapping of a field in a trait impl is a breaking change, as it can create mutable aliasing situations which did not exist before, and thus lead the borrow checker to reject some existing client code which borrows mutably from both A and B.

I don't think this is true in the existing proposal, but I think it arises in the "views" variant i've been talking about. That is, in the existing proposal, the disjointness requirement isn't something we have to check in "client code" -- rather, we check when you **define the impl** that all the disjointness conditions are met.

But there are some borrow checker interactions that weren't cleared defined in the RFC. For example, would accessing a trait field `a` be considered to overlap with a struct field `b`, presuming that `b` is not mapped to `a`? I had actually assumed it _would be_, and hence this code would error:

```rust
trait Foo {
    let a: u32
}

struct Bar {
    b: u32,
    a2: u32,
}

impl Foo for Bar {
    let a = self.a2;
}

fn main() {
    let mut bar = Bar { ... };
    let p = &mut bar.b;
    let q = &mut bar.a; // where `a` is defined in the trait
}

```

Put another way, the borrow checker here sees two paths, where I've written the field names with "fully qualified paths" telling you where they came from:

```
bar.(Bar::b) // inherent field of `Bar`
bar.(Foo::a) // field defined in trait `Foo`

```

My assumption was that we would consider two inherent fields (e.g., `b` and `a2`) to be disjoint if they come from the same struct. We would also consider two trait fields to be disjoint if they come from the same _trait_ (or supertrait/subtrait relationship). But fields from two unrelated traits would be considered to maybe overlap -- and the same for a field from some trait and some struct.

But I guess we can imagine the borrow checker "seeing through" the borrow of `a` to understand that it really maps to `a2` and hence is disjoint from `b`. In that case, we do want to think about privacy/encapsulation.

And certainly this comes up in the views concept I was kicking around.

---

_[View the full topic](https://internals.rust-lang.org/t/fields-in-traits/6933)._
