Source file src/go/types/literals.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  // Source: ../../cmd/compile/internal/types2/literals.go
     3  
     4  // Copyright 2024 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  // This file implements typechecking of literals.
     9  
    10  package types
    11  
    12  import (
    13  	"go/ast"
    14  	"go/token"
    15  	. "internal/types/errors"
    16  	"strings"
    17  )
    18  
    19  // langCompat reports an error if the representation of a numeric
    20  // literal is not compatible with the current language version.
    21  func (check *Checker) langCompat(lit *ast.BasicLit) {
    22  	s := lit.Value
    23  	if len(s) <= 2 || check.allowVersion(go1_13) {
    24  		return
    25  	}
    26  	// len(s) > 2
    27  	if strings.Contains(s, "_") {
    28  		check.versionErrorf(lit, go1_13, "underscore in numeric literal")
    29  		return
    30  	}
    31  	if s[0] != '0' {
    32  		return
    33  	}
    34  	radix := s[1]
    35  	if radix == 'b' || radix == 'B' {
    36  		check.versionErrorf(lit, go1_13, "binary literal")
    37  		return
    38  	}
    39  	if radix == 'o' || radix == 'O' {
    40  		check.versionErrorf(lit, go1_13, "0o/0O-style octal literal")
    41  		return
    42  	}
    43  	if lit.Kind != token.INT && (radix == 'x' || radix == 'X') {
    44  		check.versionErrorf(lit, go1_13, "hexadecimal floating-point literal")
    45  	}
    46  }
    47  
    48  func (check *Checker) basicLit(x *operand, e *ast.BasicLit) {
    49  	switch e.Kind {
    50  	case token.INT, token.FLOAT, token.IMAG:
    51  		check.langCompat(e)
    52  		// The max. mantissa precision for untyped numeric values
    53  		// is 512 bits, or 4048 bits for each of the two integer
    54  		// parts of a fraction for floating-point numbers that are
    55  		// represented accurately in the go/constant package.
    56  		// Constant literals that are longer than this many bits
    57  		// are not meaningful; and excessively long constants may
    58  		// consume a lot of space and time for a useless conversion.
    59  		// Cap constant length with a generous upper limit that also
    60  		// allows for separators between all digits.
    61  		const limit = 10000
    62  		if len(e.Value) > limit {
    63  			check.errorf(e, InvalidConstVal, "excessively long constant: %s... (%d chars)", e.Value[:10], len(e.Value))
    64  			x.mode = invalid
    65  			return
    66  		}
    67  	}
    68  	x.setConst(e.Kind, e.Value)
    69  	if x.mode == invalid {
    70  		// The parser already establishes syntactic correctness.
    71  		// If we reach here it's because of number under-/overflow.
    72  		// TODO(gri) setConst (and in turn the go/constant package)
    73  		// should return an error describing the issue.
    74  		check.errorf(e, InvalidConstVal, "malformed constant: %s", e.Value)
    75  		x.mode = invalid
    76  		return
    77  	}
    78  	// Ensure that integer values don't overflow (go.dev/issue/54280).
    79  	x.expr = e // make sure that check.overflow below has an error position
    80  	check.overflow(x, opPos(x.expr))
    81  }
    82  
    83  func (check *Checker) funcLit(x *operand, e *ast.FuncLit) {
    84  	if sig, ok := check.typ(e.Type).(*Signature); ok {
    85  		// Set the Scope's extent to the complete "func (...) {...}"
    86  		// so that Scope.Innermost works correctly.
    87  		sig.scope.pos = e.Pos()
    88  		sig.scope.end = endPos(e)
    89  		if !check.conf.IgnoreFuncBodies && e.Body != nil {
    90  			// Anonymous functions are considered part of the
    91  			// init expression/func declaration which contains
    92  			// them: use existing package-level declaration info.
    93  			decl := check.decl // capture for use in closure below
    94  			iota := check.iota // capture for use in closure below (go.dev/issue/22345)
    95  			// Don't type-check right away because the function may
    96  			// be part of a type definition to which the function
    97  			// body refers. Instead, type-check as soon as possible,
    98  			// but before the enclosing scope contents changes (go.dev/issue/22992).
    99  			check.later(func() {
   100  				check.funcBody(decl, "<function literal>", sig, e.Body, iota)
   101  			}).describef(e, "func literal")
   102  		}
   103  		x.mode = value
   104  		x.typ = sig
   105  	} else {
   106  		check.errorf(e, InvalidSyntaxTree, "invalid function literal %v", e)
   107  		x.mode = invalid
   108  	}
   109  }
   110  
   111  func (check *Checker) compositeLit(x *operand, e *ast.CompositeLit, hint Type) {
   112  	var typ, base Type
   113  	var isElem bool // true if composite literal is an element of an enclosing composite literal
   114  
   115  	switch {
   116  	case e.Type != nil:
   117  		// composite literal type present - use it
   118  		// [...]T array types may only appear with composite literals.
   119  		// Check for them here so we don't have to handle ... in general.
   120  		if atyp, _ := e.Type.(*ast.ArrayType); atyp != nil && isdddArray(atyp) {
   121  			// We have an "open" [...]T array type.
   122  			// Create a new ArrayType with unknown length (-1)
   123  			// and finish setting it up after analyzing the literal.
   124  			typ = &Array{len: -1, elem: check.varType(atyp.Elt)}
   125  			base = typ
   126  			break
   127  		}
   128  		typ = check.typ(e.Type)
   129  		base = typ
   130  
   131  	case hint != nil:
   132  		// no composite literal type present - use hint (element type of enclosing type)
   133  		typ = hint
   134  		base = typ
   135  		// *T implies &T{}
   136  		u, _ := commonUnder(base, nil)
   137  		if b, ok := deref(u); ok {
   138  			base = b
   139  		}
   140  		isElem = true
   141  
   142  	default:
   143  		// TODO(gri) provide better error messages depending on context
   144  		check.error(e, UntypedLit, "missing type in composite literal")
   145  		// continue with invalid type so that elements are "used" (go.dev/issue/69092)
   146  		typ = Typ[Invalid]
   147  		base = typ
   148  	}
   149  
   150  	switch u, _ := commonUnder(base, nil); utyp := u.(type) {
   151  	case *Struct:
   152  		if len(e.Elts) == 0 {
   153  			break
   154  		}
   155  		// Convention for error messages on invalid struct literals:
   156  		// we mention the struct type only if it clarifies the error
   157  		// (e.g., a duplicate field error doesn't need the struct type).
   158  		fields := utyp.fields
   159  		if _, ok := e.Elts[0].(*ast.KeyValueExpr); ok {
   160  			// all elements must have keys
   161  			visited := make([]bool, len(fields))
   162  			for _, e := range e.Elts {
   163  				kv, _ := e.(*ast.KeyValueExpr)
   164  				if kv == nil {
   165  					check.error(e, MixedStructLit, "mixture of field:value and value elements in struct literal")
   166  					continue
   167  				}
   168  				key, _ := kv.Key.(*ast.Ident)
   169  				// do all possible checks early (before exiting due to errors)
   170  				// so we don't drop information on the floor
   171  				check.expr(nil, x, kv.Value)
   172  				if key == nil {
   173  					check.errorf(kv, InvalidLitField, "invalid field name %s in struct literal", kv.Key)
   174  					continue
   175  				}
   176  				i := fieldIndex(fields, check.pkg, key.Name, false)
   177  				if i < 0 {
   178  					var alt Object
   179  					if j := fieldIndex(fields, check.pkg, key.Name, true); j >= 0 {
   180  						alt = fields[j]
   181  					}
   182  					msg := check.lookupError(base, key.Name, alt, true)
   183  					check.error(kv.Key, MissingLitField, msg)
   184  					continue
   185  				}
   186  				fld := fields[i]
   187  				check.recordUse(key, fld)
   188  				etyp := fld.typ
   189  				check.assignment(x, etyp, "struct literal")
   190  				// 0 <= i < len(fields)
   191  				if visited[i] {
   192  					check.errorf(kv, DuplicateLitField, "duplicate field name %s in struct literal", key.Name)
   193  					continue
   194  				}
   195  				visited[i] = true
   196  			}
   197  		} else {
   198  			// no element must have a key
   199  			for i, e := range e.Elts {
   200  				if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
   201  					check.error(kv, MixedStructLit, "mixture of field:value and value elements in struct literal")
   202  					continue
   203  				}
   204  				check.expr(nil, x, e)
   205  				if i >= len(fields) {
   206  					check.errorf(x, InvalidStructLit, "too many values in struct literal of type %s", base)
   207  					break // cannot continue
   208  				}
   209  				// i < len(fields)
   210  				fld := fields[i]
   211  				if !fld.Exported() && fld.pkg != check.pkg {
   212  					check.errorf(x, UnexportedLitField, "implicit assignment to unexported field %s in struct literal of type %s", fld.name, base)
   213  					continue
   214  				}
   215  				etyp := fld.typ
   216  				check.assignment(x, etyp, "struct literal")
   217  			}
   218  			if len(e.Elts) < len(fields) {
   219  				check.errorf(inNode(e, e.Rbrace), InvalidStructLit, "too few values in struct literal of type %s", base)
   220  				// ok to continue
   221  			}
   222  		}
   223  
   224  	case *Array:
   225  		n := check.indexedElts(e.Elts, utyp.elem, utyp.len)
   226  		// If we have an array of unknown length (usually [...]T arrays, but also
   227  		// arrays [n]T where n is invalid) set the length now that we know it and
   228  		// record the type for the array (usually done by check.typ which is not
   229  		// called for [...]T). We handle [...]T arrays and arrays with invalid
   230  		// length the same here because it makes sense to "guess" the length for
   231  		// the latter if we have a composite literal; e.g. for [n]int{1, 2, 3}
   232  		// where n is invalid for some reason, it seems fair to assume it should
   233  		// be 3 (see also Checked.arrayLength and go.dev/issue/27346).
   234  		if utyp.len < 0 {
   235  			utyp.len = n
   236  			// e.Type is missing if we have a composite literal element
   237  			// that is itself a composite literal with omitted type. In
   238  			// that case there is nothing to record (there is no type in
   239  			// the source at that point).
   240  			if e.Type != nil {
   241  				check.recordTypeAndValue(e.Type, typexpr, utyp, nil)
   242  			}
   243  		}
   244  
   245  	case *Slice:
   246  		check.indexedElts(e.Elts, utyp.elem, -1)
   247  
   248  	case *Map:
   249  		// If the map key type is an interface (but not a type parameter),
   250  		// the type of a constant key must be considered when checking for
   251  		// duplicates.
   252  		keyIsInterface := isNonTypeParamInterface(utyp.key)
   253  		visited := make(map[any][]Type, len(e.Elts))
   254  		for _, e := range e.Elts {
   255  			kv, _ := e.(*ast.KeyValueExpr)
   256  			if kv == nil {
   257  				check.error(e, MissingLitKey, "missing key in map literal")
   258  				continue
   259  			}
   260  			check.exprWithHint(x, kv.Key, utyp.key)
   261  			check.assignment(x, utyp.key, "map literal")
   262  			if x.mode == invalid {
   263  				continue
   264  			}
   265  			if x.mode == constant_ {
   266  				duplicate := false
   267  				xkey := keyVal(x.val)
   268  				if keyIsInterface {
   269  					for _, vtyp := range visited[xkey] {
   270  						if Identical(vtyp, x.typ) {
   271  							duplicate = true
   272  							break
   273  						}
   274  					}
   275  					visited[xkey] = append(visited[xkey], x.typ)
   276  				} else {
   277  					_, duplicate = visited[xkey]
   278  					visited[xkey] = nil
   279  				}
   280  				if duplicate {
   281  					check.errorf(x, DuplicateLitKey, "duplicate key %s in map literal", x.val)
   282  					continue
   283  				}
   284  			}
   285  			check.exprWithHint(x, kv.Value, utyp.elem)
   286  			check.assignment(x, utyp.elem, "map literal")
   287  		}
   288  
   289  	default:
   290  		// when "using" all elements unpack KeyValueExpr
   291  		// explicitly because check.use doesn't accept them
   292  		for _, e := range e.Elts {
   293  			if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
   294  				// Ideally, we should also "use" kv.Key but we can't know
   295  				// if it's an externally defined struct key or not. Going
   296  				// forward anyway can lead to other errors. Give up instead.
   297  				e = kv.Value
   298  			}
   299  			check.use(e)
   300  		}
   301  		// if utyp is invalid, an error was reported before
   302  		if isValid(utyp) {
   303  			var qualifier string
   304  			if isElem {
   305  				qualifier = " element"
   306  			}
   307  			var cause string
   308  			if utyp == nil {
   309  				cause = " (no common underlying type)"
   310  			}
   311  			check.errorf(e, InvalidLit, "invalid composite literal%s type %s%s", qualifier, typ, cause)
   312  			x.mode = invalid
   313  			return
   314  		}
   315  	}
   316  
   317  	x.mode = value
   318  	x.typ = typ
   319  }
   320  
   321  // indexedElts checks the elements (elts) of an array or slice composite literal
   322  // against the literal's element type (typ), and the element indices against
   323  // the literal length if known (length >= 0). It returns the length of the
   324  // literal (maximum index value + 1).
   325  func (check *Checker) indexedElts(elts []ast.Expr, typ Type, length int64) int64 {
   326  	visited := make(map[int64]bool, len(elts))
   327  	var index, max int64
   328  	for _, e := range elts {
   329  		// determine and check index
   330  		validIndex := false
   331  		eval := e
   332  		if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
   333  			if typ, i := check.index(kv.Key, length); isValid(typ) {
   334  				if i >= 0 {
   335  					index = i
   336  					validIndex = true
   337  				} else {
   338  					check.errorf(e, InvalidLitIndex, "index %s must be integer constant", kv.Key)
   339  				}
   340  			}
   341  			eval = kv.Value
   342  		} else if length >= 0 && index >= length {
   343  			check.errorf(e, OversizeArrayLit, "index %d is out of bounds (>= %d)", index, length)
   344  		} else {
   345  			validIndex = true
   346  		}
   347  
   348  		// if we have a valid index, check for duplicate entries
   349  		if validIndex {
   350  			if visited[index] {
   351  				check.errorf(e, DuplicateLitKey, "duplicate index %d in array or slice literal", index)
   352  			}
   353  			visited[index] = true
   354  		}
   355  		index++
   356  		if index > max {
   357  			max = index
   358  		}
   359  
   360  		// check element against composite literal element type
   361  		var x operand
   362  		check.exprWithHint(&x, eval, typ)
   363  		check.assignment(&x, typ, "array or slice literal")
   364  	}
   365  	return max
   366  }
   367  

View as plain text