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

Initial autoderef support for method calls #672

Merged
merged 1 commit into from Sep 14, 2021
Merged

Conversation

philberty
Copy link
Member

The commit message contains more detail but this reference is good:
https://doc.rust-lang.org/nightly/nomicon/dot-operator.html

This is not 100% complete since we do not yet support operator overloading
so we will require an update to support the Deref operator overload for
more complex types.

Fixes: #241

There is compiler magic for method calls the self parameter can be of
several forms:

 - specified type (self: Type)
 - immutable Self (self)
 - mutable Self (mut self)
 - immutable reference (&self)
 - mutable reference (&mut self)

This updates our HIR lowering to actually lower this correctly and apply
the apropriate Self type to the TyTy::FnDef. The code used to just default
to plain Self ignoring any modifiers.

Rust also allows something called the autoderef cycle to coerce the
receiver to the correct type for the function such as:

```rust
  struct Foo(i32);
  impl Foo {
    fn bar(&self) { }
  }

  fn main() {
    let a = Foo(123);
    a.bar();
  }
```

In this example the method call is expected to apply an implict reference
to variable 'a' such that the method call becomes:

```
  Foo::bar(&a);
```

The algorithm is detailed here:
https://doc.rust-lang.org/nightly/nomicon/dot-operator.html

  1. Try to match Self
  2. Apply an immutable Reference and Try again
  3. Apply an mutable Reference and Try again
  4. Can we dereference ? [deref and go back to 1] else [quit]

This is not 100% complete since we do not yet support operator overloading
so we wil require an update to support the Deref operator overload for
more complex types.

Fixes: #241
@philberty philberty added this to the Data Structures 3 - Traits milestone Sep 14, 2021
@philberty philberty self-assigned this Sep 14, 2021
@philberty philberty added this to In progress in Data Structures 3 - Traits via automation Sep 14, 2021
@philberty philberty moved this from In progress to Review in progress in Data Structures 3 - Traits Sep 14, 2021
@philberty
Copy link
Member Author

bors r+

@bors
Copy link
Contributor

bors bot commented Sep 14, 2021

Build succeeded:

@bors bors bot merged commit c8ffaa1 into master Sep 14, 2021
Data Structures 3 - Traits automation moved this from Review in progress to Done Sep 14, 2021
@philberty philberty deleted the phil/traits-dev-23 branch September 15, 2021 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Adjustments for method receivers
1 participant