Email me

Rust errors / Types and traits

Rust compiler error · checked with rustc 1.99.0

Rust error E0307: invalid `self` parameter type: `&Bar`

What it meansThe self parameter in a method has an invalid "receiver type".

What rustc prints

error[E0307]: invalid `self` parameter type: `&Bar`
 --> main.rs:9:18
  |
9 |     fn foo(self: &Bar) {}
  |                  ^^^^
  |
  = note: type of `self` must be `Self` or a type that dereferences to it
  = help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.

E0307Types and traits
Activein rustc 1.99.0
7related errors linked below

The compiler team's explanation

The self parameter in a method has an invalid "receiver type".

Erroneous code example:

struct Foo;
struct Bar;

trait Trait {
    fn foo(&self);
}

impl Trait for Foo {
    fn foo(self: &Bar) {}
}

Methods take a special first parameter, of which there are three variants: self, &self, and &mut self. These are syntactic sugar for self: Self, self: &Self, and self: &mut Self respectively.

trait Trait {
    fn foo(&self);
//         ^^^^^ `self` here is a reference to the receiver object
}

impl Trait for Foo {
    fn foo(&self) {}
//         ^^^^^ the receiver type is `&Foo`
}

The type Self acts as an alias to the type of the current trait implementer, or "receiver type". Besides the already mentioned Self, &Self and &mut Self valid receiver types, the following are also valid: self: Box<Self>, self: Rc<Self>, self: Arc<Self>, and self: Pin<P> (where P is one of the previous types except Self). Note that Self can also be the underlying implementing type, like Foo in the following example:

impl Trait for Foo {
    fn foo(self: &Foo) {}
}

This error will be emitted by the compiler when using an invalid receiver type, like in the following example:

impl Trait for Foo {
    fn foo(self: &Bar) {}
}

The nightly feature Arbitrary self types extends the accepted set of receiver types to also include any type that implements the Receiver trait and can follow its chain of Target types to Self. There's a blanket implementation of Receiver for T: Deref, so any type which dereferences to Self can be used.

#![feature(arbitrary_self_types)]

struct Foo;
struct Bar;

// Because you can dereference `Bar` into `Foo`...
impl std::ops::Deref for Bar {
    type Target = Foo;

    fn deref(&self) -> &Foo {
        &Foo
    }
}

impl Foo {
    fn foo(self: Bar) {}
//         ^^^^^^^^^ ...it can be used as the receiver type
}

From rustc --explain E0307 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.

Related errors

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0308: mismatched typesA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