golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/apidiff/testdata/splitting.go (about)

     1  package p
     2  
     3  // Splitting types
     4  //
     5  // In the old world, there is one type with two names, one of which is an alias.
     6  // In the new world, there are two distinct types.
     7  //
     8  // That is an incompatible change, because client code like
     9  //
    10  //     var v *T1 = new(T2)
    11  //
    12  // will succeed in the old world, where T1 and T2 name the same type,
    13  // but fail in the new world.
    14  
    15  // OK: in both old and new, A, B,  and C all name the same type.
    16  // old
    17  type (
    18  	A = B
    19  	B = C
    20  	C int
    21  )
    22  
    23  // new
    24  type (
    25  	A = B
    26  	B int
    27  	C = A
    28  )
    29  
    30  // An example of splitting:
    31  
    32  // Old has one type, D; new has E and D.
    33  // both
    34  type D int
    35  
    36  // old
    37  type E = D
    38  
    39  // new
    40  // i E: changed from D to E
    41  type E D // old D corresponds with new E
    42  // old D also corresponds with new D: problem
    43  
    44  // Here we have a benign split.
    45  // f and g are the same type in old and different types in new.
    46  // But clients have no way of constructing an expression of type f,
    47  // so they cannot write code that breaks.
    48  
    49  // both
    50  type f int
    51  
    52  var Vg g // expose g
    53  
    54  // old
    55  type g = f
    56  
    57  // new
    58  // OK: f isn't exposed
    59  type g f
    60  
    61  // Here we have another incompatible split, even
    62  // though the type names are unexported. The problem
    63  // is that both names are exposed via exported variables.
    64  
    65  // both
    66  type h int
    67  
    68  var Vj j // expose j
    69  var Vh h // expose h
    70  
    71  // old
    72  type j = h
    73  
    74  // new
    75  // i Vj: changed from h to j
    76  // e.g. p.Vj = p.Vh
    77  type j h