Skip to content

Commit

Permalink
Relax rules for capturing self in uni closures
Browse files Browse the repository at this point in the history
Closures of type `uni fn` are now allowed to capture `self` if it's a
value type or a module.

Changelog: added
  • Loading branch information
yorickpeterse committed May 30, 2024
1 parent e12d2b3 commit de057f5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/src/type_check/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4356,6 +4356,10 @@ impl<'a> CheckMethodBody<'a> {
scope: &LexicalScope,
location: &SourceLocation,
) {
if scope.surrounding_type.is_value_type(self.db()) {
return;
}

if scope.in_closure_in_recover() {
self.state
.diagnostics
Expand Down
26 changes: 26 additions & 0 deletions std/fixtures/diagnostics/closure_capture_self.inko
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class A {
fn foo {
recover fn { bar }
recover fn { self.bar }
}

fn bar {}
}

impl String {
fn foo {
recover fn { bar }
recover fn { self.bar }
}

fn bar {}
}

fn example1 {
recover fn { example2 }
}

fn example2 {}

# closure_capture_self.inko:3:18 error(invalid-type): closures inside a 'recover' can't capture or use 'self'
# closure_capture_self.inko:4:18 error(invalid-type): closures inside a 'recover' can't capture or use 'self'
6 changes: 6 additions & 0 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4141,6 +4141,12 @@ impl TypeRef {
| TypeRef::Uni(TypeId::ClassInstance(ins)) => {
ins.instance_of().is_value_type(db)
}
// Modules technically aren't values, but this allows certain checks
// for value types (e.g. to see if `self` can be captured) to
// automatically also handle modules.
TypeRef::Owned(TypeId::Module(_))
| TypeRef::Ref(TypeId::Module(_))
| TypeRef::Mut(TypeId::Module(_)) => true,
TypeRef::Owned(TypeId::Foreign(_)) => true,
TypeRef::Pointer(_) => true,
TypeRef::Placeholder(id) => {
Expand Down

0 comments on commit de057f5

Please sign in to comment.