Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-derefencing is too eager #6257

Closed
huonw opened this issue May 6, 2013 · 4 comments
Closed

Auto-derefencing is too eager #6257

huonw opened this issue May 6, 2013 · 4 comments

Comments

@huonw
Copy link
Member

huonw commented May 6, 2013

The following seems annoying (and is especially so for writing macros/syntax extensions)

fn main() {
    let x: & int =  &1;

    x.eq(&x); // `x == x` works fine
}
auto-deref.rs:6:9: 6:11 error: mismatched types: expected `&int` but found `&&int` (expected int but found &-ptr)
auto-deref.rs:6     x.eq(&x);
                         ^~
error: aborting due to previous error
@nikomatsakis
Copy link
Contributor

The equivalent of x == y, I believe, is (&x).eq(&y), not x.eq(&y). In any case, I think what is really desired here is another operator for accessing fields/methods that does not do any auto-anything (rather than modifying the auto-deref rules).

@huonw
Copy link
Member Author

huonw commented May 6, 2013

@nikomatsakis The type of eq is fn eq(&self, other: &Self) -> bool; which, as much as I understand Rust, means that calling eq is "meant" to be x.eq(&x) (at least it works with let x = 1).

However (&x).eq(&x) does seem get it to work.

@nikomatsakis
Copy link
Contributor

@huonw there is some subtlety here regarding how we select matching trait methods. In this case, you are expecting that we will matchEq with "Self = &int", but what actually happens is that we match Eq with Self = int. Note that the receiver is defined as &self.

That said, something just occurred to me. I think that the problem is not with autoderef per se, but rather with the way that we define the binary operator traits. We ought to define them as follows:

trait Eq {
    fn eq(a: &Self, b: &Self);
}

meaning that x==y would be expanded to:

Eq::eq(&x, &y)

The same would apply to other binary operator traits.

Note that there would be no auto-deref in this case.

@nikomatsakis
Copy link
Contributor

I'm inclined to close this issue. @huonw re-open is you disagree.

bors added a commit that referenced this issue May 8, 2013
…omatsakis

This "finishes" the generic deriving code (which I started in #5640), in the sense it supports everything that I can think of being useful. (Including lifetimes and type parameters on methods and traits, arguments and return values of (almost) any type, static methods.)

It closes #6149, but met with #6257, so the following doesn't work:
```rust
#[deriving(TotalEq)]
struct Foo<'self>(&'self int);
```
(It only fails for `TotalOrd`, `TotalEq` and `Clone`, since they are the only ones that call a method directly on sub-elements of the type, which means that the auto-deref interferes with the pointer.)

It also makes `Rand` (chooses a random variant, fills the fields with random values, including recursively for recursive types) and `ToStr` (`x.to_str()` is the same as `fmt!("%?", x)`) derivable, as well as converting IterBytes to the generic code (which made the code 2.5x shorter, more robust and added support for tuple structs).

({En,De}codable are trickier, so I'll convert them over later.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants