Skip to content

Commit

Permalink
Fix calling trait methods from each other
Browse files Browse the repository at this point in the history
An attempt to fix issue #4770 in the spirit of PR #5141.

Unfortunately (and a bit surprisingly), this fails when compiling stdlib:
```
  Compiling library std (/Users/anton/fuel/sway/sway-lib-std)

  Failed to compile std
  error
     --> /Users/anton/fuel/sway/sway-lib-std/src/hash.sw:322:41
      |
  320 |
  321 | #![inline(never)]
  322 | pub fn sha256<T>(s: T) -> b256 where T: Hash {
      |                                         ^^^^ Could not find symbol "Hash" in this scope.
  323 |     let mut hasher = Hasher::new();
  324 |     s.hash(hasher);
      |
  ____
```
  • Loading branch information
anton-trunov committed Oct 3, 2023
1 parent 91046eb commit 35e9e42
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ fn type_check_trait_implementation(
}
}

let mut all_items_refs: Vec<TyImplItem> = impld_item_refs.values().cloned().collect();
let all_items_refs_temp: Vec<TyImplItem> = impld_item_refs.values().cloned().collect();

// Retrieve the methods defined on the trait declaration and transform
// them into the correct typing for this impl block by using the type
Expand All @@ -684,6 +684,28 @@ fn type_check_trait_implementation(
BTreeMap::new(),
impld_item_refs,
);

let mut all_items_refs: Vec<TyImplItem> = vec![];
for item in all_items_refs_temp.iter() {
match item {
TyImplItem::Fn(decl_ref) => {
let mut method = decl_engine.get_function(decl_ref);
method.replace_decls(&decl_mapping, handler, &mut ctx)?;
all_items_refs.push(TyImplItem::Fn(
decl_engine
.insert(method)
.with_parent(decl_engine, (*decl_ref.id()).into()),
));
}
TyImplItem::Constant(decl_ref) => {
let mut const_decl = decl_engine.get_constant(decl_ref);
const_decl.replace_decls(&decl_mapping, handler, &mut ctx)?;
all_items_refs.push(TyImplItem::Constant(decl_engine.insert(const_decl)));
}
TyImplItem::Type(_) => all_items_refs.push(item.clone()),
}
}

for item in trait_items.iter() {
match item {
TyImplItem::Fn(decl_ref) => {
Expand Down

0 comments on commit 35e9e42

Please sign in to comment.