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

Commits on Sep 14, 2021

  1. Initial autoderef support for method calls

    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 committed Sep 14, 2021
    Configuration menu
    Copy the full SHA
    002313b View commit details
    Browse the repository at this point in the history