From: Rob Pike Date: Fri, 11 Sep 2009 18:51:00 +0000 (-0700) Subject: correct and clarify the rules about integer conversions. X-Git-Tag: weekly.2009-11-06~594 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=61dd8363baf8e35201cc6fe176a88d9c22f27f26;p=gostls13.git correct and clarify the rules about integer conversions. DELTA=15 (6 added, 1 deleted, 8 changed) OCL=34549 CL=34564 --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 7a76909605..c883de49c0 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -3890,22 +3890,27 @@ to a variable of type T.
  • 2) The conversion succeeds if the value would be assignment-compatible -to a variable of type T if the value type or T or any of their component +to a variable of type T if the value's type, or T, or any of their component types are unnamed (§Type identity and compatibility).
  • -3) Between integer types. If the value is a signed quantity, it is +3a) From an ideal number to an integer type. +The ideal number must be representable in the result type; it must not overflow. +For example, uint8(0xFF) is legal but int8(0xFF) is not. +
  • +
  • +3b) From a non-ideal integer value to an integer type. If the value is a signed quantity, it is sign extended to implicit infinite precision; otherwise it is zero -extended. It is then truncated to fit in the result type size. -For example, uint32(int8(0xFF)) is 0xFFFFFFFF. -The conversion always yields a valid value; there is no signal for overflow. +extended. It is then truncated to fit in the result type's size. +For example, if x := uint16(0x10F0), then uint32(int8(x)) == 0xFFFFFFF0. +The conversion always yields a valid value; there is no indication of overflow.
  • 4) Between integer and floating point types, or between floating point types. To avoid overdefining the properties of the conversion, for now it is defined as a ``best effort'' conversion. The conversion always succeeds but the value may be a NaN or other problematic -result. TODO: clarify? +result. TODO: clarify
  • 5) Strings permit three special conversions: @@ -3915,7 +3920,7 @@ result. TODO: clarify? representation of the integer.
    -string(0x65e5)  // "\u65e5"
    +string(0x65e5)  // "\u65e5" == "日" == "\xe6\x97\xa5"
     
  • @@ -3924,9 +3929,9 @@ string(0x65e5) // "\u65e5" concatenation of the individual integers converted to strings. If the slice value is nil, the result is the empty string.
    -string([]int{0x65e5, 0x672c, 0x8a9e})  // "\u65e5\u672c\u8a9e"
    -
    +string([]int{0x65e5, 0x672c, 0x8a9e}) // "\u65e5\u672c\u8a9e" == "日本語" +
  • 5c) Converting a slice of bytes yields a string whose successive bytes are those of the slice. If the slice value is nil,