reject_iface_to_concrete.mx raw

   1  // expected: needs a type assertion
   2  //
   3  // An interface value is a dynamic container: getting the concrete value out
   4  // of it needs a type assertion, so it is never assignable to a concrete type.
   5  // ConcreteAssignTypes skipped an interface on EITHER side because a
   6  // DESTINATION interface's method set may not be resolved yet, and that skip
   7  // also covered the interface SOURCE, where Assignable's false is final.
   8  // stage4 compiled `takeBox(i)` and handed the interface payload to a Box
   9  // parameter - printing garbage (1132487840) instead of failing.
  10  // Legacy reports "cannot use i (variable of interface type I2) as Box value
  11  // in argument to takeBox: need type assertion".
  12  package main
  13  
  14  type I2 interface {
  15  	Equal(x I2) (ok bool)
  16  }
  17  
  18  type Box struct {
  19  	b []byte
  20  }
  21  
  22  func (k *Box) Equal(x I2) (ok bool) { return true }
  23  
  24  func takeBox(k Box) (n int32) { return len(k.b) }
  25  
  26  func main() {
  27  	o := Box{b: []byte{1, 2, 3}}
  28  	var i I2 = &o
  29  	_ = takeBox(i)
  30  }
  31