...

Source file src/internal/types/testdata/check/cycles4.go

Documentation: internal/types/testdata/check

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package p
     6  
     7  import "unsafe"
     8  
     9  // Check that all methods of T are collected before
    10  // determining the result type of m (which embeds
    11  // all methods of T).
    12  
    13  type T interface {
    14  	m() interface {T}
    15  	E
    16  }
    17  
    18  var _ int = T.m(nil).m().e()
    19  
    20  type E interface {
    21  	e() int
    22  }
    23  
    24  // Check that unresolved forward chains are followed
    25  // (see also comment in resolver.go, checker.typeDecl).
    26  
    27  var _ int = C.m(nil).m().e()
    28  
    29  type A B
    30  
    31  type B interface {
    32  	m() interface{C}
    33  	E
    34  }
    35  
    36  type C A
    37  
    38  // Check that interface type comparison for identity
    39  // does not recur endlessly.
    40  
    41  type T1 interface {
    42  	m() interface{T1}
    43  }
    44  
    45  type T2 interface {
    46  	m() interface{T2}
    47  }
    48  
    49  func _(x T1, y T2) {
    50  	// Checking for assignability of interfaces must check
    51  	// if all methods of x are present in y, and that they
    52  	// have identical signatures. The signatures recur via
    53  	// the result type, which is an interface that embeds
    54  	// a single method m that refers to the very interface
    55  	// that contains it. This requires cycle detection in
    56  	// identity checks for interface types.
    57  	x = y
    58  }
    59  
    60  type T3 interface {
    61  	m() interface{T4}
    62  }
    63  
    64  type T4 interface {
    65  	m() interface{T3}
    66  }
    67  
    68  func _(x T1, y T3) {
    69  	x = y
    70  }
    71  
    72  // Check that interfaces are type-checked in order of
    73  // (embedded interface) dependencies (was issue 7158).
    74  
    75  var x1 T5 = T7(nil)
    76  
    77  type T5 interface {
    78  	T6
    79  }
    80  
    81  type T6 interface {
    82  	m() T7
    83  }
    84  type T7 interface {
    85  	T5
    86  }
    87  
    88  // Actual test case from issue 7158.
    89  
    90  func wrapNode() Node {
    91  	return wrapElement()
    92  }
    93  
    94  func wrapElement() Element {
    95  	return nil
    96  }
    97  
    98  type EventTarget interface {
    99  	AddEventListener(Event)
   100  }
   101  
   102  type Node interface {
   103  	EventTarget
   104  }
   105  
   106  type Element interface {
   107  	Node
   108  }
   109  
   110  type Event interface {
   111  	Target() Element
   112  }
   113  
   114  // Check that accessing an interface method too early doesn't lead
   115  // to follow-on errors due to an incorrectly computed type set.
   116  
   117  type T8 interface {
   118  	m() [unsafe.Sizeof(T8.m /* ERROR "undefined" */ )]int
   119  }
   120  
   121  var _ = T8.m // no error expected here
   122  

View as plain text