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