# Restrict auto-ref for method taking self?

**URL:** <https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909>\
**Category:** language design\
**Created:** [September 3, 2019, 4:43pm UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909 "2019-09-03T16:43:48Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rockmen1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rockmen1/32/7237_2.png) [@rockmen1](https://internals.rust-lang.org/u/rockmen1)\
**Post date:** [September 3, 2019, 4:43pm UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/1 "2019-09-03T16:43:48Z")

</div>

Let's say for example:

```rust
trait MyTrait {
    fn consume(self);
}

struct S;

impl MyTrait for &S {
    fn consume(self) {
        println!("borrowed")
    }
}

fn main() {
    let s = S;

    s.consume();
    s.consume();
}

```

compiler will accept the code, which is, programmatically correct, but contradicts the intuition s.consume() would take ownership of s. One has to know the auto-ref rule (not in official docs) as well as the existence of ``impl MyTrait for &S``` to reason about why it compiles.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [September 3, 2019, 5:22pm UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/2 "2019-09-03T17:22:19Z")

</div>

_How_ would you propose restricting auto-ref, without breaking existing code that uses this?

It might be something that starts as a clippy lint, but I fear you'll get a lot of false positives.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [September 3, 2019, 6:59pm UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/3 "2019-09-03T18:59:40Z")

</div>

The fact that references are copyable comes up in other contexts too:

```rust
fn refcopy(copy: impl Copy) {
    
}

fn main() {
    refcopy(&String::new());
}

```

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [September 3, 2019, 7:54pm UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/4 "2019-09-03T19:54:19Z")

</div>

Also note that adding

```rust
impl MyTrait for S {
    fn consume(self) {
        println!("owned")
    }
}

```

Will break your code and force you to borrow s instead of silently borrowing it.

---

<div class="post-metadata">

**Author:** ![rockmen1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rockmen1/32/7237_2.png) [@rockmen1](https://internals.rust-lang.org/u/rockmen1)\
**Post date:** [September 4, 2019, 1:23am UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/5 "2019-09-04T01:23:08Z")

</div>

> _How_ would you propose restricting auto-ref, without breaking existing code that uses this?

For any single crate, if the author later introduce `impl MyTrait for S`, it will break existing code anyway. This situation could be improved if the compiler reject the code in the first place. How about adding a flag like how NLL was introduced?

---

<div class="post-metadata">

**Author:** ![rockmen1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rockmen1/32/7237_2.png) [@rockmen1](https://internals.rust-lang.org/u/rockmen1)\
**Post date:** [September 4, 2019, 1:31am UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/6 "2019-09-04T01:31:12Z")

</div>

> [@RustyYato](#):
>
> Also note that adding
> 
> ```rust
> impl MyTrait for S {
> fn consume(self) {
> println!("owned")
> }
> }
> 
> ```
> 
> Will break your code and force you to borrow s instead of silently borrowing it.

```rust
fn main() {
    let s = S;
    let b = &s;

    b.consume();
    b.consume();
}

```

Which impl the compiler would pick?

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [September 4, 2019, 1:32am UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/7 "2019-09-04T01:32:20Z")

</div>

well, `b: &S` so it will pick `&S`'s impl. This is the forced borrow that I noted.

---

<div class="post-metadata">

**Author:** ![rockmen1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rockmen1/32/7237_2.png) [@rockmen1](https://internals.rust-lang.org/u/rockmen1)\
**Post date:** [September 4, 2019, 1:48am UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/8 "2019-09-04T01:48:31Z")

</div>

> [@RustyYato](#):
>
> well, `b: &S` so it will pick `&S` 's impl. This is the forced borrow that I noted.

Hmm..., then rules for inferring impl differ.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [September 4, 2019, 1:59am UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/9 "2019-09-04T01:59:37Z")

</div>

Here are the rules, given that `var: T`

1. Is there an impl for `T`, if so use that impl, else go to step 2
2. Is there an impl for `&T`, if so use that impl, else go to step 3,
3. Is there an impl for `&mut T`, if so use that impl, else fail to compile

[https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=93eae555568c28622e3c60c981ace295](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=93eae555568c28622e3c60c981ace295)

Very simple steps, and if you go down step 1, and fail to compile later the compiler _ **will not** _ backtrack and try another route. That would be absolutely terrible for compile times.

---

<div class="post-metadata">

**Author:** ![rockmen1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rockmen1/32/7237_2.png) [@rockmen1](https://internals.rust-lang.org/u/rockmen1)\
**Post date:** [September 4, 2019, 3:22am UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/10 "2019-09-04T03:22:44Z")

</div>

How about this?

```rust
trait MyTrait {
    fn consume(self);
}

struct S;

impl MyTrait for S {
    fn consume(self) {
        println!("owned")
    }
}

// impl MyTrait for &S {
// fn consume(self) {
// println!("borrowed")
// }
// }

fn main() {
    let s = S;
    let b = &s;

    b.consume();
    b.consume();
}

```

```rust
error[E0507]: cannot move out of `*b` which is behind a shared reference
  --> src/main.rs:23:5
   |
23 | b.consume();
   | ^ move occurs because `*b` has type `S`, which does not implement the `Copy` trait

error[E0507]: cannot move out of `*b` which is behind a shared reference
  --> src/main.rs:24:5
   |
24 | b.consume();
   | ^ move occurs because `*b` has type `S`, which does not implement the `Copy` trait

error: aborting due to 2 previous errors

```

Step1: There is an impl for `T`, so the compiler automatically changes b to \*b ?

Then uncomment `impl MyTrait for &S`, compiler still sees: step1, there is an impl for `T`, where T is &S, in this case?

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [September 4, 2019, 3:29am UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/11 "2019-09-04T03:29:42Z")

</div>

Oh, I forgot about dereferencing, in this case `T == &S`, so we don't have any of `T`, `&mut T`, or `&T` so we try dereferencing because we have an impl for `S`. In this case because `S: !Copy` this fails.

So updated list,

1. Is there an impl for `T` , if so use that impl, else go to step 2
2. Is there an impl for `&T` , if so use that impl, else go to step 3,
3. Is there an impl for `&mut T` , if so use that impl, else go to step 4
4. Is `T: Deref`, then try dereferencing (this may fail if `<T as Deref>::Target` is not `Copy` or of `T` is not `Copy`)

_disclaimer_ there may be other corner cases that I missed, but this should cover the vast majority of cases.

---

<div class="post-metadata">

**Author:** ![rockmen1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rockmen1/32/7237_2.png) [@rockmen1](https://internals.rust-lang.org/u/rockmen1)\
**Post date:** [September 4, 2019, 3:34am UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/12 "2019-09-04T03:34:23Z")

</div>

Where to put `unsized coercions`? e.g. `[T; n]` to `[T]`

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [September 4, 2019, 3:41am UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/13 "2019-09-04T03:41:53Z")

</div>

Same as deref

---

<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:** [December 3, 2019, 3:41am UTC](https://internals.rust-lang.org/t/restrict-auto-ref-for-method-taking-self/10909/14 "2019-12-03T03:41:57Z")

</div>

Closed on December 3, 2019, 3:41am UTC

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