github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/test/indirect1.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  // Verify that illegal uses of indirection are caught by the compiler.
     8  // Does not compile.
     9  
    10  package main
    11  
    12  var m0 map[string]int
    13  var m1 *map[string]int
    14  var m2 *map[string]int = &m0
    15  var m3 map[string]int = map[string]int{"a": 1}
    16  var m4 *map[string]int = &m3
    17  
    18  var s0 string
    19  var s1 *string
    20  var s2 *string = &s0
    21  var s3 string = "a"
    22  var s4 *string = &s3
    23  
    24  var a0 [10]int
    25  var a1 *[10]int
    26  var a2 *[10]int = &a0
    27  
    28  var b0 []int
    29  var b1 *[]int
    30  var b2 *[]int = &b0
    31  var b3 []int = []int{1, 2, 3}
    32  var b4 *[]int = &b3
    33  
    34  func f() {
    35  	// this is spaced funny so that
    36  	// the compiler will print a different
    37  	// line number for each len call when
    38  	// it decides there are type errors.
    39  	x :=
    40  		len(m0)+
    41  		len(m1)+	// ERROR "illegal|invalid|must be"
    42  		len(m2)+	// ERROR "illegal|invalid|must be"
    43  		len(m3)+
    44  		len(m4)+	// ERROR "illegal|invalid|must be"
    45  
    46  		len(s0)+
    47  		len(s1)+	// ERROR "illegal|invalid|must be"
    48  		len(s2)+	// ERROR "illegal|invalid|must be"
    49  		len(s3)+
    50  		len(s4)+	// ERROR "illegal|invalid|must be"
    51  
    52  		len(a0)+
    53  		len(a1)+
    54  		len(a2)+
    55  
    56  		cap(a0)+
    57  		cap(a1)+
    58  		cap(a2)+
    59  
    60  		len(b0)+
    61  		len(b1)+	// ERROR "illegal|invalid|must be"
    62  		len(b2)+	// ERROR "illegal|invalid|must be"
    63  		len(b3)+
    64  		len(b4)+	// ERROR "illegal|invalid|must be"
    65  
    66  		cap(b0)+
    67  		cap(b1)+	// ERROR "illegal|invalid|must be"
    68  		cap(b2)+	// ERROR "illegal|invalid|must be"
    69  		cap(b3)+
    70  		cap(b4)	// ERROR "illegal|invalid|must be"
    71  	_ = x
    72  }