Comments (4)
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.
from sway.
I don't know if this is the solution for this problem, but for my PR I solved this with: be3cc06#diff-35d4b64f583b6ae57aa8095e3d3049788690e178702af2aaf6443030818b74c8R707
from sway.
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.
from sway.
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`
from sway.
Related Issues (20)
- forc command docs missing in master version of the book but latest release (v0.58.0) has them HOT 1
- Hide raw logs under --raw flag for forc-test
- Implementing an empty contract gives a false warning in an autogenerated file
- forc-doc implementations missing from sidebar and enums
- Newlines in str array in `configurable` block crash compiler HOT 2
- More than 64 characters in str array in `configurable` block crashes the compiler HOT 3
- Introduce a github spesific path in HEAD resolution for forc-pkg
- test suite `--update-output-files` flag only updates in a single profile
- Flaky LSP test: `go_to_definition_for_paths`
- CI checks to prevent size regressions in the IR
- Pushing a `StorageString` to `StorageVec` overwrites all values with the latest
- Increase std-lib test coverage
- Recursive `From` usage does not throw error
- `StorageVec::insert()` does not work with nested storage types
- False dead code warning when using aliases in structs
- Struct fields reported as never accessed although they are accessed via storage
- Internal compiler error when declaring a constant inside a function HOT 1
- Tune the inliner for codesize
- Impossible to insert into a `StorageMap` within a `struct` when using a helper function
- false transaion error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sway.