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