Skip to content

Negative interaction between &self, auto-ref, and @T implementations #3585

Closed

Description

@nikomatsakis

@erickt stumbled across a negative interaction between the current auto-ref/auto-deref rules, explicit self, and implementations for @T.

If you write code like this:

trait Foo {
    fn foo(&self) -> ~str;
}

impl<T: Foo> @T: Foo {
    fn foo(&self) -> ~str {
        fmt!("@%s", (**self).foo())
    }
}

impl uint: Foo {
    fn foo(&self) -> ~str {
        fmt!("%u", *self)
    }
}

fn main() {
    let x = @3u;
    assert x.foo() == ~"@3";
}

You get an assertion failure. What happens is that auto-ref derefs the @uint to uint and then auto-refs that to &uint. This is because the current rule is "auto-deref until you can't, then auto-ref". It would be easy enough to make it try auto-refing at each stage. Probably it should, this seems like an important use case. This is a fairly small change to middle/typeck/check/method.rs.

Activity

  1. added a commit that references this issue on May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions