1
2
3
4
5 package types2
6
7 import (
8 "cmd/compile/internal/syntax"
9 "fmt"
10 "go/constant"
11 "internal/buildcfg"
12 . "internal/types/errors"
13 "slices"
14 )
15
16 func (check *Checker) declare(scope *Scope, id *syntax.Name, obj Object, pos syntax.Pos) {
17
18
19
20
21 if obj.Name() != "_" {
22 if alt := scope.Insert(obj); alt != nil {
23 err := check.newError(DuplicateDecl)
24 err.addf(obj, "%s redeclared in this block", obj.Name())
25 err.addAltDecl(alt)
26 err.report()
27 return
28 }
29 obj.setScopePos(pos)
30 }
31 if id != nil {
32 check.recordDef(id, obj)
33 }
34 }
35
36
37 func pathString(path []Object) string {
38 var s string
39 for i, p := range path {
40 if i > 0 {
41 s += "->"
42 }
43 s += p.Name()
44 }
45 return s
46 }
47
48
49
50 func (check *Checker) objDecl(obj Object, def *TypeName) {
51 if tracePos {
52 check.pushPos(obj.Pos())
53 defer func() {
54
55 if p := recover(); p != nil {
56 panic(p)
57 }
58 check.popPos()
59 }()
60 }
61
62 if check.conf.Trace && obj.Type() == nil {
63 if check.indent == 0 {
64 fmt.Println()
65 }
66 check.trace(obj.Pos(), "-- checking %s (%s, objPath = %s)", obj, obj.color(), pathString(check.objPath))
67 check.indent++
68 defer func() {
69 check.indent--
70 check.trace(obj.Pos(), "=> %s (%s)", obj, obj.color())
71 }()
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 if obj.color() == white && obj.Type() != nil {
102 obj.setColor(black)
103 return
104 }
105
106 switch obj.color() {
107 case white:
108 assert(obj.Type() == nil)
109
110
111
112 obj.setColor(grey + color(check.push(obj)))
113 defer func() {
114 check.pop().setColor(black)
115 }()
116
117 case black:
118 assert(obj.Type() != nil)
119 return
120
121 default:
122
123 fallthrough
124
125 case grey:
126
127
128
129
130
131
132
133
134
135
136 switch obj := obj.(type) {
137 case *Const:
138 if !check.validCycle(obj) || obj.typ == nil {
139 obj.typ = Typ[Invalid]
140 }
141
142 case *Var:
143 if !check.validCycle(obj) || obj.typ == nil {
144 obj.typ = Typ[Invalid]
145 }
146
147 case *TypeName:
148 if !check.validCycle(obj) {
149
150
151
152
153
154 obj.typ = Typ[Invalid]
155 }
156
157 case *Func:
158 if !check.validCycle(obj) {
159
160
161
162
163
164
165 }
166
167 default:
168 panic("unreachable")
169 }
170 assert(obj.Type() != nil)
171 return
172 }
173
174 d := check.objMap[obj]
175 if d == nil {
176 check.dump("%v: %s should have been declared", obj.Pos(), obj)
177 panic("unreachable")
178 }
179
180
181 defer func(env environment) {
182 check.environment = env
183 }(check.environment)
184 check.environment = environment{scope: d.file, version: d.version}
185
186
187
188
189
190
191 switch obj := obj.(type) {
192 case *Const:
193 check.decl = d
194 check.constDecl(obj, d.vtyp, d.init, d.inherited)
195 case *Var:
196 check.decl = d
197 check.varDecl(obj, d.lhs, d.vtyp, d.init)
198 case *TypeName:
199
200 check.typeDecl(obj, d.tdecl, def)
201 check.collectMethods(obj)
202 case *Func:
203
204 check.funcDecl(obj, d)
205 default:
206 panic("unreachable")
207 }
208 }
209
210
211
212 func (check *Checker) validCycle(obj Object) (valid bool) {
213
214 if debug {
215 info := check.objMap[obj]
216 inObjMap := info != nil && (info.fdecl == nil || info.fdecl.Recv == nil)
217 isPkgObj := obj.Parent() == check.pkg.scope
218 if isPkgObj != inObjMap {
219 check.dump("%v: inconsistent object map for %s (isPkgObj = %v, inObjMap = %v)", obj.Pos(), obj, isPkgObj, inObjMap)
220 panic("unreachable")
221 }
222 }
223
224
225 assert(obj.color() >= grey)
226 start := obj.color() - grey
227 cycle := check.objPath[start:]
228 tparCycle := false
229 nval := 0
230 ndef := 0
231 loop:
232 for _, obj := range cycle {
233 switch obj := obj.(type) {
234 case *Const, *Var:
235 nval++
236 case *TypeName:
237
238
239
240 if check.inTParamList && isGeneric(obj.typ) {
241 tparCycle = true
242 break loop
243 }
244
245
246
247
248
249
250
251
252
253
254 var alias bool
255 if check.conf.EnableAlias {
256 alias = obj.IsAlias()
257 } else {
258 if d := check.objMap[obj]; d != nil {
259 alias = d.tdecl.Alias
260 } else {
261 alias = obj.IsAlias()
262 }
263 }
264 if !alias {
265 ndef++
266 }
267 case *Func:
268
269 default:
270 panic("unreachable")
271 }
272 }
273
274 if check.conf.Trace {
275 check.trace(obj.Pos(), "## cycle detected: objPath = %s->%s (len = %d)", pathString(cycle), obj.Name(), len(cycle))
276 if tparCycle {
277 check.trace(obj.Pos(), "## cycle contains: generic type in a type parameter list")
278 } else {
279 check.trace(obj.Pos(), "## cycle contains: %d values, %d type definitions", nval, ndef)
280 }
281 defer func() {
282 if valid {
283 check.trace(obj.Pos(), "=> cycle is valid")
284 } else {
285 check.trace(obj.Pos(), "=> error: cycle is invalid")
286 }
287 }()
288 }
289
290 if !tparCycle {
291
292
293
294 if nval == len(cycle) {
295 return true
296 }
297
298
299
300
301 if nval == 0 && ndef > 0 {
302 return true
303 }
304 }
305
306 check.cycleError(cycle, firstInSrc(cycle))
307 return false
308 }
309
310
311 func (check *Checker) cycleError(cycle []Object, start int) {
312
313
314
315
316 name := func(obj Object) string {
317 return packagePrefix(obj.Pkg(), check.qualifier) + obj.Name()
318 }
319
320
321 obj := cycle[start]
322 tname, _ := obj.(*TypeName)
323 if tname != nil {
324 if check.conf.EnableAlias {
325 if a, ok := tname.Type().(*Alias); ok {
326 a.fromRHS = Typ[Invalid]
327 }
328 } else {
329 if tname.IsAlias() {
330 check.validAlias(tname, Typ[Invalid])
331 }
332 }
333 }
334
335
336 if len(cycle) == 1 {
337 if tname != nil {
338 check.errorf(obj, InvalidDeclCycle, "invalid recursive type: %s refers to itself", name(obj))
339 } else {
340 check.errorf(obj, InvalidDeclCycle, "invalid cycle in declaration: %s refers to itself", name(obj))
341 }
342 return
343 }
344
345 err := check.newError(InvalidDeclCycle)
346 if tname != nil {
347 err.addf(obj, "invalid recursive type %s", name(obj))
348 } else {
349 err.addf(obj, "invalid cycle in declaration of %s", name(obj))
350 }
351
352 for i := range cycle {
353 next := cycle[(start+i+1)%len(cycle)]
354 err.addf(obj, "%s refers to %s", name(obj), name(next))
355 obj = next
356 }
357 err.report()
358 }
359
360
361
362 func firstInSrc(path []Object) int {
363 fst, pos := 0, path[0].Pos()
364 for i, t := range path[1:] {
365 if cmpPos(t.Pos(), pos) < 0 {
366 fst, pos = i+1, t.Pos()
367 }
368 }
369 return fst
370 }
371
372 func (check *Checker) constDecl(obj *Const, typ, init syntax.Expr, inherited bool) {
373 assert(obj.typ == nil)
374
375
376 defer func(iota constant.Value, errpos syntax.Pos) {
377 check.iota = iota
378 check.errpos = errpos
379 }(check.iota, check.errpos)
380 check.iota = obj.val
381 check.errpos = nopos
382
383
384 obj.val = constant.MakeUnknown()
385
386
387 if typ != nil {
388 t := check.typ(typ)
389 if !isConstType(t) {
390
391
392 if isValid(under(t)) {
393 check.errorf(typ, InvalidConstType, "invalid constant type %s", t)
394 }
395 obj.typ = Typ[Invalid]
396 return
397 }
398 obj.typ = t
399 }
400
401
402 var x operand
403 if init != nil {
404 if inherited {
405
406
407
408
409
410
411 check.errpos = obj.pos
412 }
413 check.expr(nil, &x, init)
414 }
415 check.initConst(obj, &x)
416 }
417
418 func (check *Checker) varDecl(obj *Var, lhs []*Var, typ, init syntax.Expr) {
419 assert(obj.typ == nil)
420
421
422 if typ != nil {
423 obj.typ = check.varType(typ)
424
425
426
427
428
429
430
431
432 }
433
434
435 if init == nil {
436 if typ == nil {
437
438 obj.typ = Typ[Invalid]
439 }
440 return
441 }
442
443 if lhs == nil || len(lhs) == 1 {
444 assert(lhs == nil || lhs[0] == obj)
445 var x operand
446 check.expr(newTarget(obj.typ, obj.name), &x, init)
447 check.initVar(obj, &x, "variable declaration")
448 return
449 }
450
451 if debug {
452
453 if !slices.Contains(lhs, obj) {
454 panic("inconsistent lhs")
455 }
456 }
457
458
459
460
461
462 if typ != nil {
463 for _, lhs := range lhs {
464 lhs.typ = obj.typ
465 }
466 }
467
468 check.initVars(lhs, []syntax.Expr{init}, nil)
469 }
470
471
472 func (check *Checker) isImportedConstraint(typ Type) bool {
473 named := asNamed(typ)
474 if named == nil || named.obj.pkg == check.pkg || named.obj.pkg == nil {
475 return false
476 }
477 u, _ := named.under().(*Interface)
478 return u != nil && !u.IsMethodSet()
479 }
480
481 func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeName) {
482 assert(obj.typ == nil)
483
484
485 versionErr := false
486
487 var rhs Type
488 check.later(func() {
489 if t := asNamed(obj.typ); t != nil {
490 check.validType(t)
491 }
492
493 _ = !versionErr && check.isImportedConstraint(rhs) && check.verifyVersionf(tdecl.Type, go1_18, "using type constraint %s", rhs)
494 }).describef(obj, "validType(%s)", obj.Name())
495
496
497 var tparam0 *syntax.Field
498 if len(tdecl.TParamList) > 0 {
499 tparam0 = tdecl.TParamList[0]
500 }
501
502
503 if tdecl.Alias {
504
505
506 if !versionErr && tparam0 != nil && !check.verifyVersionf(tparam0, go1_23, "generic type alias") {
507 versionErr = true
508 }
509 if !versionErr && !check.verifyVersionf(tdecl, go1_9, "type alias") {
510 versionErr = true
511 }
512
513 if check.conf.EnableAlias {
514 alias := check.newAlias(obj, nil)
515 setDefType(def, alias)
516
517
518
519 defer func() {
520 if alias.fromRHS == nil {
521 alias.fromRHS = Typ[Invalid]
522 unalias(alias)
523 }
524 }()
525
526
527 if tparam0 != nil {
528 if !versionErr && !buildcfg.Experiment.AliasTypeParams {
529 check.error(tdecl, UnsupportedFeature, "generic type alias requires GOEXPERIMENT=aliastypeparams")
530 versionErr = true
531 }
532 check.openScope(tdecl, "type parameters")
533 defer check.closeScope()
534 check.collectTypeParams(&alias.tparams, tdecl.TParamList)
535 }
536
537 rhs = check.definedType(tdecl.Type, obj)
538 assert(rhs != nil)
539
540 alias.fromRHS = rhs
541 unalias(alias)
542 } else {
543 if !versionErr && tparam0 != nil {
544 check.error(tdecl, UnsupportedFeature, "generic type alias requires GODEBUG=gotypesalias=1 or unset")
545 versionErr = true
546 }
547
548 check.brokenAlias(obj)
549 rhs = check.typ(tdecl.Type)
550 check.validAlias(obj, rhs)
551 }
552 return
553 }
554
555
556 if !versionErr && tparam0 != nil && !check.verifyVersionf(tparam0, go1_18, "type parameter") {
557 versionErr = true
558 }
559
560 named := check.newNamed(obj, nil, nil)
561 setDefType(def, named)
562
563 if tdecl.TParamList != nil {
564 check.openScope(tdecl, "type parameters")
565 defer check.closeScope()
566 check.collectTypeParams(&named.tparams, tdecl.TParamList)
567 }
568
569
570 rhs = check.definedType(tdecl.Type, obj)
571 assert(rhs != nil)
572 named.fromRHS = rhs
573
574
575
576 if named.underlying == nil {
577 named.underlying = Typ[Invalid]
578 }
579
580
581
582
583
584
585 if isTypeParam(rhs) {
586 check.error(tdecl.Type, MisplacedTypeParam, "cannot use a type parameter as RHS in type declaration")
587 named.underlying = Typ[Invalid]
588 }
589 }
590
591 func (check *Checker) collectTypeParams(dst **TypeParamList, list []*syntax.Field) {
592 tparams := make([]*TypeParam, len(list))
593
594
595
596
597 if len(list) > 0 {
598 scopePos := list[0].Pos()
599 for i, f := range list {
600 tparams[i] = check.declareTypeParam(f.Name, scopePos)
601 }
602 }
603
604
605
606
607 *dst = bindTParams(tparams)
608
609
610
611
612
613
614
615
616 assert(!check.inTParamList)
617 check.inTParamList = true
618 defer func() {
619 check.inTParamList = false
620 }()
621
622
623 var bound Type
624 for i, f := range list {
625
626
627
628 if i == 0 || f.Type != list[i-1].Type {
629 bound = check.bound(f.Type)
630 if isTypeParam(bound) {
631
632
633
634
635 check.error(f.Type, MisplacedTypeParam, "cannot use a type parameter as constraint")
636 bound = Typ[Invalid]
637 }
638 }
639 tparams[i].bound = bound
640 }
641 }
642
643 func (check *Checker) bound(x syntax.Expr) Type {
644
645
646
647 if op, _ := x.(*syntax.Operation); op != nil && (op.Op == syntax.Tilde || op.Op == syntax.Or) {
648 t := check.typ(&syntax.InterfaceType{MethodList: []*syntax.Field{{Type: x}}})
649
650 if t, _ := t.(*Interface); t != nil {
651 t.implicit = true
652 }
653 return t
654 }
655 return check.typ(x)
656 }
657
658 func (check *Checker) declareTypeParam(name *syntax.Name, scopePos syntax.Pos) *TypeParam {
659
660
661
662
663
664
665 tname := NewTypeName(name.Pos(), check.pkg, name.Value, nil)
666 tpar := check.newTypeParam(tname, Typ[Invalid])
667 check.declare(check.scope, name, tname, scopePos)
668 return tpar
669 }
670
671 func (check *Checker) collectMethods(obj *TypeName) {
672
673
674
675
676 methods := check.methods[obj]
677 if methods == nil {
678 return
679 }
680 delete(check.methods, obj)
681 assert(!check.objMap[obj].tdecl.Alias)
682
683
684 var mset objset
685
686
687
688 base := asNamed(obj.typ)
689 if base != nil {
690 assert(base.TypeArgs().Len() == 0)
691
692
693
694 check.later(func() {
695 check.checkFieldUniqueness(base)
696 }).describef(obj, "verifying field uniqueness for %v", base)
697
698
699
700
701 for i := 0; i < base.NumMethods(); i++ {
702 m := base.Method(i)
703 assert(m.name != "_")
704 assert(mset.insert(m) == nil)
705 }
706 }
707
708
709 for _, m := range methods {
710
711
712 assert(m.name != "_")
713 if alt := mset.insert(m); alt != nil {
714 if alt.Pos().IsKnown() {
715 check.errorf(m.pos, DuplicateMethod, "method %s.%s already declared at %v", obj.Name(), m.name, alt.Pos())
716 } else {
717 check.errorf(m.pos, DuplicateMethod, "method %s.%s already declared", obj.Name(), m.name)
718 }
719 continue
720 }
721
722 if base != nil {
723 base.AddMethod(m)
724 }
725 }
726 }
727
728 func (check *Checker) checkFieldUniqueness(base *Named) {
729 if t, _ := base.under().(*Struct); t != nil {
730 var mset objset
731 for i := 0; i < base.NumMethods(); i++ {
732 m := base.Method(i)
733 assert(m.name != "_")
734 assert(mset.insert(m) == nil)
735 }
736
737
738
739 for _, fld := range t.fields {
740 if fld.name != "_" {
741 if alt := mset.insert(fld); alt != nil {
742
743
744 _ = alt.(*Func)
745
746
747
748 err := check.newError(DuplicateFieldAndMethod)
749 err.addf(alt, "field and method with the same name %s", fld.name)
750 err.addAltDecl(fld)
751 err.report()
752 }
753 }
754 }
755 }
756 }
757
758 func (check *Checker) funcDecl(obj *Func, decl *declInfo) {
759 assert(obj.typ == nil)
760
761
762 assert(check.iota == nil)
763
764 sig := new(Signature)
765 obj.typ = sig
766
767
768
769
770
771
772
773 saved := obj.color_
774 obj.color_ = black
775 fdecl := decl.fdecl
776 check.funcType(sig, fdecl.Recv, fdecl.TParamList, fdecl.Type)
777 obj.color_ = saved
778
779
780
781 sig.scope.pos = fdecl.Pos()
782 sig.scope.end = syntax.EndPos(fdecl)
783
784 if len(fdecl.TParamList) > 0 && fdecl.Body == nil {
785 check.softErrorf(fdecl, BadDecl, "generic function is missing function body")
786 }
787
788
789
790 if !check.conf.IgnoreFuncBodies && fdecl.Body != nil {
791 check.later(func() {
792 check.funcBody(decl, obj.name, sig, fdecl.Body, nil)
793 }).describef(obj, "func %s", obj.name)
794 }
795 }
796
797 func (check *Checker) declStmt(list []syntax.Decl) {
798 pkg := check.pkg
799
800 first := -1
801 var last *syntax.ConstDecl
802 for index, decl := range list {
803 if _, ok := decl.(*syntax.ConstDecl); !ok {
804 first = -1
805 }
806
807 switch s := decl.(type) {
808 case *syntax.ConstDecl:
809 top := len(check.delayed)
810
811
812 if first < 0 || s.Group == nil || list[index-1].(*syntax.ConstDecl).Group != s.Group {
813 first = index
814 last = nil
815 }
816 iota := constant.MakeInt64(int64(index - first))
817
818
819 inherited := true
820 switch {
821 case s.Type != nil || s.Values != nil:
822 last = s
823 inherited = false
824 case last == nil:
825 last = new(syntax.ConstDecl)
826 inherited = false
827 }
828
829
830 lhs := make([]*Const, len(s.NameList))
831 values := syntax.UnpackListExpr(last.Values)
832 for i, name := range s.NameList {
833 obj := NewConst(name.Pos(), pkg, name.Value, nil, iota)
834 lhs[i] = obj
835
836 var init syntax.Expr
837 if i < len(values) {
838 init = values[i]
839 }
840
841 check.constDecl(obj, last.Type, init, inherited)
842 }
843
844
845 check.arity(s.Pos(), s.NameList, values, true, inherited)
846
847
848 check.processDelayed(top)
849
850
851
852
853
854 scopePos := syntax.EndPos(s)
855 for i, name := range s.NameList {
856 check.declare(check.scope, name, lhs[i], scopePos)
857 }
858
859 case *syntax.VarDecl:
860 top := len(check.delayed)
861
862 lhs0 := make([]*Var, len(s.NameList))
863 for i, name := range s.NameList {
864 lhs0[i] = newVar(LocalVar, name.Pos(), pkg, name.Value, nil)
865 }
866
867
868 values := syntax.UnpackListExpr(s.Values)
869 for i, obj := range lhs0 {
870 var lhs []*Var
871 var init syntax.Expr
872 switch len(values) {
873 case len(s.NameList):
874
875 init = values[i]
876 case 1:
877
878 lhs = lhs0
879 init = values[0]
880 default:
881 if i < len(values) {
882 init = values[i]
883 }
884 }
885 check.varDecl(obj, lhs, s.Type, init)
886 if len(values) == 1 {
887
888
889
890
891
892 if debug {
893 for _, obj := range lhs0 {
894 assert(obj.typ != nil)
895 }
896 }
897 break
898 }
899 }
900
901
902 if s.Type == nil || values != nil {
903 check.arity(s.Pos(), s.NameList, values, false, false)
904 }
905
906
907 check.processDelayed(top)
908
909
910
911 scopePos := syntax.EndPos(s)
912 for i, name := range s.NameList {
913
914 check.declare(check.scope, name, lhs0[i], scopePos)
915 }
916
917 case *syntax.TypeDecl:
918 obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Value, nil)
919
920
921
922 scopePos := s.Name.Pos()
923 check.declare(check.scope, s.Name, obj, scopePos)
924
925 obj.setColor(grey + color(check.push(obj)))
926 check.typeDecl(obj, s, nil)
927 check.pop().setColor(black)
928
929 default:
930 check.errorf(s, InvalidSyntaxTree, "unknown syntax.Decl node %T", s)
931 }
932 }
933 }
934
View as plain text