diff mbox

Go patch committed: Better error messages for { on next line

Message ID mcrbo0liwhl.fsf@iant-glaptop.roam.corp.google.com
State New
Headers show

Commit Message

Ian Lance Taylor Dec. 12, 2013, 9:35 p.m. UTC
The Go language requires that the { starting a block for an if, for, or
switch statement be on the same line as the if/for/switch.  This patch
gives better error messages when a program does it wrong.  Bootstrapped
and ran Go testsuite on x86_64-unknown-linux-gnu.  Committed to
mainline.

Ian
diff mbox

Patch

diff -r ee6b0181e864 go/parse.cc
--- a/go/parse.cc	Thu Dec 12 13:02:33 2013 -0800
+++ b/go/parse.cc	Thu Dec 12 13:32:41 2013 -0800
@@ -4287,6 +4287,16 @@ 
 	cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL, NULL);
     }
 
+  // Check for the easy error of a newline before starting the block.
+  if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
+    {
+      Location semi_loc = this->location();
+      if (this->advance_token()->is_op(OPERATOR_LCURLY))
+	error_at(semi_loc, "missing %<{%> after if clause");
+      // Otherwise we will get an error when we call this->block
+      // below.
+    }
+
   this->gogo_->start_block(this->location());
   Location end_loc = this->block();
   Block* then_block = this->gogo_->finish_block(end_loc);
@@ -4431,7 +4441,7 @@ 
       Location token_loc = this->location();
       if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
 	  && this->advance_token()->is_op(OPERATOR_LCURLY))
-	error_at(token_loc, "unexpected semicolon or newline before %<{%>");
+	error_at(token_loc, "missing %<{%> after switch clause");
       else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
 	{
 	  error_at(token_loc, "invalid variable name");
@@ -5158,6 +5168,16 @@ 
 	}
     }
 
+  // Check for the easy error of a newline before starting the block.
+  if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
+    {
+      Location semi_loc = this->location();
+      if (this->advance_token()->is_op(OPERATOR_LCURLY))
+	error_at(semi_loc, "missing %<{%> after for clause");
+      // Otherwise we will get an error when we call this->block
+      // below.
+    }
+
   // Build the For_statement and note that it is the current target
   // for break and continue statements.
 
@@ -5224,8 +5244,7 @@ 
     *cond = NULL;
   else if (this->peek_token()->is_op(OPERATOR_LCURLY))
     {
-      error_at(this->location(),
-	       "unexpected semicolon or newline before %<{%>");
+      error_at(this->location(), "missing %<{%> after for clause");
       *cond = NULL;
       *post = NULL;
       return;