~mna/snow unlisted

6ca70999f26b8a9f7f1c3e7c472da2ce193b10ce — Martin Angers 9 months ago 22baf71
pkg/{semantic,codegen}: create test cases for interfaces with ref methods
M pkg/codegen/testdata/fn_interface_with_ref_method.snow => pkg/codegen/testdata/fn_interface_with_ref_method.snow +5 -2
@@ 10,11 10,14 @@ struct S {
}

fn main() {
  let s = S(i: 0)
  # this shouldn't even work as s is immutable, can't get its ref method
  var s = S(i: 0)
  let iface: I = s
  iface.foo()
  println(s.i)

  # this should work, s is mutable
  let tup: (int, I) = (1, s)
  tup.1.foo()
}

@extern(import: "fmt", symbol: "Println")

M pkg/codegen/translate.go => pkg/codegen/translate.go +2 -1
@@ 659,7 659,8 @@ func (t *translator) convertExprFromTo(expr goast.Expr, fromT, toT semantic.Type
	var convertCallExpr goast.Expr

	// if toT is an interface, no explicit conversion needed, Go will be able to assign fromT
	// to it (as the rules for assignability are the same).
	// to it (as the rules for assignability are the same). Only thing is if one of the methods
	// is satisfied by a ref function, the address of expr must be returned instead.
	if semantic.AsInterfaceType(toT) != nil {
		return expr
	}

M pkg/semantic/testdata/interface_satisfied.snow => pkg/semantic/testdata/interface_satisfied.snow +6 -0
@@ 47,5 47,11 @@ fn main() {
  f = ls4
  # not this either, rhs is a value
  f = S4()

  var tup = (int, Foo) 
  # this is fine
  tup = (1, vs4)
  # this is not, should fail because ls4 is immutable
  tup = (1, ls4)
}