Source file test/initializerr.go

     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  // Verify that erroneous initialization expressions are caught by the compiler
     8  // Does not compile.
     9  
    10  package main
    11  
    12  type S struct {
    13  	A, B, C, X, Y, Z int
    14  }
    15  
    16  type T struct {
    17  	S
    18  }
    19  
    20  var x = 1
    21  var a1 = S{0, X: 1}                             // ERROR "mixture|undefined" "too few values"
    22  var a2 = S{Y: 3, Z: 2, Y: 3}                    // ERROR "duplicate"
    23  var a3 = T{S{}, 2, 3, 4, 5, 6}                  // ERROR "convert|too many"
    24  var a4 = [5]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} // ERROR "index|too many"
    25  var a5 = []byte{x: 2}                           // ERROR "index"
    26  var a6 = []byte{1: 1, 2: 2, 1: 3}               // ERROR "duplicate"
    27  
    28  var ok1 = S{}       // should be ok
    29  var ok2 = T{S: ok1} // should be ok
    30  
    31  // These keys can be computed at compile time but they are
    32  // not constants as defined by the spec, so they do not trigger
    33  // compile-time errors about duplicate key values.
    34  // See issue 4555.
    35  
    36  type Key struct{ X, Y int }
    37  
    38  var _ = map[Key]string{
    39  	Key{1, 2}: "hello",
    40  	Key{1, 2}: "world",
    41  }
    42  

View as plain text