From 11788aa6e06155431c346112f7e2725b9b49347b Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 9 Feb 2022 20:49:17 -0800 Subject: [PATCH] spec: adjust rules to use core or specific types as necessary Change-Id: I64280c1bb9608d7781514f237ac70c6abbfde9f6 Reviewed-on: https://go-review.googlesource.com/c/go/+/384754 Trust: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- doc/go_spec.html | 120 ++++++++++++++++++++++++++++------------------- 1 file changed, 72 insertions(+), 48 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 99bedf2671..c7f93c953d 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1754,6 +1754,12 @@ depending on the direction of the directional channels present. +

+By definition, a core type is never a defined type, +type parameter, or +interface type. +

+

Examples of interfaces with core types:

@@ -2994,6 +3000,13 @@ non-blank identifier denoting a or a parenthesized expression.

+
+Operand     = Literal | OperandName [ TypeArgs ] | "(" Expression ")" .
+Literal     = BasicLit | CompositeLit | FunctionLit .
+BasicLit    = int_lit | float_lit | imaginary_lit | rune_lit | string_lit .
+OperandName = identifier | QualifiedIdent .
+
+

An operand name denoting a type-parameterized function may be followed by a list of type arguments; the @@ -3005,13 +3018,6 @@ The blank identifier may appear as an operand only on the left-hand side of an assignment.

-
-Operand     = Literal | OperandName [ TypeArgs ] | "(" Expression ")" .
-Literal     = BasicLit | CompositeLit | FunctionLit .
-BasicLit    = int_lit | float_lit | imaginary_lit | rune_lit | string_lit .
-OperandName = identifier | QualifiedIdent .
-
-

Qualified identifiers

@@ -3038,8 +3044,7 @@ math.Sin // denotes the Sin function in package math

Composite literals

-Composite literals construct values for structs, arrays, slices, and maps -and create a new value each time they are evaluated. +Composite literals construct new composite values each time they are evaluated. They consist of the type of the literal followed by a brace-bound list of elements. Each element may optionally be preceded by a corresponding key.

@@ -3057,11 +3062,12 @@ Element = Expression | LiteralValue .

-The LiteralType's underlying type must be a struct, array, slice, or map type +The LiteralType's core type T +must be a struct, array, slice, or map type (the grammar enforces this constraint except when the type is given as a TypeName). The types of the elements and keys must be assignable -to the respective field, element, and key types of the literal type; +to the respective field, element, and key types of type T; there is no additional conversion. The key is interpreted as a field name for struct literals, an index for array and slice literals, and a key for map literals. @@ -3318,6 +3324,8 @@ f.p[i].x()

Selectors

+ +

