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

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

View as plain text