Source file
src/go/types/call.go
1
2
3
4
5
6
7 package types
8
9 import (
10 "go/ast"
11 "go/token"
12 . "internal/types/errors"
13 "strings"
14 )
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 func (check *Checker) funcInst(T *target, pos token.Pos, x *operand, ix *indexedExpr, infer bool) ([]Type, []ast.Expr) {
36 assert(T != nil || ix != nil)
37
38 var instErrPos positioner
39 if ix != nil {
40 instErrPos = inNode(ix.orig, ix.lbrack)
41 x.expr = ix.orig
42 } else {
43 instErrPos = atPos(pos)
44 }
45 versionErr := !check.verifyVersionf(instErrPos, go1_18, "function instantiation")
46
47
48 var targs []Type
49 var xlist []ast.Expr
50 if ix != nil {
51 xlist = ix.indices
52 targs = check.typeList(xlist)
53 if targs == nil {
54 x.mode = invalid
55 return nil, nil
56 }
57 assert(len(targs) == len(xlist))
58 }
59
60
61
62
63 sig := x.typ.(*Signature)
64 got, want := len(targs), sig.TypeParams().Len()
65 if got > want {
66
67 check.errorf(ix.indices[got-1], WrongTypeArgCount, "got %d type arguments but want %d", got, want)
68 x.mode = invalid
69 return nil, nil
70 }
71
72 if got < want {
73 if !infer {
74 return targs, xlist
75 }
76
77
78
79
80
81
82
83
84
85
86
87 var args []*operand
88 var params []*Var
89 var reverse bool
90 if T != nil && sig.tparams != nil {
91 if !versionErr && !check.allowVersion(go1_21) {
92 if ix != nil {
93 check.versionErrorf(instErrPos, go1_21, "partially instantiated function in assignment")
94 } else {
95 check.versionErrorf(instErrPos, go1_21, "implicitly instantiated function in assignment")
96 }
97 }
98 gsig := NewSignatureType(nil, nil, nil, sig.params, sig.results, sig.variadic)
99 params = []*Var{NewVar(x.Pos(), check.pkg, "", gsig)}
100
101
102
103 expr := ast.NewIdent(T.desc)
104 expr.NamePos = x.Pos()
105 args = []*operand{{mode: value, expr: expr, typ: T.sig}}
106 reverse = true
107 }
108
109
110
111 tparams, params2 := check.renameTParams(pos, sig.TypeParams().list(), NewTuple(params...))
112
113 err := check.newError(CannotInferTypeArgs)
114 targs = check.infer(atPos(pos), tparams, targs, params2.(*Tuple), args, reverse, err)
115 if targs == nil {
116 if !err.empty() {
117 err.report()
118 }
119 x.mode = invalid
120 return nil, nil
121 }
122 got = len(targs)
123 }
124 assert(got == want)
125
126
127 sig = check.instantiateSignature(x.Pos(), x.expr, sig, targs, xlist)
128 x.typ = sig
129 x.mode = value
130 return nil, nil
131 }
132
133 func (check *Checker) instantiateSignature(pos token.Pos, expr ast.Expr, typ *Signature, targs []Type, xlist []ast.Expr) (res *Signature) {
134 assert(check != nil)
135 assert(len(targs) == typ.TypeParams().Len())
136
137 if check.conf._Trace {
138 check.trace(pos, "-- instantiating signature %s with %s", typ, targs)
139 check.indent++
140 defer func() {
141 check.indent--
142 check.trace(pos, "=> %s (under = %s)", res, res.Underlying())
143 }()
144 }
145
146
147
148
149 inst := check.instance(pos, typ, targs, nil, check.context()).(*Signature)
150 assert(inst.TypeParams().Len() == 0)
151 check.recordInstance(expr, targs, inst)
152 assert(len(xlist) <= len(targs))
153
154
155 check.later(func() {
156 tparams := typ.TypeParams().list()
157
158 if i, err := check.verify(pos, tparams, targs, check.context()); err != nil {
159
160 pos := pos
161 if i < len(xlist) {
162 pos = xlist[i].Pos()
163 }
164 check.softErrorf(atPos(pos), InvalidTypeArg, "%s", err)
165 } else {
166 check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist)
167 }
168 }).describef(atPos(pos), "verify instantiation")
169
170 return inst
171 }
172
173 func (check *Checker) callExpr(x *operand, call *ast.CallExpr) exprKind {
174 ix := unpackIndexedExpr(call.Fun)
175 if ix != nil {
176 if check.indexExpr(x, ix) {
177
178
179
180 assert(x.mode == value)
181 } else {
182 ix = nil
183 }
184 x.expr = call.Fun
185 check.record(x)
186 } else {
187 check.exprOrType(x, call.Fun, true)
188 }
189
190
191 switch x.mode {
192 case invalid:
193 check.use(call.Args...)
194 x.expr = call
195 return statement
196
197 case typexpr:
198
199 check.nonGeneric(nil, x)
200 if x.mode == invalid {
201 return conversion
202 }
203 T := x.typ
204 x.mode = invalid
205 switch n := len(call.Args); n {
206 case 0:
207 check.errorf(inNode(call, call.Rparen), WrongArgCount, "missing argument in conversion to %s", T)
208 case 1:
209 check.expr(nil, x, call.Args[0])
210 if x.mode != invalid {
211 if hasDots(call) {
212 check.errorf(call.Args[0], BadDotDotDotSyntax, "invalid use of ... in conversion to %s", T)
213 break
214 }
215 if t, _ := under(T).(*Interface); t != nil && !isTypeParam(T) {
216 if !t.IsMethodSet() {
217 check.errorf(call, MisplacedConstraintIface, "cannot use interface %s in conversion (contains specific type constraints or is comparable)", T)
218 break
219 }
220 }
221 check.conversion(x, T)
222 }
223 default:
224 check.use(call.Args...)
225 check.errorf(call.Args[n-1], WrongArgCount, "too many arguments in conversion to %s", T)
226 }
227 x.expr = call
228 return conversion
229
230 case builtin:
231
232 id := x.id
233 if !check.builtin(x, call, id) {
234 x.mode = invalid
235 }
236 x.expr = call
237
238 if x.mode != invalid && x.mode != constant_ {
239 check.hasCallOrRecv = true
240 }
241 return predeclaredFuncs[id].kind
242 }
243
244
245
246 cgocall := x.mode == cgofunc
247
248
249 sig, _ := coreType(x.typ).(*Signature)
250 if sig == nil {
251 check.errorf(x, InvalidCall, invalidOp+"cannot call non-function %s", x)
252 x.mode = invalid
253 x.expr = call
254 return statement
255 }
256
257
258 wasGeneric := sig.TypeParams().Len() > 0
259
260
261 var xlist []ast.Expr
262 var targs []Type
263 if ix != nil {
264 xlist = ix.indices
265 targs = check.typeList(xlist)
266 if targs == nil {
267 check.use(call.Args...)
268 x.mode = invalid
269 x.expr = call
270 return statement
271 }
272 assert(len(targs) == len(xlist))
273
274
275 got, want := len(targs), sig.TypeParams().Len()
276 if got > want {
277 check.errorf(xlist[want], WrongTypeArgCount, "got %d type arguments but want %d", got, want)
278 check.use(call.Args...)
279 x.mode = invalid
280 x.expr = call
281 return statement
282 }
283
284
285
286
287
288
289 if got == want && want > 0 {
290 check.verifyVersionf(atPos(ix.lbrack), go1_18, "function instantiation")
291 sig = check.instantiateSignature(ix.Pos(), ix.orig, sig, targs, xlist)
292
293
294 targs = nil
295 xlist = nil
296 }
297 }
298
299
300 args, atargs, atxlist := check.genericExprList(call.Args)
301 sig = check.arguments(call, sig, targs, xlist, args, atargs, atxlist)
302
303 if wasGeneric && sig.TypeParams().Len() == 0 {
304
305 check.recordTypeAndValue(call.Fun, value, sig, nil)
306 }
307
308
309 switch sig.results.Len() {
310 case 0:
311 x.mode = novalue
312 case 1:
313 if cgocall {
314 x.mode = commaerr
315 } else {
316 x.mode = value
317 }
318 x.typ = sig.results.vars[0].typ
319 default:
320 x.mode = value
321 x.typ = sig.results
322 }
323 x.expr = call
324 check.hasCallOrRecv = true
325
326
327
328 if x.mode == value && sig.TypeParams().Len() > 0 && isParameterized(sig.TypeParams().list(), x.typ) {
329 x.mode = invalid
330 }
331
332 return statement
333 }
334
335
336
337 func (check *Checker) exprList(elist []ast.Expr) (xlist []*operand) {
338 if n := len(elist); n == 1 {
339 xlist, _ = check.multiExpr(elist[0], false)
340 } else if n > 1 {
341
342 xlist = make([]*operand, n)
343 for i, e := range elist {
344 var x operand
345 check.expr(nil, &x, e)
346 xlist[i] = &x
347 }
348 }
349 return
350 }
351
352
353
354
355
356
357
358
359 func (check *Checker) genericExprList(elist []ast.Expr) (resList []*operand, targsList [][]Type, xlistList [][]ast.Expr) {
360 if debug {
361 defer func() {
362
363 assert(len(targsList) == len(xlistList))
364
365 for i, x := range resList {
366 if i < len(targsList) {
367 if n := len(targsList[i]); n > 0 {
368
369 assert(n < x.typ.(*Signature).TypeParams().Len())
370 }
371 }
372 }
373 }()
374 }
375
376
377
378 infer := true
379 n := len(elist)
380 if n > 0 && check.allowVersion(go1_21) {
381 infer = false
382 }
383
384 if n == 1 {
385
386 e := elist[0]
387 var x operand
388 if ix := unpackIndexedExpr(e); ix != nil && check.indexExpr(&x, ix) {
389
390 targs, xlist := check.funcInst(nil, x.Pos(), &x, ix, infer)
391 if targs != nil {
392
393 targsList = [][]Type{targs}
394 xlistList = [][]ast.Expr{xlist}
395
396 x.expr = ix.orig
397 } else {
398
399
400 check.record(&x)
401 }
402 resList = []*operand{&x}
403 } else {
404
405 check.rawExpr(nil, &x, e, nil, true)
406 check.exclude(&x, 1<<novalue|1<<builtin|1<<typexpr)
407 if t, ok := x.typ.(*Tuple); ok && x.mode != invalid {
408
409 resList = make([]*operand, t.Len())
410 for i, v := range t.vars {
411 resList[i] = &operand{mode: value, expr: e, typ: v.typ}
412 }
413 } else {
414
415 resList = []*operand{&x}
416 }
417 }
418 } else if n > 1 {
419
420 resList = make([]*operand, n)
421 targsList = make([][]Type, n)
422 xlistList = make([][]ast.Expr, n)
423 for i, e := range elist {
424 var x operand
425 if ix := unpackIndexedExpr(e); ix != nil && check.indexExpr(&x, ix) {
426
427 targs, xlist := check.funcInst(nil, x.Pos(), &x, ix, infer)
428 if targs != nil {
429
430 targsList[i] = targs
431 xlistList[i] = xlist
432
433 x.expr = ix.orig
434 } else {
435
436
437 check.record(&x)
438 }
439 } else {
440
441 check.genericExpr(&x, e)
442 }
443 resList[i] = &x
444 }
445 }
446
447 return
448 }
449
450
451
452
453
454
455
456
457
458
459
460
461
462 func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type, xlist []ast.Expr, args []*operand, atargs [][]Type, atxlist [][]ast.Expr) (rsig *Signature) {
463 rsig = sig
464
465
466
467
468
469
470
471
472
473
474 nargs := len(args)
475 npars := sig.params.Len()
476 ddd := hasDots(call)
477
478
479 sigParams := sig.params
480 adjusted := false
481 if sig.variadic {
482 if ddd {
483
484 if len(call.Args) == 1 && nargs > 1 {
485
486 check.errorf(inNode(call, call.Ellipsis), InvalidDotDotDot, "cannot use ... with %d-valued %s", nargs, call.Args[0])
487 return
488 }
489 } else {
490
491 if nargs >= npars-1 {
492
493
494
495 vars := make([]*Var, npars-1)
496 copy(vars, sig.params.vars)
497 last := sig.params.vars[npars-1]
498 typ := last.typ.(*Slice).elem
499 for len(vars) < nargs {
500 vars = append(vars, NewParam(last.pos, last.pkg, last.name, typ))
501 }
502 sigParams = NewTuple(vars...)
503 adjusted = true
504 npars = nargs
505 } else {
506
507 npars--
508 }
509 }
510 } else {
511 if ddd {
512
513 check.errorf(inNode(call, call.Ellipsis), NonVariadicDotDotDot, "cannot use ... in call to non-variadic %s", call.Fun)
514 return
515 }
516
517 }
518
519
520 if nargs != npars {
521 var at positioner = call
522 qualifier := "not enough"
523 if nargs > npars {
524 at = args[npars].expr
525 qualifier = "too many"
526 } else {
527 at = atPos(call.Rparen)
528 }
529
530 var params []*Var
531 if sig.params != nil {
532 params = sig.params.vars
533 }
534 err := check.newError(WrongArgCount)
535 err.addf(at, "%s arguments in call to %s", qualifier, call.Fun)
536 err.addf(noposn, "have %s", check.typesSummary(operandTypes(args), false, ddd))
537 err.addf(noposn, "want %s", check.typesSummary(varTypes(params), sig.variadic, false))
538 err.report()
539 return
540 }
541
542
543 var tparams []*TypeParam
544
545
546 n := sig.TypeParams().Len()
547 if n > 0 {
548 if !check.allowVersion(go1_18) {
549 switch call.Fun.(type) {
550 case *ast.IndexExpr, *ast.IndexListExpr:
551 ix := unpackIndexedExpr(call.Fun)
552 check.versionErrorf(inNode(call.Fun, ix.lbrack), go1_18, "function instantiation")
553 default:
554 check.versionErrorf(inNode(call, call.Lparen), go1_18, "implicit function instantiation")
555 }
556 }
557
558 var tmp Type
559 tparams, tmp = check.renameTParams(call.Pos(), sig.TypeParams().list(), sigParams)
560 sigParams = tmp.(*Tuple)
561
562 for len(targs) < len(tparams) {
563 targs = append(targs, nil)
564 }
565 }
566 assert(len(tparams) == len(targs))
567
568
569 var genericArgs []int
570 if enableReverseTypeInference {
571 for i, arg := range args {
572
573 if asig, _ := arg.typ.(*Signature); asig != nil && asig.TypeParams().Len() > 0 {
574
575
576
577
578
579
580 asig = clone(asig)
581
582
583
584
585 atparams, tmp := check.renameTParams(call.Pos(), asig.TypeParams().list(), asig)
586 asig = tmp.(*Signature)
587 asig.tparams = &TypeParamList{atparams}
588 arg.typ = asig
589 tparams = append(tparams, atparams...)
590
591 if i < len(atargs) {
592 targs = append(targs, atargs[i]...)
593 }
594
595 for len(targs) < len(tparams) {
596 targs = append(targs, nil)
597 }
598 genericArgs = append(genericArgs, i)
599 }
600 }
601 }
602 assert(len(tparams) == len(targs))
603
604
605 _ = len(genericArgs) > 0 && check.verifyVersionf(args[genericArgs[0]], go1_21, "implicitly instantiated function as argument")
606
607
608
609
610
611
612 if len(tparams) > 0 {
613 err := check.newError(CannotInferTypeArgs)
614 targs = check.infer(call, tparams, targs, sigParams, args, false, err)
615 if targs == nil {
616
617
618
619
620 if !err.empty() {
621 check.errorf(err.posn(), CannotInferTypeArgs, "in call to %s, %s", call.Fun, err.msg())
622 }
623 return
624 }
625
626
627 if n > 0 {
628 rsig = check.instantiateSignature(call.Pos(), call.Fun, sig, targs[:n], xlist)
629
630
631
632 if adjusted {
633 sigParams = check.subst(call.Pos(), sigParams, makeSubstMap(tparams[:n], targs[:n]), nil, check.context()).(*Tuple)
634 } else {
635 sigParams = rsig.params
636 }
637 }
638
639
640 j := n
641 for _, i := range genericArgs {
642 arg := args[i]
643 asig := arg.typ.(*Signature)
644 k := j + asig.TypeParams().Len()
645
646 arg.typ = check.instantiateSignature(call.Pos(), arg.expr, asig, targs[j:k], nil)
647 check.record(arg)
648 j = k
649 }
650 }
651
652
653 if len(args) > 0 {
654 context := check.sprintf("argument to %s", call.Fun)
655 for i, a := range args {
656 check.assignment(a, sigParams.vars[i].typ, context)
657 }
658 }
659
660 return
661 }
662
663 var cgoPrefixes = [...]string{
664 "_Ciconst_",
665 "_Cfconst_",
666 "_Csconst_",
667 "_Ctype_",
668 "_Cvar_",
669 "_Cfpvar_fp_",
670 "_Cfunc_",
671 "_Cmacro_",
672 }
673
674 func (check *Checker) selector(x *operand, e *ast.SelectorExpr, def *TypeName, wantType bool) {
675
676 var (
677 obj Object
678 index []int
679 indirect bool
680 )
681
682 sel := e.Sel.Name
683
684
685
686
687 if ident, ok := e.X.(*ast.Ident); ok {
688 obj := check.lookup(ident.Name)
689 if pname, _ := obj.(*PkgName); pname != nil {
690 assert(pname.pkg == check.pkg)
691 check.recordUse(ident, pname)
692 pname.used = true
693 pkg := pname.imported
694
695 var exp Object
696 funcMode := value
697 if pkg.cgo {
698
699
700
701 if sel == "malloc" {
702 sel = "_CMalloc"
703 } else {
704 funcMode = cgofunc
705 }
706 for _, prefix := range cgoPrefixes {
707
708
709 exp = check.lookup(prefix + sel)
710 if exp != nil {
711 break
712 }
713 }
714 if exp == nil {
715 if isValidName(sel) {
716 check.errorf(e.Sel, UndeclaredImportedName, "undefined: %s", ast.Expr(e))
717 }
718 goto Error
719 }
720 check.objDecl(exp, nil)
721 } else {
722 exp = pkg.scope.Lookup(sel)
723 if exp == nil {
724 if !pkg.fake && isValidName(sel) {
725 check.errorf(e.Sel, UndeclaredImportedName, "undefined: %s", ast.Expr(e))
726 }
727 goto Error
728 }
729 if !exp.Exported() {
730 check.errorf(e.Sel, UnexportedName, "name %s not exported by package %s", sel, pkg.name)
731
732 }
733 }
734 check.recordUse(e.Sel, exp)
735
736
737
738 switch exp := exp.(type) {
739 case *Const:
740 assert(exp.Val() != nil)
741 x.mode = constant_
742 x.typ = exp.typ
743 x.val = exp.val
744 case *TypeName:
745 x.mode = typexpr
746 x.typ = exp.typ
747 case *Var:
748 x.mode = variable
749 x.typ = exp.typ
750 if pkg.cgo && strings.HasPrefix(exp.name, "_Cvar_") {
751 x.typ = x.typ.(*Pointer).base
752 }
753 case *Func:
754 x.mode = funcMode
755 x.typ = exp.typ
756 if pkg.cgo && strings.HasPrefix(exp.name, "_Cmacro_") {
757 x.mode = value
758 x.typ = x.typ.(*Signature).results.vars[0].typ
759 }
760 case *Builtin:
761 x.mode = builtin
762 x.typ = exp.typ
763 x.id = exp.id
764 default:
765 check.dump("%v: unexpected object %v", e.Sel.Pos(), exp)
766 panic("unreachable")
767 }
768 x.expr = e
769 return
770 }
771 }
772
773 check.exprOrType(x, e.X, false)
774 switch x.mode {
775 case typexpr:
776
777 if def != nil && def.typ == x.typ {
778 check.cycleError([]Object{def}, 0)
779 goto Error
780 }
781 case builtin:
782
783 check.errorf(e.Sel, UncalledBuiltin, "invalid use of %s in selector expression", x)
784 goto Error
785 case invalid:
786 goto Error
787 }
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803 if wantType {
804 check.errorf(e.Sel, NotAType, "%s is not a type", ast.Expr(e))
805 goto Error
806 }
807
808 obj, index, indirect = lookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel, false)
809 if obj == nil {
810
811 if !isValid(under(x.typ)) {
812 goto Error
813 }
814
815 if index != nil {
816
817 check.errorf(e.Sel, AmbiguousSelector, "ambiguous selector %s.%s", x.expr, sel)
818 goto Error
819 }
820
821 if indirect {
822 if x.mode == typexpr {
823 check.errorf(e.Sel, InvalidMethodExpr, "invalid method expression %s.%s (needs pointer receiver (*%s).%s)", x.typ, sel, x.typ, sel)
824 } else {
825 check.errorf(e.Sel, InvalidMethodExpr, "cannot call pointer method %s on %s", sel, x.typ)
826 }
827 goto Error
828 }
829
830 var why string
831 if isInterfacePtr(x.typ) {
832 why = check.interfacePtrError(x.typ)
833 } else {
834 alt, _, _ := lookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel, true)
835 why = check.lookupError(x.typ, sel, alt, false)
836 }
837 check.errorf(e.Sel, MissingFieldOrMethod, "%s.%s undefined (%s)", x.expr, sel, why)
838 goto Error
839 }
840
841
842 if m, _ := obj.(*Func); m != nil {
843 check.objDecl(m, nil)
844 }
845
846 if x.mode == typexpr {
847
848 m, _ := obj.(*Func)
849 if m == nil {
850 check.errorf(e.Sel, MissingFieldOrMethod, "%s.%s undefined (type %s has no method %s)", x.expr, sel, x.typ, sel)
851 goto Error
852 }
853
854 check.recordSelection(e, MethodExpr, x.typ, m, index, indirect)
855
856 sig := m.typ.(*Signature)
857 if sig.recv == nil {
858 check.error(e, InvalidDeclCycle, "illegal cycle in method declaration")
859 goto Error
860 }
861
862
863
864 var params []*Var
865 if sig.params != nil {
866 params = sig.params.vars
867 }
868
869
870
871
872
873
874 name := ""
875 if len(params) > 0 && params[0].name != "" {
876
877 name = sig.recv.name
878 if name == "" {
879 name = "_"
880 }
881 }
882 params = append([]*Var{NewVar(sig.recv.pos, sig.recv.pkg, name, x.typ)}, params...)
883 x.mode = value
884 x.typ = &Signature{
885 tparams: sig.tparams,
886 params: NewTuple(params...),
887 results: sig.results,
888 variadic: sig.variadic,
889 }
890
891 check.addDeclDep(m)
892
893 } else {
894
895 switch obj := obj.(type) {
896 case *Var:
897 check.recordSelection(e, FieldVal, x.typ, obj, index, indirect)
898 if x.mode == variable || indirect {
899 x.mode = variable
900 } else {
901 x.mode = value
902 }
903 x.typ = obj.typ
904
905 case *Func:
906
907
908 check.recordSelection(e, MethodVal, x.typ, obj, index, indirect)
909
910
911
912
913
914
915
916 disabled := true
917 if !disabled && debug {
918
919
920
921
922
923 typ := x.typ
924 if x.mode == variable {
925
926
927
928
929
930 if _, ok := typ.(*Pointer); !ok && !IsInterface(typ) {
931 typ = &Pointer{base: typ}
932 }
933 }
934
935
936
937
938
939
940
941
942 mset := NewMethodSet(typ)
943 if m := mset.Lookup(check.pkg, sel); m == nil || m.obj != obj {
944 check.dump("%v: (%s).%v -> %s", e.Pos(), typ, obj.name, m)
945 check.dump("%s\n", mset)
946
947
948
949
950
951 panic("method sets and lookup don't agree")
952 }
953 }
954
955 x.mode = value
956
957
958 sig := *obj.typ.(*Signature)
959 sig.recv = nil
960 x.typ = &sig
961
962 check.addDeclDep(obj)
963
964 default:
965 panic("unreachable")
966 }
967 }
968
969
970 x.expr = e
971 return
972
973 Error:
974 x.mode = invalid
975 x.expr = e
976 }
977
978
979
980
981
982
983 func (check *Checker) use(args ...ast.Expr) bool { return check.useN(args, false) }
984
985
986
987
988 func (check *Checker) useLHS(args ...ast.Expr) bool { return check.useN(args, true) }
989
990 func (check *Checker) useN(args []ast.Expr, lhs bool) bool {
991 ok := true
992 for _, e := range args {
993 if !check.use1(e, lhs) {
994 ok = false
995 }
996 }
997 return ok
998 }
999
1000 func (check *Checker) use1(e ast.Expr, lhs bool) bool {
1001 var x operand
1002 x.mode = value
1003 switch n := ast.Unparen(e).(type) {
1004 case nil:
1005
1006 case *ast.Ident:
1007
1008 if n.Name == "_" {
1009 break
1010 }
1011
1012
1013
1014 var v *Var
1015 var v_used bool
1016 if lhs {
1017 if obj := check.lookup(n.Name); obj != nil {
1018
1019
1020
1021 if w, _ := obj.(*Var); w != nil && w.pkg == check.pkg {
1022 v = w
1023 v_used = v.used
1024 }
1025 }
1026 }
1027 check.exprOrType(&x, n, true)
1028 if v != nil {
1029 v.used = v_used
1030 }
1031 default:
1032 check.rawExpr(nil, &x, e, nil, true)
1033 }
1034 return x.mode != invalid
1035 }
1036
View as plain text