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