modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/interface/embed2.go (about)

     1  // errorcheck
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test methods derived from embedded interface and *interface values.
     8  
     9  package main
    10  
    11  import "os"
    12  
    13  const Value = 1e12
    14  
    15  type Inter interface {
    16  	M() int64
    17  }
    18  
    19  type T int64
    20  
    21  func (t T) M() int64 { return int64(t) }
    22  
    23  var t = T(Value)
    24  var pt = &t
    25  var ti Inter = t
    26  var pti = &ti
    27  
    28  type S struct{ Inter }
    29  
    30  var s = S{ti}
    31  var ps = &s
    32  
    33  type SP struct{ *Inter } // ERROR "interface"
    34  
    35  var i Inter
    36  var pi = &i
    37  
    38  var ok = true
    39  
    40  func check(s string, v int64) {
    41  	if v != Value {
    42  		println(s, v)
    43  		ok = false
    44  	}
    45  }
    46  
    47  func main() {
    48  	check("t.M()", t.M())
    49  	check("pt.M()", pt.M())
    50  	check("ti.M()", ti.M())
    51  	check("pti.M()", pti.M()) // ERROR "pointer to interface, not interface"
    52  	check("s.M()", s.M())
    53  	check("ps.M()", ps.M())
    54  
    55  	i = t
    56  	check("i = t; i.M()", i.M())
    57  	check("i = t; pi.M()", pi.M()) // ERROR "pointer to interface, not interface"
    58  
    59  	i = pt
    60  	check("i = pt; i.M()", i.M())
    61  	check("i = pt; pi.M()", pi.M()) // ERROR "pointer to interface, not interface"
    62  
    63  	i = s
    64  	check("i = s; i.M()", i.M())
    65  	check("i = s; pi.M()", pi.M()) // ERROR "pointer to interface, not interface"
    66  
    67  	i = ps
    68  	check("i = ps; i.M()", i.M())
    69  	check("i = ps; pi.M()", pi.M()) // ERROR "pointer to interface, not interface"
    70  
    71  	if !ok {
    72  		println("BUG: interface10")
    73  		os.Exit(1)
    74  	}
    75  }