Skip to content

Commit

Permalink
Allow to use private default method from a trait
Browse files Browse the repository at this point in the history
This commit fixes type checking of class that implement a trait which
contains a private method with a default implementation. Previously the
usage of such method resulted in an error during type checking saying
that the method is private.

This fixes #605.

Changelog: fixed
  • Loading branch information
bartlomieju authored and yorickpeterse committed Nov 27, 2023
1 parent 94a8b63 commit aa615ed
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion compiler/src/type_check/methods.rs
Expand Up @@ -1220,7 +1220,8 @@ impl<'a> ImplementTraitMethods<'a> {

let source = MethodSource::Implementation(trait_ins, method);
let name = method.name(self.db()).clone();
let copy = method.copy_method(self.db_mut());
let module_id = class_id.module(self.db());
let copy = method.copy_method(self.db_mut(), module_id);

// This is needed to ensure that the receiver of the default method
// is typed as the class that implements the trait, not as the trait
Expand Down
11 changes: 11 additions & 0 deletions std/test/diagnostics/class_with_trait_with_default_method.inko
@@ -0,0 +1,11 @@
import foo.Foo

class Bar {
fn baz {}
}

impl Foo for Bar {}

fn example(value: Bar) {
value.foo
}
@@ -0,0 +1,5 @@
trait pub Foo {
fn foo {
42
}
}
5 changes: 3 additions & 2 deletions types/src/lib.rs
Expand Up @@ -2186,10 +2186,11 @@ impl MethodId {
self.get(db).arguments.len()
}

pub fn copy_method(self, db: &mut Database) -> MethodId {
let copy = self.get(db).clone();
pub fn copy_method(self, db: &mut Database, module: ModuleId) -> MethodId {
let mut copy = self.get(db).clone();
let id = db.methods.len();

copy.module = module;
db.methods.push(copy);
MethodId(id)
}
Expand Down

0 comments on commit aa615ed

Please sign in to comment.