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

Check for calling of associated functions as method calls. #2198

Merged
merged 3 commits into from
Jul 5, 2022

Conversation

tritao
Copy link
Contributor

@tritao tritao commented Jul 1, 2022

This adds a new error and checking for calls of associated functions using method call syntax.

This means for code like the following:

struct Bar {}

impl Bar {
  fn associated() {}
}

fn main() -> u64 {
  let bar = Bar {};
  bar.associated();
  0
}

we now emit an error:

error
  --> main.sw:11:3
   |
 9 |
10 |   let bar = Bar {};
11 |   bar.associated();
|   ^^^^^^^^^^^^^^^^ Cannot call associated function "associated" as
method call. Use associated function syntax instead.
12 |   0
13 | }
   |

This brings us closer to Rust behaviour, which to my understanding also checks for this and only allows method call syntax for functions taking self.

It adds a few more tests for this and also updates some standard library trait methods to take self so they work with the new check.

@tritao tritao added the compiler General compiler. Should eventually become more specific as the issue is triaged label Jul 1, 2022
@tritao tritao self-assigned this Jul 1, 2022
@tritao tritao force-pushed the associated-methods branch from 9b6dc71 to c80572f Compare July 1, 2022 14:08
@tritao tritao marked this pull request as ready for review July 1, 2022 14:45
@tritao tritao requested a review from otrho as a code owner July 1, 2022 14:45
@tritao tritao requested a review from a team July 1, 2022 14:51
Copy link
Contributor

@sezna sezna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an awesome change and great for compiler UX. Thanks!

@emilyaherbert emilyaherbert requested a review from a team July 1, 2022 21:31
Copy link
Contributor

@emilyaherbert emilyaherbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with what Alex said, but other than that LGTM. When those changes go through I will approve.

@emilyaherbert emilyaherbert requested a review from a team July 1, 2022 21:32
@tritao tritao force-pushed the associated-methods branch from 4a7ef01 to a6f60cc Compare July 4, 2022 11:33
mohammadfawaz
mohammadfawaz previously approved these changes Jul 4, 2022
@mohammadfawaz
Copy link
Contributor

Merge conflicts!

This adds a new error and checking for calls of associated functions
using method call syntax.

This means for code like the following:

```
struct Bar {}

impl Bar {
  fn associated() {}
}

fn main() -> u64 {
  let bar = Bar {};
  bar.associated();
  0
}
```

we now emit an error:

```
error
  --> main.sw:11:3
   |
 9 |
10 |   let bar = Bar {};
11 |   bar.associated();
|   ^^^^^^^^^^^^^^^^ Cannot call associated function "associated" as
method call. Use associated function syntax instead.
12 |   0
13 | }
   |
```

This brings us closer to Rust behaviour, which also checks for this and
only allows functions taking a self to be called with method call
syntax.

It adds a few more tests for this and also updates some standard library
trait methods to take self so they work with the new check.
@mohammadfawaz
Copy link
Contributor

This is an awesome change and great for compiler UX. Thanks!

@sezna this PR requires your review again :)

@mohammadfawaz mohammadfawaz enabled auto-merge (squash) July 5, 2022 19:38
@mohammadfawaz mohammadfawaz merged commit dec4bd8 into FuelLabs:master Jul 5, 2022
r-sitko pushed a commit to r-sitko/sway that referenced this pull request Jul 6, 2022
…2198)

This adds a new error and checking for calls of associated functions
using method call syntax.

This means for code like the following:

```
struct Bar {}

impl Bar {
  fn associated() {}
}

fn main() -> u64 {
  let bar = Bar {};
  bar.associated();
  0
}
```

we now emit an error:

```
error
  --> main.sw:11:3
   |
 9 |
10 |   let bar = Bar {};
11 |   bar.associated();
|   ^^^^^^^^^^^^^^^^ Cannot call associated function "associated" as
method call. Use associated function syntax instead.
12 |   0
13 | }
   |
```

This brings us closer to Rust behaviour, which also checks for this and
only allows functions taking a self to be called with method call
syntax.

It adds a few more tests for this and also updates some standard library
trait methods to take self so they work with the new check.

Co-authored-by: Emily Herbert <17410721+emilyaherbert@users.noreply.github.com>
Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler General compiler. Should eventually become more specific as the issue is triaged
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants