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