Source file src/cmd/compile/internal/types2/check.go

     1  // Copyright 2011 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  // This file implements the Check function, which drives type-checking.
     6  
     7  package types2
     8  
     9  import (
    10  	"cmd/compile/internal/syntax"
    11  	"fmt"
    12  	"go/constant"
    13  	. "internal/types/errors"
    14  	"os"
    15  )
    16  
    17  // nopos indicates an unknown position
    18  var nopos syntax.Pos
    19  
    20  // debugging/development support
    21  const debug = false // leave on during development
    22  
    23  // position tracing for panics during type checking
    24  const tracePos = true
    25  
    26  // exprInfo stores information about an untyped expression.
    27  type exprInfo struct {
    28  	isLhs bool // expression is lhs operand of a shift with delayed type-check
    29  	mode  operandMode
    30  	typ   *Basic
    31  	val   constant.Value // constant value; or nil (if not a constant)
    32  }
    33  
    34  // An environment represents the environment within which an object is
    35  // type-checked.
    36  type environment struct {
    37  	decl          *declInfo                 // package-level declaration whose init expression/function body is checked
    38  	scope         *Scope                    // top-most scope for lookups
    39  	version       goVersion                 // current accepted language version; changes across files
    40  	iota          constant.Value            // value of iota in a constant declaration; nil otherwise
    41  	errpos        syntax.Pos                // if valid, identifier position of a constant with inherited initializer
    42  	inTParamList  bool                      // set if inside a type parameter list
    43  	sig           *Signature                // function signature if inside a function; nil otherwise
    44  	isPanic       map[*syntax.CallExpr]bool // set of panic call expressions (used for termination check)
    45  	hasLabel      bool                      // set if a function makes use of labels (only ~1% of functions); unused outside functions
    46  	hasCallOrRecv bool                      // set if an expression contains a function call or channel receive operation
    47  }
    48  
    49  // lookupScope looks up name in the current environment and if an object
    50  // is found it returns the scope containing the object and the object.
    51  // Otherwise it returns (nil, nil).
    52  //
    53  // Note that obj.Parent() may be different from the returned scope if the
    54  // object was inserted into the scope and already had a parent at that
    55  // time (see Scope.Insert). This can only happen for dot-imported objects
    56  // whose parent is the scope of the package that exported them.
    57  func (env *environment) lookupScope(name string) (*Scope, Object) {
    58  	for s := env.scope; s != nil; s = s.parent {
    59  		if obj := s.Lookup(name); obj != nil {
    60  			return s, obj
    61  		}
    62  	}
    63  	return nil, nil
    64  }
    65  
    66  // lookup is like lookupScope but it only returns the object (or nil).
    67  func (env *environment) lookup(name string) Object {
    68  	_, obj := env.lookupScope(name)
    69  	return obj
    70  }
    71  
    72  // An importKey identifies an imported package by import path and source directory
    73  // (directory containing the file containing the import). In practice, the directory
    74  // may always be the same, or may not matter. Given an (import path, directory), an
    75  // importer must always return the same package (but given two different import paths,
    76  // an importer may still return the same package by mapping them to the same package
    77  // paths).
    78  type importKey struct {
    79  	path, dir string
    80  }
    81  
    82  // A dotImportKey describes a dot-imported object in the given scope.
    83  type dotImportKey struct {
    84  	scope *Scope
    85  	name  string
    86  }
    87  
    88  // An action describes a (delayed) action.
    89  type action struct {
    90  	version goVersion   // applicable language version
    91  	f       func()      // action to be executed
    92  	desc    *actionDesc // action description; may be nil, requires debug to be set
    93  }
    94  
    95  // If debug is set, describef sets a printf-formatted description for action a.
    96  // Otherwise, it is a no-op.
    97  func (a *action) describef(pos poser, format string, args ...any) {
    98  	if debug {
    99  		a.desc = &actionDesc{pos, format, args}
   100  	}
   101  }
   102  
   103  // An actionDesc provides information on an action.
   104  // For debugging only.
   105  type actionDesc struct {
   106  	pos    poser
   107  	format string
   108  	args   []any
   109  }
   110  
   111  // A Checker maintains the state of the type checker.
   112  // It must be created with NewChecker.
   113  type Checker struct {
   114  	// package information
   115  	// (initialized by NewChecker, valid for the life-time of checker)
   116  	conf *Config
   117  	ctxt *Context // context for de-duplicating instances
   118  	pkg  *Package
   119  	*Info
   120  	nextID  uint64                 // unique Id for type parameters (first valid Id is 1)
   121  	objMap  map[Object]*declInfo   // maps package-level objects and (non-interface) methods to declaration info
   122  	objList []Object               // source-ordered keys of objMap
   123  	impMap  map[importKey]*Package // maps (import path, source directory) to (complete or fake) package
   124  	// see TODO in validtype.go
   125  	// valids  instanceLookup      // valid *Named (incl. instantiated) types per the validType check
   126  
   127  	// pkgPathMap maps package names to the set of distinct import paths we've
   128  	// seen for that name, anywhere in the import graph. It is used for
   129  	// disambiguating package names in error messages.
   130  	//
   131  	// pkgPathMap is allocated lazily, so that we don't pay the price of building
   132  	// it on the happy path. seenPkgMap tracks the packages that we've already
   133  	// walked.
   134  	pkgPathMap map[string]map[string]bool
   135  	seenPkgMap map[*Package]bool
   136  
   137  	// information collected during type-checking of a set of package files
   138  	// (initialized by Files, valid only for the duration of check.Files;
   139  	// maps and lists are allocated on demand)
   140  	files         []*syntax.File             // list of package files
   141  	versions      map[*syntax.PosBase]string // maps files to version strings (each file has an entry); shared with Info.FileVersions if present; may be unaltered Config.GoVersion
   142  	imports       []*PkgName                 // list of imported packages
   143  	dotImportMap  map[dotImportKey]*PkgName  // maps dot-imported objects to the package they were dot-imported through
   144  	brokenAliases map[*TypeName]bool         // set of aliases with broken (not yet determined) types
   145  	unionTypeSets map[*Union]*_TypeSet       // computed type sets for union types
   146  	usedVars      map[*Var]bool              // set of used variables
   147  	usedPkgNames  map[*PkgName]bool          // set of used package names
   148  	mono          monoGraph                  // graph for detecting non-monomorphizable instantiation loops
   149  
   150  	firstErr   error                    // first error encountered
   151  	methods    map[*TypeName][]*Func    // maps package scope type names to associated non-blank (non-interface) methods
   152  	untyped    map[syntax.Expr]exprInfo // map of expressions without final type
   153  	delayed    []action                 // stack of delayed action segments; segments are processed in FIFO order
   154  	objPath    []Object                 // path of object dependencies during type-checking (for cycle reporting)
   155  	objPathIdx map[Object]int           // map of object to object path index during type-checking (for cycle reporting)
   156  	cleaners   []cleaner                // list of types that may need a final cleanup at the end of type-checking
   157  
   158  	// environment within which the current object is type-checked (valid only
   159  	// for the duration of type-checking a specific object)
   160  	environment
   161  
   162  	// debugging
   163  	posStack []syntax.Pos // stack of source positions seen; used for panic tracing
   164  	indent   int          // indentation for tracing
   165  }
   166  
   167  // addDeclDep adds the dependency edge (check.decl -> to) if check.decl exists
   168  func (check *Checker) addDeclDep(to Object) {
   169  	from := check.decl
   170  	if from == nil {
   171  		return // not in a package-level init expression
   172  	}
   173  	if _, found := check.objMap[to]; !found {
   174  		return // to is not a package-level object
   175  	}
   176  	from.addDep(to)
   177  }
   178  
   179  func (check *Checker) rememberUntyped(e syntax.Expr, lhs bool, mode operandMode, typ *Basic, val constant.Value) {
   180  	m := check.untyped
   181  	if m == nil {
   182  		m = make(map[syntax.Expr]exprInfo)
   183  		check.untyped = m
   184  	}
   185  	m[e] = exprInfo{lhs, mode, typ, val}
   186  }
   187  
   188  // later pushes f on to the stack of actions that will be processed later;
   189  // either at the end of the current statement, or in case of a local constant
   190  // or variable declaration, before the constant or variable is in scope
   191  // (so that f still sees the scope before any new declarations).
   192  // later returns the pushed action so one can provide a description
   193  // via action.describef for debugging, if desired.
   194  func (check *Checker) later(f func()) *action {
   195  	i := len(check.delayed)
   196  	check.delayed = append(check.delayed, action{version: check.version, f: f})
   197  	return &check.delayed[i]
   198  }
   199  
   200  // push pushes obj onto the object path and records its index in the path index map.
   201  func (check *Checker) push(obj Object) {
   202  	if check.objPathIdx == nil {
   203  		check.objPathIdx = make(map[Object]int)
   204  	}
   205  	check.objPathIdx[obj] = len(check.objPath)
   206  	check.objPath = append(check.objPath, obj)
   207  }
   208  
   209  // pop pops an object from the object path and removes it from the path index map.
   210  func (check *Checker) pop() {
   211  	i := len(check.objPath) - 1
   212  	obj := check.objPath[i]
   213  	check.objPath[i] = nil // help the garbage collector
   214  	check.objPath = check.objPath[:i]
   215  	delete(check.objPathIdx, obj)
   216  }
   217  
   218  type cleaner interface {
   219  	cleanup()
   220  }
   221  
   222  // needsCleanup records objects/types that implement the cleanup method
   223  // which will be called at the end of type-checking.
   224  func (check *Checker) needsCleanup(c cleaner) {
   225  	check.cleaners = append(check.cleaners, c)
   226  }
   227  
   228  // NewChecker returns a new Checker instance for a given package.
   229  // Package files may be added incrementally via checker.Files.
   230  func NewChecker(conf *Config, pkg *Package, info *Info) *Checker {
   231  	// make sure we have a configuration
   232  	if conf == nil {
   233  		conf = new(Config)
   234  	}
   235  
   236  	// make sure we have an info struct
   237  	if info == nil {
   238  		info = new(Info)
   239  	}
   240  
   241  	// Note: clients may call NewChecker with the Unsafe package, which is
   242  	// globally shared and must not be mutated. Therefore NewChecker must not
   243  	// mutate *pkg.
   244  	//
   245  	// (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
   246  
   247  	return &Checker{
   248  		conf:         conf,
   249  		ctxt:         conf.Context,
   250  		pkg:          pkg,
   251  		Info:         info,
   252  		objMap:       make(map[Object]*declInfo),
   253  		impMap:       make(map[importKey]*Package),
   254  		usedVars:     make(map[*Var]bool),
   255  		usedPkgNames: make(map[*PkgName]bool),
   256  	}
   257  }
   258  
   259  // initFiles initializes the files-specific portion of checker.
   260  // The provided files must all belong to the same package.
   261  func (check *Checker) initFiles(files []*syntax.File) {
   262  	// start with a clean slate (check.Files may be called multiple times)
   263  	// TODO(gri): what determines which fields are zeroed out here, vs at the end
   264  	// of checkFiles?
   265  	check.files = nil
   266  	check.imports = nil
   267  	check.dotImportMap = nil
   268  
   269  	check.firstErr = nil
   270  	check.methods = nil
   271  	check.untyped = nil
   272  	check.delayed = nil
   273  	check.objPath = nil
   274  	check.objPathIdx = nil
   275  	check.cleaners = nil
   276  
   277  	// We must initialize usedVars and usedPkgNames both here and in NewChecker,
   278  	// because initFiles is not called in the CheckExpr or Eval codepaths, yet we
   279  	// want to free this memory at the end of Files ('used' predicates are
   280  	// only needed in the context of a given file).
   281  	check.usedVars = make(map[*Var]bool)
   282  	check.usedPkgNames = make(map[*PkgName]bool)
   283  
   284  	// determine package name and collect valid files
   285  	pkg := check.pkg
   286  	for _, file := range files {
   287  		switch name := file.PkgName.Value; pkg.name {
   288  		case "":
   289  			if name != "_" {
   290  				pkg.name = name
   291  			} else {
   292  				check.error(file.PkgName, BlankPkgName, "invalid package name _")
   293  			}
   294  			fallthrough
   295  
   296  		case name:
   297  			check.files = append(check.files, file)
   298  
   299  		default:
   300  			check.errorf(file, MismatchedPkgName, "package %s; expected package %s", name, pkg.name)
   301  			// ignore this file
   302  		}
   303  	}
   304  
   305  	// reuse Info.FileVersions if provided
   306  	versions := check.Info.FileVersions
   307  	if versions == nil {
   308  		versions = make(map[*syntax.PosBase]string)
   309  	}
   310  	check.versions = versions
   311  
   312  	pkgVersion := asGoVersion(check.conf.GoVersion)
   313  	if pkgVersion.isValid() && len(files) > 0 && pkgVersion.cmp(go_current) > 0 {
   314  		check.errorf(files[0], TooNew, "package requires newer Go version %v (application built with %v)",
   315  			pkgVersion, go_current)
   316  	}
   317  
   318  	// determine Go version for each file
   319  	for _, file := range check.files {
   320  		// use unaltered Config.GoVersion by default
   321  		// (This version string may contain dot-release numbers as in go1.20.1,
   322  		// unlike file versions which are Go language versions only, if valid.)
   323  		v := check.conf.GoVersion
   324  
   325  		// If the file specifies a version, use max(fileVersion, go1.21).
   326  		if fileVersion := asGoVersion(file.GoVersion); fileVersion.isValid() {
   327  			// Go 1.21 introduced the feature of allowing //go:build lines
   328  			// to sometimes set the Go version in a given file. Versions Go 1.21 and later
   329  			// can be set backwards compatibly as that was the first version
   330  			// files with go1.21 or later build tags could be built with.
   331  			//
   332  			// Set the version to max(fileVersion, go1.21): That will allow a
   333  			// downgrade to a version before go1.22, where the for loop semantics
   334  			// change was made, while being backwards compatible with versions of
   335  			// go before the new //go:build semantics were introduced.
   336  			v = string(versionMax(fileVersion, go1_21))
   337  
   338  			// Report a specific error for each tagged file that's too new.
   339  			// (Normally the build system will have filtered files by version,
   340  			// but clients can present arbitrary files to the type checker.)
   341  			if fileVersion.cmp(go_current) > 0 {
   342  				// Use position of 'package [p]' for types/types2 consistency.
   343  				// (Ideally we would use the //build tag itself.)
   344  				check.errorf(file.PkgName, TooNew, "file requires newer Go version %v", fileVersion)
   345  			}
   346  		}
   347  		versions[file.Pos().FileBase()] = v // file.Pos().FileBase() may be nil for tests
   348  	}
   349  }
   350  
   351  func versionMax(a, b goVersion) goVersion {
   352  	if a.cmp(b) > 0 {
   353  		return a
   354  	}
   355  	return b
   356  }
   357  
   358  // pushPos pushes pos onto the pos stack.
   359  func (check *Checker) pushPos(pos syntax.Pos) {
   360  	check.posStack = append(check.posStack, pos)
   361  }
   362  
   363  // popPos pops from the pos stack.
   364  func (check *Checker) popPos() {
   365  	check.posStack = check.posStack[:len(check.posStack)-1]
   366  }
   367  
   368  // A bailout panic is used for early termination.
   369  type bailout struct{}
   370  
   371  func (check *Checker) handleBailout(err *error) {
   372  	switch p := recover().(type) {
   373  	case nil, bailout:
   374  		// normal return or early exit
   375  		*err = check.firstErr
   376  	default:
   377  		if len(check.posStack) > 0 {
   378  			doPrint := func(ps []syntax.Pos) {
   379  				for i := len(ps) - 1; i >= 0; i-- {
   380  					fmt.Fprintf(os.Stderr, "\t%v\n", ps[i])
   381  				}
   382  			}
   383  
   384  			fmt.Fprintln(os.Stderr, "The following panic happened checking types near:")
   385  			if len(check.posStack) <= 10 {
   386  				doPrint(check.posStack)
   387  			} else {
   388  				// if it's long, truncate the middle; it's least likely to help
   389  				doPrint(check.posStack[len(check.posStack)-5:])
   390  				fmt.Fprintln(os.Stderr, "\t...")
   391  				doPrint(check.posStack[:5])
   392  			}
   393  		}
   394  
   395  		// re-panic
   396  		panic(p)
   397  	}
   398  }
   399  
   400  // Files checks the provided files as part of the checker's package.
   401  func (check *Checker) Files(files []*syntax.File) (err error) {
   402  	if check.pkg == Unsafe {
   403  		// Defensive handling for Unsafe, which cannot be type checked, and must
   404  		// not be mutated. See https://go.dev/issue/61212 for an example of where
   405  		// Unsafe is passed to NewChecker.
   406  		return nil
   407  	}
   408  
   409  	// Avoid early returns here! Nearly all errors can be
   410  	// localized to a piece of syntax and needn't prevent
   411  	// type-checking of the rest of the package.
   412  
   413  	defer check.handleBailout(&err)
   414  	check.checkFiles(files)
   415  	return
   416  }
   417  
   418  // checkFiles type-checks the specified files. Errors are reported as
   419  // a side effect, not by returning early, to ensure that well-formed
   420  // syntax is properly type annotated even in a package containing
   421  // errors.
   422  func (check *Checker) checkFiles(files []*syntax.File) {
   423  	print := func(msg string) {
   424  		if check.conf.Trace {
   425  			fmt.Println()
   426  			fmt.Println(msg)
   427  		}
   428  	}
   429  
   430  	print("== initFiles ==")
   431  	check.initFiles(files)
   432  
   433  	print("== collectObjects ==")
   434  	check.collectObjects()
   435  
   436  	print("== sortObjects ==")
   437  	check.sortObjects()
   438  
   439  	print("== directCycles ==")
   440  	check.directCycles()
   441  
   442  	print("== packageObjects ==")
   443  	check.packageObjects()
   444  
   445  	print("== processDelayed ==")
   446  	check.processDelayed(0) // incl. all functions
   447  
   448  	print("== cleanup ==")
   449  	check.cleanup()
   450  
   451  	print("== initOrder ==")
   452  	check.initOrder()
   453  
   454  	if !check.conf.DisableUnusedImportCheck {
   455  		print("== unusedImports ==")
   456  		check.unusedImports()
   457  	}
   458  
   459  	print("== recordUntyped ==")
   460  	check.recordUntyped()
   461  
   462  	if check.firstErr == nil {
   463  		// TODO(mdempsky): Ensure monomorph is safe when errors exist.
   464  		check.monomorph()
   465  	}
   466  
   467  	check.pkg.goVersion = check.conf.GoVersion
   468  	check.pkg.complete = true
   469  
   470  	// no longer needed - release memory
   471  	check.imports = nil
   472  	check.dotImportMap = nil
   473  	check.pkgPathMap = nil
   474  	check.seenPkgMap = nil
   475  	check.brokenAliases = nil
   476  	check.unionTypeSets = nil
   477  	check.usedVars = nil
   478  	check.usedPkgNames = nil
   479  	check.ctxt = nil
   480  
   481  	// TODO(gri): shouldn't the cleanup above occur after the bailout?
   482  	// TODO(gri) There's more memory we should release at this point.
   483  }
   484  
   485  // processDelayed processes all delayed actions pushed after top.
   486  func (check *Checker) processDelayed(top int) {
   487  	// If each delayed action pushes a new action, the
   488  	// stack will continue to grow during this loop.
   489  	// However, it is only processing functions (which
   490  	// are processed in a delayed fashion) that may
   491  	// add more actions (such as nested functions), so
   492  	// this is a sufficiently bounded process.
   493  	savedVersion := check.version
   494  	for i := top; i < len(check.delayed); i++ {
   495  		a := &check.delayed[i]
   496  		if check.conf.Trace {
   497  			if a.desc != nil {
   498  				check.trace(a.desc.pos.Pos(), "-- "+a.desc.format, a.desc.args...)
   499  			} else {
   500  				check.trace(nopos, "-- delayed %p", a.f)
   501  			}
   502  		}
   503  		check.version = a.version // reestablish the effective Go version captured earlier
   504  		a.f()                     // may append to check.delayed
   505  		if check.conf.Trace {
   506  			fmt.Println()
   507  		}
   508  	}
   509  	assert(top <= len(check.delayed)) // stack must not have shrunk
   510  	check.delayed = check.delayed[:top]
   511  	check.version = savedVersion
   512  }
   513  
   514  // cleanup runs cleanup for all collected cleaners.
   515  func (check *Checker) cleanup() {
   516  	// Don't use a range clause since Named.cleanup may add more cleaners.
   517  	for i := 0; i < len(check.cleaners); i++ {
   518  		check.cleaners[i].cleanup()
   519  	}
   520  	check.cleaners = nil
   521  }
   522  
   523  // types2-specific support for recording type information in the syntax tree.
   524  func (check *Checker) recordTypeAndValueInSyntax(x syntax.Expr, mode operandMode, typ Type, val constant.Value) {
   525  	if check.StoreTypesInSyntax {
   526  		tv := TypeAndValue{mode, typ, val}
   527  		stv := syntax.TypeAndValue{Type: typ, Value: val}
   528  		if tv.IsVoid() {
   529  			stv.SetIsVoid()
   530  		}
   531  		if tv.IsType() {
   532  			stv.SetIsType()
   533  		}
   534  		if tv.IsBuiltin() {
   535  			stv.SetIsBuiltin()
   536  		}
   537  		if tv.IsValue() {
   538  			stv.SetIsValue()
   539  		}
   540  		if tv.IsNil() {
   541  			stv.SetIsNil()
   542  		}
   543  		if tv.Addressable() {
   544  			stv.SetAddressable()
   545  		}
   546  		if tv.Assignable() {
   547  			stv.SetAssignable()
   548  		}
   549  		if tv.HasOk() {
   550  			stv.SetHasOk()
   551  		}
   552  		x.SetTypeInfo(stv)
   553  	}
   554  }
   555  
   556  // types2-specific support for recording type information in the syntax tree.
   557  func (check *Checker) recordCommaOkTypesInSyntax(x syntax.Expr, t0, t1 Type) {
   558  	if check.StoreTypesInSyntax {
   559  		// Note: this loop is duplicated because the type of tv is different.
   560  		// Above it is types2.TypeAndValue, here it is syntax.TypeAndValue.
   561  		for {
   562  			tv := x.GetTypeInfo()
   563  			assert(tv.Type != nil) // should have been recorded already
   564  			pos := x.Pos()
   565  			tv.Type = NewTuple(
   566  				NewParam(pos, check.pkg, "", t0),
   567  				NewParam(pos, check.pkg, "", t1),
   568  			)
   569  			x.SetTypeInfo(tv)
   570  			p, _ := x.(*syntax.ParenExpr)
   571  			if p == nil {
   572  				break
   573  			}
   574  			x = p.X
   575  		}
   576  	}
   577  }
   578  
   579  // instantiatedIdent determines the identifier of the type instantiated in expr.
   580  // Helper function for recordInstance in recording.go.
   581  func instantiatedIdent(expr syntax.Expr) *syntax.Name {
   582  	var selOrIdent syntax.Expr
   583  	switch e := expr.(type) {
   584  	case *syntax.IndexExpr:
   585  		selOrIdent = e.X
   586  	case *syntax.SelectorExpr, *syntax.Name:
   587  		selOrIdent = e
   588  	}
   589  	switch x := selOrIdent.(type) {
   590  	case *syntax.Name:
   591  		return x
   592  	case *syntax.SelectorExpr:
   593  		return x.Sel
   594  	}
   595  
   596  	// extra debugging of go.dev/issue/63933
   597  	panic(sprintf(nil, true, "instantiated ident not found; please report: %s", expr))
   598  }
   599  

View as plain text