From: Robert Griesemer Date: Tue, 2 Oct 2018 22:55:38 +0000 (-0700) Subject: spec: distinguish between explicit and implicit conversions (clarification) X-Git-Tag: go1.12beta1~856 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=26d22609c389cd9b5a21939183b6411e5861e16b;p=gostls13.git spec: distinguish between explicit and implicit conversions (clarification) The spec used the term "conversion" somewhat indiscriminately for explicit conversions that appear literally in the source, and implicit conversions that are implied by the context of an expression. Be clearer about it by defining the terms. Also, state that integer to string conversions of the form string(x) are never implicit. This clarifies situations where implicit conversions might require an integer to change to a string (but don't and never have done so). See line 3948. Fixes #26313. Change-Id: I8939466df6b5950933ae7c987662ef9f88827fda Reviewed-on: https://go-review.googlesource.com/c/139099 Reviewed-by: Rob Pike Reviewed-by: Ian Lance Taylor --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 32336e86f8..6c7f2aa902 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -811,7 +811,7 @@ To avoid portability issues all numeric types are de types and thus distinct except byte, which is an alias for uint8, and rune, which is an alias for int32. -Conversions +Explicit conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a @@ -1348,7 +1348,7 @@ ChannelType = ( "chan" | "chan" "<-" | "<-" "chan" ) ElementType . The optional <- operator specifies the channel direction, send or receive. If no direction is given, the channel is bidirectional. -A channel may be constrained only to send or only to receive by +A channel may be constrained only to send or only to receive by explicit conversion or assignment.

@@ -2069,9 +2069,9 @@ Otherwise, each variable is initialized to its zero va If a type is present, each variable is given that type. Otherwise, each variable is given the type of the corresponding initialization value in the assignment. -If that value is an untyped constant, it is first +If that value is an untyped constant, it is first implicitly converted to its default type; -if it is an untyped boolean value, it is first converted to type bool. +if it is an untyped boolean value, it is first implicitly converted to type bool. The predeclared value nil cannot be used to initialize a variable with no explicit type.

@@ -3433,7 +3433,7 @@ For operations involving constants only, see the section on

Except for shift operations, if one operand is an untyped constant -and the other operand is not, the constant is converted +and the other operand is not, the constant is implicitly converted to the type of the other operand.

@@ -3442,7 +3442,7 @@ The right operand in a shift expression must have unsigned integer type or be an untyped constant representable by a value of type uint. If the left operand of a non-constant shift expression is an untyped constant, -it is first converted to the type it would assume if the shift expression were +it is first implicitly converted to the type it would assume if the shift expression were replaced by its left operand alone.

@@ -3645,7 +3645,7 @@ occurs is implementation-specific. An implementation may combine multiple floating-point operations into a single fused operation, possibly across statements, and produce a result that differs from the value obtained by executing and rounding the instructions individually. -A floating-point type conversion explicitly rounds to +An explicit floating-point type conversion rounds to the precision of the target type, preventing fusion that would discard that rounding.

@@ -3907,7 +3907,14 @@ channel is closed and empty.

Conversions

-Conversions are expressions of the form T(x) +A conversion changes the type of an expression +to the type specified by the conversion. +A conversion may appear literally in the source, or it may be implied +by the context in which an expression appears. +

+ +

+An explicit conversion is an expression of the form T(x) where T is a type and x is an expression that can be converted to type T.

@@ -3938,7 +3945,7 @@ func() int(x) // x is converted to func() int (unambiguous) A constant value x can be converted to type T if x is representable by a value of T. -As a special case, an integer constant x can be converted to a +As a special case, an integer constant x can be explicitly converted to a string type using the same rule as for non-constant x. @@ -4672,13 +4679,13 @@ to the type of the operand to which it is assigned, with the following special c
  • If an untyped constant is assigned to a variable of interface type or the blank identifier, - the constant is first converted to its + the constant is first implicitly converted to its default type.
  • If an untyped boolean value is assigned to a variable of interface type or - the blank identifier, it is first converted to type bool. + the blank identifier, it is first implicitly converted to type bool.
  • @@ -4764,14 +4771,14 @@ ExprSwitchCase = "case" ExpressionList | "default" .

    -If the switch expression evaluates to an untyped constant, it is first +If the switch expression evaluates to an untyped constant, it is first implicitly converted to its default type; -if it is an untyped boolean value, it is first converted to type bool. +if it is an untyped boolean value, it is first implicitly converted to type bool. The predeclared untyped value nil cannot be used as a switch expression.

    -If a case expression is untyped, it is first converted +If a case expression is untyped, it is first implicitly converted to the type of the switch expression. For each (possibly converted) case expression x and the value t of the switch expression, x == t must be a valid comparison. @@ -5881,7 +5888,7 @@ floating-point type and the return type is the complex type with the corresponding floating-point constituents: complex64 for float32 arguments, and complex128 for float64 arguments. -If one of the arguments evaluates to an untyped constant, it is first +If one of the arguments evaluates to an untyped constant, it is first implicitly converted to the type of the other argument. If both arguments evaluate to untyped constants, they must be non-complex numbers or their imaginary parts must be zero, and the return value of