main.mx raw

   1  // Regression: cross-package interface dispatch called a pointer-receiver
   2  // method with a truncated receiver.
   3  //
   4  // findIfaceImpls' cross-package scan registered a pointer-receiver method with
   5  // recvType = the value Named while its typeID said pointer:named. For a Named
   6  // over int32, llvmType is "i32", so materializeIfaceValue took the inline
   7  // scalar path and ptrtoint/trunc'd the real receiver pointer to 32 bits; the
   8  // callee then read the low half of its own address and returned "other". A
   9  // struct worked only by accident, because intBits returns 0 for a non-scalar
  10  // and the pointer passed through untouched.
  11  //
  12  // The value form had no arm at all: pkg/types keeps a pointer-receiver method
  13  // in T's method set (Moxie bans value receivers), so `var e error = lib.K` was
  14  // accepted and then panicked "interface dispatch: no impl for Error".
  15  //
  16  // Fixed in: _mxc_stage4/ir_iface.mx (recvType is *T for a pointer receiver; the
  17  // value form is registered for pointer-receiver methods; emitIfaceAssertOK
  18  // counts each form once per method).
  19  
  20  package main
  21  
  22  import "tests/regressions/should_compile/iface_ptr_recv_crosspkg/lib"
  23  
  24  func chk(name, got, exp string) {
  25  	if got != exp {
  26  		println("FAIL: " | name | " got [" | got | "] want [" | exp | "]")
  27  		panic("mismatch")
  28  	}
  29  }
  30  
  31  func main() {
  32  	// Termination: a batch program must still be stoppable, so the entry
  33  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  34  	select {
  35  	case <-shutdown:
  36  		return
  37  	default:
  38  	}
  39  
  40  	p := lib.MK()
  41  	chk("direct-ptr", p.Error(), "one")
  42  
  43  	var ep error = lib.MK()
  44  	chk("iface-ptr", ep.Error(), "one")
  45  
  46  	var ev error = lib.C(1)
  47  	chk("iface-value", ev.Error(), "cone")
  48  
  49  	var ek error = lib.K
  50  	chk("iface-const", ek.Error(), "cone")
  51  
  52  	var es error = lib.NS()
  53  	chk("iface-struct-ptr", es.Error(), "sone")
  54  
  55  	var esv error = lib.S{V: 1}
  56  	chk("iface-struct-value", esv.Error(), "sone")
  57  
  58  	println("ok")
  59  }
  60