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

Problem replacing dummy trait methods #5673

Closed
xunilrj opened this issue Feb 27, 2024 · 4 comments · Fixed by #5684
Closed

Problem replacing dummy trait methods #5673

xunilrj opened this issue Feb 27, 2024 · 4 comments · Fixed by #5684
Assignees
Labels
bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen

Comments

@xunilrj
Copy link
Contributor

xunilrj commented Feb 27, 2024

The code below is triggering the following error:

 error
           --> /home/xunilrj/github/sway/test/src/e2e_vm_tests/test_programs/should_pass/language/main_args/main_args_empty/src/main.sw:4:8
            |
          2 | 
          3 | trait AbiEncode2 {
          4 |     fn abi_encode2(self, ref mut buffer: Buffer);
            |        ^^^^^^^^^^^ Internal compiler error: Method abi_encode2_3 is a trait method dummy and was not properly replaced.
          Please file an issue on the repository and include the code that triggered this error.
          5 | }
            |
          ____

            Aborting due to 1 error.

The code. This is a simplified version of the new encoding that is being implemented.

script;

trait AbiEncode2 {
    fn abi_encode2(self, ref mut buffer: Buffer);
}

impl AbiEncode2 for u64 { fn abi_encode2(self, ref mut buffer: Buffer) { } }
impl AbiEncode2 for u32 { fn abi_encode2(self, ref mut buffer: Buffer) { } }
impl AbiEncode2 for u16 { fn abi_encode2(self, ref mut buffer: Buffer) { } }
impl AbiEncode2 for u8 { fn abi_encode2(self, ref mut buffer: Buffer) { } }

struct GenericBimbam<U> {
    val: U,
}

impl<U> AbiEncode2 for GenericBimbam<U> where U: AbiEncode2,
 {
    fn abi_encode2(self, ref mut buffer: Buffer) {
        self.val.abi_encode2(buffer);
    }
}

struct GenericSnack<T, V> {
    twix: GenericBimbam<T>,
    mars: V,
}

impl<T, V> AbiEncode2 for GenericSnack<T, V> where T: AbiEncode2, V: AbiEncode2,
 {
    fn abi_encode2(self, ref mut buffer: Buffer) {
        self.twix.abi_encode2(buffer);
        self.mars.abi_encode2(buffer);
    }
}

fn encode2<T>(item: T) -> raw_slice where T: AbiEncode2  {
    let mut buffer = Buffer::new();
    item.abi_encode2(buffer);
    buffer.as_raw_slice()
}

fn main() {
    encode2(GenericSnack { twix: GenericBimbam { val: 0u64 }, mars: 2u32 });
}

This blocks: #5427

@xunilrj
Copy link
Contributor Author

xunilrj commented Feb 28, 2024

I don't know if this is the solution for this problem, but for my PR I solved this with: be3cc06#diff-35d4b64f583b6ae57aa8095e3d3049788690e178702af2aaf6443030818b74c8R707

@tritao
Copy link
Contributor

tritao commented Feb 28, 2024

So this seems to be a problem with trait constraints and our replace decls machinery. Me and @esdrubal been looking at this and found a workaround, if you move the where clauses to the method then it seems to work fine.

script;

trait AbiEncode2 {
    fn abi_encode2(self, ref mut buffer: Buffer);
}

impl AbiEncode2 for u64 { fn abi_encode2(self, ref mut buffer: Buffer) { } }
impl AbiEncode2 for u32 { fn abi_encode2(self, ref mut buffer: Buffer) { } }
impl AbiEncode2 for u16 { fn abi_encode2(self, ref mut buffer: Buffer) { } }
impl AbiEncode2 for u8 { fn abi_encode2(self, ref mut buffer: Buffer) { } }

struct GenericBimbam<U> {
    val: U,
}

impl<U> AbiEncode2 for GenericBimbam<U>
 {
    fn abi_encode2(self, ref mut buffer: Buffer) where U: AbiEncode2 {
        self.val.abi_encode2(buffer);
    }
}

struct GenericSnack<T, V> {
    twix: GenericBimbam<T>,
    mars: V,
}

impl<T, V> AbiEncode2 for GenericSnack<T, V>
 {
    fn abi_encode2(self, ref mut buffer: Buffer) where T: AbiEncode2, V: AbiEncode2 {
        self.twix.abi_encode2(buffer);
        self.mars.abi_encode2(buffer);
    }
}

fn encode2<T>(item: T) -> raw_slice where T: AbiEncode2  {
    let mut buffer = Buffer::new();
    item.abi_encode2(buffer);
    buffer.as_raw_slice()
}

fn main() {
    encode2(GenericSnack { twix: GenericBimbam { val: 0u64 }, mars: 2u32 });
}

The proper bugfix for this will probably take a bit of time to get done, so hopefully this is a workable workaround so we can get this feature out ASAP.

@xunilrj
Copy link
Contributor Author

xunilrj commented Feb 29, 2024

But should this supported? Rust correctly complains that the impl has more constraints than the trait declaration

error[E0276]: impl has stricter requirements than trait
  --> src/main.rs:36:57
   |
10 |     fn abi_encode2(self, buffer: &mut Buffer);
   |     ------------------------------------------ definition of `abi_encode2` from trait
...
36 |     fn abi_encode2(self,  buffer: &mut Buffer) where T: AbiEncode2, V: AbiEncode2 {
   |                                                         ^^^^^^^^^^ impl has extra requirement `T: AbiEncode2`

@IGI-111
Copy link
Contributor

IGI-111 commented Feb 29, 2024

Rust is right here, the signature of a trait method implementation should be the same as the trait method including the constraints. So this is exploiting a bug.

Looks like we didn't include those in the signature check when we implemented trait constraints. But we should.

@esdrubal esdrubal self-assigned this Mar 4, 2024
@esdrubal esdrubal added bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen labels Mar 4, 2024
esdrubal added a commit that referenced this issue Mar 4, 2024
When using nested generic methods, as in the use case added, the lower method
would not be properly replaced and would remain as a trait dummy method.

The fix was to do find_method_for_type in the ReplaceDecls of FunctionAplication,
this allows the replacement of the function aplication dummy method with
a proper method implementation.

Fixes #5673
tritao pushed a commit that referenced this issue Mar 4, 2024
## Description

When using nested generic methods, as in the use case added, the lower
method would not be properly replaced and would remain as a trait dummy
method.

The fix was to do find_method_for_type in the ReplaceDecls of
FunctionAplication, this allows the replacement of the function
application dummy method with a proper method implementation.

Fixes #5673


## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants