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