github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/test/fixedbugs/bug206.go (about)

     1  // cmpout
     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  package main
     8  
     9  import "go/ast";
    10  
    11  func g(list []ast.Expr) {
    12  	n := len(list)-1;
    13  	println(list[n].Pos());
    14  }
    15  
    16  
    17  // f is the same as g except that the expression assigned to n is inlined.
    18  func f(list []ast.Expr) {
    19  	// n := len(list)-1;
    20  	println(list[len(list)-1 /* n */].Pos());
    21  }
    22  
    23  
    24  func main() {
    25  	list := []ast.Expr{&ast.Ident{}};
    26  	g(list);  // this works
    27  	f(list);  // this doesn't
    28  }
    29  
    30  
    31  /*
    32  0
    33  throw: index out of range
    34  
    35  panic PC=0x2bcf10
    36  throw+0x33 /home/gri/go/src/pkg/runtime/runtime.c:71
    37  	throw(0x470f8, 0x0)
    38  sys·throwindex+0x1c /home/gri/go/src/pkg/runtime/runtime.c:45
    39  	sys·throwindex()
    40  main·f+0x26 /home/gri/go/test/bugs/bug206.go:16
    41  	main·f(0x2b9560, 0x0)
    42  main·main+0xc3 /home/gri/go/test/bugs/bug206.go:23
    43  	main·main()
    44  mainstart+0xf /home/gri/go/src/pkg/runtime/amd64/asm.s:55
    45  	mainstart()
    46  goexit /home/gri/go/src/pkg/runtime/proc.c:133
    47  	goexit()
    48  */