...

Source file src/go/types/decl.go

Documentation: go/types

		 1  // Copyright 2014 The Go Authors. All rights reserved.
		 2  // Use of this source code is governed by a BSD-style
		 3  // license that can be found in the LICENSE file.
		 4  
		 5  package types
		 6  
		 7  import (
		 8  	"fmt"
		 9  	"go/ast"
		10  	"go/constant"
		11  	"go/internal/typeparams"
		12  	"go/token"
		13  )
		14  
		15  func (check *Checker) reportAltDecl(obj Object) {
		16  	if pos := obj.Pos(); pos.IsValid() {
		17  		// We use "other" rather than "previous" here because
		18  		// the first declaration seen may not be textually
		19  		// earlier in the source.
		20  		check.errorf(obj, _DuplicateDecl, "\tother declaration of %s", obj.Name()) // secondary error, \t indented
		21  	}
		22  }
		23  
		24  func (check *Checker) declare(scope *Scope, id *ast.Ident, obj Object, pos token.Pos) {
		25  	// spec: "The blank identifier, represented by the underscore
		26  	// character _, may be used in a declaration like any other
		27  	// identifier but the declaration does not introduce a new
		28  	// binding."
		29  	if obj.Name() != "_" {
		30  		if alt := scope.Insert(obj); alt != nil {
		31  			check.errorf(obj, _DuplicateDecl, "%s redeclared in this block", obj.Name())
		32  			check.reportAltDecl(alt)
		33  			return
		34  		}
		35  		obj.setScopePos(pos)
		36  	}
		37  	if id != nil {
		38  		check.recordDef(id, obj)
		39  	}
		40  }
		41  
		42  // pathString returns a string of the form a->b-> ... ->g for a path [a, b, ... g].
		43  func pathString(path []Object) string {
		44  	var s string
		45  	for i, p := range path {
		46  		if i > 0 {
		47  			s += "->"
		48  		}
		49  		s += p.Name()
		50  	}
		51  	return s
		52  }
		53  
		54  // objDecl type-checks the declaration of obj in its respective (file) context.
		55  // For the meaning of def, see Checker.definedType, in typexpr.go.
		56  func (check *Checker) objDecl(obj Object, def *Named) {
		57  	if trace && obj.Type() == nil {
		58  		if check.indent == 0 {
		59  			fmt.Println() // empty line between top-level objects for readability
		60  		}
		61  		check.trace(obj.Pos(), "-- checking %s (%s, objPath = %s)", obj, obj.color(), pathString(check.objPath))
		62  		check.indent++
		63  		defer func() {
		64  			check.indent--
		65  			check.trace(obj.Pos(), "=> %s (%s)", obj, obj.color())
		66  		}()
		67  	}
		68  
		69  	// Checking the declaration of obj means inferring its type
		70  	// (and possibly its value, for constants).
		71  	// An object's type (and thus the object) may be in one of
		72  	// three states which are expressed by colors:
		73  	//
		74  	// - an object whose type is not yet known is painted white (initial color)
		75  	// - an object whose type is in the process of being inferred is painted grey
		76  	// - an object whose type is fully inferred is painted black
		77  	//
		78  	// During type inference, an object's color changes from white to grey
		79  	// to black (pre-declared objects are painted black from the start).
		80  	// A black object (i.e., its type) can only depend on (refer to) other black
		81  	// ones. White and grey objects may depend on white and black objects.
		82  	// A dependency on a grey object indicates a cycle which may or may not be
		83  	// valid.
		84  	//
		85  	// When objects turn grey, they are pushed on the object path (a stack);
		86  	// they are popped again when they turn black. Thus, if a grey object (a
		87  	// cycle) is encountered, it is on the object path, and all the objects
		88  	// it depends on are the remaining objects on that path. Color encoding
		89  	// is such that the color value of a grey object indicates the index of
		90  	// that object in the object path.
		91  
		92  	// During type-checking, white objects may be assigned a type without
		93  	// traversing through objDecl; e.g., when initializing constants and
		94  	// variables. Update the colors of those objects here (rather than
		95  	// everywhere where we set the type) to satisfy the color invariants.
		96  	if obj.color() == white && obj.Type() != nil {
		97  		obj.setColor(black)
		98  		return
		99  	}
	 100  
	 101  	switch obj.color() {
	 102  	case white:
	 103  		assert(obj.Type() == nil)
	 104  		// All color values other than white and black are considered grey.
	 105  		// Because black and white are < grey, all values >= grey are grey.
	 106  		// Use those values to encode the object's index into the object path.
	 107  		obj.setColor(grey + color(check.push(obj)))
	 108  		defer func() {
	 109  			check.pop().setColor(black)
	 110  		}()
	 111  
	 112  	case black:
	 113  		assert(obj.Type() != nil)
	 114  		return
	 115  
	 116  	default:
	 117  		// Color values other than white or black are considered grey.
	 118  		fallthrough
	 119  
	 120  	case grey:
	 121  		// We have a cycle.
	 122  		// In the existing code, this is marked by a non-nil type
	 123  		// for the object except for constants and variables whose
	 124  		// type may be non-nil (known), or nil if it depends on the
	 125  		// not-yet known initialization value.
	 126  		// In the former case, set the type to Typ[Invalid] because
	 127  		// we have an initialization cycle. The cycle error will be
	 128  		// reported later, when determining initialization order.
	 129  		// TODO(gri) Report cycle here and simplify initialization
	 130  		// order code.
	 131  		switch obj := obj.(type) {
	 132  		case *Const:
	 133  			if check.cycle(obj) || obj.typ == nil {
	 134  				obj.typ = Typ[Invalid]
	 135  			}
	 136  
	 137  		case *Var:
	 138  			if check.cycle(obj) || obj.typ == nil {
	 139  				obj.typ = Typ[Invalid]
	 140  			}
	 141  
	 142  		case *TypeName:
	 143  			if check.cycle(obj) {
	 144  				// break cycle
	 145  				// (without this, calling underlying()
	 146  				// below may lead to an endless loop
	 147  				// if we have a cycle for a defined
	 148  				// (*Named) type)
	 149  				obj.typ = Typ[Invalid]
	 150  			}
	 151  
	 152  		case *Func:
	 153  			if check.cycle(obj) {
	 154  				// Don't set obj.typ to Typ[Invalid] here
	 155  				// because plenty of code type-asserts that
	 156  				// functions have a *Signature type. Grey
	 157  				// functions have their type set to an empty
	 158  				// signature which makes it impossible to
	 159  				// initialize a variable with the function.
	 160  			}
	 161  
	 162  		default:
	 163  			unreachable()
	 164  		}
	 165  		assert(obj.Type() != nil)
	 166  		return
	 167  	}
	 168  
	 169  	d := check.objMap[obj]
	 170  	if d == nil {
	 171  		check.dump("%v: %s should have been declared", obj.Pos(), obj)
	 172  		unreachable()
	 173  	}
	 174  
	 175  	// save/restore current context and setup object context
	 176  	defer func(ctxt context) {
	 177  		check.context = ctxt
	 178  	}(check.context)
	 179  	check.context = context{
	 180  		scope: d.file,
	 181  	}
	 182  
	 183  	// Const and var declarations must not have initialization
	 184  	// cycles. We track them by remembering the current declaration
	 185  	// in check.decl. Initialization expressions depending on other
	 186  	// consts, vars, or functions, add dependencies to the current
	 187  	// check.decl.
	 188  	switch obj := obj.(type) {
	 189  	case *Const:
	 190  		check.decl = d // new package-level const decl
	 191  		check.constDecl(obj, d.vtyp, d.init, d.inherited)
	 192  	case *Var:
	 193  		check.decl = d // new package-level var decl
	 194  		check.varDecl(obj, d.lhs, d.vtyp, d.init)
	 195  	case *TypeName:
	 196  		// invalid recursive types are detected via path
	 197  		check.typeDecl(obj, d.tdecl, def)
	 198  		check.collectMethods(obj) // methods can only be added to top-level types
	 199  	case *Func:
	 200  		// functions may be recursive - no need to track dependencies
	 201  		check.funcDecl(obj, d)
	 202  	default:
	 203  		unreachable()
	 204  	}
	 205  }
	 206  
	 207  // cycle checks if the cycle starting with obj is valid and
	 208  // reports an error if it is not.
	 209  func (check *Checker) cycle(obj Object) (isCycle bool) {
	 210  	// The object map contains the package scope objects and the non-interface methods.
	 211  	if debug {
	 212  		info := check.objMap[obj]
	 213  		inObjMap := info != nil && (info.fdecl == nil || info.fdecl.Recv == nil) // exclude methods
	 214  		isPkgObj := obj.Parent() == check.pkg.scope
	 215  		if isPkgObj != inObjMap {
	 216  			check.dump("%v: inconsistent object map for %s (isPkgObj = %v, inObjMap = %v)", obj.Pos(), obj, isPkgObj, inObjMap)
	 217  			unreachable()
	 218  		}
	 219  	}
	 220  
	 221  	// Count cycle objects.
	 222  	assert(obj.color() >= grey)
	 223  	start := obj.color() - grey // index of obj in objPath
	 224  	cycle := check.objPath[start:]
	 225  	nval := 0 // number of (constant or variable) values in the cycle
	 226  	ndef := 0 // number of type definitions in the cycle
	 227  	for _, obj := range cycle {
	 228  		switch obj := obj.(type) {
	 229  		case *Const, *Var:
	 230  			nval++
	 231  		case *TypeName:
	 232  			// Determine if the type name is an alias or not. For
	 233  			// package-level objects, use the object map which
	 234  			// provides syntactic information (which doesn't rely
	 235  			// on the order in which the objects are set up). For
	 236  			// local objects, we can rely on the order, so use
	 237  			// the object's predicate.
	 238  			// TODO(gri) It would be less fragile to always access
	 239  			// the syntactic information. We should consider storing
	 240  			// this information explicitly in the object.
	 241  			var alias bool
	 242  			if d := check.objMap[obj]; d != nil {
	 243  				alias = d.tdecl.Assign.IsValid() // package-level object
	 244  			} else {
	 245  				alias = obj.IsAlias() // function local object
	 246  			}
	 247  			if !alias {
	 248  				ndef++
	 249  			}
	 250  		case *Func:
	 251  			// ignored for now
	 252  		default:
	 253  			unreachable()
	 254  		}
	 255  	}
	 256  
	 257  	if trace {
	 258  		check.trace(obj.Pos(), "## cycle detected: objPath = %s->%s (len = %d)", pathString(cycle), obj.Name(), len(cycle))
	 259  		check.trace(obj.Pos(), "## cycle contains: %d values, %d type definitions", nval, ndef)
	 260  		defer func() {
	 261  			if isCycle {
	 262  				check.trace(obj.Pos(), "=> error: cycle is invalid")
	 263  			}
	 264  		}()
	 265  	}
	 266  
	 267  	// A cycle involving only constants and variables is invalid but we
	 268  	// ignore them here because they are reported via the initialization
	 269  	// cycle check.
	 270  	if nval == len(cycle) {
	 271  		return false
	 272  	}
	 273  
	 274  	// A cycle involving only types (and possibly functions) must have at least
	 275  	// one type definition to be permitted: If there is no type definition, we
	 276  	// have a sequence of alias type names which will expand ad infinitum.
	 277  	if nval == 0 && ndef > 0 {
	 278  		return false // cycle is permitted
	 279  	}
	 280  
	 281  	check.cycleError(cycle)
	 282  
	 283  	return true
	 284  }
	 285  
	 286  type typeInfo uint
	 287  
	 288  // validType verifies that the given type does not "expand" infinitely
	 289  // producing a cycle in the type graph. Cycles are detected by marking
	 290  // defined types.
	 291  // (Cycles involving alias types, as in "type A = [10]A" are detected
	 292  // earlier, via the objDecl cycle detection mechanism.)
	 293  func (check *Checker) validType(typ Type, path []Object) typeInfo {
	 294  	const (
	 295  		unknown typeInfo = iota
	 296  		marked
	 297  		valid
	 298  		invalid
	 299  	)
	 300  
	 301  	switch t := typ.(type) {
	 302  	case *Array:
	 303  		return check.validType(t.elem, path)
	 304  
	 305  	case *Struct:
	 306  		for _, f := range t.fields {
	 307  			if check.validType(f.typ, path) == invalid {
	 308  				return invalid
	 309  			}
	 310  		}
	 311  
	 312  	case *Interface:
	 313  		for _, etyp := range t.embeddeds {
	 314  			if check.validType(etyp, path) == invalid {
	 315  				return invalid
	 316  			}
	 317  		}
	 318  
	 319  	case *Named:
	 320  		// don't touch the type if it is from a different package or the Universe scope
	 321  		// (doing so would lead to a race condition - was issue #35049)
	 322  		if t.obj.pkg != check.pkg {
	 323  			return valid
	 324  		}
	 325  
	 326  		// don't report a 2nd error if we already know the type is invalid
	 327  		// (e.g., if a cycle was detected earlier, via under).
	 328  		if t.underlying == Typ[Invalid] {
	 329  			t.info = invalid
	 330  			return invalid
	 331  		}
	 332  
	 333  		switch t.info {
	 334  		case unknown:
	 335  			t.info = marked
	 336  			t.info = check.validType(t.orig, append(path, t.obj)) // only types of current package added to path
	 337  		case marked:
	 338  			// cycle detected
	 339  			for i, tn := range path {
	 340  				if t.obj.pkg != check.pkg {
	 341  					panic("internal error: type cycle via package-external type")
	 342  				}
	 343  				if tn == t.obj {
	 344  					check.cycleError(path[i:])
	 345  					t.info = invalid
	 346  					t.underlying = Typ[Invalid]
	 347  					return t.info
	 348  				}
	 349  			}
	 350  			panic("internal error: cycle start not found")
	 351  		}
	 352  		return t.info
	 353  
	 354  	case *instance:
	 355  		return check.validType(t.expand(), path)
	 356  	}
	 357  
	 358  	return valid
	 359  }
	 360  
	 361  // cycleError reports a declaration cycle starting with
	 362  // the object in cycle that is "first" in the source.
	 363  func (check *Checker) cycleError(cycle []Object) {
	 364  	// TODO(gri) Should we start with the last (rather than the first) object in the cycle
	 365  	//					 since that is the earliest point in the source where we start seeing the
	 366  	//					 cycle? That would be more consistent with other error messages.
	 367  	i := firstInSrc(cycle)
	 368  	obj := cycle[i]
	 369  	check.errorf(obj, _InvalidDeclCycle, "illegal cycle in declaration of %s", obj.Name())
	 370  	for range cycle {
	 371  		check.errorf(obj, _InvalidDeclCycle, "\t%s refers to", obj.Name()) // secondary error, \t indented
	 372  		i++
	 373  		if i >= len(cycle) {
	 374  			i = 0
	 375  		}
	 376  		obj = cycle[i]
	 377  	}
	 378  	check.errorf(obj, _InvalidDeclCycle, "\t%s", obj.Name())
	 379  }
	 380  
	 381  // firstInSrc reports the index of the object with the "smallest"
	 382  // source position in path. path must not be empty.
	 383  func firstInSrc(path []Object) int {
	 384  	fst, pos := 0, path[0].Pos()
	 385  	for i, t := range path[1:] {
	 386  		if t.Pos() < pos {
	 387  			fst, pos = i+1, t.Pos()
	 388  		}
	 389  	}
	 390  	return fst
	 391  }
	 392  
	 393  type (
	 394  	decl interface {
	 395  		node() ast.Node
	 396  	}
	 397  
	 398  	importDecl struct{ spec *ast.ImportSpec }
	 399  	constDecl	struct {
	 400  		spec			*ast.ValueSpec
	 401  		iota			int
	 402  		typ			 ast.Expr
	 403  		init			[]ast.Expr
	 404  		inherited bool
	 405  	}
	 406  	varDecl	struct{ spec *ast.ValueSpec }
	 407  	typeDecl struct{ spec *ast.TypeSpec }
	 408  	funcDecl struct{ decl *ast.FuncDecl }
	 409  )
	 410  
	 411  func (d importDecl) node() ast.Node { return d.spec }
	 412  func (d constDecl) node() ast.Node	{ return d.spec }
	 413  func (d varDecl) node() ast.Node		{ return d.spec }
	 414  func (d typeDecl) node() ast.Node	 { return d.spec }
	 415  func (d funcDecl) node() ast.Node	 { return d.decl }
	 416  
	 417  func (check *Checker) walkDecls(decls []ast.Decl, f func(decl)) {
	 418  	for _, d := range decls {
	 419  		check.walkDecl(d, f)
	 420  	}
	 421  }
	 422  
	 423  func (check *Checker) walkDecl(d ast.Decl, f func(decl)) {
	 424  	switch d := d.(type) {
	 425  	case *ast.BadDecl:
	 426  		// ignore
	 427  	case *ast.GenDecl:
	 428  		var last *ast.ValueSpec // last ValueSpec with type or init exprs seen
	 429  		for iota, s := range d.Specs {
	 430  			switch s := s.(type) {
	 431  			case *ast.ImportSpec:
	 432  				f(importDecl{s})
	 433  			case *ast.ValueSpec:
	 434  				switch d.Tok {
	 435  				case token.CONST:
	 436  					// determine which initialization expressions to use
	 437  					inherited := true
	 438  					switch {
	 439  					case s.Type != nil || len(s.Values) > 0:
	 440  						last = s
	 441  						inherited = false
	 442  					case last == nil:
	 443  						last = new(ast.ValueSpec) // make sure last exists
	 444  						inherited = false
	 445  					}
	 446  					check.arityMatch(s, last)
	 447  					f(constDecl{spec: s, iota: iota, typ: last.Type, init: last.Values, inherited: inherited})
	 448  				case token.VAR:
	 449  					check.arityMatch(s, nil)
	 450  					f(varDecl{s})
	 451  				default:
	 452  					check.invalidAST(s, "invalid token %s", d.Tok)
	 453  				}
	 454  			case *ast.TypeSpec:
	 455  				f(typeDecl{s})
	 456  			default:
	 457  				check.invalidAST(s, "unknown ast.Spec node %T", s)
	 458  			}
	 459  		}
	 460  	case *ast.FuncDecl:
	 461  		f(funcDecl{d})
	 462  	default:
	 463  		check.invalidAST(d, "unknown ast.Decl node %T", d)
	 464  	}
	 465  }
	 466  
	 467  func (check *Checker) constDecl(obj *Const, typ, init ast.Expr, inherited bool) {
	 468  	assert(obj.typ == nil)
	 469  
	 470  	// use the correct value of iota
	 471  	defer func(iota constant.Value, errpos positioner) {
	 472  		check.iota = iota
	 473  		check.errpos = errpos
	 474  	}(check.iota, check.errpos)
	 475  	check.iota = obj.val
	 476  	check.errpos = nil
	 477  
	 478  	// provide valid constant value under all circumstances
	 479  	obj.val = constant.MakeUnknown()
	 480  
	 481  	// determine type, if any
	 482  	if typ != nil {
	 483  		t := check.typ(typ)
	 484  		if !isConstType(t) {
	 485  			// don't report an error if the type is an invalid C (defined) type
	 486  			// (issue #22090)
	 487  			if under(t) != Typ[Invalid] {
	 488  				check.errorf(typ, _InvalidConstType, "invalid constant type %s", t)
	 489  			}
	 490  			obj.typ = Typ[Invalid]
	 491  			return
	 492  		}
	 493  		obj.typ = t
	 494  	}
	 495  
	 496  	// check initialization
	 497  	var x operand
	 498  	if init != nil {
	 499  		if inherited {
	 500  			// The initialization expression is inherited from a previous
	 501  			// constant declaration, and (error) positions refer to that
	 502  			// expression and not the current constant declaration. Use
	 503  			// the constant identifier position for any errors during
	 504  			// init expression evaluation since that is all we have
	 505  			// (see issues #42991, #42992).
	 506  			check.errpos = atPos(obj.pos)
	 507  		}
	 508  		check.expr(&x, init)
	 509  	}
	 510  	check.initConst(obj, &x)
	 511  }
	 512  
	 513  func (check *Checker) varDecl(obj *Var, lhs []*Var, typ, init ast.Expr) {
	 514  	assert(obj.typ == nil)
	 515  
	 516  	// determine type, if any
	 517  	if typ != nil {
	 518  		obj.typ = check.varType(typ)
	 519  		// We cannot spread the type to all lhs variables if there
	 520  		// are more than one since that would mark them as checked
	 521  		// (see Checker.objDecl) and the assignment of init exprs,
	 522  		// if any, would not be checked.
	 523  		//
	 524  		// TODO(gri) If we have no init expr, we should distribute
	 525  		// a given type otherwise we need to re-evalate the type
	 526  		// expr for each lhs variable, leading to duplicate work.
	 527  	}
	 528  
	 529  	// check initialization
	 530  	if init == nil {
	 531  		if typ == nil {
	 532  			// error reported before by arityMatch
	 533  			obj.typ = Typ[Invalid]
	 534  		}
	 535  		return
	 536  	}
	 537  
	 538  	if lhs == nil || len(lhs) == 1 {
	 539  		assert(lhs == nil || lhs[0] == obj)
	 540  		var x operand
	 541  		check.expr(&x, init)
	 542  		check.initVar(obj, &x, "variable declaration")
	 543  		return
	 544  	}
	 545  
	 546  	if debug {
	 547  		// obj must be one of lhs
	 548  		found := false
	 549  		for _, lhs := range lhs {
	 550  			if obj == lhs {
	 551  				found = true
	 552  				break
	 553  			}
	 554  		}
	 555  		if !found {
	 556  			panic("inconsistent lhs")
	 557  		}
	 558  	}
	 559  
	 560  	// We have multiple variables on the lhs and one init expr.
	 561  	// Make sure all variables have been given the same type if
	 562  	// one was specified, otherwise they assume the type of the
	 563  	// init expression values (was issue #15755).
	 564  	if typ != nil {
	 565  		for _, lhs := range lhs {
	 566  			lhs.typ = obj.typ
	 567  		}
	 568  	}
	 569  
	 570  	check.initVars(lhs, []ast.Expr{init}, token.NoPos)
	 571  }
	 572  
	 573  // under returns the expanded underlying type of n0; possibly by following
	 574  // forward chains of named types. If an underlying type is found, resolve
	 575  // the chain by setting the underlying type for each defined type in the
	 576  // chain before returning it. If no underlying type is found or a cycle
	 577  // is detected, the result is Typ[Invalid]. If a cycle is detected and
	 578  // n0.check != nil, the cycle is reported.
	 579  func (n0 *Named) under() Type {
	 580  	u := n0.underlying
	 581  
	 582  	if u == Typ[Invalid] {
	 583  		return u
	 584  	}
	 585  
	 586  	// If the underlying type of a defined type is not a defined
	 587  	// (incl. instance) type, then that is the desired underlying
	 588  	// type.
	 589  	switch u.(type) {
	 590  	case nil:
	 591  		return Typ[Invalid]
	 592  	default:
	 593  		// common case
	 594  		return u
	 595  	case *Named, *instance:
	 596  		// handled below
	 597  	}
	 598  
	 599  	if n0.check == nil {
	 600  		panic("internal error: Named.check == nil but type is incomplete")
	 601  	}
	 602  
	 603  	// Invariant: after this point n0 as well as any named types in its
	 604  	// underlying chain should be set up when this function exits.
	 605  	check := n0.check
	 606  
	 607  	// If we can't expand u at this point, it is invalid.
	 608  	n := asNamed(u)
	 609  	if n == nil {
	 610  		n0.underlying = Typ[Invalid]
	 611  		return n0.underlying
	 612  	}
	 613  
	 614  	// Otherwise, follow the forward chain.
	 615  	seen := map[*Named]int{n0: 0}
	 616  	path := []Object{n0.obj}
	 617  	for {
	 618  		u = n.underlying
	 619  		if u == nil {
	 620  			u = Typ[Invalid]
	 621  			break
	 622  		}
	 623  		var n1 *Named
	 624  		switch u1 := u.(type) {
	 625  		case *Named:
	 626  			n1 = u1
	 627  		case *instance:
	 628  			n1, _ = u1.expand().(*Named)
	 629  			if n1 == nil {
	 630  				u = Typ[Invalid]
	 631  			}
	 632  		}
	 633  		if n1 == nil {
	 634  			break // end of chain
	 635  		}
	 636  
	 637  		seen[n] = len(seen)
	 638  		path = append(path, n.obj)
	 639  		n = n1
	 640  
	 641  		if i, ok := seen[n]; ok {
	 642  			// cycle
	 643  			check.cycleError(path[i:])
	 644  			u = Typ[Invalid]
	 645  			break
	 646  		}
	 647  	}
	 648  
	 649  	for n := range seen {
	 650  		// We should never have to update the underlying type of an imported type;
	 651  		// those underlying types should have been resolved during the import.
	 652  		// Also, doing so would lead to a race condition (was issue #31749).
	 653  		// Do this check always, not just in debug mode (it's cheap).
	 654  		if n.obj.pkg != check.pkg {
	 655  			panic("internal error: imported type with unresolved underlying type")
	 656  		}
	 657  		n.underlying = u
	 658  	}
	 659  
	 660  	return u
	 661  }
	 662  
	 663  func (n *Named) setUnderlying(typ Type) {
	 664  	if n != nil {
	 665  		n.underlying = typ
	 666  	}
	 667  }
	 668  
	 669  func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *Named) {
	 670  	assert(obj.typ == nil)
	 671  
	 672  	check.later(func() {
	 673  		check.validType(obj.typ, nil)
	 674  	})
	 675  
	 676  	alias := tdecl.Assign.IsValid()
	 677  	if alias && typeparams.Get(tdecl) != nil {
	 678  		// The parser will ensure this but we may still get an invalid AST.
	 679  		// Complain and continue as regular type definition.
	 680  		check.error(atPos(tdecl.Assign), 0, "generic type cannot be alias")
	 681  		alias = false
	 682  	}
	 683  
	 684  	if alias {
	 685  		// type alias declaration
	 686  		if !check.allowVersion(check.pkg, 1, 9) {
	 687  			check.errorf(atPos(tdecl.Assign), _BadDecl, "type aliases requires go1.9 or later")
	 688  		}
	 689  
	 690  		obj.typ = Typ[Invalid]
	 691  		obj.typ = check.anyType(tdecl.Type)
	 692  
	 693  	} else {
	 694  		// defined type declaration
	 695  
	 696  		named := check.newNamed(obj, nil, nil)
	 697  		def.setUnderlying(named)
	 698  		obj.typ = named // make sure recursive type declarations terminate
	 699  
	 700  		if tparams := typeparams.Get(tdecl); tparams != nil {
	 701  			check.openScope(tdecl, "type parameters")
	 702  			defer check.closeScope()
	 703  			named.tparams = check.collectTypeParams(tparams)
	 704  		}
	 705  
	 706  		// determine underlying type of named
	 707  		named.orig = check.definedType(tdecl.Type, named)
	 708  
	 709  		// The underlying type of named may be itself a named type that is
	 710  		// incomplete:
	 711  		//
	 712  		//	type (
	 713  		//		A B
	 714  		//		B *C
	 715  		//		C A
	 716  		//	)
	 717  		//
	 718  		// The type of C is the (named) type of A which is incomplete,
	 719  		// and which has as its underlying type the named type B.
	 720  		// Determine the (final, unnamed) underlying type by resolving
	 721  		// any forward chain.
	 722  		// TODO(gri) Investigate if we can just use named.origin here
	 723  		//					 and rely on lazy computation of the underlying type.
	 724  		named.underlying = under(named)
	 725  	}
	 726  
	 727  }
	 728  
	 729  func (check *Checker) collectTypeParams(list *ast.FieldList) (tparams []*TypeName) {
	 730  	// Type parameter lists should not be empty. The parser will
	 731  	// complain but we still may get an incorrect AST: ignore it.
	 732  	if list.NumFields() == 0 {
	 733  		return
	 734  	}
	 735  
	 736  	// Declare type parameters up-front, with empty interface as type bound.
	 737  	// The scope of type parameters starts at the beginning of the type parameter
	 738  	// list (so we can have mutually recursive parameterized interfaces).
	 739  	for _, f := range list.List {
	 740  		tparams = check.declareTypeParams(tparams, f.Names)
	 741  	}
	 742  
	 743  	setBoundAt := func(at int, bound Type) {
	 744  		assert(IsInterface(bound))
	 745  		tparams[at].typ.(*_TypeParam).bound = bound
	 746  	}
	 747  
	 748  	index := 0
	 749  	var bound Type
	 750  	for _, f := range list.List {
	 751  		if f.Type == nil {
	 752  			goto next
	 753  		}
	 754  
	 755  		// The predeclared identifier "any" is visible only as a constraint
	 756  		// in a type parameter list. Look for it before general constraint
	 757  		// resolution.
	 758  		if tident, _ := unparen(f.Type).(*ast.Ident); tident != nil && tident.Name == "any" && check.lookup("any") == nil {
	 759  			bound = universeAny
	 760  		} else {
	 761  			bound = check.typ(f.Type)
	 762  		}
	 763  
	 764  		// type bound must be an interface
	 765  		// TODO(gri) We should delay the interface check because
	 766  		//					 we may not have a complete interface yet:
	 767  		//					 type C(type T C) interface {}
	 768  		//					 (issue #39724).
	 769  		if _, ok := under(bound).(*Interface); ok {
	 770  			// Otherwise, set the bound for each type parameter.
	 771  			for i := range f.Names {
	 772  				setBoundAt(index+i, bound)
	 773  			}
	 774  		} else if bound != Typ[Invalid] {
	 775  			check.errorf(f.Type, _Todo, "%s is not an interface", bound)
	 776  		}
	 777  
	 778  	next:
	 779  		index += len(f.Names)
	 780  	}
	 781  
	 782  	return
	 783  }
	 784  
	 785  func (check *Checker) declareTypeParams(tparams []*TypeName, names []*ast.Ident) []*TypeName {
	 786  	for _, name := range names {
	 787  		tpar := NewTypeName(name.Pos(), check.pkg, name.Name, nil)
	 788  		check.newTypeParam(tpar, len(tparams), &emptyInterface) // assigns type to tpar as a side-effect
	 789  		check.declare(check.scope, name, tpar, check.scope.pos) // TODO(gri) check scope position
	 790  		tparams = append(tparams, tpar)
	 791  	}
	 792  
	 793  	if trace && len(names) > 0 {
	 794  		check.trace(names[0].Pos(), "type params = %v", tparams[len(tparams)-len(names):])
	 795  	}
	 796  
	 797  	return tparams
	 798  }
	 799  
	 800  func (check *Checker) collectMethods(obj *TypeName) {
	 801  	// get associated methods
	 802  	// (Checker.collectObjects only collects methods with non-blank names;
	 803  	// Checker.resolveBaseTypeName ensures that obj is not an alias name
	 804  	// if it has attached methods.)
	 805  	methods := check.methods[obj]
	 806  	if methods == nil {
	 807  		return
	 808  	}
	 809  	delete(check.methods, obj)
	 810  	assert(!check.objMap[obj].tdecl.Assign.IsValid()) // don't use TypeName.IsAlias (requires fully set up object)
	 811  
	 812  	// use an objset to check for name conflicts
	 813  	var mset objset
	 814  
	 815  	// spec: "If the base type is a struct type, the non-blank method
	 816  	// and field names must be distinct."
	 817  	base := asNamed(obj.typ) // shouldn't fail but be conservative
	 818  	if base != nil {
	 819  		if t, _ := base.underlying.(*Struct); t != nil {
	 820  			for _, fld := range t.fields {
	 821  				if fld.name != "_" {
	 822  					assert(mset.insert(fld) == nil)
	 823  				}
	 824  			}
	 825  		}
	 826  
	 827  		// Checker.Files may be called multiple times; additional package files
	 828  		// may add methods to already type-checked types. Add pre-existing methods
	 829  		// so that we can detect redeclarations.
	 830  		for _, m := range base.methods {
	 831  			assert(m.name != "_")
	 832  			assert(mset.insert(m) == nil)
	 833  		}
	 834  	}
	 835  
	 836  	// add valid methods
	 837  	for _, m := range methods {
	 838  		// spec: "For a base type, the non-blank names of methods bound
	 839  		// to it must be unique."
	 840  		assert(m.name != "_")
	 841  		if alt := mset.insert(m); alt != nil {
	 842  			switch alt.(type) {
	 843  			case *Var:
	 844  				check.errorf(m, _DuplicateFieldAndMethod, "field and method with the same name %s", m.name)
	 845  			case *Func:
	 846  				check.errorf(m, _DuplicateMethod, "method %s already declared for %s", m.name, obj)
	 847  			default:
	 848  				unreachable()
	 849  			}
	 850  			check.reportAltDecl(alt)
	 851  			continue
	 852  		}
	 853  
	 854  		if base != nil {
	 855  			base.methods = append(base.methods, m)
	 856  		}
	 857  	}
	 858  }
	 859  
	 860  func (check *Checker) funcDecl(obj *Func, decl *declInfo) {
	 861  	assert(obj.typ == nil)
	 862  
	 863  	// func declarations cannot use iota
	 864  	assert(check.iota == nil)
	 865  
	 866  	sig := new(Signature)
	 867  	obj.typ = sig // guard against cycles
	 868  
	 869  	// Avoid cycle error when referring to method while type-checking the signature.
	 870  	// This avoids a nuisance in the best case (non-parameterized receiver type) and
	 871  	// since the method is not a type, we get an error. If we have a parameterized
	 872  	// receiver type, instantiating the receiver type leads to the instantiation of
	 873  	// its methods, and we don't want a cycle error in that case.
	 874  	// TODO(gri) review if this is correct and/or whether we still need this?
	 875  	saved := obj.color_
	 876  	obj.color_ = black
	 877  	fdecl := decl.fdecl
	 878  	check.funcType(sig, fdecl.Recv, fdecl.Type)
	 879  	obj.color_ = saved
	 880  
	 881  	// function body must be type-checked after global declarations
	 882  	// (functions implemented elsewhere have no body)
	 883  	if !check.conf.IgnoreFuncBodies && fdecl.Body != nil {
	 884  		check.later(func() {
	 885  			check.funcBody(decl, obj.name, sig, fdecl.Body, nil)
	 886  		})
	 887  	}
	 888  }
	 889  
	 890  func (check *Checker) declStmt(d ast.Decl) {
	 891  	pkg := check.pkg
	 892  
	 893  	check.walkDecl(d, func(d decl) {
	 894  		switch d := d.(type) {
	 895  		case constDecl:
	 896  			top := len(check.delayed)
	 897  
	 898  			// declare all constants
	 899  			lhs := make([]*Const, len(d.spec.Names))
	 900  			for i, name := range d.spec.Names {
	 901  				obj := NewConst(name.Pos(), pkg, name.Name, nil, constant.MakeInt64(int64(d.iota)))
	 902  				lhs[i] = obj
	 903  
	 904  				var init ast.Expr
	 905  				if i < len(d.init) {
	 906  					init = d.init[i]
	 907  				}
	 908  
	 909  				check.constDecl(obj, d.typ, init, d.inherited)
	 910  			}
	 911  
	 912  			// process function literals in init expressions before scope changes
	 913  			check.processDelayed(top)
	 914  
	 915  			// spec: "The scope of a constant or variable identifier declared
	 916  			// inside a function begins at the end of the ConstSpec or VarSpec
	 917  			// (ShortVarDecl for short variable declarations) and ends at the
	 918  			// end of the innermost containing block."
	 919  			scopePos := d.spec.End()
	 920  			for i, name := range d.spec.Names {
	 921  				check.declare(check.scope, name, lhs[i], scopePos)
	 922  			}
	 923  
	 924  		case varDecl:
	 925  			top := len(check.delayed)
	 926  
	 927  			lhs0 := make([]*Var, len(d.spec.Names))
	 928  			for i, name := range d.spec.Names {
	 929  				lhs0[i] = NewVar(name.Pos(), pkg, name.Name, nil)
	 930  			}
	 931  
	 932  			// initialize all variables
	 933  			for i, obj := range lhs0 {
	 934  				var lhs []*Var
	 935  				var init ast.Expr
	 936  				switch len(d.spec.Values) {
	 937  				case len(d.spec.Names):
	 938  					// lhs and rhs match
	 939  					init = d.spec.Values[i]
	 940  				case 1:
	 941  					// rhs is expected to be a multi-valued expression
	 942  					lhs = lhs0
	 943  					init = d.spec.Values[0]
	 944  				default:
	 945  					if i < len(d.spec.Values) {
	 946  						init = d.spec.Values[i]
	 947  					}
	 948  				}
	 949  				check.varDecl(obj, lhs, d.spec.Type, init)
	 950  				if len(d.spec.Values) == 1 {
	 951  					// If we have a single lhs variable we are done either way.
	 952  					// If we have a single rhs expression, it must be a multi-
	 953  					// valued expression, in which case handling the first lhs
	 954  					// variable will cause all lhs variables to have a type
	 955  					// assigned, and we are done as well.
	 956  					if debug {
	 957  						for _, obj := range lhs0 {
	 958  							assert(obj.typ != nil)
	 959  						}
	 960  					}
	 961  					break
	 962  				}
	 963  			}
	 964  
	 965  			// process function literals in init expressions before scope changes
	 966  			check.processDelayed(top)
	 967  
	 968  			// declare all variables
	 969  			// (only at this point are the variable scopes (parents) set)
	 970  			scopePos := d.spec.End() // see constant declarations
	 971  			for i, name := range d.spec.Names {
	 972  				// see constant declarations
	 973  				check.declare(check.scope, name, lhs0[i], scopePos)
	 974  			}
	 975  
	 976  		case typeDecl:
	 977  			obj := NewTypeName(d.spec.Name.Pos(), pkg, d.spec.Name.Name, nil)
	 978  			// spec: "The scope of a type identifier declared inside a function
	 979  			// begins at the identifier in the TypeSpec and ends at the end of
	 980  			// the innermost containing block."
	 981  			scopePos := d.spec.Name.Pos()
	 982  			check.declare(check.scope, d.spec.Name, obj, scopePos)
	 983  			// mark and unmark type before calling typeDecl; its type is still nil (see Checker.objDecl)
	 984  			obj.setColor(grey + color(check.push(obj)))
	 985  			check.typeDecl(obj, d.spec, nil)
	 986  			check.pop().setColor(black)
	 987  		default:
	 988  			check.invalidAST(d.node(), "unknown ast.Decl node %T", d.node())
	 989  		}
	 990  	})
	 991  }
	 992  

View as plain text