1
0
mirror of https://github.com/golang/go synced 2024-11-24 16:50:13 -07:00

spec: clarify expression statements

Function and method calls are valid expression statements,
but calling certain built-in functions is not permitted.
Enumerate the built-ins.

Also: unsafe.Offsetof permits parenthesized selectors as
arguments.

This is simply documenting existing compiler behavior
(both gc and gccgo agree).

R=r, rsc, iant, ken
CC=golang-dev
https://golang.org/cl/6498138
This commit is contained in:
Robert Griesemer 2012-09-18 11:25:53 -07:00
parent 384af66984
commit 8c058b32d1

View File

@ -1,6 +1,6 @@
<!--{ <!--{
"Title": "The Go Programming Language Specification", "Title": "The Go Programming Language Specification",
"Subtitle": "Version of September 17, 2012", "Subtitle": "Version of September 18, 2012",
"Path": "/ref/spec" "Path": "/ref/spec"
}--> }-->
@ -3798,7 +3798,9 @@ Error: log.Panic("error encountered")
<h3 id="Expression_statements">Expression statements</h3> <h3 id="Expression_statements">Expression statements</h3>
<p> <p>
Function calls, method calls, and receive operations With the exception of specific built-in functions,
function and method <a href="#Calls">calls</a> and
<a href="#Receive_operator">receive operations</a>
can appear in statement context. Such statements may be parenthesized. can appear in statement context. Such statements may be parenthesized.
</p> </p>
@ -3806,11 +3808,21 @@ can appear in statement context. Such statements may be parenthesized.
ExpressionStmt = Expression . ExpressionStmt = Expression .
</pre> </pre>
<p>
The following built-in functions are not permitted in statement context:
</p>
<pre>
append cap complex imag len make new real
unsafe.Alignof unsafe.Offsetof unsafe.Sizeof
</pre>
<pre> <pre>
h(x+y) h(x+y)
f.Close() f.Close()
&lt;-ch &lt;-ch
(&lt;-ch) (&lt;-ch)
len("foo") // illegal if len is the built-in function
</pre> </pre>
@ -5492,8 +5504,8 @@ of any type and return the alignment or size, respectively, of a hypothetical va
as if <code>v</code> was declared via <code>var v = x</code>. as if <code>v</code> was declared via <code>var v = x</code>.
</p> </p>
<p> <p>
The function <code>Offsetof</code> takes a selector (§<a href="#Selectors">Selectors</a>) denoting a struct The function <code>Offsetof</code> takes a (possibly parenthesized) <a href="#Selectors">selector</a>
field of any type and returns the field offset in bytes relative to the denoting a struct field of any type and returns the field offset in bytes relative to the
struct's address. struct's address.
For a struct <code>s</code> with field <code>f</code>: For a struct <code>s</code> with field <code>f</code>:
</p> </p>