Commit c25edd44 by Ian Lance Taylor

libgo: update to go1.8rc2

    
    Fix a bug in the generation of the hash value in reflect.FuncOf.
    
    The merge script missed a bunch of testdata files over the years.
    Copy them over.
    
    Reviewed-on: https://go-review.googlesource.com/35570

From-SVN: r244835
parent 28826a66
0655e25d8e4acfac50c6b1422dc32eca3e30803a
fb609ff6d940768cf4db4ab7deb93b2ab686e45d
The first line of this file holds the git revision number of the last
merge done from the gofrontend repository.
3de6e96e4b8147f5267a2e8218a7c780b09a434f
59f181b6fda68ece22882945853ca2df9dbf1c88
The first line of this file holds the git revision number of the
last merge done from the master library sources.
......@@ -20,11 +20,10 @@ import (
var cmdBug = &Command{
Run: runBug,
UsageLine: "bug",
Short: "print information for bug reports",
Short: "start a bug report",
Long: `
Bug prints information that helps file effective bug reports.
Bugs may be reported at https://golang.org/issue/new.
Bug opens the default browser and starts a new bug report.
The report includes useful system information.
`,
}
......
......@@ -906,9 +906,13 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
if buildContext.GOOS == "darwin" {
if buildContext.GOARCH == "arm" || buildContext.GOARCH == "arm64" {
t.NeedCgo = true
t.IsIOS = true
t.NeedOS = true
}
}
if t.TestMain == nil {
t.NeedOS = true
}
for _, cp := range pmain.imports {
if len(cp.coverVars) > 0 {
......@@ -1355,7 +1359,8 @@ type testFuncs struct {
NeedTest bool
ImportXtest bool
NeedXtest bool
NeedCgo bool
NeedOS bool
IsIOS bool
Cover []coverInfo
}
......@@ -1456,7 +1461,7 @@ var testmainTmpl = template.Must(template.New("main").Parse(`
package main
import (
{{if not .TestMain}}
{{if .NeedOS}}
"os"
{{end}}
"testing"
......@@ -1472,8 +1477,10 @@ import (
_cover{{$i}} {{$p.Package.ImportPath | printf "%q"}}
{{end}}
{{if .NeedCgo}}
{{if .IsIOS}}
"os/signal"
_ "runtime/cgo"
"syscall"
{{end}}
)
......@@ -1535,6 +1542,32 @@ func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts
{{end}}
func main() {
{{if .IsIOS}}
// Send a SIGUSR2, which will be intercepted by LLDB to
// tell the test harness that installation was successful.
// See misc/ios/go_darwin_arm_exec.go.
signal.Notify(make(chan os.Signal), syscall.SIGUSR2)
syscall.Kill(0, syscall.SIGUSR2)
signal.Reset(syscall.SIGUSR2)
// The first argument supplied to an iOS test is an offset
// suffix for the current working directory.
// Process it here, and remove it from os.Args.
const hdr = "cwdSuffix="
if len(os.Args) < 2 || len(os.Args[1]) <= len(hdr) || os.Args[1][:len(hdr)] != hdr {
panic("iOS test not passed a working directory suffix")
}
suffix := os.Args[1][len(hdr):]
dir, err := os.Getwd()
if err != nil {
panic(err)
}
if err := os.Chdir(dir + "/" + suffix); err != nil {
panic(err)
}
os.Args = append([]string{os.Args[0]}, os.Args[2:]...)
{{end}}
{{if .CoverEnabled}}
testing.RegisterCover(testing.Cover{
Mode: {{printf "%q" .CoverMode}},
......
//gofmt -s
// Test case for issue 7631.
package main
// Keep this declaration
var ()
const (
// Keep this declaration
)
func main() {}
//gofmt -s
// Test case for issue 7631.
package main
// Keep this declaration
var ()
const (
// Keep this declaration
)
type ()
func main() {}
\ No newline at end of file
//gofmt -s
// Test cases for range simplification.
package p
func _() {
for a, b = range x {
}
for a = range x {
}
for _, b = range x {
}
for range x {
}
for a = range x {
}
for range x {
}
for a, b := range x {
}
for a := range x {
}
for _, b := range x {
}
for a := range x {
}
}
//gofmt -s
// Test cases for range simplification.
package p
func _() {
for a, b = range x {}
for a, _ = range x {}
for _, b = range x {}
for _, _ = range x {}
for a = range x {}
for _ = range x {}
for a, b := range x {}
for a, _ := range x {}
for _, b := range x {}
for a := range x {}
}
//gofmt -stdin
i := 5 // Line comment without newline.
\ No newline at end of file
//gofmt -stdin
i :=5// Line comment without newline.
\ No newline at end of file
//gofmt -stdin
if err != nil {
source := strings.NewReader(`line 1.
line 2.
`)
return source
}
f := func(hat, tail string) {
fmt.Println(hat+`
foo
`+tail,
"more",
"and more")
}
//gofmt -stdin
if err != nil {
source := strings.NewReader(`line 1.
line 2.
`)
return source
}
f:=func( hat, tail string){
fmt. Println ( hat+ `
foo
`+ tail ,
"more" ,
"and more" )
}
//gofmt -stdin
if err != nil {
source := strings.NewReader(`line 1.
line 2.
`)
return source
}
f := func(hat, tail string) {
fmt.Println(hat+`
foo
`+tail,
"more",
"and more")
}
//gofmt -stdin
if err != nil {
source := strings.NewReader(`line 1.
line 2.
`)
return source
}
f:=func( hat, tail string){
fmt. Println ( hat+ `
foo
`+ tail ,
"more" ,
"and more" )
}
......@@ -204,12 +204,6 @@ func TestMTF(t *testing.T) {
}
}
var (
digits = mustLoadFile("testdata/e.txt.bz2")
twain = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt.bz2")
random = mustLoadFile("testdata/random.data.bz2")
)
func benchmarkDecode(b *testing.B, compressed []byte) {
// Determine the uncompressed size of testfile.
uncompressedSize, err := io.Copy(ioutil.Discard, NewReader(bytes.NewReader(compressed)))
......@@ -227,6 +221,18 @@ func benchmarkDecode(b *testing.B, compressed []byte) {
}
}
func BenchmarkDecodeDigits(b *testing.B) { benchmarkDecode(b, digits) }
func BenchmarkDecodeTwain(b *testing.B) { benchmarkDecode(b, twain) }
func BenchmarkDecodeRand(b *testing.B) { benchmarkDecode(b, random) }
func BenchmarkDecodeDigits(b *testing.B) {
digits := mustLoadFile("testdata/e.txt.bz2")
b.ResetTimer()
benchmarkDecode(b, digits)
}
func BenchmarkDecodeTwain(b *testing.B) {
twain := mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt.bz2")
b.ResetTimer()
benchmarkDecode(b, twain)
}
func BenchmarkDecodeRand(b *testing.B) {
random := mustLoadFile("testdata/random.data.bz2")
b.ResetTimer()
benchmarkDecode(b, random)
}
......@@ -136,14 +136,17 @@ func (d *compressor) fillDeflate(b []byte) int {
delta := d.hashOffset - 1
d.hashOffset -= delta
d.chainHead -= delta
for i, v := range d.hashPrev {
// Iterate over slices instead of arrays to avoid copying
// the entire table onto the stack (Issue #18625).
for i, v := range d.hashPrev[:] {
if int(v) > delta {
d.hashPrev[i] = uint32(int(v) - delta)
} else {
d.hashPrev[i] = 0
}
}
for i, v := range d.hashHead {
for i, v := range d.hashHead[:] {
if int(v) > delta {
d.hashHead[i] = uint32(int(v) - delta)
} else {
......
......@@ -12,6 +12,7 @@ import (
"io"
"io/ioutil"
"reflect"
"runtime/debug"
"sync"
"testing"
)
......@@ -864,3 +865,33 @@ func TestBestSpeedMaxMatchOffset(t *testing.T) {
}
}
}
func TestMaxStackSize(t *testing.T) {
// This test must not run in parallel with other tests as debug.SetMaxStack
// affects all goroutines.
n := debug.SetMaxStack(1 << 16)
defer debug.SetMaxStack(n)
var wg sync.WaitGroup
defer wg.Wait()
b := make([]byte, 1<<20)
for level := HuffmanOnly; level <= BestCompression; level++ {
// Run in separate goroutine to increase probability of stack regrowth.
wg.Add(1)
go func(level int) {
defer wg.Done()
zw, err := NewWriter(ioutil.Discard, level)
if err != nil {
t.Errorf("level %d, NewWriter() = %v, want nil", level, err)
}
if n, err := zw.Write(b); n != len(b) || err != nil {
t.Errorf("level %d, Write() = (%d, %v), want (%d, nil)", level, n, err, len(b))
}
if err := zw.Close(); err != nil {
t.Errorf("level %d, Close() = %v, want nil", level, err)
}
zw.Reset(ioutil.Discard)
}(level)
}
}
......@@ -60,7 +60,7 @@ func newDeflateFast() *deflateFast {
func (e *deflateFast) encode(dst []token, src []byte) []token {
// Ensure that e.cur doesn't wrap.
if e.cur > 1<<30 {
*e = deflateFast{cur: maxStoreBlockSize, prev: e.prev[:0]}
e.resetAll()
}
// This check isn't in the Snappy implementation, but there, the caller
......@@ -265,6 +265,21 @@ func (e *deflateFast) reset() {
// Protect against e.cur wraparound.
if e.cur > 1<<30 {
*e = deflateFast{cur: maxStoreBlockSize, prev: e.prev[:0]}
e.resetAll()
}
}
// resetAll resets the deflateFast struct and is only called in rare
// situations to prevent integer overflow. It manually resets each field
// to avoid causing large stack growth.
//
// See https://golang.org/issue/18636.
func (e *deflateFast) resetAll() {
// This is equivalent to:
// *e = deflateFast{cur: maxStoreBlockSize, prev: e.prev[:0]}
e.cur = maxStoreBlockSize
e.prev = e.prev[:0]
for i := range e.table {
e.table[i] = tableEntry{}
}
}
......@@ -9,11 +9,17 @@ import (
"testing"
)
// Per golang.org/issue/14937, check that every .gz file
// in the tree has a zero mtime.
// TestGZIPFilesHaveZeroMTimes checks that every .gz file in the tree
// has a zero MTIME. This is a requirement for the Debian maintainers
// to be able to have deterministic packages.
//
// See https://golang.org/issue/14937.
func TestGZIPFilesHaveZeroMTimes(t *testing.T) {
if testing.Short() && testenv.Builder() == "" {
t.Skip("skipping in short mode")
// To avoid spurious false positives due to untracked GZIP files that
// may be in the user's GOROOT (Issue 18604), we only run this test on
// the builders, which should have a clean checkout of the tree.
if testenv.Builder() == "" {
t.Skip("skipping test on non-builder")
}
goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
if err != nil {
......
......@@ -84,15 +84,15 @@ var cipherSuites = []*cipherSuite{
{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
{TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
{TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, cipherAES, macSHA256, nil},
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
{TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
{TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
{TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
{TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
{TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
{TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
......
......@@ -6,8 +6,8 @@
package tls
// BUG(agl): The crypto/tls package only implements some countermeasures
// against Lucky13 attacks on CBC-mode encryption. See
// http://www.isg.rhul.ac.uk/tls/TLStiming.pdf and
// against Lucky13 attacks on CBC-mode encryption, and only on SHA1
// variants. See http://www.isg.rhul.ac.uk/tls/TLStiming.pdf and
// https://www.imperialviolet.org/2013/02/04/luckythirteen.html.
import (
......
......@@ -4,7 +4,11 @@
package x509
import "encoding/pem"
import (
"encoding/pem"
"errors"
"runtime"
)
// CertPool is a set of certificates.
type CertPool struct {
......@@ -26,6 +30,11 @@ func NewCertPool() *CertPool {
// Any mutations to the returned pool are not written to disk and do
// not affect any other pool.
func SystemCertPool() (*CertPool, error) {
if runtime.GOOS == "windows" {
// Issue 16736, 18609:
return nil, errors.New("crypto/x509: system root pool is not available on Windows")
}
return loadSystemRoots()
}
......
......@@ -226,6 +226,11 @@ func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate
}
func loadSystemRoots() (*CertPool, error) {
// TODO: restore this functionality on Windows. We tried to do
// it in Go 1.8 but had to revert it. See Issue 18609.
// Returning (nil, nil) was the old behavior, prior to CL 30578.
return nil, nil
const CRYPT_E_NOT_FOUND = 0x80092004
store, err := syscall.CertOpenSystemStore(0, syscall.StringToUTF16Ptr("ROOT"))
......
......@@ -24,6 +24,7 @@ import (
"net"
"os/exec"
"reflect"
"runtime"
"strings"
"testing"
"time"
......@@ -1477,6 +1478,9 @@ func TestMultipleRDN(t *testing.T) {
}
func TestSystemCertPool(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("not implemented on Windows; Issue 16736, 18609")
}
_, err := SystemCertPool()
if err != nil {
t.Fatal(err)
......
......@@ -70,10 +70,8 @@ func (s *Scope) String() string {
// The Data fields contains object-specific data:
//
// Kind Data type Data value
// Pkg *types.Package package scope
// Pkg *Scope package scope
// Con int iota for the respective declaration
// Con != nil constant value
// Typ *Scope (used as method scope during type checking - transient)
//
type Object struct {
Kind ObjKind
......
......@@ -25,7 +25,7 @@ var files = flag.String("files", "", "consider only Go test files matching this
const dataDir = "testdata"
var templateTxt = readTemplate("template.txt")
var templateTxt *template.Template
func readTemplate(filename string) *template.Template {
t := template.New(filename)
......@@ -96,6 +96,9 @@ func test(t *testing.T, mode Mode) {
if err != nil {
t.Fatal(err)
}
if templateTxt == nil {
templateTxt = readTemplate("template.txt")
}
// test packages
for _, pkg := range pkgs {
......
v1;
package complexnums;
pkgpath complexnums;
priority 1;
const NN = -0.1E1-0.1E1i ;
const NP = -0.1E1+0.1E1i ;
const PN = 0.1E1-0.1E1i ;
const PP = 0.1E1+0.1E1i ;
v2;
package conversions;
prefix go;
package conversions go.conversions go.conversions;
const Bits <type 1 "Units" <type -16>> = convert(<type 1>, "bits");
type <type 1>;
v1;
package imports;
pkgpath imports;
priority 7;
import fmt fmt "fmt";
init imports imports..import 7 math math..import 1 runtime runtime..import 1 strconv strconv..import 2 io io..import 3 reflect reflect..import 3 syscall syscall..import 3 time time..import 4 os os..import 5 fmt fmt..import 6;
var Hello <type -16>;
v1;
package pointer;
pkgpath pointer;
type <type 1 "Int8Ptr" <type 2 *<type -1>>>;
......@@ -10,17 +10,12 @@ import (
"testing"
)
var src = readFile("parser.go")
func readFile(filename string) []byte {
data, err := ioutil.ReadFile(filename)
func BenchmarkParse(b *testing.B) {
src, err := ioutil.ReadFile("parser.go")
if err != nil {
panic(err)
b.Fatal(err)
}
return data
}
func BenchmarkParse(b *testing.B) {
b.ResetTimer()
b.SetBytes(int64(len(src)))
for i := 0; i < b.N; i++ {
if _, err := ParseFile(token.NewFileSet(), "", src, ParseComments); err != nil {
......
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package _ /* ERROR invalid package name */
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// constant declarations
package const0
// constants declarations must be initialized by constants
var x = 0
const c0 = x /* ERROR "not constant" */
// typed constants must have constant types
const _ interface /* ERROR invalid constant type */ {} = 0
func _ () {
const _ interface /* ERROR invalid constant type */ {} = 0
for i := 0; i < 10; i++ {} // don't crash with non-nil iota here
}
// untyped constants
const (
// boolean values
ub0 = false
ub1 = true
ub2 = 2 < 1
ub3 = ui1 == uf1
ub4 = true /* ERROR "cannot convert" */ == 0
// integer values
ui0 = 0
ui1 = 1
ui2 = 42
ui3 = 3141592653589793238462643383279502884197169399375105820974944592307816406286
ui4 = -10
ui5 = ui0 + ui1
ui6 = ui1 - ui1
ui7 = ui2 * ui1
ui8 = ui3 / ui3
ui9 = ui3 % ui3
ui10 = 1 / 0 /* ERROR "division by zero" */
ui11 = ui1 / 0 /* ERROR "division by zero" */
ui12 = ui3 / ui0 /* ERROR "division by zero" */
ui13 = 1 % 0 /* ERROR "division by zero" */
ui14 = ui1 % 0 /* ERROR "division by zero" */
ui15 = ui3 % ui0 /* ERROR "division by zero" */
ui16 = ui2 & ui3
ui17 = ui2 | ui3
ui18 = ui2 ^ ui3
ui19 = 1 /* ERROR "invalid operation" */ % 1.0
// floating point values
uf0 = 0.
uf1 = 1.
uf2 = 4.2e1
uf3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
uf4 = 1e-1
uf5 = uf0 + uf1
uf6 = uf1 - uf1
uf7 = uf2 * uf1
uf8 = uf3 / uf3
uf9 = uf3 /* ERROR "not defined" */ % uf3
uf10 = 1 / 0 /* ERROR "division by zero" */
uf11 = uf1 / 0 /* ERROR "division by zero" */
uf12 = uf3 / uf0 /* ERROR "division by zero" */
uf16 = uf2 /* ERROR "not defined" */ & uf3
uf17 = uf2 /* ERROR "not defined" */ | uf3
uf18 = uf2 /* ERROR "not defined" */ ^ uf3
// complex values
uc0 = 0.i
uc1 = 1.i
uc2 = 4.2e1i
uc3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286i
uc4 = 1e-1i
uc5 = uc0 + uc1
uc6 = uc1 - uc1
uc7 = uc2 * uc1
uc8 = uc3 / uc3
uc9 = uc3 /* ERROR "not defined" */ % uc3
uc10 = 1 / 0 /* ERROR "division by zero" */
uc11 = uc1 / 0 /* ERROR "division by zero" */
uc12 = uc3 / uc0 /* ERROR "division by zero" */
uc16 = uc2 /* ERROR "not defined" */ & uc3
uc17 = uc2 /* ERROR "not defined" */ | uc3
uc18 = uc2 /* ERROR "not defined" */ ^ uc3
)
type (
mybool bool
myint int
myfloat float64
mycomplex complex128
)
// typed constants
const (
// boolean values
tb0 bool = false
tb1 bool = true
tb2 mybool = 2 < 1
tb3 mybool = ti1 /* ERROR "mismatched types" */ == tf1
// integer values
ti0 int8 = ui0
ti1 int32 = ui1
ti2 int64 = ui2
ti3 myint = ui3 /* ERROR "overflows" */
ti4 myint = ui4
ti5 = ti0 /* ERROR "mismatched types" */ + ti1
ti6 = ti1 - ti1
ti7 = ti2 /* ERROR "mismatched types" */ * ti1
ti8 = ti3 / ti3
ti9 = ti3 % ti3
ti10 = 1 / 0 /* ERROR "division by zero" */
ti11 = ti1 / 0 /* ERROR "division by zero" */
ti12 = ti3 /* ERROR "mismatched types" */ / ti0
ti13 = 1 % 0 /* ERROR "division by zero" */
ti14 = ti1 % 0 /* ERROR "division by zero" */
ti15 = ti3 /* ERROR "mismatched types" */ % ti0
ti16 = ti2 /* ERROR "mismatched types" */ & ti3
ti17 = ti2 /* ERROR "mismatched types" */ | ti4
ti18 = ti2 ^ ti5 // no mismatched types error because the type of ti5 is unknown
// floating point values
tf0 float32 = 0.
tf1 float32 = 1.
tf2 float64 = 4.2e1
tf3 myfloat = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
tf4 myfloat = 1e-1
tf5 = tf0 + tf1
tf6 = tf1 - tf1
tf7 = tf2 /* ERROR "mismatched types" */ * tf1
tf8 = tf3 / tf3
tf9 = tf3 /* ERROR "not defined" */ % tf3
tf10 = 1 / 0 /* ERROR "division by zero" */
tf11 = tf1 / 0 /* ERROR "division by zero" */
tf12 = tf3 /* ERROR "mismatched types" */ / tf0
tf16 = tf2 /* ERROR "mismatched types" */ & tf3
tf17 = tf2 /* ERROR "mismatched types" */ | tf3
tf18 = tf2 /* ERROR "mismatched types" */ ^ tf3
// complex values
tc0 = 0.i
tc1 = 1.i
tc2 = 4.2e1i
tc3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286i
tc4 = 1e-1i
tc5 = tc0 + tc1
tc6 = tc1 - tc1
tc7 = tc2 * tc1
tc8 = tc3 / tc3
tc9 = tc3 /* ERROR "not defined" */ % tc3
tc10 = 1 / 0 /* ERROR "division by zero" */
tc11 = tc1 / 0 /* ERROR "division by zero" */
tc12 = tc3 / tc0 /* ERROR "division by zero" */
tc16 = tc2 /* ERROR "not defined" */ & tc3
tc17 = tc2 /* ERROR "not defined" */ | tc3
tc18 = tc2 /* ERROR "not defined" */ ^ tc3
)
// initialization cycles
const (
a /* ERROR "initialization cycle" */ = a
b /* ERROR "initialization cycle" */ , c /* ERROR "initialization cycle" */, d, e = e, d, c, b // TODO(gri) should only have one cycle error
f float64 = d
)
// multiple initialization
const (
a1, a2, a3 = 7, 3.1415926, "foo"
b1, b2, b3 = b3, b1, 42
c1, c2, c3 /* ERROR "missing init expr for c3" */ = 1, 2
d1, d2, d3 = 1, 2, 3, 4 /* ERROR "extra init expr 4" */
_p0 = assert(a1 == 7)
_p1 = assert(a2 == 3.1415926)
_p2 = assert(a3 == "foo")
_p3 = assert(b1 == 42)
_p4 = assert(b2 == 42)
_p5 = assert(b3 == 42)
)
func _() {
const (
a1, a2, a3 = 7, 3.1415926, "foo"
b1, b2, b3 = b3, b1, 42
c1, c2, c3 /* ERROR "missing init expr for c3" */ = 1, 2
d1, d2, d3 = 1, 2, 3, 4 /* ERROR "extra init expr 4" */
_p0 = assert(a1 == 7)
_p1 = assert(a2 == 3.1415926)
_p2 = assert(a3 == "foo")
_p3 = assert(b1 == 42)
_p4 = assert(b2 == 42)
_p5 = assert(b3 == 42)
)
}
// iota
const (
iota0 = iota
iota1 = iota
iota2 = iota*2
_a0 = assert(iota0 == 0)
_a1 = assert(iota1 == 1)
_a2 = assert(iota2 == 4)
iota6 = iota*3
iota7
iota8
_a3 = assert(iota7 == 21)
_a4 = assert(iota8 == 24)
)
const (
_b0 = iota
_b1 = assert(iota + iota2 == 5)
_b2 = len([iota]int{}) // iota may appear in a type!
_b3 = assert(_b2 == 2)
_b4 = len(A{})
)
type A [iota /* ERROR "cannot use iota" */ ]int
// constant expressions with operands across different
// constant declarations must use the right iota values
const (
_c0 = iota
_c1
_c2
_x = _c2 + _d1 + _e0 // 3
)
const (
_d0 = iota
_d1
)
const (
_e0 = iota
)
var _ = assert(_x == 3)
// special cases
const (
_n0 = nil /* ERROR "not constant" */
_n1 = [ /* ERROR "not constant" */ ]int{}
)
// iotas must not be usable in expressions outside constant declarations
type _ [iota /* ERROR "iota outside constant decl" */ ]byte
var _ = iota /* ERROR "iota outside constant decl" */
func _() {
_ = iota /* ERROR "iota outside constant decl" */
const _ = iota
_ = iota /* ERROR "iota outside constant decl" */
}
func _() {
iota := 123
const x = iota /* ERROR "is not constant" */
var y = iota
_ = y
}
// constant arithmetic precision and rounding must lead to expected (integer) results
var _ = []int64{
0.0005 * 1e9,
0.001 * 1e9,
0.005 * 1e9,
0.01 * 1e9,
0.05 * 1e9,
0.1 * 1e9,
0.5 * 1e9,
1 * 1e9,
5 * 1e9,
}
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// constant conversions
package const1
const(
mi = ^int(0)
mu = ^uint(0)
mp = ^uintptr(0)
logSizeofInt = uint(mi>>8&1 + mi>>16&1 + mi>>32&1)
logSizeofUint = uint(mu>>8&1 + mu>>16&1 + mu>>32&1)
logSizeofUintptr = uint(mp>>8&1 + mp>>16&1 + mp>>32&1)
)
const (
minInt8 = -1<<(8<<iota - 1)
minInt16
minInt32
minInt64
minInt = -1<<(8<<logSizeofInt - 1)
)
const (
maxInt8 = 1<<(8<<iota - 1) - 1
maxInt16
maxInt32
maxInt64
maxInt = 1<<(8<<logSizeofInt - 1) - 1
)
const (
maxUint8 = 1<<(8<<iota) - 1
maxUint16
maxUint32
maxUint64
maxUint = 1<<(8<<logSizeofUint) - 1
maxUintptr = 1<<(8<<logSizeofUintptr) - 1
)
const (
smallestFloat32 = 1.0 / (1<<(127 - 1 + 23))
smallestFloat64 = 1.0 / (1<<(1023 - 1 + 52))
)
const (
_ = assert(smallestFloat32 > 0)
_ = assert(smallestFloat64 > 0)
)
const (
maxFloat32 = 1<<127 * (1<<24 - 1) / (1.0<<23)
maxFloat64 = 1<<1023 * (1<<53 - 1) / (1.0<<52)
)
const (
_ int8 = minInt8 /* ERROR "overflows" */ - 1
_ int8 = minInt8
_ int8 = maxInt8
_ int8 = maxInt8 /* ERROR "overflows" */ + 1
_ int8 = smallestFloat64 /* ERROR "truncated" */
_ = int8(minInt8 /* ERROR "cannot convert" */ - 1)
_ = int8(minInt8)
_ = int8(maxInt8)
_ = int8(maxInt8 /* ERROR "cannot convert" */ + 1)
_ = int8(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ int16 = minInt16 /* ERROR "overflows" */ - 1
_ int16 = minInt16
_ int16 = maxInt16
_ int16 = maxInt16 /* ERROR "overflows" */ + 1
_ int16 = smallestFloat64 /* ERROR "truncated" */
_ = int16(minInt16 /* ERROR "cannot convert" */ - 1)
_ = int16(minInt16)
_ = int16(maxInt16)
_ = int16(maxInt16 /* ERROR "cannot convert" */ + 1)
_ = int16(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ int32 = minInt32 /* ERROR "overflows" */ - 1
_ int32 = minInt32
_ int32 = maxInt32
_ int32 = maxInt32 /* ERROR "overflows" */ + 1
_ int32 = smallestFloat64 /* ERROR "truncated" */
_ = int32(minInt32 /* ERROR "cannot convert" */ - 1)
_ = int32(minInt32)
_ = int32(maxInt32)
_ = int32(maxInt32 /* ERROR "cannot convert" */ + 1)
_ = int32(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ int64 = minInt64 /* ERROR "overflows" */ - 1
_ int64 = minInt64
_ int64 = maxInt64
_ int64 = maxInt64 /* ERROR "overflows" */ + 1
_ int64 = smallestFloat64 /* ERROR "truncated" */
_ = int64(minInt64 /* ERROR "cannot convert" */ - 1)
_ = int64(minInt64)
_ = int64(maxInt64)
_ = int64(maxInt64 /* ERROR "cannot convert" */ + 1)
_ = int64(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ int = minInt /* ERROR "overflows" */ - 1
_ int = minInt
_ int = maxInt
_ int = maxInt /* ERROR "overflows" */ + 1
_ int = smallestFloat64 /* ERROR "truncated" */
_ = int(minInt /* ERROR "cannot convert" */ - 1)
_ = int(minInt)
_ = int(maxInt)
_ = int(maxInt /* ERROR "cannot convert" */ + 1)
_ = int(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ uint8 = 0 /* ERROR "overflows" */ - 1
_ uint8 = 0
_ uint8 = maxUint8
_ uint8 = maxUint8 /* ERROR "overflows" */ + 1
_ uint8 = smallestFloat64 /* ERROR "truncated" */
_ = uint8(0 /* ERROR "cannot convert" */ - 1)
_ = uint8(0)
_ = uint8(maxUint8)
_ = uint8(maxUint8 /* ERROR "cannot convert" */ + 1)
_ = uint8(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ uint16 = 0 /* ERROR "overflows" */ - 1
_ uint16 = 0
_ uint16 = maxUint16
_ uint16 = maxUint16 /* ERROR "overflows" */ + 1
_ uint16 = smallestFloat64 /* ERROR "truncated" */
_ = uint16(0 /* ERROR "cannot convert" */ - 1)
_ = uint16(0)
_ = uint16(maxUint16)
_ = uint16(maxUint16 /* ERROR "cannot convert" */ + 1)
_ = uint16(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ uint32 = 0 /* ERROR "overflows" */ - 1
_ uint32 = 0
_ uint32 = maxUint32
_ uint32 = maxUint32 /* ERROR "overflows" */ + 1
_ uint32 = smallestFloat64 /* ERROR "truncated" */
_ = uint32(0 /* ERROR "cannot convert" */ - 1)
_ = uint32(0)
_ = uint32(maxUint32)
_ = uint32(maxUint32 /* ERROR "cannot convert" */ + 1)
_ = uint32(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ uint64 = 0 /* ERROR "overflows" */ - 1
_ uint64 = 0
_ uint64 = maxUint64
_ uint64 = maxUint64 /* ERROR "overflows" */ + 1
_ uint64 = smallestFloat64 /* ERROR "truncated" */
_ = uint64(0 /* ERROR "cannot convert" */ - 1)
_ = uint64(0)
_ = uint64(maxUint64)
_ = uint64(maxUint64 /* ERROR "cannot convert" */ + 1)
_ = uint64(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ uint = 0 /* ERROR "overflows" */ - 1
_ uint = 0
_ uint = maxUint
_ uint = maxUint /* ERROR "overflows" */ + 1
_ uint = smallestFloat64 /* ERROR "truncated" */
_ = uint(0 /* ERROR "cannot convert" */ - 1)
_ = uint(0)
_ = uint(maxUint)
_ = uint(maxUint /* ERROR "cannot convert" */ + 1)
_ = uint(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ uintptr = 0 /* ERROR "overflows" */ - 1
_ uintptr = 0
_ uintptr = maxUintptr
_ uintptr = maxUintptr /* ERROR "overflows" */ + 1
_ uintptr = smallestFloat64 /* ERROR "truncated" */
_ = uintptr(0 /* ERROR "cannot convert" */ - 1)
_ = uintptr(0)
_ = uintptr(maxUintptr)
_ = uintptr(maxUintptr /* ERROR "cannot convert" */ + 1)
_ = uintptr(smallestFloat64 /* ERROR "cannot convert" */)
)
const (
_ float32 = minInt64
_ float64 = minInt64
_ complex64 = minInt64
_ complex128 = minInt64
_ = float32(minInt64)
_ = float64(minInt64)
_ = complex64(minInt64)
_ = complex128(minInt64)
)
const (
_ float32 = maxUint64
_ float64 = maxUint64
_ complex64 = maxUint64
_ complex128 = maxUint64
_ = float32(maxUint64)
_ = float64(maxUint64)
_ = complex64(maxUint64)
_ = complex128(maxUint64)
)
// TODO(gri) find smaller deltas below
const delta32 = maxFloat32/(1 << 23)
const (
_ float32 = - /* ERROR "overflow" */ (maxFloat32 + delta32)
_ float32 = -maxFloat32
_ float32 = maxFloat32
_ float32 = maxFloat32 /* ERROR "overflow" */ + delta32
_ = float32(- /* ERROR "cannot convert" */ (maxFloat32 + delta32))
_ = float32(-maxFloat32)
_ = float32(maxFloat32)
_ = float32(maxFloat32 /* ERROR "cannot convert" */ + delta32)
_ = assert(float32(smallestFloat32) == smallestFloat32)
_ = assert(float32(smallestFloat32/2) == 0)
_ = assert(float32(smallestFloat64) == 0)
_ = assert(float32(smallestFloat64/2) == 0)
)
const delta64 = maxFloat64/(1 << 52)
const (
_ float64 = - /* ERROR "overflow" */ (maxFloat64 + delta64)
_ float64 = -maxFloat64
_ float64 = maxFloat64
_ float64 = maxFloat64 /* ERROR "overflow" */ + delta64
_ = float64(- /* ERROR "cannot convert" */ (maxFloat64 + delta64))
_ = float64(-maxFloat64)
_ = float64(maxFloat64)
_ = float64(maxFloat64 /* ERROR "cannot convert" */ + delta64)
_ = assert(float64(smallestFloat32) == smallestFloat32)
_ = assert(float64(smallestFloat32/2) == smallestFloat32/2)
_ = assert(float64(smallestFloat64) == smallestFloat64)
_ = assert(float64(smallestFloat64/2) == 0)
)
const (
_ complex64 = - /* ERROR "overflow" */ (maxFloat32 + delta32)
_ complex64 = -maxFloat32
_ complex64 = maxFloat32
_ complex64 = maxFloat32 /* ERROR "overflow" */ + delta32
_ = complex64(- /* ERROR "cannot convert" */ (maxFloat32 + delta32))
_ = complex64(-maxFloat32)
_ = complex64(maxFloat32)
_ = complex64(maxFloat32 /* ERROR "cannot convert" */ + delta32)
)
const (
_ complex128 = - /* ERROR "overflow" */ (maxFloat64 + delta64)
_ complex128 = -maxFloat64
_ complex128 = maxFloat64
_ complex128 = maxFloat64 /* ERROR "overflow" */ + delta64
_ = complex128(- /* ERROR "cannot convert" */ (maxFloat64 + delta64))
_ = complex128(-maxFloat64)
_ = complex128(maxFloat64)
_ = complex128(maxFloat64 /* ERROR "cannot convert" */ + delta64)
)
// Initialization of typed constant and conversion are the same:
const (
f32 = 1 + smallestFloat32
x32 float32 = f32
y32 = float32(f32)
_ = assert(x32 - y32 == 0)
)
const (
f64 = 1 + smallestFloat64
x64 float64 = f64
y64 = float64(f64)
_ = assert(x64 - y64 == 0)
)
const (
_ = int8(-1) << 7
_ = int8 /* ERROR "overflows" */ (-1) << 8
_ = uint32(1) << 31
_ = uint32 /* ERROR "overflows" */ (1) << 32
)
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package constdecl
import "math"
var v int
// Const decls must be initialized by constants.
const _ = v /* ERROR "not constant" */
const _ = math /* ERROR "not constant" */ .Sin(0)
const _ = int /* ERROR "not an expression" */
func _() {
const _ = v /* ERROR "not constant" */
const _ = math /* ERROR "not constant" */ .Sin(0)
const _ = int /* ERROR "not an expression" */
}
// Identifier and expression arity must match.
// The first error message is produced by the parser.
// In a real-world scenario, the type-checker would not be run
// in this case and the 2nd error message would not appear.
const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
const _ = 1, 2 /* ERROR "extra init expr 2" */
const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
const _ int = 1, 2 /* ERROR "extra init expr 2" */
const (
_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
_ = 1, 2 /* ERROR "extra init expr 2" */
_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
_ int = 1, 2 /* ERROR "extra init expr 2" */
)
const (
_ = 1
_
_, _ /* ERROR "missing init expr for _" */
_
)
const (
_, _ = 1, 2
_, _
_ /* ERROR "extra init expr at" */
_, _
_, _, _ /* ERROR "missing init expr for _" */
_, _
)
func _() {
const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
const _ = 1, 2 /* ERROR "extra init expr 2" */
const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
const _ int = 1, 2 /* ERROR "extra init expr 2" */
const (
_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
_ = 1, 2 /* ERROR "extra init expr 2" */
_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
_ int = 1, 2 /* ERROR "extra init expr 2" */
)
const (
_ = 1
_
_, _ /* ERROR "missing init expr for _" */
_
)
const (
_, _ = 1, 2
_, _
_ /* ERROR "extra init expr at" */
_, _
_, _, _ /* ERROR "missing init expr for _" */
_, _
)
}
// Test case for constant with invalid initialization.
// Caused panic because the constant value was not set up (gri - 7/8/2014).
func _() {
const (
x string = missing /* ERROR "undeclared name" */
y = x + ""
)
}
// TODO(gri) move extra tests from testdata/const0.src into here
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// conversions
package conversions
import "unsafe"
// argument count
var (
_ = int() /* ERROR "missing argument" */
_ = int(1, 2 /* ERROR "too many arguments" */ )
)
// numeric constant conversions are in const1.src.
func string_conversions() {
const A = string(65)
assert(A == "A")
const E = string(-1)
assert(E == "\uFFFD")
assert(E == string(1234567890))
type myint int
assert(A == string(myint(65)))
type mystring string
const _ mystring = mystring("foo")
const _ = string(true /* ERROR "cannot convert" */ )
const _ = string(1.2 /* ERROR "cannot convert" */ )
const _ = string(nil /* ERROR "cannot convert" */ )
// issues 11357, 11353: argument must be of integer type
_ = string(0.0 /* ERROR "cannot convert" */ )
_ = string(0i /* ERROR "cannot convert" */ )
_ = string(1 /* ERROR "cannot convert" */ + 2i)
}
func interface_conversions() {
type E interface{}
type I1 interface{
m1()
}
type I2 interface{
m1()
m2(x int)
}
type I3 interface{
m1()
m2() int
}
var e E
var i1 I1
var i2 I2
var i3 I3
_ = E(0)
_ = E(nil)
_ = E(e)
_ = E(i1)
_ = E(i2)
_ = I1(0 /* ERROR "cannot convert" */ )
_ = I1(nil)
_ = I1(i1)
_ = I1(e /* ERROR "cannot convert" */ )
_ = I1(i2)
_ = I2(nil)
_ = I2(i1 /* ERROR "cannot convert" */ )
_ = I2(i2)
_ = I2(i3 /* ERROR "cannot convert" */ )
_ = I3(nil)
_ = I3(i1 /* ERROR "cannot convert" */ )
_ = I3(i2 /* ERROR "cannot convert" */ )
_ = I3(i3)
// TODO(gri) add more tests, improve error message
}
func issue6326() {
type T unsafe.Pointer
var x T
_ = uintptr(x) // see issue 6326
}
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test various valid and invalid struct assignments and conversions.
// Does not compile.
package conversions2
type I interface {
m()
}
// conversions between structs
func _() {
type S struct{}
type T struct{}
var s S
var t T
var u struct{}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u
s = S(s)
s = S(t)
s = S(u)
t = u
t = T(u)
}
func _() {
type S struct{ x int }
type T struct {
x int "foo"
}
var s S
var t T
var u struct {
x int "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = S(s)
s = S(t)
s = S(u)
t = u // ERROR "cannot use .* in assignment"
t = T(u)
}
func _() {
type E struct{ x int }
type S struct{ x E }
type T struct {
x E "foo"
}
var s S
var t T
var u struct {
x E "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = S(s)
s = S(t)
s = S(u)
t = u // ERROR "cannot use .* in assignment"
t = T(u)
}
func _() {
type S struct {
x struct {
x int "foo"
}
}
type T struct {
x struct {
x int "bar"
} "foo"
}
var s S
var t T
var u struct {
x struct {
x int "bar"
} "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = S(s)
s = S(t)
s = S(u)
t = u // ERROR "cannot use .* in assignment"
t = T(u)
}
func _() {
type E1 struct {
x int "foo"
}
type E2 struct {
x int "bar"
}
type S struct{ x E1 }
type T struct {
x E2 "foo"
}
var s S
var t T
var u struct {
x E2 "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = S(s)
s = S(t /* ERROR "cannot convert" */ )
s = S(u /* ERROR "cannot convert" */ )
t = u // ERROR "cannot use .* in assignment"
t = T(u)
}
func _() {
type E struct{ x int }
type S struct {
f func(struct {
x int "foo"
})
}
type T struct {
f func(struct {
x int "bar"
})
}
var s S
var t T
var u struct{ f func(E) }
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = S(s)
s = S(t)
s = S(u /* ERROR "cannot convert" */ )
t = u // ERROR "cannot use .* in assignment"
t = T(u /* ERROR "cannot convert" */ )
}
// conversions between pointers to structs
func _() {
type S struct{}
type T struct{}
var s *S
var t *T
var u *struct{}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u)
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u)
}
func _() {
type S struct{ x int }
type T struct {
x int "foo"
}
var s *S
var t *T
var u *struct {
x int "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u)
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u)
}
func _() {
type E struct{ x int }
type S struct{ x E }
type T struct {
x E "foo"
}
var s *S
var t *T
var u *struct {
x E "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u)
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u)
}
func _() {
type S struct {
x struct {
x int "foo"
}
}
type T struct {
x struct {
x int "bar"
} "foo"
}
var s *S
var t *T
var u *struct {
x struct {
x int "bar"
} "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u)
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u)
}
func _() {
type E1 struct {
x int "foo"
}
type E2 struct {
x int "bar"
}
type S struct{ x E1 }
type T struct {
x E2 "foo"
}
var s *S
var t *T
var u *struct {
x E2 "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t /* ERROR "cannot convert" */ )
s = (*S)(u /* ERROR "cannot convert" */ )
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u)
}
func _() {
type E struct{ x int }
type S struct {
f func(struct {
x int "foo"
})
}
type T struct {
f func(struct {
x int "bar"
})
}
var s *S
var t *T
var u *struct{ f func(E) }
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u /* ERROR "cannot convert" */ )
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u /* ERROR "cannot convert" */ )
}
func _() {
type E struct{ x int }
type S struct {
f func(*struct {
x int "foo"
})
}
type T struct {
f func(*struct {
x int "bar"
})
}
var s *S
var t *T
var u *struct{ f func(E) }
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u /* ERROR "cannot convert" */ )
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u /* ERROR "cannot convert" */ )
}
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cycles
type (
T0 int
T1 /* ERROR cycle */ T1
T2 *T2
T3 /* ERROR cycle */ T4
T4 T5
T5 T3
T6 T7
T7 *T8
T8 T6
// arrays
A0 /* ERROR cycle */ [10]A0
A1 [10]*A1
A2 /* ERROR cycle */ [10]A3
A3 [10]A4
A4 A2
A5 [10]A6
A6 *A5
// slices
L0 []L0
// structs
S0 /* ERROR cycle */ struct{ _ S0 }
S1 /* ERROR cycle */ struct{ S1 }
S2 struct{ _ *S2 }
S3 struct{ *S3 }
S4 /* ERROR cycle */ struct{ S5 }
S5 struct{ S6 }
S6 S4
// pointers
P0 *P0
// functions
F0 func(F0)
F1 func() F1
F2 func(F2) F2
// interfaces
I0 /* ERROR cycle */ interface{ I0 }
I1 interface{ I2 }
I2 interface{ I3 }
I3 /* ERROR cycle */ interface{ I1 }
I4 interface{ f(I4) }
// testcase for issue 5090
I5 interface{ f(I6) }
I6 interface{ I5 }
// maps
M0 map[M0 /* ERROR invalid map key */ ]M0
// channels
C0 chan C0
)
func _() {
type (
t1 /* ERROR cycle */ t1
t2 *t2
t3 t4 /* ERROR undeclared */
t4 t5 /* ERROR undeclared */
t5 t3
// arrays
a0 /* ERROR cycle */ [10]a0
a1 [10]*a1
// slices
l0 []l0
// structs
s0 /* ERROR cycle */ struct{ _ s0 }
s1 /* ERROR cycle */ struct{ s1 }
s2 struct{ _ *s2 }
s3 struct{ *s3 }
// pointers
p0 *p0
// functions
f0 func(f0)
f1 func() f1
f2 func(f2) f2
// interfaces
i0 /* ERROR cycle */ interface{ i0 }
// maps
m0 map[m0 /* ERROR invalid map key */ ]m0
// channels
c0 chan c0
)
}
// test cases for issue 6667
type A [10]map[A /* ERROR invalid map key */ ]bool
type S struct {
m map[S /* ERROR invalid map key */ ]bool
}
// test cases for issue 7236
// (cycle detection must not be dependent on starting point of resolution)
type (
P1 *T9
T9 /* ERROR cycle */ T9
T10 /* ERROR cycle */ T10
P2 *T10
)
func (T11) m() {}
type T11 /* ERROR cycle */ struct{ T11 }
type T12 /* ERROR cycle */ struct{ T12 }
func (*T12) m() {}
type (
P3 *T13
T13 /* ERROR cycle */ T13
)
\ No newline at end of file
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
type (
A interface {
a() interface {
ABC1
}
}
B interface {
b() interface {
ABC2
}
}
C interface {
c() interface {
ABC3
}
}
AB interface {
A
B
}
BC interface {
B
C
}
ABC1 interface {
A
B
C
}
ABC2 interface {
AB
C
}
ABC3 interface {
A
BC
}
)
var (
x1 ABC1
x2 ABC2
x3 ABC3
)
func _() {
// all types have the same method set
x1 = x2
x2 = x1
x1 = x3
x3 = x1
x2 = x3
x3 = x2
// all methods return the same type again
x1 = x1.a()
x1 = x1.b()
x1 = x1.c()
x2 = x2.a()
x2 = x2.b()
x2 = x2.c()
x3 = x3.a()
x3 = x3.b()
x3 = x3.c()
}
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
import "unsafe"
// Test case for issue 5090
type t interface {
f(u)
}
type u interface {
t
}
func _() {
var t t
var u u
t.f(t)
t.f(u)
u.f(t)
u.f(u)
}
// Test case for issue 6589.
type A interface {
a() interface {
AB
}
}
type B interface {
a() interface {
AB
}
}
type AB interface {
a() interface {
A
B /* ERROR a redeclared */
}
b() interface {
A
B /* ERROR a redeclared */
}
}
var x AB
var y interface {
A
B /* ERROR a redeclared */
}
var _ = x /* ERROR cannot compare */ == y
// Test case for issue 6638.
type T interface {
m() [T /* ERROR no value */ (nil).m()[0]]int
}
// Variations of this test case.
type T1 interface {
m() [x1 /* ERROR no value */ .m()[0]]int
}
var x1 T1
type T2 interface {
m() [len(x2 /* ERROR no value */ .m())]int
}
var x2 T2
type T3 interface {
m() [unsafe.Sizeof(x3.m)]int
}
var x3 T3
// The test case below should also report an error for
// the cast inside the T4 interface (like it does for the
// variable initialization). The reason why it does not is
// that inside T4, the method x4.m depends on T4 which is not
// fully set up yet. The x4.m method happens to have an empty
// signature which is why the cast is permitted.
// TODO(gri) Consider marking methods as incomplete and provide
// a better error message in that case.
type T4 interface {
m() [unsafe.Sizeof(cast4(x4.m))]int
}
var x4 T4
var _ = cast4(x4 /* ERROR cannot convert */.m)
type cast4 func()
// This test is symmetric to the T4 case: Here the cast is
// "correct", but it doesn't work inside the T5 interface.
type T5 interface {
m() [unsafe.Sizeof(cast5(x5 /* ERROR cannot convert */ .m))]int
}
var x5 T5
var _ = cast5(x5.m)
type cast5 func() [0]int
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
import "unsafe"
var (
_ A = A(nil).a().b().c().d().e().f()
_ A = A(nil).b().c().d().e().f()
_ A = A(nil).c().d().e().f()
_ A = A(nil).d().e().f()
_ A = A(nil).e().f()
_ A = A(nil).f()
_ A = A(nil)
)
type (
A interface {
a() B
B
}
B interface {
b() C
C
}
C interface {
c() D
D
}
D interface {
d() E
E
}
E interface {
e() F
F
}
F interface {
f() A
}
)
type (
U interface {
V
}
V interface {
v() [unsafe.Sizeof(u)]int
}
)
var u U
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
// Check that all methods of T are collected before
// determining the result type of m (which embeds
// all methods of T).
type T interface {
m() interface {T}
E
}
var _ = T.m(nil).m().e()
type E interface {
e() int
}
// Check that unresolved forward chains are followed
// (see also comment in resolver.go, checker.typeDecl).
var _ = C.m(nil).m().e()
type A B
type B interface {
m() interface{C}
E
}
type C A
// Check that interface type comparison for identity
// does not recur endlessly.
type T1 interface {
m() interface{T1}
}
type T2 interface {
m() interface{T2}
}
func _(x T1, y T2) {
// Checking for assignability of interfaces must check
// if all methods of x are present in y, and that they
// have identical signatures. The signatures recur via
// the result type, which is an interface that embeds
// a single method m that refers to the very interface
// that contains it. This requires cycle detection in
// identity checks for interface types.
x = y
}
type T3 interface {
m() interface{T4}
}
type T4 interface {
m() interface{T3}
}
func _(x T1, y T3) {
x = y
}
// Check that interfaces are type-checked in order of
// (embedded interface) dependencies (was issue 7158).
var x1 T5 = T7(nil)
type T5 interface {
T6
}
type T6 interface {
m() T7
}
type T7 interface {
T5
}
// Actual test case from issue 7158.
func wrapNode() Node {
return wrapElement()
}
func wrapElement() Element {
return nil
}
type EventTarget interface {
AddEventListener(Event)
}
type Node interface {
EventTarget
}
type Element interface {
Node
}
type Event interface {
Target() Element
}
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// type declarations
package decls0
import "unsafe"
const pi = 3.1415
type (
N undeclared /* ERROR "undeclared" */
B bool
I int32
A [10]P
T struct {
x, y P
}
P *T
R (*R)
F func(A) I
Y interface {
f(A) I
}
S [](((P)))
M map[I]F
C chan<- I
// blank types must be typechecked
_ pi /* ERROR "not a type" */
_ struct{}
_ struct{ pi /* ERROR "not a type" */ }
)
// declarations of init
const _, init /* ERROR "cannot declare init" */ , _ = 0, 1, 2
type init /* ERROR "cannot declare init" */ struct{}
var _, init /* ERROR "cannot declare init" */ int
func init() {}
func init /* ERROR "missing function body" */ ()
func _() { const init = 0 }
func _() { type init int }
func _() { var init int; _ = init }
// invalid array types
type (
iA0 [... /* ERROR "invalid use of '...'" */ ]byte
// The error message below could be better. At the moment
// we believe an integer that is too large is not an integer.
// But at least we get an error.
iA1 [1 /* ERROR "must be integer" */ <<100]int
iA2 [- /* ERROR "invalid array length" */ 1]complex128
iA3 ["foo" /* ERROR "must be integer" */ ]string
iA4 [float64 /* ERROR "must be integer" */ (0)]int
)
type (
p1 pi /* ERROR "no field or method foo" */ .foo
p2 unsafe.Pointer
)
type (
Pi pi /* ERROR "not a type" */
a /* ERROR "illegal cycle" */ a
a /* ERROR "redeclared" */ int
// where the cycle error appears depends on the
// order in which declarations are processed
// (which depends on the order in which a map
// is iterated through)
b /* ERROR "illegal cycle" */ c
c d
d e
e b
t *t
U V
V *W
W U
P1 *S2
P2 P1
S0 struct {
}
S1 struct {
a, b, c int
u, v, a /* ERROR "redeclared" */ float32
}
S2 struct {
S0 // anonymous field
S0 /* ERROR "redeclared" */ int
}
S3 struct {
x S2
}
S4/* ERROR "illegal cycle" */ struct {
S4
}
S5 /* ERROR "illegal cycle" */ struct {
S6
}
S6 struct {
field S7
}
S7 struct {
S5
}
L1 []L1
L2 []int
A1 [10.0]int
A2 /* ERROR "illegal cycle" */ [10]A2
A3 /* ERROR "illegal cycle" */ [10]struct {
x A4
}
A4 [10]A3
F1 func()
F2 func(x, y, z float32)
F3 func(x, y, x /* ERROR "redeclared" */ float32)
F4 func() (x, y, x /* ERROR "redeclared" */ float32)
F5 func(x int) (x /* ERROR "redeclared" */ float32)
F6 func(x ...int)
I1 interface{}
I2 interface {
m1()
}
I3 interface {
m1()
m1 /* ERROR "redeclared" */ ()
}
I4 interface {
m1(x, y, x /* ERROR "redeclared" */ float32)
m2() (x, y, x /* ERROR "redeclared" */ float32)
m3(x int) (x /* ERROR "redeclared" */ float32)
}
I5 interface {
m1(I5)
}
I6 interface {
S0 /* ERROR "not an interface" */
}
I7 interface {
I1
I1
}
I8 /* ERROR "illegal cycle" */ interface {
I8
}
I9 interface {
I10
}
I10 interface {
I11
}
I11 /* ERROR "illegal cycle" */ interface {
I9
}
C1 chan int
C2 <-chan int
C3 chan<- C3
C4 chan C5
C5 chan C6
C6 chan C4
M1 map[Last]string
M2 map[string]M2
Last int
)
// cycles in function/method declarations
// (test cases for issue 5217 and variants)
func f1(x f1 /* ERROR "not a type" */ ) {}
func f2(x *f2 /* ERROR "not a type" */ ) {}
func f3() (x f3 /* ERROR "not a type" */ ) { return }
func f4() (x *f4 /* ERROR "not a type" */ ) { return }
func (S0) m1(x S0 /* ERROR "field or method" */ .m1) {}
func (S0) m2(x *S0 /* ERROR "field or method" */ .m2) {}
func (S0) m3() (x S0 /* ERROR "field or method" */ .m3) { return }
func (S0) m4() (x *S0 /* ERROR "field or method" */ .m4) { return }
// interfaces may not have any blank methods
type BlankI interface {
_ /* ERROR "invalid method name" */ ()
_ /* ERROR "invalid method name" */ (float32) int
m()
}
// non-interface types may have multiple blank methods
type BlankT struct{}
func (BlankT) _() {}
func (BlankT) _(int) {}
func (BlankT) _() int { return 0 }
func (BlankT) _(int) int { return 0}
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// variable declarations
package decls1
import (
"math"
)
// Global variables without initialization
var (
a, b bool
c byte
d uint8
r rune
i int
j, k, l int
x, y float32
xx, yy float64
u, v complex64
uu, vv complex128
s, t string
array []byte
iface interface{}
blank _ /* ERROR "cannot use _" */
)
// Global variables with initialization
var (
s1 = i + j
s2 = i /* ERROR "mismatched types" */ + x
s3 = c + d
s4 = s + t
s5 = s /* ERROR "invalid operation" */ / t
s6 = array[t1]
s7 = array[x /* ERROR "integer" */]
s8 = &a
s10 = &42 /* ERROR "cannot take address" */
s11 = &v
s12 = -(u + *t11) / *&v
s13 = a /* ERROR "shifted operand" */ << d
s14 = i << j /* ERROR "must be unsigned" */
s18 = math.Pi * 10.0
s19 = s1 /* ERROR "cannot call" */ ()
s20 = f0 /* ERROR "no value" */ ()
s21 = f6(1, s1, i)
s22 = f6(1, s1, uu /* ERROR "cannot use .* in argument" */ )
t1 int = i + j
t2 int = i /* ERROR "mismatched types" */ + x
t3 int = c /* ERROR "cannot use .* variable declaration" */ + d
t4 string = s + t
t5 string = s /* ERROR "invalid operation" */ / t
t6 byte = array[t1]
t7 byte = array[x /* ERROR "must be integer" */]
t8 *int = & /* ERROR "cannot use .* variable declaration" */ a
t10 *int = &42 /* ERROR "cannot take address" */
t11 *complex64 = &v
t12 complex64 = -(u + *t11) / *&v
t13 int = a /* ERROR "shifted operand" */ << d
t14 int = i << j /* ERROR "must be unsigned" */
t15 math /* ERROR "not in selector" */
t16 math /* ERROR "not declared" */ .xxx
t17 math /* ERROR "not a type" */ .Pi
t18 float64 = math.Pi * 10.0
t19 int = t1 /* ERROR "cannot call" */ ()
t20 int = f0 /* ERROR "no value" */ ()
t21 int = a /* ERROR "cannot use .* variable declaration" */
)
// Various more complex expressions
var (
u1 = x /* ERROR "not an interface" */ .(int)
u2 = iface.([]int)
u3 = iface.(a /* ERROR "not a type" */ )
u4, ok = iface.(int)
u5, ok2, ok3 = iface /* ERROR "assignment count mismatch" */ .(int)
)
// Constant expression initializations
var (
v1 = 1 /* ERROR "cannot convert" */ + "foo"
v2 = c + 255
v3 = c + 256 /* ERROR "overflows" */
v4 = r + 2147483647
v5 = r + 2147483648 /* ERROR "overflows" */
v6 = 42
v7 = v6 + 9223372036854775807
v8 = v6 + 9223372036854775808 /* ERROR "overflows" */
v9 = i + 1 << 10
v10 byte = 1024 /* ERROR "overflows" */
v11 = xx/yy*yy - xx
v12 = true && false
v13 = nil /* ERROR "use of untyped nil" */
)
// Multiple assignment expressions
var (
m1a, m1b = 1, 2
m2a, m2b, m2c /* ERROR "missing init expr for m2c" */ = 1, 2
m3a, m3b = 1, 2, 3 /* ERROR "extra init expr 3" */
)
func _() {
var (
m1a, m1b = 1, 2
m2a, m2b, m2c /* ERROR "missing init expr for m2c" */ = 1, 2
m3a, m3b = 1, 2, 3 /* ERROR "extra init expr 3" */
)
_, _ = m1a, m1b
_, _, _ = m2a, m2b, m2c
_, _ = m3a, m3b
}
// Declaration of parameters and results
func f0() {}
func f1(a /* ERROR "not a type" */) {}
func f2(a, b, c d /* ERROR "not a type" */) {}
func f3() int { return 0 }
func f4() a /* ERROR "not a type" */ { return 0 }
func f5() (a, b, c d /* ERROR "not a type" */) { return }
func f6(a, b, c int) complex128 { return 0 }
// Declaration of receivers
type T struct{}
func (T) m0() {}
func (*T) m1() {}
func (x T) m2() {}
func (x *T) m3() {}
// Initialization functions
func init() {}
func /* ERROR "no arguments and no return values" */ init(int) {}
func /* ERROR "no arguments and no return values" */ init() int { return 0 }
func /* ERROR "no arguments and no return values" */ init(int) int { return 0 }
func (T) init(int) int { return 0 }
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// method declarations
package decls2
import "time"
import "unsafe"
// T1 declared before its methods.
type T1 struct{
f int
}
func (T1) m() {}
func (T1) m /* ERROR "already declared" */ () {}
func (x *T1) f /* ERROR "field and method" */ () {}
// Conflict between embedded field and method name,
// with the embedded field being a basic type.
type T1b struct {
int
}
func (T1b) int /* ERROR "field and method" */ () {}
type T1c struct {
time.Time
}
func (T1c) Time /* ERROR "field and method" */ () int { return 0 }
// Disabled for now: LookupFieldOrMethod will find Pointer even though
// it's double-declared (it would cost extra in the common case to verify
// this). But the MethodSet computation will not find it due to the name
// collision caused by the double-declaration, leading to an internal
// inconsistency while we are verifying one computation against the other.
// var _ = T1c{}.Pointer
// T2's method declared before the type.
func (*T2) f /* ERROR "field and method" */ () {}
type T2 struct {
f int
}
// Methods declared without a declared type.
func (undeclared /* ERROR "undeclared" */) m() {}
func (x *undeclared /* ERROR "undeclared" */) m() {}
func (pi /* ERROR "not a type" */) m1() {}
func (x pi /* ERROR "not a type" */) m2() {}
func (x *pi /* ERROR "not a type" */ ) m3() {}
// Blank types.
type _ struct { m int }
type _ struct { m int }
func (_ /* ERROR "cannot use _" */) m() {}
func m(_ /* ERROR "cannot use _" */) {}
// Methods with receiver base type declared in another file.
func (T3) m1() {}
func (*T3) m2() {}
func (x T3) m3() {}
func (x *T3) f /* ERROR "field and method" */ () {}
// Methods of non-struct type.
type T4 func()
func (self T4) m() func() { return self }
// Methods associated with an interface.
type T5 interface {
m() int
}
func (T5 /* ERROR "invalid receiver" */ ) m1() {}
func (T5 /* ERROR "invalid receiver" */ ) m2() {}
// Methods associated with a named pointer type.
type ptr *int
func (ptr /* ERROR "invalid receiver" */ ) _() {}
func (* /* ERROR "invalid receiver" */ ptr) _() {}
// Methods with zero or multiple receivers.
func ( /* ERROR "missing receiver" */ ) _() {}
func (T3, * /* ERROR "exactly one receiver" */ T3) _() {}
func (T3, T3, T3 /* ERROR "exactly one receiver" */ ) _() {}
func (a, b /* ERROR "exactly one receiver" */ T3) _() {}
func (a, b, c /* ERROR "exactly one receiver" */ T3) _() {}
// Methods associated with non-local or unnamed types.
func (int /* ERROR "invalid receiver" */ ) m() {}
func ([ /* ERROR "invalid receiver" */ ]int) m() {}
func (time /* ERROR "invalid receiver" */ .Time) m() {}
func (* /* ERROR "invalid receiver" */ time.Time) m() {}
func (x /* ERROR "invalid receiver" */ interface{}) m() {}
// Unsafe.Pointer is treated like a pointer when used as receiver type.
type UP unsafe.Pointer
func (UP /* ERROR "invalid" */ ) m1() {}
func (* /* ERROR "invalid" */ UP) m2() {}
// Double declarations across package files
const c_double = 0
type t_double int
var v_double int
func f_double() {}
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// method declarations
package decls2
import "io"
const pi = 3.1415
func (T1) m /* ERROR "already declared" */ () {}
func (T2) m(io.Writer) {}
type T3 struct {
f *T3
}
type T6 struct {
x int
}
func (t *T6) m1() int {
return t.x
}
func f() {
var t *T6
t.m1()
}
// Double declarations across package files
const c_double /* ERROR "redeclared" */ = 0
type t_double /* ERROR "redeclared" */ int
var v_double /* ERROR "redeclared" */ int
func f_double /* ERROR "redeclared" */ () {}
// Blank methods need to be type-checked.
// Verify by checking that errors are reported.
func (T /* ERROR "undeclared" */ ) _() {}
func (T1) _(undeclared /* ERROR "undeclared" */ ) {}
func (T1) _() int { return "foo" /* ERROR "cannot convert" */ }
// Methods with undeclared receiver type can still be checked.
// Verify by checking that errors are reported.
func (Foo /* ERROR "undeclared" */ ) m() {}
func (Foo /* ERROR "undeclared" */ ) m(undeclared /* ERROR "undeclared" */ ) {}
func (Foo /* ERROR "undeclared" */ ) m() int { return "foo" /* ERROR "cannot convert" */ }
func (Foo /* ERROR "undeclared" */ ) _() {}
func (Foo /* ERROR "undeclared" */ ) _(undeclared /* ERROR "undeclared" */ ) {}
func (Foo /* ERROR "undeclared" */ ) _() int { return "foo" /* ERROR "cannot convert" */ }
// Receiver declarations are regular parameter lists;
// receiver types may use parentheses, and the list
// may have a trailing comma.
type T7 struct {}
func (T7) m1() {}
func ((T7)) m2() {}
func ((*T7)) m3() {}
func (x *(T7),) m4() {}
func (x (*(T7)),) m5() {}
func (x ((*((T7)))),) m6() {}
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// embedded types
package decls3
import "unsafe"
import "fmt"
// fields with the same name at the same level cancel each other out
func _() {
type (
T1 struct { X int }
T2 struct { X int }
T3 struct { T1; T2 } // X is embedded twice at the same level via T1->X, T2->X
)
var t T3
_ = t /* ERROR "ambiguous selector" */ .X
}
func _() {
type (
T1 struct { X int }
T2 struct { T1 }
T3 struct { T1 }
T4 struct { T2; T3 } // X is embedded twice at the same level via T2->T1->X, T3->T1->X
)
var t T4
_ = t /* ERROR "ambiguous selector" */ .X
}
func issue4355() {
type (
T1 struct {X int}
T2 struct {T1}
T3 struct {T2}
T4 struct {T2}
T5 struct {T3; T4} // X is embedded twice at the same level via T3->T2->T1->X, T4->T2->T1->X
)
var t T5
_ = t /* ERROR "ambiguous selector" */ .X
}
func _() {
type State int
type A struct{ State }
type B struct{ fmt.State }
type T struct{ A; B }
var t T
_ = t /* ERROR "ambiguous selector" */ .State
}
// Embedded fields can be predeclared types.
func _() {
type T0 struct{
int
float32
f int
}
var x T0
_ = x.int
_ = x.float32
_ = x.f
type T1 struct{
T0
}
var y T1
_ = y.int
_ = y.float32
_ = y.f
}
// Restrictions on embedded field types.
func _() {
type I1 interface{}
type I2 interface{}
type P1 *int
type P2 *int
type UP unsafe.Pointer
type T1 struct {
I1
* /* ERROR "cannot be a pointer to an interface" */ I2
* /* ERROR "cannot be a pointer to an interface" */ error
P1 /* ERROR "cannot be a pointer" */
* /* ERROR "cannot be a pointer" */ P2
}
// unsafe.Pointers are treated like regular pointers when embedded
type T2 struct {
unsafe /* ERROR "cannot be unsafe.Pointer" */ .Pointer
*/* ERROR "cannot be unsafe.Pointer" */ unsafe.Pointer
UP /* ERROR "cannot be unsafe.Pointer" */
* /* ERROR "cannot be unsafe.Pointer" */ UP
}
}
// Named types that are pointers.
type S struct{ x int }
func (*S) m() {}
type P *S
func _() {
var s *S
_ = s.x
_ = s.m
var p P
_ = p.x
_ = p /* ERROR "no field or method" */ .m
_ = P /* ERROR "no field or method" */ .m
}
// Borrowed from the FieldByName test cases in reflect/all_test.go.
type D1 struct {
d int
}
type D2 struct {
d int
}
type S0 struct {
A, B, C int
D1
D2
}
type S1 struct {
B int
S0
}
type S2 struct {
A int
*S1
}
type S1x struct {
S1
}
type S1y struct {
S1
}
type S3 struct {
S1x
S2
D, E int
*S1y
}
type S4 struct {
*S4
A int
}
// The X in S6 and S7 annihilate, but they also block the X in S8.S9.
type S5 struct {
S6
S7
S8
}
type S6 struct {
X int
}
type S7 S6
type S8 struct {
S9
}
type S9 struct {
X int
Y int
}
// The X in S11.S6 and S12.S6 annihilate, but they also block the X in S13.S8.S9.
type S10 struct {
S11
S12
S13
}
type S11 struct {
S6
}
type S12 struct {
S6
}
type S13 struct {
S8
}
func _() {
_ = struct /* ERROR "no field or method" */ {}{}.Foo
_ = S0{}.A
_ = S0 /* ERROR "no field or method" */ {}.D
_ = S1{}.A
_ = S1{}.B
_ = S1{}.S0
_ = S1{}.C
_ = S2{}.A
_ = S2{}.S1
_ = S2{}.B
_ = S2{}.C
_ = S2 /* ERROR "no field or method" */ {}.D
_ = S3 /* ERROR "ambiguous selector" */ {}.S1
_ = S3{}.A
_ = S3 /* ERROR "ambiguous selector" */ {}.B
_ = S3{}.D
_ = S3{}.E
_ = S4{}.A
_ = S4 /* ERROR "no field or method" */ {}.B
_ = S5 /* ERROR "ambiguous selector" */ {}.X
_ = S5{}.Y
_ = S10 /* ERROR "ambiguous selector" */ {}.X
_ = S10{}.Y
}
// Borrowed from the FieldByName benchmark in reflect/all_test.go.
type R0 struct {
*R1
*R2
*R3
*R4
}
type R1 struct {
*R5
*R6
*R7
*R8
}
type R2 R1
type R3 R1
type R4 R1
type R5 struct {
*R9
*R10
*R11
*R12
}
type R6 R5
type R7 R5
type R8 R5
type R9 struct {
*R13
*R14
*R15
*R16
}
type R10 R9
type R11 R9
type R12 R9
type R13 struct {
*R17
*R18
*R19
*R20
}
type R14 R13
type R15 R13
type R16 R13
type R17 struct {
*R21
*R22
*R23
*R24
}
type R18 R17
type R19 R17
type R20 R17
type R21 struct {
X int
}
type R22 R21
type R23 R21
type R24 R21
var _ = R0 /* ERROR "ambiguous selector" */ {}.X
\ No newline at end of file
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package errors
// Testing precise operand formatting in error messages
// (matching messages are regular expressions, hence the \'s).
func f(x int, m map[string]int) {
// no values
_ = f /* ERROR "f\(0, m\) \(no value\) used as value" */ (0, m)
// built-ins
_ = println /* ERROR "println \(built-in\) must be called" */
// types
_ = complex128 /* ERROR "complex128 \(type\) is not an expression" */
// constants
const c1 = 991
const c2 float32 = 0.5
0 /* ERROR "0 \(untyped int constant\) is not used" */
c1 /* ERROR "c1 \(untyped int constant 991\) is not used" */
c2 /* ERROR "c2 \(constant 0.5 of type float32\) is not used" */
c1 /* ERROR "c1 \+ c2 \(constant 991.5 of type float32\) is not used" */ + c2
// variables
x /* ERROR "x \(variable of type int\) is not used" */
// values
x /* ERROR "x != x \(untyped bool value\) is not used" */ != x
x /* ERROR "x \+ x \(value of type int\) is not used" */ + x
// value, ok's
const s = "foo"
m /* ERROR "m\[s\] \(map index expression of type int\) is not used" */ [s]
}
// Valid ERROR comments can have a variety of forms.
func _() {
0 /* ERROR "0 .* is not used" */
0 /* ERROR 0 .* is not used */
0 // ERROR "0 .* is not used"
0 // ERROR 0 .* is not used
}
// Don't report spurious errors as a consequence of earlier errors.
// Add more tests as needed.
func _() {
if err := foo /* ERROR undeclared */ (); err != nil /* no error here */ {}
}
// Use unqualified names for package-local objects.
type T struct{}
var _ int = T /* ERROR value of type T */ {} // use T in error message rather then errors.T
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// unary expressions
package expr0
type mybool bool
var (
// bool
b0 = true
b1 bool = b0
b2 = !true
b3 = !b1
b4 bool = !true
b5 bool = !b4
b6 = +b0 /* ERROR "not defined" */
b7 = -b0 /* ERROR "not defined" */
b8 = ^b0 /* ERROR "not defined" */
b9 = *b0 /* ERROR "cannot indirect" */
b10 = &true /* ERROR "cannot take address" */
b11 = &b0
b12 = <-b0 /* ERROR "cannot receive" */
b13 = & & /* ERROR "cannot take address" */ b0
// byte
_ = byte(0)
_ = byte(- /* ERROR "cannot convert" */ 1)
_ = - /* ERROR "-byte\(1\) \(constant -1 of type byte\) overflows byte" */ byte(1) // test for issue 11367
_ = byte /* ERROR "overflows byte" */ (0) - byte(1)
// int
i0 = 1
i1 int = i0
i2 = +1
i3 = +i0
i4 int = +1
i5 int = +i4
i6 = -1
i7 = -i0
i8 int = -1
i9 int = -i4
i10 = !i0 /* ERROR "not defined" */
i11 = ^1
i12 = ^i0
i13 int = ^1
i14 int = ^i4
i15 = *i0 /* ERROR "cannot indirect" */
i16 = &i0
i17 = *i16
i18 = <-i16 /* ERROR "cannot receive" */
// uint
u0 = uint(1)
u1 uint = u0
u2 = +1
u3 = +u0
u4 uint = +1
u5 uint = +u4
u6 = -1
u7 = -u0
u8 uint = - /* ERROR "overflows" */ 1
u9 uint = -u4
u10 = !u0 /* ERROR "not defined" */
u11 = ^1
u12 = ^i0
u13 uint = ^ /* ERROR "overflows" */ 1
u14 uint = ^u4
u15 = *u0 /* ERROR "cannot indirect" */
u16 = &u0
u17 = *u16
u18 = <-u16 /* ERROR "cannot receive" */
u19 = ^uint(0)
// float64
f0 = float64(1)
f1 float64 = f0
f2 = +1
f3 = +f0
f4 float64 = +1
f5 float64 = +f4
f6 = -1
f7 = -f0
f8 float64 = -1
f9 float64 = -f4
f10 = !f0 /* ERROR "not defined" */
f11 = ^1
f12 = ^i0
f13 float64 = ^1
f14 float64 = ^f4 /* ERROR "not defined" */
f15 = *f0 /* ERROR "cannot indirect" */
f16 = &f0
f17 = *u16
f18 = <-u16 /* ERROR "cannot receive" */
// complex128
c0 = complex128(1)
c1 complex128 = c0
c2 = +1
c3 = +c0
c4 complex128 = +1
c5 complex128 = +c4
c6 = -1
c7 = -c0
c8 complex128 = -1
c9 complex128 = -c4
c10 = !c0 /* ERROR "not defined" */
c11 = ^1
c12 = ^i0
c13 complex128 = ^1
c14 complex128 = ^c4 /* ERROR "not defined" */
c15 = *c0 /* ERROR "cannot indirect" */
c16 = &c0
c17 = *u16
c18 = <-u16 /* ERROR "cannot receive" */
// string
s0 = "foo"
s1 = +"foo" /* ERROR "not defined" */
s2 = -s0 /* ERROR "not defined" */
s3 = !s0 /* ERROR "not defined" */
s4 = ^s0 /* ERROR "not defined" */
s5 = *s4
s6 = &s4
s7 = *s6
s8 = <-s7
// channel
ch chan int
rc <-chan float64
sc chan <- string
ch0 = +ch /* ERROR "not defined" */
ch1 = -ch /* ERROR "not defined" */
ch2 = !ch /* ERROR "not defined" */
ch3 = ^ch /* ERROR "not defined" */
ch4 = *ch /* ERROR "cannot indirect" */
ch5 = &ch
ch6 = *ch5
ch7 = <-ch
ch8 = <-rc
ch9 = <-sc /* ERROR "cannot receive" */
ch10, ok = <-ch
// ok is of type bool
ch11, myok = <-ch
_ mybool = myok /* ERROR "cannot use .* in variable declaration" */
)
// address of composite literals
type T struct{x, y int}
func f() T { return T{} }
var (
_ = &T{1, 2}
_ = &[...]int{}
_ = &[]int{}
_ = &[]int{}
_ = &map[string]T{}
_ = &(T{1, 2})
_ = &((((T{1, 2}))))
_ = &f /* ERROR "cannot take address" */ ()
)
// recursive pointer types
type P *P
var (
p1 P = new(P)
p2 P = *p1
p3 P = &p2
)
func g() (a, b int) { return }
func _() {
_ = -g /* ERROR 2-valued g */ ()
_ = <-g /* ERROR 2-valued g */ ()
}
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// binary expressions
package expr1
type mybool bool
func _(x, y bool, z mybool) {
x = x || y
x = x || true
x = x || false
x = x && y
x = x && true
x = x && false
z = z /* ERROR mismatched types */ || y
z = z || true
z = z || false
z = z /* ERROR mismatched types */ && y
z = z && true
z = z && false
}
type myint int
func _(x, y int, z myint) {
x = x + 1
x = x + 1.0
x = x + 1.1 // ERROR truncated to int
x = x + y
x = x - y
x = x * y
x = x / y
x = x % y
x = x << y // ERROR must be unsigned integer
x = x >> y // ERROR must be unsigned integer
z = z + 1
z = z + 1.0
z = z + 1.1 // ERROR truncated to int
z = z /* ERROR mismatched types */ + y
z = z /* ERROR mismatched types */ - y
z = z /* ERROR mismatched types */ * y
z = z /* ERROR mismatched types */ / y
z = z /* ERROR mismatched types */ % y
z = z << y // ERROR must be unsigned integer
z = z >> y // ERROR must be unsigned integer
}
type myuint uint
func _(x, y uint, z myuint) {
x = x + 1
x = x + - /* ERROR overflows uint */ 1
x = x + 1.0
x = x + 1.1 // ERROR truncated to uint
x = x + y
x = x - y
x = x * y
x = x / y
x = x % y
x = x << y
x = x >> y
z = z + 1
z = x + - /* ERROR overflows uint */ 1
z = z + 1.0
z = z + 1.1 // ERROR truncated to uint
z = z /* ERROR mismatched types */ + y
z = z /* ERROR mismatched types */ - y
z = z /* ERROR mismatched types */ * y
z = z /* ERROR mismatched types */ / y
z = z /* ERROR mismatched types */ % y
z = z << y
z = z >> y
}
type myfloat64 float64
func _(x, y float64, z myfloat64) {
x = x + 1
x = x + -1
x = x + 1.0
x = x + 1.1
x = x + y
x = x - y
x = x * y
x = x / y
x = x /* ERROR not defined */ % y
x = x /* ERROR operand x .* must be integer */ << y
x = x /* ERROR operand x .* must be integer */ >> y
z = z + 1
z = z + -1
z = z + 1.0
z = z + 1.1
z = z /* ERROR mismatched types */ + y
z = z /* ERROR mismatched types */ - y
z = z /* ERROR mismatched types */ * y
z = z /* ERROR mismatched types */ / y
z = z /* ERROR mismatched types */ % y
z = z /* ERROR operand z .* must be integer */ << y
z = z /* ERROR operand z .* must be integer */ >> y
}
type mystring string
func _(x, y string, z mystring) {
x = x + "foo"
x = x /* ERROR not defined */ - "foo"
x = x + 1 // ERROR cannot convert
x = x + y
x = x /* ERROR not defined */ - y
x = x * 10 // ERROR cannot convert
}
func f() (a, b int) { return }
func _(x int) {
_ = f /* ERROR 2-valued f */ () + 1
_ = x + f /* ERROR 2-valued f */ ()
_ = f /* ERROR 2-valued f */ () + f
_ = f /* ERROR 2-valued f */ () + f /* ERROR 2-valued f */ ()
}
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// comparisons
package expr2
func _bool() {
const t = true == true
const f = true == false
_ = t /* ERROR "cannot compare" */ < f
_ = 0 /* ERROR "cannot convert" */ == t
var b bool
var x, y float32
b = x < y
_ = b
_ = struct{b bool}{x < y}
}
// corner cases
var (
v0 = nil /* ERROR "cannot compare" */ == nil
)
func arrays() {
// basics
var a, b [10]int
_ = a == b
_ = a != b
_ = a /* ERROR < not defined */ < b
_ = a == nil /* ERROR cannot convert */
type C [10]int
var c C
_ = a == c
type D [10]int
var d D
_ = c /* ERROR mismatched types */ == d
var e [10]func() int
_ = e /* ERROR == not defined */ == e
}
func structs() {
// basics
var s, t struct {
x int
a [10]float32
_ bool
}
_ = s == t
_ = s != t
_ = s /* ERROR < not defined */ < t
_ = s == nil /* ERROR cannot convert */
type S struct {
x int
a [10]float32
_ bool
}
type T struct {
x int
a [10]float32
_ bool
}
var ss S
var tt T
_ = s == ss
_ = ss /* ERROR mismatched types */ == tt
var u struct {
x int
a [10]map[string]int
}
_ = u /* ERROR cannot compare */ == u
}
func pointers() {
// nil
_ = nil /* ERROR == not defined */ == nil
_ = nil /* ERROR != not defined */ != nil
_ = nil /* ERROR < not defined */ < nil
_ = nil /* ERROR <= not defined */ <= nil
_ = nil /* ERROR > not defined */ > nil
_ = nil /* ERROR >= not defined */ >= nil
// basics
var p, q *int
_ = p == q
_ = p != q
_ = p == nil
_ = p != nil
_ = nil == q
_ = nil != q
_ = p /* ERROR < not defined */ < q
_ = p /* ERROR <= not defined */ <= q
_ = p /* ERROR > not defined */ > q
_ = p /* ERROR >= not defined */ >= q
// various element types
type (
S1 struct{}
S2 struct{}
P1 *S1
P2 *S2
)
var (
ps1 *S1
ps2 *S2
p1 P1
p2 P2
)
_ = ps1 == ps1
_ = ps1 /* ERROR mismatched types */ == ps2
_ = ps2 /* ERROR mismatched types */ == ps1
_ = p1 == p1
_ = p1 /* ERROR mismatched types */ == p2
_ = p1 == ps1
}
func channels() {
// basics
var c, d chan int
_ = c == d
_ = c != d
_ = c == nil
_ = c /* ERROR < not defined */ < d
// various element types (named types)
type (
C1 chan int
C1r <-chan int
C1s chan<- int
C2 chan float32
)
var (
c1 C1
c1r C1r
c1s C1s
c1a chan int
c2 C2
)
_ = c1 == c1
_ = c1 /* ERROR mismatched types */ == c1r
_ = c1 /* ERROR mismatched types */ == c1s
_ = c1r /* ERROR mismatched types */ == c1s
_ = c1 == c1a
_ = c1a == c1
_ = c1 /* ERROR mismatched types */ == c2
_ = c1a /* ERROR mismatched types */ == c2
// various element types (unnamed types)
var (
d1 chan int
d1r <-chan int
d1s chan<- int
d1a chan<- int
d2 chan float32
)
_ = d1 == d1
_ = d1 == d1r
_ = d1 == d1s
_ = d1r /* ERROR mismatched types */ == d1s
_ = d1 == d1a
_ = d1a == d1
_ = d1 /* ERROR mismatched types */ == d2
_ = d1a /* ERROR mismatched types */ == d2
}
// for interfaces test
type S1 struct{}
type S11 struct{}
type S2 struct{}
func (*S1) m() int
func (*S11) m() int
func (*S11) n()
func (*S2) m() float32
func interfaces() {
// basics
var i, j interface{ m() int }
_ = i == j
_ = i != j
_ = i == nil
_ = i /* ERROR < not defined */ < j
// various interfaces
var ii interface { m() int; n() }
var k interface { m() float32 }
_ = i == ii
_ = i /* ERROR mismatched types */ == k
// interfaces vs values
var s1 S1
var s11 S11
var s2 S2
_ = i == 0 /* ERROR cannot convert */
_ = i /* ERROR mismatched types */ == s1
_ = i == &s1
_ = i == &s11
_ = i /* ERROR mismatched types */ == s2
_ = i /* ERROR mismatched types */ == &s2
}
func slices() {
// basics
var s []int
_ = s == nil
_ = s != nil
_ = s /* ERROR < not defined */ < nil
// slices are not otherwise comparable
_ = s /* ERROR == not defined */ == s
_ = s /* ERROR < not defined */ < s
}
func maps() {
// basics
var m map[string]int
_ = m == nil
_ = m != nil
_ = m /* ERROR < not defined */ < nil
// maps are not otherwise comparable
_ = m /* ERROR == not defined */ == m
_ = m /* ERROR < not defined */ < m
}
func funcs() {
// basics
var f func(int) float32
_ = f == nil
_ = f != nil
_ = f /* ERROR < not defined */ < nil
// funcs are not otherwise comparable
_ = f /* ERROR == not defined */ == f
_ = f /* ERROR < not defined */ < f
}
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file is a modified copy of $GOROOT/test/goto.go.
package gotos
var (
i, n int
x []int
c chan int
m map[int]int
s string
)
// goto after declaration okay
func _() {
x := 1
goto L
L:
_ = x
}
// goto before declaration okay
func _() {
goto L
L:
x := 1
_ = x
}
// goto across declaration not okay
func _() {
goto L /* ERROR "goto L jumps over variable declaration at line 36" */
x := 1
_ = x
L:
}
// goto across declaration in inner scope okay
func _() {
goto L
{
x := 1
_ = x
}
L:
}
// goto across declaration after inner scope not okay
func _() {
goto L /* ERROR "goto L jumps over variable declaration at line 58" */
{
x := 1
_ = x
}
x := 1
_ = x
L:
}
// goto across declaration in reverse okay
func _() {
L:
x := 1
_ = x
goto L
}
func _() {
L: L1:
x := 1
_ = x
goto L
goto L1
}
// error shows first offending variable
func _() {
goto L /* ERROR "goto L jumps over variable declaration at line 84" */
x := 1
_ = x
y := 1
_ = y
L:
}
// goto not okay even if code path is dead
func _() {
goto L /* ERROR "goto L jumps over variable declaration" */
x := 1
_ = x
y := 1
_ = y
return
L:
}
// goto into outer block okay
func _() {
{
goto L
}
L:
}
func _() {
{
goto L
goto L1
}
L: L1:
}
// goto backward into outer block okay
func _() {
L:
{
goto L
}
}
func _() {
L: L1:
{
goto L
goto L1
}
}
// goto into inner block not okay
func _() {
goto L /* ERROR "goto L jumps into block" */
{
L:
}
}
func _() {
goto L /* ERROR "goto L jumps into block" */
goto L1 /* ERROR "goto L1 jumps into block" */
{
L: L1:
}
}
// goto backward into inner block still not okay
func _() {
{
L:
}
goto L /* ERROR "goto L jumps into block" */
}
func _() {
{
L: L1:
}
goto L /* ERROR "goto L jumps into block" */
goto L1 /* ERROR "goto L1 jumps into block" */
}
// error shows first (outermost) offending block
func _() {
goto L /* ERROR "goto L jumps into block" */
{
{
{
L:
}
}
}
}
// error prefers block diagnostic over declaration diagnostic
func _() {
goto L /* ERROR "goto L jumps into block" */
x := 1
_ = x
{
L:
}
}
// many kinds of blocks, all invalid to jump into or among,
// but valid to jump out of
// if
func _() {
L:
if true {
goto L
}
}
func _() {
L:
if true {
goto L
} else {
}
}
func _() {
L:
if false {
} else {
goto L
}
}
func _() {
goto L /* ERROR "goto L jumps into block" */
if true {
L:
}
}
func _() {
goto L /* ERROR "goto L jumps into block" */
if true {
L:
} else {
}
}
func _() {
goto L /* ERROR "goto L jumps into block" */
if true {
} else {
L:
}
}
func _() {
if false {
L:
} else {
goto L /* ERROR "goto L jumps into block" */
}
}
func _() {
if true {
goto L /* ERROR "goto L jumps into block" */
} else {
L:
}
}
func _() {
if true {
goto L /* ERROR "goto L jumps into block" */
} else if false {
L:
}
}
func _() {
if true {
goto L /* ERROR "goto L jumps into block" */
} else if false {
L:
} else {
}
}
func _() {
if true {
goto L /* ERROR "goto L jumps into block" */
} else if false {
} else {
L:
}
}
func _() {
if true {
goto L /* ERROR "goto L jumps into block" */
} else {
L:
}
}
func _() {
if true {
L:
} else {
goto L /* ERROR "goto L jumps into block" */
}
}
// for
func _() {
for {
goto L
}
L:
}
func _() {
for {
goto L
L:
}
}
func _() {
for {
L:
}
goto L /* ERROR "goto L jumps into block" */
}
func _() {
for {
goto L
L1:
}
L:
goto L1 /* ERROR "goto L1 jumps into block" */
}
func _() {
for i < n {
L:
}
goto L /* ERROR "goto L jumps into block" */
}
func _() {
for i = 0; i < n; i++ {
L:
}
goto L /* ERROR "goto L jumps into block" */
}
func _() {
for i = range x {
L:
}
goto L /* ERROR "goto L jumps into block" */
}
func _() {
for i = range c {
L:
}
goto L /* ERROR "goto L jumps into block" */
}
func _() {
for i = range m {
L:
}
goto L /* ERROR "goto L jumps into block" */
}
func _() {
for i = range s {
L:
}
goto L /* ERROR "goto L jumps into block" */
}
// switch
func _() {
L:
switch i {
case 0:
goto L
}
}
func _() {
L:
switch i {
case 0:
default:
goto L
}
}
func _() {
switch i {
case 0:
default:
L:
goto L
}
}
func _() {
switch i {
case 0:
default:
goto L
L:
}
}
func _() {
switch i {
case 0:
goto L
L:
;
default:
}
}
func _() {
goto L /* ERROR "goto L jumps into block" */
switch i {
case 0:
L:
}
}
func _() {
goto L /* ERROR "goto L jumps into block" */
switch i {
case 0:
L:
;
default:
}
}
func _() {
goto L /* ERROR "goto L jumps into block" */
switch i {
case 0:
default:
L:
}
}
func _() {
switch i {
default:
goto L /* ERROR "goto L jumps into block" */
case 0:
L:
}
}
func _() {
switch i {
case 0:
L:
;
default:
goto L /* ERROR "goto L jumps into block" */
}
}
// select
// different from switch. the statement has no implicit block around it.
func _() {
L:
select {
case <-c:
goto L
}
}
func _() {
L:
select {
case c <- 1:
default:
goto L
}
}
func _() {
select {
case <-c:
default:
L:
goto L
}
}
func _() {
select {
case c <- 1:
default:
goto L
L:
}
}
func _() {
select {
case <-c:
goto L
L:
;
default:
}
}
func _() {
goto L /* ERROR "goto L jumps into block" */
select {
case c <- 1:
L:
}
}
func _() {
goto L /* ERROR "goto L jumps into block" */
select {
case c <- 1:
L:
;
default:
}
}
func _() {
goto L /* ERROR "goto L jumps into block" */
select {
case <-c:
default:
L:
}
}
func _() {
select {
default:
goto L /* ERROR "goto L jumps into block" */
case <-c:
L:
}
}
func _() {
select {
case <-c:
L:
;
default:
goto L /* ERROR "goto L jumps into block" */
}
}
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package importC
import "C"
import _ /* ERROR cannot rename import "C" */ "C"
import foo /* ERROR cannot rename import "C" */ "C"
import . /* ERROR cannot rename import "C" */ "C"
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package importdecl0
import ()
import (
// we can have multiple blank imports (was bug)
_ "math"
_ "net/rpc"
init /* ERROR "cannot declare init" */ "fmt"
// reflect defines a type "flag" which shows up in the gc export data
"reflect"
. /* ERROR "imported but not used" */ "reflect"
)
import "math" /* ERROR "imported but not used" */
import m /* ERROR "imported but not used as m" */ "math"
import _ "math"
import (
"math/big" /* ERROR "imported but not used" */
b /* ERROR "imported but not used" */ "math/big"
_ "math/big"
)
import "fmt"
import f1 "fmt"
import f2 "fmt"
// reflect.flag must not be visible in this package
type flag int
type _ reflect /* ERROR "not exported" */ .flag
// imported package name may conflict with local objects
type reflect /* ERROR "reflect already declared" */ int
// dot-imported exported objects may conflict with local objects
type Value /* ERROR "Value already declared through dot-import of package reflect" */ struct{}
var _ = fmt.Println // use "fmt"
func _() {
f1.Println() // use "fmt"
}
func _() {
_ = func() {
f2.Println() // use "fmt"
}
}
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package importdecl0
import "math"
import m "math"
import . "testing" // declares T in file scope
import . /* ERROR "imported but not used" */ "unsafe"
import . "fmt" // declares Println in file scope
import (
// TODO(gri) At the moment, 2 errors are reported because both go/parser
// and the type checker report it. Eventually, this test should not be
// done by the parser anymore.
"" /* ERROR invalid import path */ /* ERROR invalid import path */
"a!b" /* ERROR invalid import path */ /* ERROR invalid import path */
"abc\xffdef" /* ERROR invalid import path */ /* ERROR invalid import path */
)
// using "math" in this file doesn't affect its use in other files
const Pi0 = math.Pi
const Pi1 = m.Pi
type _ T // use "testing"
func _() func() interface{} {
return func() interface{} {
return Println // use "fmt"
}
}
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test case for issue 8969.
package importdecl1
import . "unsafe"
var _ Pointer // use dot-imported package unsafe
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package importdecl1
import . /* ERROR "imported but not used" */ "unsafe"
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// initialization cycles
package init0
// initialization cycles (we don't know the types)
const (
s0 /* ERROR initialization cycle */ = s0
x0 /* ERROR initialization cycle */ = y0
y0 = x0
a0 = b0
b0 /* ERROR initialization cycle */ = c0
c0 = d0
d0 = b0
)
var (
s1 /* ERROR initialization cycle */ = s1
x1 /* ERROR initialization cycle */ = y1
y1 = x1
a1 = b1
b1 /* ERROR initialization cycle */ = c1
c1 = d1
d1 = b1
)
// initialization cycles (we know the types)
const (
s2 /* ERROR initialization cycle */ int = s2
x2 /* ERROR initialization cycle */ int = y2
y2 = x2
a2 = b2
b2 /* ERROR initialization cycle */ int = c2
c2 = d2
d2 = b2
)
var (
s3 /* ERROR initialization cycle */ int = s3
x3 /* ERROR initialization cycle */ int = y3
y3 = x3
a3 = b3
b3 /* ERROR initialization cycle */ int = c3
c3 = d3
d3 = b3
)
// cycles via struct fields
type S1 struct {
f int
}
const cx3 S1 /* ERROR invalid constant type */ = S1{cx3.f}
var vx3 /* ERROR initialization cycle */ S1 = S1{vx3.f}
// cycles via functions
var x4 = x5
var x5 /* ERROR initialization cycle */ = f1()
func f1() int { return x5*10 }
var x6, x7 /* ERROR initialization cycle */ = f2()
var x8 = x7
func f2() (int, int) { return f3() + f3(), 0 }
func f3() int { return x8 }
// cycles via closures
var x9 /* ERROR initialization cycle */ = func() int { return x9 }()
var x10 /* ERROR initialization cycle */ = f4()
func f4() int {
_ = func() {
_ = x10
}
return 0
}
// cycles via method expressions
type T1 struct{}
func (T1) m() bool { _ = x11; return false }
var x11 /* ERROR initialization cycle */ = T1.m(T1{})
// cycles via method values
type T2 struct{}
func (T2) m() bool { _ = x12; return false }
var t1 T2
var x12 /* ERROR initialization cycle */ = t1.m
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// initialization cycles
package init1
// issue 6683 (marked as WorkingAsIntended)
type T0 struct{}
func (T0) m() int { return y0 }
var x0 = T0{}
var y0 /* ERROR initialization cycle */ = x0.m()
type T1 struct{}
func (T1) m() int { return y1 }
var x1 interface {
m() int
} = T1{}
var y1 = x1.m() // no cycle reported, x1 is of interface type
// issue 6703 (modified)
var x2 /* ERROR initialization cycle */ = T2.m
var y2 = x2
type T2 struct{}
func (T2) m() int {
_ = y2
return 0
}
var x3 /* ERROR initialization cycle */ = T3.m(T3{}) // <<<< added (T3{})
var y3 = x3
type T3 struct{}
func (T3) m() int {
_ = y3
return 0
}
var x4 /* ERROR initialization cycle */ = T4{}.m // <<<< added {}
var y4 = x4
type T4 struct{}
func (T4) m() int {
_ = y4
return 0
}
var x5 /* ERROR initialization cycle */ = T5{}.m() // <<<< added ()
var y5 = x5
type T5 struct{}
func (T5) m() int {
_ = y5
return 0
}
// issue 4847
// simplified test case
var x6 = f6
var y6 /* ERROR initialization cycle */ = f6
func f6() { _ = y6 }
// full test case
type (
E int
S int
)
type matcher func(s *S) E
func matchList(s *S) E { return matcher(matchAnyFn)(s) }
var foo = matcher(matchList)
var matchAny /* ERROR initialization cycle */ = matcher(matchList)
func matchAnyFn(s *S) (err E) { return matchAny(s) }
\ No newline at end of file
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// initialization cycles
package init2
// cycles through functions
func f1() int { _ = x1; return 0 }
var x1 /* ERROR initialization cycle */ = f1
func f2() int { _ = x2; return 0 }
var x2 /* ERROR initialization cycle */ = f2()
// cycles through method expressions
type T3 int
func (T3) m() int { _ = x3; return 0 }
var x3 /* ERROR initialization cycle */ = T3.m
type T4 int
func (T4) m() int { _ = x4; return 0 }
var x4 /* ERROR initialization cycle */ = T4.m(0)
type T3p int
func (*T3p) m() int { _ = x3p; return 0 }
var x3p /* ERROR initialization cycle */ = (*T3p).m
type T4p int
func (*T4p) m() int { _ = x4p; return 0 }
var x4p /* ERROR initialization cycle */ = (*T4p).m(nil)
// cycles through method expressions of embedded methods
type T5 struct { E5 }
type E5 int
func (E5) m() int { _ = x5; return 0 }
var x5 /* ERROR initialization cycle */ = T5.m
type T6 struct { E6 }
type E6 int
func (E6) m() int { _ = x6; return 0 }
var x6 /* ERROR initialization cycle */ = T6.m(T6{0})
type T5p struct { E5p }
type E5p int
func (*E5p) m() int { _ = x5p; return 0 }
var x5p /* ERROR initialization cycle */ = (*T5p).m
type T6p struct { E6p }
type E6p int
func (*E6p) m() int { _ = x6p; return 0 }
var x6p /* ERROR initialization cycle */ = (*T6p).m(nil)
// cycles through method values
type T7 int
func (T7) m() int { _ = x7; return 0 }
var x7 /* ERROR initialization cycle */ = T7(0).m
type T8 int
func (T8) m() int { _ = x8; return 0 }
var x8 /* ERROR initialization cycle */ = T8(0).m()
type T7p int
func (*T7p) m() int { _ = x7p; return 0 }
var x7p /* ERROR initialization cycle */ = new(T7p).m
type T8p int
func (*T8p) m() int { _ = x8p; return 0 }
var x8p /* ERROR initialization cycle */ = new(T8p).m()
type T7v int
func (T7v) m() int { _ = x7v; return 0 }
var x7var T7v
var x7v /* ERROR initialization cycle */ = x7var.m
type T8v int
func (T8v) m() int { _ = x8v; return 0 }
var x8var T8v
var x8v /* ERROR initialization cycle */ = x8var.m()
type T7pv int
func (*T7pv) m() int { _ = x7pv; return 0 }
var x7pvar *T7pv
var x7pv /* ERROR initialization cycle */ = x7pvar.m
type T8pv int
func (*T8pv) m() int { _ = x8pv; return 0 }
var x8pvar *T8pv
var x8pv /* ERROR initialization cycle */ = x8pvar.m()
// cycles through method values of embedded methods
type T9 struct { E9 }
type E9 int
func (E9) m() int { _ = x9; return 0 }
var x9 /* ERROR initialization cycle */ = T9{0}.m
type T10 struct { E10 }
type E10 int
func (E10) m() int { _ = x10; return 0 }
var x10 /* ERROR initialization cycle */ = T10{0}.m()
type T9p struct { E9p }
type E9p int
func (*E9p) m() int { _ = x9p; return 0 }
var x9p /* ERROR initialization cycle */ = new(T9p).m
type T10p struct { E10p }
type E10p int
func (*E10p) m() int { _ = x10p; return 0 }
var x10p /* ERROR initialization cycle */ = new(T10p).m()
type T9v struct { E9v }
type E9v int
func (E9v) m() int { _ = x9v; return 0 }
var x9var T9v
var x9v /* ERROR initialization cycle */ = x9var.m
type T10v struct { E10v }
type E10v int
func (E10v) m() int { _ = x10v; return 0 }
var x10var T10v
var x10v /* ERROR initialization cycle */ = x10var.m()
type T9pv struct { E9pv }
type E9pv int
func (*E9pv) m() int { _ = x9pv; return 0 }
var x9pvar *T9pv
var x9pv /* ERROR initialization cycle */ = x9pvar.m
type T10pv struct { E10pv }
type E10pv int
func (*E10pv) m() int { _ = x10pv; return 0 }
var x10pvar *T10pv
var x10pv /* ERROR initialization cycle */ = x10pvar.m()
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package issues
import "fmt"
func issue7035() {
type T struct{ X int }
_ = func() {
fmt.Println() // must refer to imported fmt rather than the fmt below
}
fmt := new(T)
_ = fmt.X
}
func issue8066() {
const (
_ = float32(340282356779733661637539395458142568447)
_ = float32(340282356779733661637539395458142568448 /* ERROR cannot convert */ )
)
}
// Check that a missing identifier doesn't lead to a spurious error cascade.
func issue8799a() {
x, ok := missing /* ERROR undeclared */ ()
_ = !ok
_ = x
}
func issue8799b(x int, ok bool) {
x, ok = missing /* ERROR undeclared */ ()
_ = !ok
_ = x
}
func issue9182() {
type Point C /* ERROR undeclared */ .Point
// no error for composite literal based on unknown type
_ = Point{x: 1, y: 2}
}
func f0() (a []int) { return }
func f1() (a []int, b int) { return }
func f2() (a, b []int) { return }
func append_([]int, ...int) {}
func issue9473(a []int, b ...int) {
// variadic builtin function
_ = append(f0())
_ = append(f0(), f0()...)
_ = append(f1())
_ = append(f2 /* ERROR cannot use .* in argument */ ())
_ = append(f2()... /* ERROR cannot use ... */ )
_ = append(f0(), f1 /* ERROR 2-valued f1 */ ())
_ = append(f0(), f2 /* ERROR 2-valued f2 */ ())
_ = append(f0(), f1 /* ERROR 2-valued f1 */ ()...)
_ = append(f0(), f2 /* ERROR 2-valued f2 */ ()...)
// variadic user-defined function
append_(f0())
append_(f0(), f0()...)
append_(f1())
append_(f2 /* ERROR cannot use .* in argument */ ())
append_(f2()... /* ERROR cannot use ... */ )
append_(f0(), f1 /* ERROR 2-valued f1 */ ())
append_(f0(), f2 /* ERROR 2-valued f2 */ ())
append_(f0(), f1 /* ERROR 2-valued f1 */ ()...)
append_(f0(), f2 /* ERROR 2-valued f2 */ ()...)
}
// Check that embedding a non-interface type in an interface results in a good error message.
func issue10979() {
type _ interface {
int /* ERROR int is not an interface */
}
type T struct{}
type _ interface {
T /* ERROR T is not an interface */
}
type _ interface {
nosuchtype /* ERROR undeclared name: nosuchtype */
}
type _ interface {
fmt /* ERROR Nosuchtype not declared by package fmt */ .Nosuchtype
}
type _ interface {
nosuchpkg /* ERROR undeclared name: nosuchpkg */ .Nosuchtype
}
type I interface {
I /* ERROR I\.m \(value of type func\(I\)\) is not a type */ .m
m()
}
}
// issue11347
// These should not crash.
var a1, b1 /* ERROR cycle */ , c1 /* ERROR cycle */ b1 = 0 > 0<<""[""[c1]]>c1
var a2, b2 /* ERROR cycle */ = 0 /* ERROR mismatch */ /* ERROR mismatch */ > 0<<""[b2]
var a3, b3 /* ERROR cycle */ = int /* ERROR mismatch */ /* ERROR mismatch */ (1<<""[b3])
// issue10260
// Check that error messages explain reason for interface assignment failures.
type (
I0 interface{}
I1 interface{ foo() }
I2 interface{ foo(x int) }
T0 struct{}
T1 struct{}
T2 struct{}
)
func (*T1) foo() {}
func (*T2) foo(x int) {}
func issue10260() {
var (
i0 I0
i1 I1
i2 I2
t0 *T0
t1 *T1
t2 *T2
)
i1 = i0 /* ERROR cannot use .* missing method foo */
i1 = t0 /* ERROR cannot use .* missing method foo */
i1 = i2 /* ERROR cannot use .* wrong type for method foo */
i1 = t2 /* ERROR cannot use .* wrong type for method foo */
i2 = i1 /* ERROR cannot use .* wrong type for method foo */
i2 = t1 /* ERROR cannot use .* wrong type for method foo */
_ = func() I1 { return i0 /* ERROR cannot use .* missing method foo */ }
_ = func() I1 { return t0 /* ERROR cannot use .* missing method foo */ }
_ = func() I1 { return i2 /* ERROR cannot use .* wrong type for method foo */ }
_ = func() I1 { return t2 /* ERROR cannot use .* wrong type for method foo */ }
_ = func() I2 { return i1 /* ERROR cannot use .* wrong type for method foo */ }
_ = func() I2 { return t1 /* ERROR cannot use .* wrong type for method foo */ }
// a few more - less exhaustive now
f := func(I1, I2){}
f(i0 /* ERROR cannot use .* missing method foo */ , i1 /* ERROR cannot use .* wrong type for method foo */)
_ = [...]I1{i0 /* ERROR cannot use .* missing method foo */ }
_ = [...]I1{i2 /* ERROR cannot use .* wrong type for method foo */ }
_ = []I1{i0 /* ERROR cannot use .* missing method foo */ }
_ = []I1{i2 /* ERROR cannot use .* wrong type for method foo */ }
_ = map[int]I1{0: i0 /* ERROR cannot use .* missing method foo */ }
_ = map[int]I1{0: i2 /* ERROR cannot use .* wrong type for method foo */ }
make(chan I1) <- i0 /* ERROR cannot use .* in send: missing method foo */
make(chan I1) <- i2 /* ERROR cannot use .* in send: wrong type for method foo */
}
// Check that constants representable as integers are in integer form
// before being used in operations that are only defined on integers.
func issue14229() {
// from the issue
const _ = int64(-1<<63) % 1e6
// related
const (
a int = 3
b = 4.0
_ = a / b
_ = a % b
_ = b / a
_ = b % a
)
}
// Check that in a n:1 variable declaration with type and initialization
// expression the type is distributed to all variables of the lhs before
// the initialization expression assignment is checked.
func issue15755() {
// from issue
var i interface{}
type b bool
var x, y b = i.(b)
_ = x == y
// related: we should see an error since the result of f1 is ([]int, int)
var u, v []int = f1 /* ERROR cannot use f1 */ ()
_ = u
_ = v
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment