Skip to content

Changing the type of self in methods doesn't work #27941

Closed
@jonas-schievink

Description

@jonas-schievink

The book states:

Methods take a special first parameter, of which there are three variants: self, &self, and &mut self.

The reference uses typed self in a few places, but doesn't explain the exact rules.

The compiler also prints redundant and misleading diagnostics:

struct S;

impl S {
    fn f(self: Vec<Self>) {}
}

gives:

<anon>:6:10: 6:28 error: mismatched self type: expected `S`: expected struct `S`, found struct `collections::vec::Vec` [E0211]
<anon>:6     fn f(self: Vec<Self>) {}
                  ^~~~~~~~~~~~~~~~~~

Since replacing Vec<Self> with Box<Self> makes the code compile, "expected struct S" isn't quite correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions