Black Lives Matter. Support the Equal Justice Initiative.

Source file test/devirt.go

Documentation: test

     1  // errorcheck -0 -d=ssa/opt/debug=1
     2  
     3  package main
     4  
     5  // Trivial interface call devirtualization test.
     6  
     7  type real struct {
     8  	value int
     9  }
    10  
    11  func (r *real) Value() int { return r.value }
    12  
    13  type Valuer interface {
    14  	Value() int
    15  }
    16  
    17  type indirectiface struct {
    18  	a, b, c int
    19  }
    20  
    21  func (i indirectiface) Value() int {
    22  	return i.a + i.b + i.c
    23  }
    24  
    25  func main() {
    26  	var r Valuer
    27  	rptr := &real{value: 3}
    28  	r = rptr
    29  
    30  	if r.Value() != 3 { // ERROR "de-virtualizing call$"
    31  		panic("not 3")
    32  	}
    33  
    34  	// Can't do types that aren't "direct" interfaces (yet).
    35  	r = indirectiface{3, 4, 5}
    36  	if r.Value() != 12 {
    37  		panic("not 12")
    38  	}
    39  }
    40  

View as plain text