From 9905cec0dc04fd7cddb7c7504006b7aa618abd94 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 4 Mar 2013 13:55:35 -0800 Subject: [PATCH] spec: terminating statements for functions The only functional change is the new section on terminating statements. There is a minor syntax rewrite (not change) of function declarations to make it easier to refer to the notion of a function from all places where it is used (function decls, method decls, and function literals). Includes some minor fixes/additions of missing links. Based closely on Russ' proposal. Fixes #65. R=rsc, r, iant, ken, bradfitz CC=golang-dev https://golang.org/cl/7415050 --- doc/go_spec.html | 122 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 105 insertions(+), 17 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 83e0f582de..0fc918471d 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1469,12 +1469,13 @@ Any value may be assigned to the blank identifierBlocks

-A block is a sequence of declarations and statements within matching -brace brackets. +A block is a possibly empty sequence of declarations and statements +within matching brace brackets.

-Block = "{" { Statement ";" } "}" .
+Block = "{" StatementList "}" .
+StatementList = { Statement ";" } .
 

@@ -1490,10 +1491,13 @@ In addition to explicit blocks in the source code, there are implicit blocks:

  • Each file has a file block containing all Go source text in that file.
  • -
  • Each if, for, and switch +
  • Each "if", + "for", and + "switch" statement is considered to be in its own implicit block.
  • -
  • Each clause in a switch or select statement +
  • Each clause in a "switch" + or "select" statement acts as an implicit block.
  • @@ -1948,11 +1952,18 @@ to a function.

    -FunctionDecl = "func" FunctionName Signature [ Body ] .
    +FunctionDecl = "func" FunctionName ( Function | Signature ) .
     FunctionName = identifier .
    -Body         = Block .
    +Function     = Signature FunctionBody .
    +FunctionBody = Block .
     
    +

    +If the function's signature declares +result parameters, the function body's statement list must end in +a terminating statement. +

    +

    A function declaration may omit the body. Such a declaration provides the signature for a function implemented outside Go, such as an assembly routine. @@ -1972,13 +1983,13 @@ func flushICache(begin, end uintptr) // implemented externally

    Method declarations

    -A method is a function with a receiver. -A method declaration binds an identifier, the method name, to a method. -It also associates the method with the receiver's base type. +A method is a function with a receiver. +A method declaration binds an identifier, the method name, to a method, +and associates the method with the receiver's base type.

    -MethodDecl   = "func" Receiver MethodName Signature [ Body ] .
    +MethodDecl   = "func" Receiver MethodName ( Function | Signature ) .
     Receiver     = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
     BaseTypeName = identifier .
     
    @@ -2284,12 +2295,11 @@ noteFrequency := map[string]float32{

    Function literals

    -A function literal represents an anonymous function. -It consists of a specification of the function type and a function body. +A function literal represents an anonymous function.

    -FunctionLit = FunctionType Body .
    +FunctionLit = "func" Function .
     
    @@ -3843,6 +3853,84 @@ Statement =
     SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl .
     
    +

    Terminating statements

    + +

    +A terminating statement is one of the following: +

    + +
      +
    1. + A "return" or + "goto" statement. + +
      +
    2. + +
    3. + A call to the built-in function + panic. + +
      +
    4. + +
    5. + A block in which the statement list ends in a terminating statement. + +
      +
    6. + +
    7. + An "if" statement in which: +
        +
      • the "else" branch is present, and
      • +
      • both branches are terminating statements.
      • +
      +
    8. + +
    9. + A "for" statement in which: +
        +
      • there are no "break" statements referring to the "for" statement, and
      • +
      • the loop condition is absent.
      • +
      +
    10. + +
    11. + A "switch" statement in which: +
        +
      • there are no "break" statements referring to the "switch" statement,
      • +
      • there is a default case, and
      • +
      • the statement lists in each case, including the default, end in a terminating + statement, or a possibly labeled "fallthrough" + statement.
      • +
      +
    12. + +
    13. + A "select" statement in which: +
        +
      • there are no "break" statements referring to the "select" statement, and
      • +
      • the statement lists in each case, including the default if present, + end in a terminating statement.
      • +
      +
    14. + +
    15. + A labeled statement labeling + a terminating statement. +
    16. +
    + +

    +All other statements are not terminating. +

    + +

    +A statement list ends in a terminating statement if the list +is not empty and its final statement is terminating. +

    +

    Empty statements

    @@ -4149,7 +4237,7 @@ the expression true.
     ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
    -ExprCaseClause = ExprSwitchCase ":" { Statement ";" } .
    +ExprCaseClause = ExprSwitchCase ":" StatementList .
     ExprSwitchCase = "case" ExpressionList | "default" .
     
    @@ -4213,7 +4301,7 @@ expression x. As with type assertions, x must be of
     TypeSwitchStmt  = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
     TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" .
    -TypeCaseClause  = TypeSwitchCase ":" { Statement ";" } .
    +TypeCaseClause  = TypeSwitchCase ":" StatementList .
     TypeSwitchCase  = "case" TypeList | "default" .
     TypeList        = Type { "," Type } .
     
    @@ -4536,7 +4624,7 @@ cases all referring to communication operations.
     SelectStmt = "select" "{" { CommClause } "}" .
    -CommClause = CommCase ":" { Statement ";" } .
    +CommClause = CommCase ":" StatementList .
     CommCase   = "case" ( SendStmt | RecvStmt ) | "default" .
     RecvStmt   = [ ExpressionList "=" | IdentifierList ":=" ] RecvExpr .
     RecvExpr   = Expression .
    -- 
    2.48.1