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

     1  // Copyright 2012 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 conversions.
     6  
     7  package types2
     8  
     9  import (
    10  	"go/constant"
    11  	. "internal/types/errors"
    12  	"unicode"
    13  )
    14  
    15  // conversion type-checks the conversion T(x).
    16  // The result is in x.
    17  func (check *Checker) conversion(x *operand, T Type) {
    18  	constArg := x.mode == constant_
    19  
    20  	constConvertibleTo := func(T Type, val *constant.Value) bool {
    21  		switch t, _ := under(T).(*Basic); {
    22  		case t == nil:
    23  			// nothing to do
    24  		case representableConst(x.val, check, t, val):
    25  			return true
    26  		case isInteger(x.typ) && isString(t):
    27  			codepoint := unicode.ReplacementChar
    28  			if i, ok := constant.Uint64Val(x.val); ok && i <= unicode.MaxRune {
    29  				codepoint = rune(i)
    30  			}
    31  			if val != nil {
    32  				*val = constant.MakeString(string(codepoint))
    33  			}
    34  			return true
    35  		}
    36  		return false
    37  	}
    38  
    39  	var ok bool
    40  	var cause string
    41  	switch {
    42  	case constArg && isConstType(T):
    43  		// constant conversion
    44  		ok = constConvertibleTo(T, &x.val)
    45  		// A conversion from an integer constant to an integer type
    46  		// can only fail if there's overflow. Give a concise error.
    47  		// (go.dev/issue/63563)
    48  		if !ok && isInteger(x.typ) && isInteger(T) {
    49  			check.errorf(x, InvalidConversion, "constant %s overflows %s", x.val, T)
    50  			x.mode = invalid
    51  			return
    52  		}
    53  	case constArg && isTypeParam(T):
    54  		// x is convertible to T if it is convertible
    55  		// to each specific type in the type set of T.
    56  		// If T's type set is empty, or if it doesn't
    57  		// have specific types, constant x cannot be
    58  		// converted.
    59  		ok = T.(*TypeParam).underIs(func(u Type) bool {
    60  			// u is nil if there are no specific type terms
    61  			if u == nil {
    62  				cause = check.sprintf("%s does not contain specific types", T)
    63  				return false
    64  			}
    65  			if isString(x.typ) && isBytesOrRunes(u) {
    66  				return true
    67  			}
    68  			if !constConvertibleTo(u, nil) {
    69  				if isInteger(x.typ) && isInteger(u) {
    70  					// see comment above on constant conversion
    71  					cause = check.sprintf("constant %s overflows %s (in %s)", x.val, u, T)
    72  				} else {
    73  					cause = check.sprintf("cannot convert %s to type %s (in %s)", x, u, T)
    74  				}
    75  				return false
    76  			}
    77  			return true
    78  		})
    79  		x.mode = value // type parameters are not constants
    80  	case x.convertibleTo(check, T, &cause):
    81  		// non-constant conversion
    82  		ok = true
    83  		x.mode = value
    84  	}
    85  
    86  	if !ok {
    87  		if cause != "" {
    88  			check.errorf(x, InvalidConversion, "cannot convert %s to type %s: %s", x, T, cause)
    89  		} else {
    90  			check.errorf(x, InvalidConversion, "cannot convert %s to type %s", x, T)
    91  		}
    92  		x.mode = invalid
    93  		return
    94  	}
    95  
    96  	// The conversion argument types are final. For untyped values the
    97  	// conversion provides the type, per the spec: "A constant may be
    98  	// given a type explicitly by a constant declaration or conversion,...".
    99  	if isUntyped(x.typ) {
   100  		final := T
   101  		// - For conversions to interfaces, except for untyped nil arguments
   102  		//   and isTypes2, use the argument's default type.
   103  		// - For conversions of untyped constants to non-constant types, also
   104  		//   use the default type (e.g., []byte("foo") should report string
   105  		//   not []byte as type for the constant "foo").
   106  		// - If !isTypes2, keep untyped nil for untyped nil arguments.
   107  		// - For constant integer to string conversions, keep the argument type.
   108  		//   (See also the TODO below.)
   109  		if isTypes2 && x.typ == Typ[UntypedNil] {
   110  			// ok
   111  		} else if isNonTypeParamInterface(T) || constArg && !isConstType(T) || !isTypes2 && x.isNil() {
   112  			final = Default(x.typ) // default type of untyped nil is untyped nil
   113  		} else if x.mode == constant_ && isInteger(x.typ) && allString(T) {
   114  			final = x.typ
   115  		}
   116  		check.updateExprType(x.expr, final, true)
   117  	}
   118  
   119  	x.typ = T
   120  }
   121  
   122  // TODO(gri) convertibleTo checks if T(x) is valid. It assumes that the type
   123  // of x is fully known, but that's not the case for say string(1<<s + 1.0):
   124  // Here, the type of 1<<s + 1.0 will be UntypedFloat which will lead to the
   125  // (correct!) refusal of the conversion. But the reported error is essentially
   126  // "cannot convert untyped float value to string", yet the correct error (per
   127  // the spec) is that we cannot shift a floating-point value: 1 in 1<<s should
   128  // be converted to UntypedFloat because of the addition of 1.0. Fixing this
   129  // is tricky because we'd have to run updateExprType on the argument first.
   130  // (go.dev/issue/21982.)
   131  
   132  // convertibleTo reports whether T(x) is valid. In the failure case, *cause
   133  // may be set to the cause for the failure.
   134  // The check parameter may be nil if convertibleTo is invoked through an
   135  // exported API call, i.e., when all methods have been type-checked.
   136  func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
   137  	// "x is assignable to T"
   138  	if ok, _ := x.assignableTo(check, T, cause); ok {
   139  		return true
   140  	}
   141  
   142  	// "V and T have identical underlying types if tags are ignored
   143  	// and V and T are not type parameters"
   144  	V := x.typ
   145  	Vu := under(V)
   146  	Tu := under(T)
   147  	Vp, _ := V.(*TypeParam)
   148  	Tp, _ := T.(*TypeParam)
   149  	if IdenticalIgnoreTags(Vu, Tu) && Vp == nil && Tp == nil {
   150  		return true
   151  	}
   152  
   153  	// "V and T are unnamed pointer types and their pointer base types
   154  	// have identical underlying types if tags are ignored
   155  	// and their pointer base types are not type parameters"
   156  	if V, ok := V.(*Pointer); ok {
   157  		if T, ok := T.(*Pointer); ok {
   158  			if IdenticalIgnoreTags(under(V.base), under(T.base)) && !isTypeParam(V.base) && !isTypeParam(T.base) {
   159  				return true
   160  			}
   161  		}
   162  	}
   163  
   164  	// "V and T are both integer or floating point types"
   165  	if isIntegerOrFloat(Vu) && isIntegerOrFloat(Tu) {
   166  		return true
   167  	}
   168  
   169  	// "V and T are both complex types"
   170  	if isComplex(Vu) && isComplex(Tu) {
   171  		return true
   172  	}
   173  
   174  	// "V is an integer or a slice of bytes or runes and T is a string type"
   175  	if (isInteger(Vu) || isBytesOrRunes(Vu)) && isString(Tu) {
   176  		return true
   177  	}
   178  
   179  	// "V is a string and T is a slice of bytes or runes"
   180  	if isString(Vu) && isBytesOrRunes(Tu) {
   181  		return true
   182  	}
   183  
   184  	// package unsafe:
   185  	// "any pointer or value of underlying type uintptr can be converted into a unsafe.Pointer"
   186  	if (isPointer(Vu) || isUintptr(Vu)) && isUnsafePointer(Tu) {
   187  		return true
   188  	}
   189  	// "and vice versa"
   190  	if isUnsafePointer(Vu) && (isPointer(Tu) || isUintptr(Tu)) {
   191  		return true
   192  	}
   193  
   194  	// "V is a slice, T is an array or pointer-to-array type,
   195  	// and the slice and array types have identical element types."
   196  	if s, _ := Vu.(*Slice); s != nil {
   197  		switch a := Tu.(type) {
   198  		case *Array:
   199  			if Identical(s.Elem(), a.Elem()) {
   200  				if check == nil || check.allowVersion(x, go1_20) {
   201  					return true
   202  				}
   203  				// check != nil
   204  				if cause != nil {
   205  					// TODO(gri) consider restructuring versionErrorf so we can use it here and below
   206  					*cause = "conversion of slice to array requires go1.20 or later"
   207  				}
   208  				return false
   209  			}
   210  		case *Pointer:
   211  			if a, _ := under(a.Elem()).(*Array); a != nil {
   212  				if Identical(s.Elem(), a.Elem()) {
   213  					if check == nil || check.allowVersion(x, go1_17) {
   214  						return true
   215  					}
   216  					// check != nil
   217  					if cause != nil {
   218  						*cause = "conversion of slice to array pointer requires go1.17 or later"
   219  					}
   220  					return false
   221  				}
   222  			}
   223  		}
   224  	}
   225  
   226  	// optimization: if we don't have type parameters, we're done
   227  	if Vp == nil && Tp == nil {
   228  		return false
   229  	}
   230  
   231  	errorf := func(format string, args ...any) {
   232  		if check != nil && cause != nil {
   233  			msg := check.sprintf(format, args...)
   234  			if *cause != "" {
   235  				msg += "\n\t" + *cause
   236  			}
   237  			*cause = msg
   238  		}
   239  	}
   240  
   241  	// generic cases with specific type terms
   242  	// (generic operands cannot be constants, so we can ignore x.val)
   243  	switch {
   244  	case Vp != nil && Tp != nil:
   245  		x := *x // don't clobber outer x
   246  		return Vp.is(func(V *term) bool {
   247  			if V == nil {
   248  				return false // no specific types
   249  			}
   250  			x.typ = V.typ
   251  			return Tp.is(func(T *term) bool {
   252  				if T == nil {
   253  					return false // no specific types
   254  				}
   255  				if !x.convertibleTo(check, T.typ, cause) {
   256  					errorf("cannot convert %s (in %s) to type %s (in %s)", V.typ, Vp, T.typ, Tp)
   257  					return false
   258  				}
   259  				return true
   260  			})
   261  		})
   262  	case Vp != nil:
   263  		x := *x // don't clobber outer x
   264  		return Vp.is(func(V *term) bool {
   265  			if V == nil {
   266  				return false // no specific types
   267  			}
   268  			x.typ = V.typ
   269  			if !x.convertibleTo(check, T, cause) {
   270  				errorf("cannot convert %s (in %s) to type %s", V.typ, Vp, T)
   271  				return false
   272  			}
   273  			return true
   274  		})
   275  	case Tp != nil:
   276  		return Tp.is(func(T *term) bool {
   277  			if T == nil {
   278  				return false // no specific types
   279  			}
   280  			if !x.convertibleTo(check, T.typ, cause) {
   281  				errorf("cannot convert %s to type %s (in %s)", x.typ, T.typ, Tp)
   282  				return false
   283  			}
   284  			return true
   285  		})
   286  	}
   287  
   288  	return false
   289  }
   290  
   291  func isUintptr(typ Type) bool {
   292  	t, _ := under(typ).(*Basic)
   293  	return t != nil && t.kind == Uintptr
   294  }
   295  
   296  func isUnsafePointer(typ Type) bool {
   297  	t, _ := under(typ).(*Basic)
   298  	return t != nil && t.kind == UnsafePointer
   299  }
   300  
   301  func isPointer(typ Type) bool {
   302  	_, ok := under(typ).(*Pointer)
   303  	return ok
   304  }
   305  
   306  func isBytesOrRunes(typ Type) bool {
   307  	if s, _ := under(typ).(*Slice); s != nil {
   308  		t, _ := under(s.elem).(*Basic)
   309  		return t != nil && (t.kind == Byte || t.kind == Rune)
   310  	}
   311  	return false
   312  }
   313  

View as plain text