Skip to content

Commit

Permalink
Fixes Self not found in match statement. (#4459)
Browse files Browse the repository at this point in the history
## Description

`Self` was not working when qualifying an enum variant in a match
statement.

The solution was to add the implementing type declaration as an
impl_trait namespace symbol named "Self".

Closes #4263.

## 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.

Co-authored-by: IGI-111 <[email protected]>
Co-authored-by: Joshua Batty <[email protected]>
  • Loading branch information
3 people authored Apr 24, 2023
1 parent c4368c3 commit 666db0f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 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 @@ -305,6 +305,17 @@ impl ty::TyImplTrait {
.with_help_text("")
.with_type_annotation(type_engine.insert(decl_engine, TypeInfo::Unknown));

// Insert implementing type decl as `Self` symbol.
let self_decl: Option<ty::TyDecl> = match type_engine.get(implementing_for.type_id) {
TypeInfo::Enum(r) => Some(r.into()),
TypeInfo::Struct(r) => Some(r.into()),
_ => None,
};
if let Some(self_decl) = self_decl {
ctx.namespace
.insert_symbol(Ident::new_no_span("Self".to_string()), self_decl);
}

// type check the items inside of the impl block
let mut new_items = vec![];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ impl Eq for Initialized {
}
}

impl Initialized {
fn foo(self) -> bool {
match self {
Self::True(_) => true,
Self::False(_) => false,
}
}
}

fn main() -> u64 {
let a = Initialized::True;
let b = Initialized::False;
let c = a == b;
assert(c == false);

assert(a.foo());

1
}

0 comments on commit 666db0f

Please sign in to comment.