Skip to content

Commit

Permalink
Allow method calls between impl self methods. (#4933)
Browse files Browse the repository at this point in the history
## Description

This adds support for method calls between impl self methods.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: IGI-111 <[email protected]>
  • Loading branch information
tritao and IGI-111 authored Oct 23, 2023
1 parent f894ba6 commit 13f33f5
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ jobs:
[workspace]" >> test-proj/Cargo.toml
- name: Build and Run Default Integration Test
run: (cd test-proj && cargo test)
run: (cd test-proj && cargo test --release)

cargo-build-workspace:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -304,7 +304,7 @@ jobs:
- name: Build All Tests
run: cargo run --locked -p forc -- build --locked --path ./test/src/sdk-harness
- name: Cargo Test sway-lib-std
run: cargo test --locked --manifest-path ./test/src/sdk-harness/Cargo.toml -- --nocapture
run: cargo test --locked --release --manifest-path ./test/src/sdk-harness/Cargo.toml -- --nocapture

forc-run-benchmarks:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -402,7 +402,7 @@ jobs:
toolchain: ${{ env.RUST_VERSION }}
- uses: Swatinem/rust-cache@v2
- name: Run tests
run: cargo test --locked
run: cargo test --locked --release

cargo-unused-deps-check:
runs-on: ubuntu-latest
Expand Down
15 changes: 15 additions & 0 deletions sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,21 @@ impl TyImplTrait {
implementing_for,
};

ctx.insert_trait_implementation(
handler,
impl_trait.trait_name.clone(),
impl_trait.trait_type_arguments.clone(),
impl_trait.implementing_for.type_id,
&impl_trait.items,
&impl_trait.span,
impl_trait
.trait_decl_ref
.as_ref()
.map(|decl_ref| decl_ref.decl_span().clone()),
IsImplSelf::Yes,
IsExtendingExistingImpl::No,
)?;

// Now lets do a partial type check of the body of the functions (while deferring full
// monomorphization of function applications). We will use this tree to perform type check
// analysis (mainly dependency analysis), and re-type check the items ordered by dependency.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'associated_const_impl_self_order'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "associated_const_impl_self_order"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
script;

struct Struct { }

impl Struct {
fn foo(self) -> u32 {
Self::ID
}

const ID: u32 = 1;
}

fn main() -> u32 {
let s = Struct {};
s.foo()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "return", value = 1 }
expected_warnings = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'impl_self_method'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
implicit-std = false
license = "Apache-2.0"
name = "impl_self_method"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
script;

struct Struct {
}

impl Struct {
pub fn bar(self) -> u32 { self.foo() }
pub fn foo(self) -> u32 { 10 }
}

fn main() -> u32 {
let s = Struct {};
s.bar()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "return", value = 10 }
expected_warnings = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'impl_self_method_multiple'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
implicit-std = false
license = "Apache-2.0"
name = "impl_self_method_multiple"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
script;

struct Struct {
}

impl Struct {
pub fn bar(self) -> u32 { self.foo() }
}

impl Struct {
pub fn foo(self) -> u32 { 10 }
}

fn main() -> u32 {
let s = Struct {};
s.bar()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "disabled"
expected_result = { action = "return", value = 10 }
expected_warnings = 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'impl_self_method_order'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
implicit-std = false
license = "Apache-2.0"
name = "impl_self_method_order"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
script;

struct Struct {
}

impl Struct {
pub fn foo(self) -> u32 { 10 }
pub fn bar(self) -> u32 { self.foo() }
}

fn main() -> u32 {
let s = Struct {};
s.bar()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "return", value = 10 }
expected_warnings = 0

0 comments on commit 13f33f5

Please sign in to comment.