For a primary expression x that is not a package name, the @@ -3361,8 +3369,7 @@ The following rules apply to selectors: For a value x of type T or *T where T is not a pointer or interface type, x.f denotes the field or method at the shallowest depth -in T where there -is such an f. +in T where there is such an f. If there is not exactly one f with shallowest depth, the selector expression is illegal. @@ -3722,7 +3729,8 @@ The following rules apply: If a is not a map:

    -
  • the index x must be of integer type or an untyped constant
  • +
  • the index x must be an untyped constant or its + core type must be an integer
  • a constant index must be non-negative and representable by a value of type int
  • a constant index that is untyped is given type int
  • @@ -3844,7 +3852,7 @@ and high bound, and a full form that also specifies a bound on the capacity.

    Simple slice expressions

    -For a string, array, pointer to array, or slice a, the primary expression +The primary expression

    @@ -3852,7 +3860,9 @@ a[low : high]
     

    -constructs a substring or slice. The indices low and +constructs a substring or slice. The core type of +a must be a string, array, pointer to array, or slice. +The indices low and high select which elements of operand a appear in the result. The result has indices starting at 0 and length equal to high - low. @@ -3928,7 +3938,7 @@ s2[1] = 42 // s2[1] == s1[2] == a[5] == 42; they all refer to the same under

    Full slice expressions

    -For an array, pointer to array, or slice a (but not a string), the primary expression +The primary expression

    @@ -3939,6 +3949,8 @@ a[low : high : max]
     constructs a slice of the same type, and with the same length and elements as the simple slice
     expression a[low : high]. Additionally, it controls the resulting slice's capacity
     by setting it to max - low. Only the first index may be omitted; it defaults to 0.
    +The core type of a must be an array, pointer to array,
    +or slice (but not a string).
     After slicing the array a
     

    @@ -4044,8 +4056,8 @@ No run-time panic occurs in this case.

    Calls

    -Given an expression f of function type -F, +Given an expression f with a core type +F of function type,

    @@ -5148,7 +5160,8 @@ var x *int = nil
     

    Receive operator

    -For an operand ch of channel type, +For an operand ch whose core type is a +channel, the value of the receive operation <-ch is the value received from the channel ch. The channel direction must permit receive operations, and the type of the receive operation is the element type of the channel. @@ -5861,7 +5874,8 @@ len("foo") // illegal if len is the built-in function

    A send statement sends a value on a channel. -The channel expression must be of channel type, +The channel expression's core type +must be a channel, the channel direction must permit send operations, and the type of the value to be sent must be assignable to the channel's element type. @@ -6407,7 +6421,8 @@ RangeClause = [ ExpressionList "=" | IdentifierList ":=" ] "range" Expression .

    The expression on the right in the "range" clause is called the range expression, -which may be an array, pointer to an array, slice, string, map, or channel permitting +its core type must be +an array, pointer to an array, slice, string, map, or channel permitting receive operations. As with an assignment, if present the operands on the left must be addressable or map index expressions; they @@ -6992,9 +7007,10 @@ they cannot be used as function values.

    Close

    -For a channel c, the built-in function close(c) +For an argument ch with a core type +that is a channel, the built-in function close records that no more values will be sent on the channel. -It is an error if c is a receive-only channel. +It is an error if ch is a receive-only channel. Sending to or closing a closed channel causes a run-time panic. Closing the nil channel also causes a run-time panic. After calling close, and after any previously @@ -7110,24 +7126,25 @@ of the location.

    The built-in function make takes a type T, -which must be a slice, map or channel type, optionally followed by a type-specific list of expressions. +The core type of T must +be a slice, map or channel. It returns a value of type T (not *T). The memory is initialized as described in the section on initial values.

    -Call             Type T     Result
    +Call             Core type    Result
     
    -make(T, n)       slice      slice of type T with length n and capacity n
    -make(T, n, m)    slice      slice of type T with length n and capacity m
    +make(T, n)       slice        slice of type T with length n and capacity n
    +make(T, n, m)    slice        slice of type T with length n and capacity m
     
    -make(T)          map        map of type T
    -make(T, n)       map        map of type T with initial space for approximately n elements
    +make(T)          map          map of type T
    +make(T, n)       map          map of type T with initial space for approximately n elements
     
    -make(T)          channel    unbuffered channel of type T
    -make(T, n)       channel    buffered channel of type T, buffer size n
    +make(T)          channel      unbuffered channel of type T
    +make(T, n)       channel      buffered channel of type T, buffer size n
     
    @@ -7169,21 +7186,20 @@ by the arguments overlaps.

    The variadic function append -appends zero or more values x -to s of type S, which must be a slice type, and -returns the resulting slice, also of type S. -The values x are passed to a parameter of type ...T -where T is the element type of -S and the respective -parameter passing rules apply. -As a special case, append also accepts a first argument -assignable to type []byte with a second argument of -string type followed by .... This form appends the -bytes of the string. +appends zero or more values x to a slice s +and returns the resulting slice. +The core type of s must be a slice +of the form []E. +The values x are passed to a parameter of type ...E +and the respective parameter +passing rules apply. +As a special case, if the core type of s is []byte, +append also accepts a second argument with core type string +followed by .... This form appends the bytes of the string.

    -append(s S, x ...T) S  // T is the element type of S
    +append(s S, x ...E) S  // E is the element type of the core type of S
     

    @@ -7211,12 +7227,12 @@ b = append(b, "bar"...) // append string contents b == []byte{'b The function copy copies slice elements from a source src to a destination dst and returns the number of elements copied. -Both arguments must have identical element type T and must be -assignable to a slice of type []T. +The core types of both arguments must be slices +with identical element type. The number of elements copied is the minimum of len(src) and len(dst). -As a special case, copy also accepts a destination argument assignable -to type []byte with a source argument of a string type. +As a special case, if the destination's core type is []byte, +copy also accepts a source argument with core type string. This form copies the bytes from the string into the byte slice.

    @@ -7252,6 +7268,12 @@ to the key type of m. delete(m, k) // remove element m[k] from map m
    +

    +If the type of m is a type parameter, +it must have specific types, all specific types +must be maps, and they must all have identical key types. +

    +

    If the map m is nil or the element m[k] does not exist, delete is a no-op. @@ -7260,6 +7282,8 @@ does not exist, delete is a no-op.

    Manipulating complex numbers

    + +

    Three functions assemble and disassemble complex numbers. The built-in function complex constructs a complex -- 2.50.0