cgen: fix using pointer as interface receiver

pull/4708/head
Enzo Baldisserri 2020-05-04 14:21:03 +02:00 committed by GitHub
parent 0e241162d9
commit 8fd69e845f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View File

@ -3429,7 +3429,8 @@ _Interface I_${cctype}_to_Interface(${cctype}* x) {
};
}')
methods_struct.writeln('\t{')
for method in ityp.methods {
st_sym := g.table.get_type_symbol(st)
for method in st_sym.methods {
// .speak = Cat_speak
mut method_call := '${cctype}_${method.name}'
if !method.args[0].typ.is_ptr() {

View File

@ -7,11 +7,13 @@ struct Cat {
breed string
}
fn (d Cat) name() string {
fn (mut c Cat) name() string {
assert c.breed == 'Persian'
return 'Cat'
}
fn (d Cat) speak(s string) {
fn (c &Cat) speak(s string) {
assert c.breed == 'Persian'
assert s == 'Hi !'
println('meow')
}
@ -32,7 +34,6 @@ fn test_todo() {
else{}
}
fn perform_speak(s Animal) {
s.speak('Hi !')
assert true
@ -47,9 +48,9 @@ fn perform_speak(s Animal) {
fn test_perform_speak() {
dog := Dog{breed: 'Labrador Retriever'}
perform_speak(dog)
cat := Cat{}
cat := Cat{breed: 'Persian'}
perform_speak(cat)
perform_speak(Cat{})
perform_speak(Cat{breed: 'Persian'})
handle_animals([dog, cat])
/*
f := Foo {
@ -94,7 +95,6 @@ interface Animal {
speak(s string)
}
fn test_interface_array() {
mut animals := []Animal{}
animals = [ Cat{}, Dog{} ]