Source file src/internal/types/testdata/fixedbugs/issue78884.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package p
     6  
     7  // multiple assignments
     8  func f0()                         { return }
     9  func f1() (_ int)                 { return }
    10  func f2() (_ string, _ int)       { return }
    11  func f3() (_, _ string, _ int)    { return }
    12  func f4() (_, _, _ string, _ int) { return }
    13  
    14  func _() {
    15  	var _ string = f0 /* ERROR "f0() (no value) used as value" */ ()
    16  	var _ string = f1 /* ERROR "cannot use f1() (value of type int) as string value in variable declaration" */ ()
    17  	var _, _ string = f2 /* ERROR "cannot use 2nd function result (value of type int) as string value in multiple assignment" */ ()
    18  	var _, _, _ string = f3 /* ERROR "cannot use 3rd function result (value of type int) as string value in multiple assignment" */ ()
    19  	var _, _, _, _ string = f4 /* ERROR "cannot use 4th function result (value of type int) as string value in multiple assignment" */ ()
    20  }
    21  
    22  // comma, ok values
    23  func _() {
    24  	var (
    25  		m map[string]int
    26  		s string
    27  		i int
    28  		b bool
    29  	)
    30  	_, _, _, _ = m, s, i, b
    31  	s = m /* ERROR "cannot use m[s] (map index expression of type int) as string value in assignment" */ [s]
    32  	s, b = m /* ERROR "cannot use m[s] (map index expression of type int) as string value in multiple assignment" */ [s]
    33  	i, s = m /* ERROR "cannot use ok value of (comma, ok) expression (untyped bool value) as string value in multiple assignment" */ [s]
    34  }
    35  
    36  // test case from issue
    37  
    38  func f() (int, bool) { return 1, false }
    39  
    40  func g() {
    41  	var s string
    42  	s, b := f /* ERROR "cannot use 1st function result (value of type int) as string value in multiple assignment" */ ()
    43  	_, _ = s, b
    44  }
    45  

View as plain text