OSDN Git Service

compiler: Fix a, b, c := b, a, 1 when a and b already exist.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / parse.cc
index c8b55c5..ab2bb7c 100644 (file)
@@ -35,21 +35,23 @@ Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1,
   // If we get here it means that a single nested function refers to
   // two different variables defined in enclosing functions, and both
   // variables have the same name.  I think this is impossible.
-  gcc_unreachable();
+  go_unreachable();
 }
 
 // Class Parse.
 
 Parse::Parse(Lex* lex, Gogo* gogo)
   : lex_(lex),
-    token_(Token::make_invalid_token(0)),
-    unget_token_(Token::make_invalid_token(0)),
+    token_(Token::make_invalid_token(Linemap::unknown_location())),
+    unget_token_(Token::make_invalid_token(Linemap::unknown_location())),
     unget_token_valid_(false),
+    is_erroneous_function_(false),
     gogo_(gogo),
-    break_stack_(),
-    continue_stack_(),
+    break_stack_(NULL),
+    continue_stack_(NULL),
     iota_(0),
-    enclosing_vars_()
+    enclosing_vars_(),
+    type_switch_vars_()
 {
 }
 
@@ -85,14 +87,14 @@ Parse::advance_token()
 void
 Parse::unget_token(const Token& token)
 {
-  gcc_assert(!this->unget_token_valid_);
+  go_assert(!this->unget_token_valid_);
   this->unget_token_ = token;
   this->unget_token_valid_ = true;
 }
 
 // The location of the current token.
 
-source_location
+Location
 Parse::location()
 {
   return this->peek_token()->location();
@@ -124,25 +126,29 @@ Parse::identifier_list(Typed_identifier_list* til)
 
 // ExpressionList = Expression { "," Expression } .
 
+// If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
+// literal.
+
 // If MAY_BE_SINK is true, the expressions in the list may be "_".
 
 Expression_list*
-Parse::expression_list(Expression* first, bool may_be_sink)
+Parse::expression_list(Expression* first, bool may_be_sink,
+                      bool may_be_composite_lit)
 {
   Expression_list* ret = new Expression_list();
   if (first != NULL)
     ret->push_back(first);
   while (true)
     {
-      ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink, true,
-                                     NULL));
+      ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink,
+                                     may_be_composite_lit, NULL));
 
       const Token* token = this->peek_token();
       if (!token->is_op(OPERATOR_COMMA))
        return ret;
 
       // Most expression lists permit a trailing comma.
-      source_location location = token->location();
+      Location location = token->location();
       this->advance_token();
       if (!this->expression_may_start_here())
        {
@@ -242,9 +248,12 @@ Parse::type()
     return this->interface_type();
   else if (token->is_keyword(KEYWORD_FUNC))
     {
-      source_location location = token->location();
+      Location location = token->location();
       this->advance_token();
-      return this->signature(NULL, location);
+      Type* type = this->signature(NULL, location);
+      if (type == NULL)
+       return Type::make_error_type();
+      return type;
     }
   else if (token->is_keyword(KEYWORD_MAP))
     return this->map_type();
@@ -296,7 +305,7 @@ Parse::type_may_start_here()
 Type*
 Parse::type_name(bool issue_error)
 {
-  source_location location = this->location();
+  Location location = this->location();
 
   std::string name;
   Named_object* package;
@@ -314,13 +323,13 @@ Parse::type_name(bool issue_error)
          && package->name() != this->gogo_->package_name())
        {
          // Check whether the name is there but hidden.
-         std::string s = ('.' + package->package_value()->unique_prefix()
-                          + '.' + package->package_value()->name()
+         std::string s = ('.' + package->package_value()->pkgpath()
                           + '.' + name);
          named_object = package->package_value()->lookup(s);
          if (named_object != NULL)
            {
-             const std::string& packname(package->package_value()->name());
+             Package* p = package->package_value();
+             const std::string& packname(p->package_name());
              error_at(location, "invalid reference to hidden type %<%s.%s%>",
                       Gogo::message_name(packname).c_str(),
                       Gogo::message_name(name).c_str());
@@ -332,10 +341,17 @@ Parse::type_name(bool issue_error)
   bool ok = true;
   if (named_object == NULL)
     {
-      if (package != NULL)
-       ok = false;
-      else
+      if (package == NULL)
        named_object = this->gogo_->add_unknown_name(name, location);
+      else
+       {
+         const std::string& packname(package->package_value()->package_name());
+         error_at(location, "reference to undefined identifier %<%s.%s%>",
+                  Gogo::message_name(packname).c_str(),
+                  Gogo::message_name(name).c_str());
+         issue_error = false;
+         ok = false;
+       }
     }
   else if (named_object->is_type())
     {
@@ -359,7 +375,7 @@ Parse::type_name(bool issue_error)
   else if (named_object->is_unknown() || named_object->is_type_declaration())
     return Type::make_forward_declaration(named_object);
   else
-    gcc_unreachable();
+    go_unreachable();
 }
 
 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
@@ -369,7 +385,7 @@ Parse::type_name(bool issue_error)
 Type*
 Parse::array_type(bool may_use_ellipsis)
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
+  go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
   const Token* token = this->advance_token();
 
   Expression* length = NULL;
@@ -415,8 +431,8 @@ Parse::array_type(bool may_use_ellipsis)
 Type*
 Parse::map_type()
 {
-  source_location location = this->location();
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
+  Location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
   if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
     {
       error_at(this->location(), "expected %<[%>");
@@ -446,11 +462,11 @@ Parse::map_type()
 Type*
 Parse::struct_type()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
+  Location location = this->location();
   if (!this->advance_token()->is_op(OPERATOR_LCURLY))
     {
-      source_location token_loc = this->location();
+      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 %<{%>");
@@ -504,7 +520,7 @@ void
 Parse::field_decl(Struct_field_list* sfl)
 {
   const Token* token = this->peek_token();
-  source_location location = token->location();
+  Location location = token->location();
   bool is_anonymous;
   bool is_anonymous_pointer;
   if (token->is_op(OPERATOR_MULT))
@@ -516,7 +532,7 @@ Parse::field_decl(Struct_field_list* sfl)
     {
       std::string id = token->identifier();
       bool is_id_exported = token->is_identifier_exported();
-      source_location id_location = token->location();
+      Location id_location = token->location();
       token = this->advance_token();
       is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
                      || token->is_op(OPERATOR_RCURLY)
@@ -529,6 +545,7 @@ Parse::field_decl(Struct_field_list* sfl)
   else
     {
       error_at(this->location(), "expected field name");
+      this->gogo_->mark_locals_used();
       while (!token->is_op(OPERATOR_SEMICOLON)
             && !token->is_op(OPERATOR_RCURLY)
             && !token->is_eof())
@@ -544,6 +561,7 @@ Parse::field_decl(Struct_field_list* sfl)
          if (!this->peek_token()->is_identifier())
            {
              error_at(this->location(), "expected field name");
+             this->gogo_->mark_locals_used();
              while (!token->is_op(OPERATOR_SEMICOLON)
                     && !token->is_op(OPERATOR_RCURLY)
                     && !token->is_eof())
@@ -615,7 +633,7 @@ Parse::field_decl(Struct_field_list* sfl)
 Type*
 Parse::pointer_type()
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_MULT));
+  go_assert(this->peek_token()->is_op(OPERATOR_MULT));
   this->advance_token();
   Type* type = this->type();
   if (type->is_error_type())
@@ -646,34 +664,92 @@ Parse::channel_type()
     }
   else
     {
-      gcc_assert(token->is_keyword(KEYWORD_CHAN));
+      go_assert(token->is_keyword(KEYWORD_CHAN));
       if (this->advance_token()->is_op(OPERATOR_CHANOP))
        {
          receive = false;
          this->advance_token();
        }
     }
+
+  // Better error messages for the common error of omitting the
+  // channel element type.
+  if (!this->type_may_start_here())
+    {
+      token = this->peek_token();
+      if (token->is_op(OPERATOR_RCURLY))
+       error_at(this->location(), "unexpected %<}%> in channel type");
+      else if (token->is_op(OPERATOR_RPAREN))
+       error_at(this->location(), "unexpected %<)%> in channel type");
+      else if (token->is_op(OPERATOR_COMMA))
+       error_at(this->location(), "unexpected comma in channel type");
+      else
+       error_at(this->location(), "expected channel element type");
+      return Type::make_error_type();
+    }
+
   Type* element_type = this->type();
   return Type::make_channel_type(send, receive, element_type);
 }
 
+// Give an error for a duplicate parameter or receiver name.
+
+void
+Parse::check_signature_names(const Typed_identifier_list* params,
+                            Parse::Names* names)
+{
+  for (Typed_identifier_list::const_iterator p = params->begin();
+       p != params->end();
+       ++p)
+    {
+      if (p->name().empty() || Gogo::is_sink_name(p->name()))
+       continue;
+      std::pair<std::string, const Typed_identifier*> val =
+       std::make_pair(p->name(), &*p);
+      std::pair<Parse::Names::iterator, bool> ins = names->insert(val);
+      if (!ins.second)
+       {
+         error_at(p->location(), "redefinition of %qs",
+                  Gogo::message_name(p->name()).c_str());
+         inform(ins.first->second->location(),
+                "previous definition of %qs was here",
+                Gogo::message_name(p->name()).c_str());
+       }
+    }
+}
+
 // Signature      = Parameters [ Result ] .
 
 // RECEIVER is the receiver if there is one, or NULL.  LOCATION is the
 // location of the start of the type.
 
+// This returns NULL on a parse error.
+
 Function_type*
-Parse::signature(Typed_identifier* receiver, source_location location)
+Parse::signature(Typed_identifier* receiver, Location location)
 {
   bool is_varargs = false;
-  Typed_identifier_list* params = this->parameters(&is_varargs);
+  Typed_identifier_list* params;
+  bool params_ok = this->parameters(&params, &is_varargs);
 
-  Typed_identifier_list* result = NULL;
+  Typed_identifier_list* results = NULL;
   if (this->peek_token()->is_op(OPERATOR_LPAREN)
       || this->type_may_start_here())
-    result = this->result();
+    {
+      if (!this->result(&results))
+       return NULL;
+    }
+
+  if (!params_ok)
+    return NULL;
+
+  Parse::Names names;
+  if (params != NULL)
+    this->check_signature_names(params, &names);
+  if (results != NULL)
+    this->check_signature_names(results, &names);
 
-  Function_type* ret = Type::make_function_type(receiver, params, result,
+  Function_type* ret = Type::make_function_type(receiver, params, results,
                                                location);
   if (is_varargs)
     ret->set_is_varargs();
@@ -682,21 +758,28 @@ Parse::signature(Typed_identifier* receiver, source_location location)
 
 // Parameters     = "(" [ ParameterList [ "," ] ] ")" .
 
-Typed_identifier_list*
-Parse::parameters(bool* is_varargs)
+// This returns false on a parse error.
+
+bool
+Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
 {
+  *pparams = NULL;
+
   if (!this->peek_token()->is_op(OPERATOR_LPAREN))
     {
       error_at(this->location(), "expected %<(%>");
-      return NULL;
+      return false;
     }
 
   Typed_identifier_list* params = NULL;
+  bool saw_error = false;
 
   const Token* token = this->advance_token();
   if (!token->is_op(OPERATOR_RPAREN))
     {
       params = this->parameter_list(is_varargs);
+      if (params == NULL)
+       saw_error = true;
       token = this->peek_token();
     }
 
@@ -707,7 +790,11 @@ Parse::parameters(bool* is_varargs)
   else
     this->advance_token();
 
-  return params;
+  if (saw_error)
+    return false;
+
+  *pparams = params;
+  return true;
 }
 
 // ParameterList  = ParameterDecl { "," ParameterDecl } .
@@ -717,12 +804,16 @@ Parse::parameters(bool* is_varargs)
 
 // We pick up an optional trailing comma.
 
+// This returns NULL if some error is seen.
+
 Typed_identifier_list*
 Parse::parameter_list(bool* is_varargs)
 {
-  source_location location = this->location();
+  Location location = this->location();
   Typed_identifier_list* ret = new Typed_identifier_list();
 
+  bool saw_error = false;
+
   // If we see an identifier and then a comma, then we don't know
   // whether we are looking at a list of identifiers followed by a
   // type, or a list of types given by name.  We have to do an
@@ -739,7 +830,7 @@ Parse::parameter_list(bool* is_varargs)
     {
       std::string name = token->identifier();
       bool is_exported = token->is_identifier_exported();
-      source_location location = token->location();
+      Location location = token->location();
       token = this->advance_token();
       if (!token->is_op(OPERATOR_COMMA))
        {
@@ -826,7 +917,7 @@ Parse::parameter_list(bool* is_varargs)
 
          if (parameters_have_names)
            {
-             gcc_assert(!just_saw_comma);
+             go_assert(!just_saw_comma);
              // We have just seen ID1, ID2 xxx.
              Type* type;
              if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
@@ -834,15 +925,16 @@ Parse::parameter_list(bool* is_varargs)
              else
                {
                  error_at(this->location(), "%<...%> only permits one name");
+                 saw_error = true;
                  this->advance_token();
                  type = this->type();
                }
              for (size_t i = 0; i < ret->size(); ++i)
                ret->set_type(i, type);
              if (!this->peek_token()->is_op(OPERATOR_COMMA))
-               return ret;
+               return saw_error ? NULL : ret;
              if (this->advance_token()->is_op(OPERATOR_RPAREN))
-               return ret;
+               return saw_error ? NULL : ret;
            }
          else
            {
@@ -865,6 +957,7 @@ Parse::parameter_list(bool* is_varargs)
                    {
                      error_at(p->location(), "expected %<%s%> to be a type",
                               Gogo::message_name(p->name()).c_str());
+                     saw_error = true;
                      type = Type::make_error_type();
                    }
                  tret->push_back(Typed_identifier("", type, p->location()));
@@ -873,7 +966,7 @@ Parse::parameter_list(bool* is_varargs)
              ret = tret;
              if (!just_saw_comma
                  || this->peek_token()->is_op(OPERATOR_RPAREN))
-               return ret;
+               return saw_error ? NULL : ret;
            }
        }
     }
@@ -883,13 +976,24 @@ Parse::parameter_list(bool* is_varargs)
   while (this->peek_token()->is_op(OPERATOR_COMMA))
     {
       if (is_varargs != NULL && *is_varargs)
-       error_at(this->location(), "%<...%> must be last parameter");
+       {
+         error_at(this->location(), "%<...%> must be last parameter");
+         saw_error = true;
+       }
       if (this->advance_token()->is_op(OPERATOR_RPAREN))
        break;
       this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
     }
   if (mix_error)
-    error_at(location, "invalid named/anonymous mix");
+    {
+      error_at(location, "invalid named/anonymous mix");
+      saw_error = true;
+    }
+  if (saw_error)
+    {
+      delete ret;
+      return NULL;
+    }
   return ret;
 }
 
@@ -904,7 +1008,7 @@ Parse::parameter_decl(bool parameters_have_names,
   if (!parameters_have_names)
     {
       Type* type;
-      source_location location = this->location();
+      Location location = this->location();
       if (!this->peek_token()->is_identifier())
        {
          if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
@@ -973,18 +1077,26 @@ Parse::parameter_decl(bool parameters_have_names,
 
 // Result         = Parameters | Type .
 
-Typed_identifier_list*
-Parse::result()
+// This returns false on a parse error.
+
+bool
+Parse::result(Typed_identifier_list** presults)
 {
   if (this->peek_token()->is_op(OPERATOR_LPAREN))
-    return this->parameters(NULL);
+    return this->parameters(presults, NULL);
   else
     {
-      source_location location = this->location();
-      Typed_identifier_list* til = new Typed_identifier_list();
+      Location location = this->location();
       Type* type = this->type();
+      if (type->is_error_type())
+       {
+         *presults = NULL;
+         return false;
+       }
+      Typed_identifier_list* til = new Typed_identifier_list();
       til->push_back(Typed_identifier("", type, location));
-      return til;
+      *presults = til;
+      return true;
     }
 }
 
@@ -992,19 +1104,19 @@ Parse::result()
 
 // Returns the location of the closing brace.
 
-source_location
+Location
 Parse::block()
 {
   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
     {
-      source_location loc = this->location();
+      Location loc = this->location();
       if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
          && this->advance_token()->is_op(OPERATOR_LCURLY))
        error_at(loc, "unexpected semicolon or newline before %<{%>");
       else
        {
          error_at(this->location(), "expected %<{%>");
-         return UNKNOWN_LOCATION;
+         return Linemap::unknown_location();
        }
     }
 
@@ -1019,9 +1131,11 @@ Parse::block()
          if (!token->is_eof() || !saw_errors())
            error_at(this->location(), "expected %<}%>");
 
+         this->gogo_->mark_locals_used();
+
          // Skip ahead to the end of the block, in hopes of avoiding
          // lots of meaningless errors.
-         source_location ret = token->location();
+         Location ret = token->location();
          int nest = 0;
          while (!token->is_eof())
            {
@@ -1043,7 +1157,7 @@ Parse::block()
        }
     }
 
-  source_location ret = token->location();
+  Location ret = token->location();
   this->advance_token();
   return ret;
 }
@@ -1054,12 +1168,12 @@ Parse::block()
 Type*
 Parse::interface_type()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
+  Location location = this->location();
 
   if (!this->advance_token()->is_op(OPERATOR_LCURLY))
     {
-      source_location token_loc = this->location();
+      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 %<{%>");
@@ -1108,25 +1222,27 @@ Parse::interface_type()
 // MethodName         = identifier .
 // InterfaceTypeName  = TypeName .
 
-bool
+void
 Parse::method_spec(Typed_identifier_list* methods)
 {
   const Token* token = this->peek_token();
   if (!token->is_identifier())
     {
       error_at(this->location(), "expected identifier");
-      return false;
+      return;
     }
 
   std::string name = token->identifier();
   bool is_exported = token->is_identifier_exported();
-  source_location location = token->location();
+  Location location = token->location();
 
   if (this->advance_token()->is_op(OPERATOR_LPAREN))
     {
       // This is a MethodName.
       name = this->gogo_->pack_hidden_name(name, is_exported);
-      Function_type* type = this->signature(NULL, location);
+      Type* type = this->signature(NULL, location);
+      if (type == NULL)
+       return;
       methods->push_back(Typed_identifier(name, type, location));
     }
   else
@@ -1143,20 +1259,19 @@ Parse::method_spec(Typed_identifier_list* methods)
                     "name list not allowed in interface type");
          else
            error_at(location, "expected signature or type name");
+         this->gogo_->mark_locals_used();
          token = this->peek_token();
          while (!token->is_eof()
                 && !token->is_op(OPERATOR_SEMICOLON)
                 && !token->is_op(OPERATOR_RCURLY))
            token = this->advance_token();
-         return false;
+         return;
        }
       // This must be an interface type, but we can't check that now.
       // We check it and pull out the methods in
       // Interface_type::do_verify.
       methods->push_back(Typed_identifier("", type, location));
     }
-
-  return false;
 }
 
 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
@@ -1195,6 +1310,13 @@ Parse::declaration_may_start_here()
 void
 Parse::decl(void (Parse::*pfn)(void*), void* varg)
 {
+  if (this->peek_token()->is_eof())
+    {
+      if (!saw_errors())
+       error_at(this->location(), "unexpected end of file");
+      return;
+    }
+
   if (!this->peek_token()->is_op(OPERATOR_LPAREN))
     (this->*pfn)(varg);
   else
@@ -1242,7 +1364,7 @@ Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
 void
 Parse::const_decl()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
   this->advance_token();
   this->reset_iota();
 
@@ -1307,7 +1429,7 @@ Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
   else
     {
       this->advance_token();
-      expr_list = this->expression_list(NULL, false);
+      expr_list = this->expression_list(NULL, false, true);
       *last_type = type;
       if (*last_expr_list != NULL)
        delete *last_expr_list;
@@ -1343,7 +1465,7 @@ Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
 void
 Parse::type_decl()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
   this->advance_token();
   this->decl(&Parse::type_spec, NULL);
 }
@@ -1361,7 +1483,7 @@ Parse::type_spec(void*)
     }
   std::string name = token->identifier();
   bool is_exported = token->is_identifier_exported();
-  source_location location = token->location();
+  Location location = token->location();
   token = this->advance_token();
 
   // The scope of the type name starts at the point where the
@@ -1387,6 +1509,7 @@ Parse::type_spec(void*)
 
   if (type->is_error_type())
     {
+      this->gogo_->mark_locals_used();
       while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
             && !this->peek_token()->is_eof())
        this->advance_token();
@@ -1408,7 +1531,7 @@ Parse::type_spec(void*)
          this->gogo_->define_type(named_type,
                                   Type::make_named_type(named_type, type,
                                                         location));
-         gcc_assert(named_type->package() == NULL);
+         go_assert(named_type->package() == NULL);
        }
       else
        {
@@ -1423,7 +1546,7 @@ Parse::type_spec(void*)
 void
 Parse::var_decl()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
   this->advance_token();
   this->decl(&Parse::var_spec, NULL);
 }
@@ -1438,7 +1561,7 @@ Parse::var_spec(void*)
   Typed_identifier_list til;
   this->identifier_list(&til);
 
-  source_location location = this->location();
+  Location location = this->location();
 
   Type* type = NULL;
   Expression_list* init = NULL;
@@ -1447,6 +1570,7 @@ Parse::var_spec(void*)
       type = this->type();
       if (type->is_error_type())
        {
+         this->gogo_->mark_locals_used();
          while (!this->peek_token()->is_op(OPERATOR_EQ)
                 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
                 && !this->peek_token()->is_eof())
@@ -1455,13 +1579,13 @@ Parse::var_spec(void*)
       if (this->peek_token()->is_op(OPERATOR_EQ))
        {
          this->advance_token();
-         init = this->expression_list(NULL, false);
+         init = this->expression_list(NULL, false, true);
        }
     }
   else
     {
       this->advance_token();
-      init = this->expression_list(NULL, false);
+      init = this->expression_list(NULL, false, true);
     }
 
   this->init_vars(&til, type, init, false, location);
@@ -1477,7 +1601,7 @@ Parse::var_spec(void*)
 void
 Parse::init_vars(const Typed_identifier_list* til, Type* type,
                 Expression_list* init, bool is_coloneq,
-                source_location location)
+                Location location)
 {
   // Check for an initialization which can yield multiple values.
   if (init != NULL && init->size() == 1 && til->size() > 1)
@@ -1507,27 +1631,32 @@ Parse::init_vars(const Typed_identifier_list* til, Type* type,
 
   // Note that INIT was already parsed with the old name bindings, so
   // we don't have to worry that it will accidentally refer to the
-  // newly declared variables.
+  // newly declared variables.  But we do have to worry about a mix of
+  // newly declared variables and old variables if the old variables
+  // appear in the initializations.
 
   Expression_list::const_iterator pexpr;
   if (init != NULL)
     pexpr = init->begin();
   bool any_new = false;
+  Expression_list* vars = new Expression_list();
+  Expression_list* vals = new Expression_list();
   for (Typed_identifier_list::const_iterator p = til->begin();
        p != til->end();
        ++p)
     {
       if (init != NULL)
-       gcc_assert(pexpr != init->end());
+       go_assert(pexpr != init->end());
       this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
-                    false, &any_new);
+                    false, &any_new, vars, vals);
       if (init != NULL)
        ++pexpr;
     }
   if (init != NULL)
-    gcc_assert(pexpr == init->end());
+    go_assert(pexpr == init->end());
   if (is_coloneq && !any_new)
     error_at(location, "variables redeclared but no variable is new");
+  this->finish_init_vars(vars, vals, location);
 }
 
 // See if we need to initialize a list of variables from a function
@@ -1537,7 +1666,7 @@ Parse::init_vars(const Typed_identifier_list* til, Type* type,
 bool
 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
                           Expression* expr, bool is_coloneq,
-                          source_location location)
+                          Location location)
 {
   Call_expression* call = expr->call_expression();
   if (call == NULL)
@@ -1547,19 +1676,39 @@ Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
   // the right number of values, but it might.  Declare the variables,
   // and then assign the results of the call to them.
 
+  Named_object* first_var = NULL;
   unsigned int index = 0;
   bool any_new = false;
+  Expression_list* ivars = new Expression_list();
+  Expression_list* ivals = new Expression_list();
   for (Typed_identifier_list::const_iterator pv = vars->begin();
        pv != vars->end();
        ++pv, ++index)
     {
       Expression* init = Expression::make_call_result(call, index);
-      this->init_var(*pv, type, init, is_coloneq, false, &any_new);
+      Named_object* no = this->init_var(*pv, type, init, is_coloneq, false,
+                                       &any_new, ivars, ivals);
+
+      if (this->gogo_->in_global_scope() && no->is_variable())
+       {
+         if (first_var == NULL)
+           first_var = no;
+         else
+           {
+             // The subsequent vars have an implicit dependency on
+             // the first one, so that everything gets initialized in
+             // the right order and so that we detect cycles
+             // correctly.
+             this->gogo_->record_var_depends_on(no->var_value(), first_var);
+           }
+       }
     }
 
   if (is_coloneq && !any_new)
     error_at(location, "variables redeclared but no variable is new");
 
+  this->finish_init_vars(ivars, ivals, location);
+
   return true;
 }
 
@@ -1570,7 +1719,7 @@ Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
 bool
 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
                          Expression* expr, bool is_coloneq,
-                         source_location location)
+                         Location location)
 {
   Index_expression* index = expr->index_expression();
   if (index == NULL)
@@ -1585,7 +1734,7 @@ Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
   Typed_identifier_list::const_iterator p = vars->begin();
   Expression* init = type == NULL ? index : NULL;
   Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
-                                       type == NULL, &any_new);
+                                       type == NULL, &any_new, NULL, NULL);
   if (type == NULL && any_new && val_no->is_variable())
     val_no->var_value()->set_type_from_init_tuple();
   Expression* val_var = Expression::make_var_reference(val_no, location);
@@ -1595,7 +1744,7 @@ Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
   if (var_type == NULL)
     var_type = Type::lookup_bool_type();
   Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
-                                   &any_new);
+                                   &any_new, NULL, NULL);
   Expression* present_var = Expression::make_var_reference(no, location);
 
   if (is_coloneq && !any_new)
@@ -1606,8 +1755,24 @@ Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
 
   if (!this->gogo_->in_global_scope())
     this->gogo_->add_statement(s);
+  else if (!val_no->is_sink())
+    {
+      if (val_no->is_variable())
+       val_no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
+  else if (!no->is_sink())
+    {
+      if (no->is_variable())
+       no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
   else
-    val_no->var_value()->add_preinit_statement(s);
+    {
+      // Execute the map index expression just so that we can fail if
+      // the map is nil.
+      Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
+                                                     NULL, location);
+      dummy->var_value()->add_preinit_statement(this->gogo_, s);
+    }
 
   return true;
 }
@@ -1619,7 +1784,7 @@ Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
 bool
 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
                              Expression* expr, bool is_coloneq,
-                             source_location location)
+                             Location location)
 {
   Receive_expression* receive = expr->receive_expression();
   if (receive == NULL)
@@ -1634,7 +1799,7 @@ Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
   Typed_identifier_list::const_iterator p = vars->begin();
   Expression* init = type == NULL ? receive : NULL;
   Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
-                                       type == NULL, &any_new);
+                                       type == NULL, &any_new, NULL, NULL);
   if (type == NULL && any_new && val_no->is_variable())
     val_no->var_value()->set_type_from_init_tuple();
   Expression* val_var = Expression::make_var_reference(val_no, location);
@@ -1644,7 +1809,7 @@ Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
   if (var_type == NULL)
     var_type = Type::lookup_bool_type();
   Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
-                                   &any_new);
+                                   &any_new, NULL, NULL);
   Expression* received_var = Expression::make_var_reference(no, location);
 
   if (is_coloneq && !any_new)
@@ -1657,8 +1822,22 @@ Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
 
   if (!this->gogo_->in_global_scope())
     this->gogo_->add_statement(s);
+  else if (!val_no->is_sink())
+    {
+      if (val_no->is_variable())
+       val_no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
+  else if (!no->is_sink())
+    {
+      if (no->is_variable())
+       no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
   else
-    val_no->var_value()->add_preinit_statement(s);
+    {
+      Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
+                                                     NULL, location);
+      dummy->var_value()->add_preinit_statement(this->gogo_, s);
+    }
 
   return true;
 }
@@ -1670,7 +1849,7 @@ Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
 bool
 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
                                 Type* type, Expression* expr,
-                                bool is_coloneq, source_location location)
+                                bool is_coloneq, Location location)
 {
   Type_guard_expression* type_guard = expr->type_guard_expression();
   if (type_guard == NULL)
@@ -1687,7 +1866,7 @@ Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
   if (var_type == NULL)
     var_type = type_guard->type();
   Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
-                                       &any_new);
+                                       &any_new, NULL, NULL);
   Expression* val_var = Expression::make_var_reference(val_no, location);
 
   ++p;
@@ -1695,7 +1874,7 @@ Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
   if (var_type == NULL)
     var_type = Type::lookup_bool_type();
   Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
-                                   &any_new);
+                                   &any_new, NULL, NULL);
   Expression* ok_var = Expression::make_var_reference(no, location);
 
   Expression* texpr = type_guard->expr();
@@ -1709,8 +1888,21 @@ Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
 
   if (!this->gogo_->in_global_scope())
     this->gogo_->add_statement(s);
+  else if (!val_no->is_sink())
+    {
+      if (val_no->is_variable())
+       val_no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
+  else if (!no->is_sink())
+    {
+      if (no->is_variable())
+       no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
   else
-    val_no->var_value()->add_preinit_statement(s);
+    {
+      Named_object* dummy = this->create_dummy_global(type, NULL, location);
+      dummy->var_value()->add_preinit_statement(this->gogo_, s);
+    }
 
   return true;
 }
@@ -1721,29 +1913,36 @@ Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
 
 Named_object*
 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
-               bool is_coloneq, bool type_from_init, bool* is_new)
+               bool is_coloneq, bool type_from_init, bool* is_new,
+               Expression_list* vars, Expression_list* vals)
 {
-  source_location location = tid.location();
+  Location location = tid.location();
 
   if (Gogo::is_sink_name(tid.name()))
     {
       if (!type_from_init && init != NULL)
        {
-         if (!this->gogo_->in_global_scope())
-           this->gogo_->add_statement(Statement::make_statement(init));
+         if (this->gogo_->in_global_scope())
+           return this->create_dummy_global(type, init, location);
+         else if (type == NULL)
+           this->gogo_->add_statement(Statement::make_statement(init, true));
          else
            {
-             // Create a dummy global variable to force the
-             // initializer to be run in the right place.
-             Variable* var = new Variable(type, init, true, false, false,
+             // With both a type and an initializer, create a dummy
+             // variable so that we will check whether the
+             // initializer can be assigned to the type.
+             Variable* var = new Variable(type, init, false, false, false,
                                           location);
+             var->set_is_used();
              static int count;
              char buf[30];
-             snprintf(buf, sizeof buf, "_.%d", count);
+             snprintf(buf, sizeof buf, "sink$%d", count);
              ++count;
              return this->gogo_->add_variable(buf, var);
            }
        }
+      if (type != NULL)
+       this->gogo_->add_type_to_verify(type);
       return this->gogo_->add_sink();
     }
 
@@ -1757,9 +1956,9 @@ Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
          // like v, ok := x.(int).
          if (!type_from_init && init != NULL)
            {
-             Expression *v = Expression::make_var_reference(no, location);
-             Statement *s = Statement::make_assignment(v, init, location);
-             this->gogo_->add_statement(s);
+             go_assert(vars != NULL && vals != NULL);
+             vars->push_back(Expression::make_var_reference(no, location));
+             vals->push_back(init);
            }
          return no;
        }
@@ -1767,7 +1966,61 @@ Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
   *is_new = true;
   Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
                               false, false, location);
-  return this->gogo_->add_variable(tid.name(), var);
+  Named_object* no = this->gogo_->add_variable(tid.name(), var);
+  if (!no->is_variable())
+    {
+      // The name is already defined, so we just gave an error.
+      return this->gogo_->add_sink();
+    }
+  return no;
+}
+
+// Create a dummy global variable to force an initializer to be run in
+// the right place.  This is used when a sink variable is initialized
+// at global scope.
+
+Named_object*
+Parse::create_dummy_global(Type* type, Expression* init,
+                          Location location)
+{
+  if (type == NULL && init == NULL)
+    type = Type::lookup_bool_type();
+  Variable* var = new Variable(type, init, true, false, false, location);
+  static int count;
+  char buf[30];
+  snprintf(buf, sizeof buf, "_.%d", count);
+  ++count;
+  return this->gogo_->add_variable(buf, var);
+}
+
+// Finish the variable initialization by executing any assignments to
+// existing variables when using :=.  These must be done as a tuple
+// assignment in case of something like n, a, b := 1, b, a.
+
+void
+Parse::finish_init_vars(Expression_list* vars, Expression_list* vals,
+                       Location location)
+{
+  if (vars->empty())
+    {
+      delete vars;
+      delete vals;
+    }
+  else if (vars->size() == 1)
+    {
+      go_assert(!this->gogo_->in_global_scope());
+      this->gogo_->add_statement(Statement::make_assignment(vars->front(),
+                                                           vals->front(),
+                                                           location));
+      delete vars;
+      delete vals;
+    }
+  else
+    {
+      go_assert(!this->gogo_->in_global_scope());
+      this->gogo_->add_statement(Statement::make_tuple_assignment(vars, vals,
+                                                                 location));
+    }
 }
 
 // SimpleVarDecl = identifier ":=" Expression .
@@ -1779,6 +2032,9 @@ Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
 // tuple assignments here as well.
 
+// If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
+// side may be a composite literal.
+
 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
 // RangeClause.
 
@@ -1787,7 +2043,8 @@ Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
 
 void
 Parse::simple_var_decl_or_assignment(const std::string& name,
-                                    source_location location,
+                                    Location location,
+                                    bool may_be_composite_lit,
                                     Range_clause* p_range_clause,
                                     Type_switch* p_type_switch)
 {
@@ -1798,7 +2055,7 @@ Parse::simple_var_decl_or_assignment(const std::string& name,
   // "a, *p = 1, 2".
   if (this->peek_token()->is_op(OPERATOR_COMMA))
     {
-      gcc_assert(p_type_switch == NULL);
+      go_assert(p_type_switch == NULL);
       while (true)
        {
          const Token* token = this->advance_token();
@@ -1807,7 +2064,7 @@ Parse::simple_var_decl_or_assignment(const std::string& name,
 
          std::string id = token->identifier();
          bool is_id_exported = token->is_identifier_exported();
-         source_location id_location = token->location();
+         Location id_location = token->location();
 
          token = this->advance_token();
          if (!token->is_op(OPERATOR_COMMA))
@@ -1844,19 +2101,20 @@ Parse::simple_var_decl_or_assignment(const std::string& name,
            exprs->push_back(this->id_to_expression(p->name(),
                                                    p->location()));
 
-         Expression_list* more_exprs = this->expression_list(NULL, true);
+         Expression_list* more_exprs =
+           this->expression_list(NULL, true, may_be_composite_lit);
          for (Expression_list::const_iterator p = more_exprs->begin();
               p != more_exprs->end();
               ++p)
            exprs->push_back(*p);
          delete more_exprs;
 
-         this->tuple_assignment(exprs, p_range_clause);
+         this->tuple_assignment(exprs, may_be_composite_lit, p_range_clause);
          return;
        }
     }
 
-  gcc_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
+  go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
   const Token* token = this->advance_token();
 
   if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
@@ -1867,11 +2125,12 @@ Parse::simple_var_decl_or_assignment(const std::string& name,
 
   Expression_list* init;
   if (p_type_switch == NULL)
-    init = this->expression_list(NULL, false);
+    init = this->expression_list(NULL, false, may_be_composite_lit);
   else
     {
       bool is_type_switch = false;
-      Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
+      Expression* expr = this->expression(PRECEDENCE_NORMAL, false,
+                                         may_be_composite_lit,
                                          &is_type_switch);
       if (is_type_switch)
        {
@@ -1890,7 +2149,7 @@ Parse::simple_var_decl_or_assignment(const std::string& name,
       else
        {
          this->advance_token();
-         init = this->expression_list(expr, false);
+         init = this->expression_list(expr, false, may_be_composite_lit);
        }
     }
 
@@ -1900,17 +2159,19 @@ Parse::simple_var_decl_or_assignment(const std::string& name,
 // FunctionDecl = "func" identifier Signature [ Block ] .
 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
 
-// gcc extension:
+// Deprecated gcc extension:
 //   FunctionDecl = "func" identifier Signature
 //                    __asm__ "(" string_lit ")" .
 // This extension means a function whose real name is the identifier
-// inside the asm.
+// inside the asm.  This extension will be removed at some future
+// date.  It has been replaced with //extern comments.
 
 void
 Parse::function_decl()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
+  Location location = this->location();
+  std::string extern_name = this->lex_->extern_name();
   const Token* token = this->advance_token();
 
   Typed_identifier* rec = NULL;
@@ -1956,15 +2217,18 @@ Parse::function_decl()
          return;
        }
       this->advance_token();
-      named_object = this->gogo_->declare_function(name, fntype, location);
-      if (named_object->is_function_declaration())
-       named_object->func_declaration_value()->set_asm_name(asm_name);
+      if (!Gogo::is_sink_name(name))
+       {
+         named_object = this->gogo_->declare_function(name, fntype, location);
+         if (named_object->is_function_declaration())
+           named_object->func_declaration_value()->set_asm_name(asm_name);
+       }
     }
 
   // Check for the easy error of a newline before the opening brace.
   if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
     {
-      source_location semi_loc = this->location();
+      Location semi_loc = this->location();
       if (this->advance_token()->is_op(OPERATOR_LCURLY))
        error_at(this->location(),
                 "unexpected semicolon or newline before %<{%>");
@@ -1975,14 +2239,39 @@ Parse::function_decl()
 
   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
     {
-      if (named_object == NULL)
-       named_object = this->gogo_->declare_function(name, fntype, location);
+      if (named_object == NULL && !Gogo::is_sink_name(name))
+       {
+         if (fntype == NULL)
+           this->gogo_->add_erroneous_name(name);
+         else
+           {
+             named_object = this->gogo_->declare_function(name, fntype,
+                                                          location);
+             if (!extern_name.empty()
+                 && named_object->is_function_declaration())
+               {
+                 Function_declaration* fd =
+                   named_object->func_declaration_value();
+                 fd->set_asm_name(extern_name);
+               }
+           }
+       }
     }
   else
     {
+      bool hold_is_erroneous_function = this->is_erroneous_function_;
+      if (fntype == NULL)
+       {
+         fntype = Type::make_function_type(NULL, NULL, NULL, location);
+         this->is_erroneous_function_ = true;
+         if (!Gogo::is_sink_name(name))
+           this->gogo_->add_erroneous_name(name);
+         name = this->gogo_->pack_hidden_name("_", false);
+       }
       this->gogo_->start_function(name, fntype, true, location);
-      source_location end_loc = this->block();
+      Location end_loc = this->block();
       this->gogo_->finish_function(end_loc);
+      this->is_erroneous_function_ = hold_is_erroneous_function;
     }
 }
 
@@ -1992,16 +2281,17 @@ Parse::function_decl()
 Typed_identifier*
 Parse::receiver()
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
+  go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
 
   std::string name;
   const Token* token = this->advance_token();
-  source_location location = token->location();
+  Location location = token->location();
   if (!token->is_op(OPERATOR_MULT))
     {
       if (!token->is_identifier())
        {
          error_at(this->location(), "method has no receiver");
+         this->gogo_->mark_locals_used();
          while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
            token = this->advance_token();
          if (!token->is_eof())
@@ -2041,6 +2331,7 @@ Parse::receiver()
   if (!token->is_identifier())
     {
       error_at(this->location(), "expected receiver name or type");
+      this->gogo_->mark_locals_used();
       int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
       while (!token->is_eof())
        {
@@ -2072,6 +2363,7 @@ Parse::receiver()
        error_at(this->location(), "method has multiple receivers");
       else
        error_at(this->location(), "expected %<)%>");
+      this->gogo_->mark_locals_used();
       while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
        token = this->advance_token();
       if (!token->is_eof())
@@ -2097,7 +2389,7 @@ Parse::operand(bool may_be_sink)
     {
     case Token::TOKEN_IDENTIFIER:
       {
-       source_location location = token->location();
+       Location location = token->location();
        std::string id = token->identifier();
        bool is_exported = token->is_identifier_exported();
        std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
@@ -2121,7 +2413,7 @@ Parse::operand(bool may_be_sink)
            packed = this->gogo_->pack_hidden_name(id, is_exported);
            named_object = package->lookup(packed);
            location = this->location();
-           gcc_assert(in_function == NULL);
+           go_assert(in_function == NULL);
          }
 
        this->advance_token();
@@ -2130,9 +2422,9 @@ Parse::operand(bool may_be_sink)
            && named_object->is_type()
            && !named_object->type_value()->is_visible())
          {
-           gcc_assert(package != NULL);
+           go_assert(package != NULL);
            error_at(location, "invalid reference to hidden type %<%s.%s%>",
-                    Gogo::message_name(package->name()).c_str(),
+                    Gogo::message_name(package->package_name()).c_str(),
                     Gogo::message_name(id).c_str());
            return Expression::make_error(location);
          }
@@ -2142,7 +2434,7 @@ Parse::operand(bool may_be_sink)
          {
            if (package != NULL)
              {
-               std::string n1 = Gogo::message_name(package->name());
+               std::string n1 = Gogo::message_name(package->package_name());
                std::string n2 = Gogo::message_name(id);
                if (!is_exported)
                  error_at(location,
@@ -2179,6 +2471,7 @@ Parse::operand(bool may_be_sink)
            }
          case Named_object::NAMED_OBJECT_VAR:
          case Named_object::NAMED_OBJECT_RESULT_VAR:
+           this->mark_var_used(named_object);
            return Expression::make_var_reference(named_object, location);
          case Named_object::NAMED_OBJECT_SINK:
            if (may_be_sink)
@@ -2193,18 +2486,32 @@ Parse::operand(bool may_be_sink)
            return Expression::make_func_reference(named_object, NULL,
                                                   location);
          case Named_object::NAMED_OBJECT_UNKNOWN:
-           return Expression::make_unknown_reference(named_object, location);
+           {
+             Unknown_expression* ue =
+               Expression::make_unknown_reference(named_object, location);
+             if (this->is_erroneous_function_)
+               ue->set_no_error_message();
+             return ue;
+           }
+         case Named_object::NAMED_OBJECT_ERRONEOUS:
+           return Expression::make_error(location);
          default:
-           gcc_unreachable();
+           go_unreachable();
          }
       }
-      gcc_unreachable();
+      go_unreachable();
 
     case Token::TOKEN_STRING:
       ret = Expression::make_string(token->string_value(), token->location());
       this->advance_token();
       return ret;
 
+    case Token::TOKEN_CHARACTER:
+      ret = Expression::make_character(token->character_value(), NULL,
+                                      token->location());
+      this->advance_token();
+      return ret;
+
     case Token::TOKEN_INTEGER:
       ret = Expression::make_integer(token->integer_value(), NULL,
                                     token->location());
@@ -2238,7 +2545,7 @@ Parse::operand(bool may_be_sink)
        case KEYWORD_MAP:
        case KEYWORD_STRUCT:
          {
-           source_location location = token->location();
+           Location location = token->location();
            return Expression::make_type(this->type(), location);
          }
        default:
@@ -2250,7 +2557,7 @@ Parse::operand(bool may_be_sink)
       if (token->is_op(OPERATOR_LPAREN))
        {
          this->advance_token();
-         ret = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+         ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL);
          if (!this->peek_token()->is_op(OPERATOR_RPAREN))
            error_at(this->location(), "missing %<)%>");
          else
@@ -2261,7 +2568,7 @@ Parse::operand(bool may_be_sink)
        {
          // Here we call array_type directly, as this is the only
          // case where an ellipsis is permitted for an array type.
-         source_location location = token->location();
+         Location location = token->location();
          return Expression::make_type(this->array_type(true), location);
        }
       break;
@@ -2281,9 +2588,11 @@ Parse::operand(bool may_be_sink)
 
 Expression*
 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
-                              source_location location)
+                              Location location)
 {
-  gcc_assert(var->is_variable() || var->is_result_variable());
+  go_assert(var->is_variable() || var->is_result_variable());
+
+  this->mark_var_used(var);
 
   Named_object* this_function = this->gogo_->current_function();
   Named_object* closure = this_function->func_value()->closure_var();
@@ -2317,7 +2626,9 @@ Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
 // LiteralValue  = "{" [ ElementList [ "," ] ] "}" .
 // ElementList   = Element { "," Element } .
 // Element       = [ Key ":" ] Value .
-// Key           = Expression .
+// Key           = FieldName | ElementIndex .
+// FieldName     = identifier .
+// ElementIndex  = Expression .
 // Value         = Expression | LiteralValue .
 
 // We have already seen the type if there is one, and we are now
@@ -2329,9 +2640,9 @@ Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
 // 1.  In [][][]int{{{1}}} it will be 2.
 
 Expression*
-Parse::composite_lit(Type* type, int depth, source_location location)
+Parse::composite_lit(Type* type, int depth, Location location)
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
+  go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
   this->advance_token();
 
   if (this->peek_token()->is_op(OPERATOR_RCURLY))
@@ -2350,7 +2661,33 @@ Parse::composite_lit(Type* type, int depth, source_location location)
 
       const Token* token = this->peek_token();
 
-      if (!token->is_op(OPERATOR_LCURLY))
+      if (token->is_identifier())
+       {
+         std::string identifier = token->identifier();
+         bool is_exported = token->is_identifier_exported();
+         Location location = token->location();
+
+         if (this->advance_token()->is_op(OPERATOR_COLON))
+           {
+             // This may be a field name.  We don't know for sure--it
+             // could also be an expression for an array index.  We
+             // don't want to parse it as an expression because may
+             // trigger various errors, e.g., if this identifier
+             // happens to be the name of a package.
+             Gogo* gogo = this->gogo_;
+             val = this->id_to_expression(gogo->pack_hidden_name(identifier,
+                                                                 is_exported),
+                                          location);
+           }
+         else
+           {
+             this->unget_token(Token::make_identifier_token(identifier,
+                                                            is_exported,
+                                                            location));
+             val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+           }
+       }
+      else if (!token->is_op(OPERATOR_LCURLY))
        val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
       else
        {
@@ -2426,8 +2763,13 @@ Parse::composite_lit(Type* type, int depth, source_location location)
        }
       else
        {
-         error_at(this->location(), "expected %<,%> or %<}%>");
+         if (token->is_op(OPERATOR_SEMICOLON))
+           error_at(this->location(),
+                    "need trailing comma before newline in composite literal");
+         else
+           error_at(this->location(), "expected %<,%> or %<}%>");
 
+         this->gogo_->mark_locals_used();
          int depth = 0;
          while (!token->is_eof()
                 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
@@ -2454,26 +2796,50 @@ Parse::composite_lit(Type* type, int depth, source_location location)
 Expression*
 Parse::function_lit()
 {
-  source_location location = this->location();
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
+  Location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
   this->advance_token();
 
   Enclosing_vars hold_enclosing_vars;
   hold_enclosing_vars.swap(this->enclosing_vars_);
 
   Function_type* type = this->signature(NULL, location);
+  bool fntype_is_error = false;
+  if (type == NULL)
+    {
+      type = Type::make_function_type(NULL, NULL, NULL, location);
+      fntype_is_error = true;
+    }
 
   // For a function literal, the next token must be a '{'.  If we
   // don't see that, then we may have a type expression.
   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
     return Expression::make_type(type, location);
 
+  bool hold_is_erroneous_function = this->is_erroneous_function_;
+  if (fntype_is_error)
+    this->is_erroneous_function_ = true;
+
+  Bc_stack* hold_break_stack = this->break_stack_;
+  Bc_stack* hold_continue_stack = this->continue_stack_;
+  this->break_stack_ = NULL;
+  this->continue_stack_ = NULL;
+
   Named_object* no = this->gogo_->start_function("", type, true, location);
 
-  source_location end_loc = this->block();
+  Location end_loc = this->block();
 
   this->gogo_->finish_function(end_loc);
 
+  if (this->break_stack_ != NULL)
+    delete this->break_stack_;
+  if (this->continue_stack_ != NULL)
+    delete this->continue_stack_;
+  this->break_stack_ = hold_break_stack;
+  this->continue_stack_ = hold_continue_stack;
+
+  this->is_erroneous_function_ = hold_is_erroneous_function;
+
   hold_enclosing_vars.swap(this->enclosing_vars_);
 
   Expression* closure = this->create_closure(no, &hold_enclosing_vars,
@@ -2490,7 +2856,7 @@ Parse::function_lit()
 
 Expression*
 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
-                     source_location location)
+                     Location location)
 {
   if (enclosing_vars->empty())
     return NULL;
@@ -2511,7 +2877,7 @@ Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
   Expression_list* initializer = new Expression_list;
   for (size_t i = 0; i < enclosing_var_count; ++i)
     {
-      gcc_assert(ev[i].index() == i);
+      go_assert(ev[i].index() == i);
       Named_object* var = ev[i].var();
       Expression* ref;
       if (ev[i].in_function() == enclosing_function)
@@ -2545,7 +2911,7 @@ Expression*
 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
                    bool* is_type_switch)
 {
-  source_location start_loc = this->location();
+  Location start_loc = this->location();
   bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
 
   Expression* ret = this->operand(may_be_sink);
@@ -2569,24 +2935,52 @@ Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
     {
       if (this->peek_token()->is_op(OPERATOR_LCURLY))
        {
-         if (is_parenthesized)
+         if (!may_be_composite_lit)
+           {
+             Type* t = ret->type();
+             if (t->named_type() != NULL
+                 || t->forward_declaration_type() != NULL)
+               error_at(start_loc,
+                        _("parentheses required around this composite literal"
+                          "to avoid parsing ambiguity"));
+           }
+         else if (is_parenthesized)
            error_at(start_loc,
                     "cannot parenthesize type in composite literal");
          ret = this->composite_lit(ret->type(), 0, ret->location());
        }
       else if (this->peek_token()->is_op(OPERATOR_LPAREN))
        {
-         source_location loc = this->location();
+         Location loc = this->location();
          this->advance_token();
          Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
                                              NULL);
+         if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
+           {
+             error_at(this->location(),
+                      "invalid use of %<...%> in type conversion");
+             this->advance_token();
+           }
          if (!this->peek_token()->is_op(OPERATOR_RPAREN))
            error_at(this->location(), "expected %<)%>");
          else
            this->advance_token();
          if (expr->is_error_expression())
-           return expr;
-         ret = Expression::make_cast(ret->type(), expr, loc);
+           ret = expr;
+         else
+           {
+             Type* t = ret->type();
+             if (t->classification() == Type::TYPE_ARRAY
+                 && t->array_type()->length() != NULL
+                 && t->array_type()->length()->is_nil_expression())
+               {
+                 error_at(ret->location(),
+                          "invalid use of %<...%> in type conversion");
+                 ret = Expression::make_error(loc);
+               }
+             else
+               ret = Expression::make_cast(t, expr, loc);
+           }
        }
     }
 
@@ -2623,8 +3017,8 @@ Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
 Expression*
 Parse::selector(Expression* left, bool* is_type_switch)
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_DOT));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_op(OPERATOR_DOT));
+  Location location = this->location();
 
   const Token* token = this->advance_token();
   if (token->is_identifier())
@@ -2647,12 +3041,18 @@ Parse::selector(Expression* left, bool* is_type_switch)
     {
       this->advance_token();
       Type* type = NULL;
-      if (is_type_switch == NULL
-         || !this->peek_token()->is_keyword(KEYWORD_TYPE))
+      if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
        type = this->type();
       else
        {
-         *is_type_switch = true;
+         if (is_type_switch != NULL)
+           *is_type_switch = true;
+         else
+           {
+             error_at(this->location(),
+                      "use of %<.(type)%> outside type switch");
+             type = Type::make_error_type();
+           }
          this->advance_token();
        }
       if (!this->peek_token()->is_op(OPERATOR_RPAREN))
@@ -2676,8 +3076,8 @@ Parse::selector(Expression* left, bool* is_type_switch)
 Expression*
 Parse::index(Expression* expr)
 {
-  source_location location = this->location();
-  gcc_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
+  Location location = this->location();
+  go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
   this->advance_token();
 
   Expression* start;
@@ -2713,13 +3113,13 @@ Parse::index(Expression* expr)
 Expression*
 Parse::call(Expression* func)
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
+  go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
   Expression_list* args = NULL;
   bool is_varargs = false;
   const Token* token = this->advance_token();
   if (!token->is_op(OPERATOR_RPAREN))
     {
-      args = this->expression_list(NULL, false);
+      args = this->expression_list(NULL, false, true);
       token = this->peek_token();
       if (token->is_op(OPERATOR_ELLIPSIS))
        {
@@ -2741,7 +3141,7 @@ Parse::call(Expression* func)
 // Return an expression for a single unqualified identifier.
 
 Expression*
-Parse::id_to_expression(const std::string& name, source_location location)
+Parse::id_to_expression(const std::string& name, Location location)
 {
   Named_object* in_function;
   Named_object* named_object = this->gogo_->lookup(name, &in_function);
@@ -2760,6 +3160,7 @@ Parse::id_to_expression(const std::string& name, source_location location)
       return Expression::make_const_reference(named_object, location);
     case Named_object::NAMED_OBJECT_VAR:
     case Named_object::NAMED_OBJECT_RESULT_VAR:
+      this->mark_var_used(named_object);
       return Expression::make_var_reference(named_object, location);
     case Named_object::NAMED_OBJECT_SINK:
       return Expression::make_sink(location);
@@ -2767,7 +3168,27 @@ Parse::id_to_expression(const std::string& name, source_location location)
     case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
       return Expression::make_func_reference(named_object, NULL, location);
     case Named_object::NAMED_OBJECT_UNKNOWN:
-      return Expression::make_unknown_reference(named_object, location);
+      {
+       Unknown_expression* ue =
+         Expression::make_unknown_reference(named_object, location);
+       if (this->is_erroneous_function_)
+         ue->set_no_error_message();
+       return ue;
+      }
+    case Named_object::NAMED_OBJECT_PACKAGE:
+    case Named_object::NAMED_OBJECT_TYPE:
+    case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
+      {
+       // These cases can arise for a field name in a composite
+       // literal.
+       Unknown_expression* ue =
+         Expression::make_unknown_reference(named_object, location);
+       if (this->is_erroneous_function_)
+         ue->set_no_error_message();
+       return ue;
+      }
+    case Named_object::NAMED_OBJECT_ERRONEOUS:
+      return Expression::make_error(location);
     default:
       error_at(this->location(), "unexpected type of identifier");
       return Expression::make_error(location);
@@ -2814,9 +3235,6 @@ Parse::expression(Precedence precedence, bool may_be_sink,
        case OPERATOR_ANDAND:
          right_precedence = PRECEDENCE_ANDAND;
          break;
-       case OPERATOR_CHANOP:
-         right_precedence = PRECEDENCE_CHANOP;
-         break;
        case OPERATOR_EQEQ:
        case OPERATOR_NOTEQ:
        case OPERATOR_LT:
@@ -2852,7 +3270,7 @@ Parse::expression(Precedence precedence, bool may_be_sink,
        }
 
       Operator op = token->op();
-      source_location binop_location = token->location();
+      Location binop_location = token->location();
 
       if (precedence >= right_precedence)
        {
@@ -2866,11 +3284,8 @@ Parse::expression(Precedence precedence, bool may_be_sink,
       left = this->verify_not_sink(left);
       Expression* right = this->expression(right_precedence, false,
                                           may_be_composite_lit,
-                                          is_type_switch);
-      if (op == OPERATOR_CHANOP)
-       left = Expression::make_send(left, right, binop_location);
-      else
-       left = Expression::make_binary(op, left, right, binop_location);
+                                          NULL);
+      left = Expression::make_binary(op, left, right, binop_location);
     }
 }
 
@@ -2915,12 +3330,13 @@ Parse::expression_may_start_here()
        default:
          return false;
        }
+    case Token::TOKEN_CHARACTER:
     case Token::TOKEN_INTEGER:
     case Token::TOKEN_FLOAT:
     case Token::TOKEN_IMAGINARY:
       return true;
     default:
-      gcc_unreachable();
+      go_unreachable();
     }
 }
 
@@ -2939,6 +3355,61 @@ Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
                  bool* is_type_switch)
 {
   const Token* token = this->peek_token();
+
+  // There is a complex parse for <- chan.  The choices are
+  // Convert x to type <- chan int:
+  //   (<- chan int)(x)         
+  // Receive from (x converted to type chan <- chan int):
+  //   (<- chan <- chan int (x))
+  // Convert x to type <- chan (<- chan int).
+  //   (<- chan <- chan int)(x)
+  if (token->is_op(OPERATOR_CHANOP))
+    {
+      Location location = token->location();
+      if (this->advance_token()->is_keyword(KEYWORD_CHAN))
+       {
+         Expression* expr = this->primary_expr(false, may_be_composite_lit,
+                                               NULL);
+         if (expr->is_error_expression())
+           return expr;
+         else if (!expr->is_type_expression())
+           return Expression::make_receive(expr, location);
+         else
+           {
+             if (expr->type()->is_error_type())
+               return expr;
+
+             // We picked up "chan TYPE", but it is not a type
+             // conversion.
+             Channel_type* ct = expr->type()->channel_type();
+             if (ct == NULL)
+               {
+                 // This is probably impossible.
+                 error_at(location, "expected channel type");
+                 return Expression::make_error(location);
+               }
+             else if (ct->may_receive())
+               {
+                 // <- chan TYPE.
+                 Type* t = Type::make_channel_type(false, true,
+                                                   ct->element_type());
+                 return Expression::make_type(t, location);
+               }
+             else
+               {
+                 // <- chan <- TYPE.  Because we skipped the leading
+                 // <-, we parsed this as chan <- TYPE.  With the
+                 // leading <-, we parse it as <- chan (<- TYPE).
+                 Type *t = this->reassociate_chan_direction(ct, location);
+                 return Expression::make_type(t, location);
+               }
+           }
+       }
+
+      this->unget_token(Token::make_operator_token(OPERATOR_CHANOP, location));
+      token = this->peek_token();
+    }
+
   if (token->is_op(OPERATOR_PLUS)
       || token->is_op(OPERATOR_MINUS)
       || token->is_op(OPERATOR_NOT)
@@ -2947,20 +3418,11 @@ Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
       || token->is_op(OPERATOR_MULT)
       || token->is_op(OPERATOR_AND))
     {
-      source_location location = token->location();
+      Location location = token->location();
       Operator op = token->op();
       this->advance_token();
 
-      if (op == OPERATOR_CHANOP
-         && this->peek_token()->is_keyword(KEYWORD_CHAN))
-       {
-         // This is "<- chan" which must be the start of a type.
-         this->unget_token(Token::make_operator_token(op, location));
-         return Expression::make_type(this->type(), location);
-       }
-
-      Expression* expr = this->unary_expr(false, may_be_composite_lit,
-                                         is_type_switch);
+      Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL);
       if (expr->is_error_expression())
        ;
       else if (op == OPERATOR_MULT && expr->is_type_expression())
@@ -2979,6 +3441,32 @@ Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
                              is_type_switch);
 }
 
+// This is called for the obscure case of
+//   (<- chan <- chan int)(x)
+// In unary_expr we remove the leading <- and parse the remainder,
+// which gives us
+//   chan <- (chan int)
+// When we add the leading <- back in, we really want
+//   <- chan (<- chan int)
+// This means that we need to reassociate.
+
+Type*
+Parse::reassociate_chan_direction(Channel_type *ct, Location location)
+{
+  Channel_type* ele = ct->element_type()->channel_type();
+  if (ele == NULL)
+    {
+      error_at(location, "parse error");
+      return Type::make_error_type();
+    }
+  Type* sub = ele;
+  if (ele->may_send())
+    sub = Type::make_channel_type(false, true, ele->element_type());
+  else
+    sub = this->reassociate_chan_direction(ele, location);
+  return Type::make_channel_type(false, true, sub);
+}
+
 // Statement =
 //     Declaration | LabeledStmt | SimpleStmt |
 //     GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
@@ -2988,7 +3476,7 @@ Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
 // LABEL is the label of this statement if it has one.
 
 void
-Parse::statement(const Label* label)
+Parse::statement(Label* label)
 {
   const Token* token = this->peek_token();
   switch (token->classification())
@@ -3006,7 +3494,7 @@ Parse::statement(const Label* label)
          case KEYWORD_MAP:
          case KEYWORD_STRUCT:
          case KEYWORD_INTERFACE:
-           this->simple_stat(true, false, NULL, NULL);
+           this->simple_stat(true, NULL, NULL, NULL);
            break;
          case KEYWORD_GO:
          case KEYWORD_DEFER:
@@ -3048,7 +3536,7 @@ Parse::statement(const Label* label)
       {
        std::string identifier = token->identifier();
        bool is_exported = token->is_identifier_exported();
-       source_location location = token->location();
+       Location location = token->location();
        if (this->advance_token()->is_op(OPERATOR_COLON))
          {
            this->advance_token();
@@ -3059,7 +3547,7 @@ Parse::statement(const Label* label)
            this->unget_token(Token::make_identifier_token(identifier,
                                                           is_exported,
                                                           location));
-           this->simple_stat(true, false, NULL, NULL);
+           this->simple_stat(true, NULL, NULL, NULL);
          }
       }
       break;
@@ -3067,21 +3555,22 @@ Parse::statement(const Label* label)
     case Token::TOKEN_OPERATOR:
       if (token->is_op(OPERATOR_LCURLY))
        {
-         source_location location = token->location();
+         Location location = token->location();
          this->gogo_->start_block(location);
-         source_location end_loc = this->block();
+         Location end_loc = this->block();
          this->gogo_->add_block(this->gogo_->finish_block(end_loc),
                                 location);
        }
       else if (!token->is_op(OPERATOR_SEMICOLON))
-       this->simple_stat(true, false, NULL, NULL);
+       this->simple_stat(true, NULL, NULL, NULL);
       break;
 
     case Token::TOKEN_STRING:
+    case Token::TOKEN_CHARACTER:
     case Token::TOKEN_INTEGER:
     case Token::TOKEN_FLOAT:
     case Token::TOKEN_IMAGINARY:
-      this->simple_stat(true, false, NULL, NULL);
+      this->simple_stat(true, NULL, NULL, NULL);
       break;
 
     default:
@@ -3137,6 +3626,7 @@ Parse::statement_may_start_here()
        return this->expression_may_start_here();
 
     case Token::TOKEN_STRING:
+    case Token::TOKEN_CHARACTER:
     case Token::TOKEN_INTEGER:
     case Token::TOKEN_FLOAT:
     case Token::TOKEN_IMAGINARY:
@@ -3151,7 +3641,7 @@ Parse::statement_may_start_here()
 // Label       = identifier .
 
 void
-Parse::labeled_stmt(const std::string& label_name, source_location location)
+Parse::labeled_stmt(const std::string& label_name, Location location)
 {
   Label* label = this->gogo_->add_label_definition(label_name, location);
 
@@ -3164,6 +3654,10 @@ Parse::labeled_stmt(const std::string& label_name, source_location location)
 
   if (!this->statement_may_start_here())
     {
+      // Mark the label as used to avoid a useless error about an
+      // unused label.
+      label->set_is_used();
+
       error_at(location, "missing statement after label");
       this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
                                                   location));
@@ -3173,14 +3667,18 @@ Parse::labeled_stmt(const std::string& label_name, source_location location)
   this->statement(label);
 }
 
-// SimpleStat =
-//   ExpressionStat | IncDecStat | Assignment | SimpleVarDecl .
+// SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
+//     Assignment | ShortVarDecl .
+
+// EmptyStmt was handled in Parse::statement.
 
 // In order to make this work for if and switch statements, if
-// RETURN_EXP is true, and we see an ExpressionStat, we return the
+// RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
 // expression rather than adding an expression statement to the
 // current block.  If we see something other than an ExpressionStat,
-// we add the statement and return NULL.
+// we add the statement, set *RETURN_EXP to true if we saw a send
+// statement, and return NULL.  The handling of send statements is for
+// better error messages.
 
 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
 // RangeClause.
@@ -3189,7 +3687,7 @@ Parse::labeled_stmt(const std::string& label_name, source_location location)
 // guard (var := expr.("type") using the literal keyword "type").
 
 Expression*
-Parse::simple_stat(bool may_be_composite_lit, bool return_exp,
+Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
                   Range_clause* p_range_clause, Type_switch* p_type_switch)
 {
   const Token* token = this->peek_token();
@@ -3199,7 +3697,7 @@ Parse::simple_stat(bool may_be_composite_lit, bool return_exp,
     {
       std::string identifier = token->identifier();
       bool is_exported = token->is_identifier_exported();
-      source_location location = token->location();
+      Location location = token->location();
 
       token = this->advance_token();
       if (token->is_op(OPERATOR_COLONEQ)
@@ -3207,6 +3705,7 @@ Parse::simple_stat(bool may_be_composite_lit, bool return_exp,
        {
          identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
          this->simple_var_decl_or_assignment(identifier, location,
+                                             may_be_composite_lit,
                                              p_range_clause,
                                              (token->is_op(OPERATOR_COLONEQ)
                                               ? p_type_switch
@@ -3231,11 +3730,18 @@ Parse::simple_stat(bool may_be_composite_lit, bool return_exp,
       return NULL;
     }
   token = this->peek_token();
-  if (token->is_op(OPERATOR_PLUSPLUS) || token->is_op(OPERATOR_MINUSMINUS))
+  if (token->is_op(OPERATOR_CHANOP))
+    {
+      this->send_stmt(this->verify_not_sink(exp));
+      if (return_exp != NULL)
+       *return_exp = true;
+    }
+  else if (token->is_op(OPERATOR_PLUSPLUS)
+          || token->is_op(OPERATOR_MINUSMINUS))
     this->inc_dec_stat(this->verify_not_sink(exp));
   else if (token->is_op(OPERATOR_COMMA)
           || token->is_op(OPERATOR_EQ))
-    this->assignment(exp, p_range_clause);
+    this->assignment(exp, may_be_composite_lit, p_range_clause);
   else if (token->is_op(OPERATOR_PLUSEQ)
           || token->is_op(OPERATOR_MINUSEQ)
           || token->is_op(OPERATOR_OREQ)
@@ -3247,11 +3753,27 @@ Parse::simple_stat(bool may_be_composite_lit, bool return_exp,
           || token->is_op(OPERATOR_RSHIFTEQ)
           || token->is_op(OPERATOR_ANDEQ)
           || token->is_op(OPERATOR_BITCLEAREQ))
-    this->assignment(this->verify_not_sink(exp), p_range_clause);
-  else if (return_exp)
+    this->assignment(this->verify_not_sink(exp), may_be_composite_lit,
+                    p_range_clause);
+  else if (return_exp != NULL)
     return this->verify_not_sink(exp);
   else
-    this->expression_stat(this->verify_not_sink(exp));
+    {
+      exp = this->verify_not_sink(exp);
+
+      if (token->is_op(OPERATOR_COLONEQ))
+       {
+         if (!exp->is_error_expression())
+           error_at(token->location(), "non-name on left side of %<:=%>");
+         this->gogo_->mark_locals_used();
+         while (!token->is_op(OPERATOR_SEMICOLON)
+                && !token->is_eof())
+           token = this->advance_token();
+         return NULL;
+       }
+
+      this->expression_stat(exp);
+    }
 
   return NULL;
 }
@@ -3297,8 +3819,21 @@ Parse::statement_list_may_start_here()
 void
 Parse::expression_stat(Expression* exp)
 {
-  exp->discarding_value();
-  this->gogo_->add_statement(Statement::make_statement(exp));
+  this->gogo_->add_statement(Statement::make_statement(exp, false));
+}
+
+// SendStmt = Channel "&lt;-" Expression .
+// Channel  = Expression .
+
+void
+Parse::send_stmt(Expression* channel)
+{
+  go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
+  Location loc = this->location();
+  this->advance_token();
+  Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+  Statement* s = Statement::make_send_statement(channel, val, loc);
+  this->gogo_->add_statement(s);
 }
 
 // IncDecStat = Expression ( "++" | "--" ) .
@@ -3317,7 +3852,7 @@ Parse::inc_dec_stat(Expression* exp)
   else if (token->is_op(OPERATOR_MINUSMINUS))
     this->gogo_->add_statement(Statement::make_dec_statement(exp));
   else
-    gcc_unreachable();
+    go_unreachable();
   this->advance_token();
 }
 
@@ -3325,11 +3860,15 @@ Parse::inc_dec_stat(Expression* exp)
 
 // EXP is an expression that we have already parsed.
 
+// If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
+// side may be a composite literal.
+
 // If RANGE_CLAUSE is not NULL, then this will recognize a
 // RangeClause.
 
 void
-Parse::assignment(Expression* expr, Range_clause* p_range_clause)
+Parse::assignment(Expression* expr, bool may_be_composite_lit,
+                 Range_clause* p_range_clause)
 {
   Expression_list* vars;
   if (!this->peek_token()->is_op(OPERATOR_COMMA))
@@ -3340,20 +3879,24 @@ Parse::assignment(Expression* expr, Range_clause* p_range_clause)
   else
     {
       this->advance_token();
-      vars = this->expression_list(expr, true);
+      vars = this->expression_list(expr, true, may_be_composite_lit);
     }
 
-  this->tuple_assignment(vars, p_range_clause);
+  this->tuple_assignment(vars, may_be_composite_lit, p_range_clause);
 }
 
 // An assignment statement.  LHS is the list of expressions which
 // appear on the left hand side.
 
+// If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
+// side may be a composite literal.
+
 // If RANGE_CLAUSE is not NULL, then this will recognize a
 // RangeClause.
 
 void
-Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
+Parse::tuple_assignment(Expression_list* lhs, bool may_be_composite_lit,
+                       Range_clause* p_range_clause)
 {
   const Token* token = this->peek_token();
   if (!token->is_op(OPERATOR_EQ)
@@ -3373,7 +3916,7 @@ Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
       return;
     }
   Operator op = token->op();
-  source_location location = token->location();
+  Location location = token->location();
 
   token = this->advance_token();
 
@@ -3385,7 +3928,8 @@ Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
       return;
     }
 
-  Expression_list* vals = this->expression_list(NULL, false);
+  Expression_list* vals = this->expression_list(NULL, false,
+                                               may_be_composite_lit);
 
   // We've parsed everything; check for errors.
   if (lhs == NULL || vals == NULL)
@@ -3517,12 +4061,12 @@ Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
 void
 Parse::go_or_defer_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_GO)
+  go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
             || this->peek_token()->is_keyword(KEYWORD_DEFER));
   bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
-  source_location stat_location = this->location();
+  Location stat_location = this->location();
   this->advance_token();
-  source_location expr_location = this->location();
+  Location expr_location = this->location();
   Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
   Call_expression* call_expr = expr->call_expression();
   if (call_expr == NULL)
@@ -3549,33 +4093,53 @@ Parse::go_or_defer_stat()
 void
 Parse::return_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
+  Location location = this->location();
   this->advance_token();
   Expression_list* vals = NULL;
   if (this->expression_may_start_here())
-    vals = this->expression_list(NULL, false);
-  const Function* function = this->gogo_->current_function()->func_value();
-  const Typed_identifier_list* results = function->type()->results();
-  this->gogo_->add_statement(Statement::make_return_statement(results, vals,
-                                                             location));
+    vals = this->expression_list(NULL, false, true);
+  this->gogo_->add_statement(Statement::make_return_statement(vals, location));
+
+  if (vals == NULL
+      && this->gogo_->current_function()->func_value()->results_are_named())
+    {
+      Named_object* function = this->gogo_->current_function();
+      Function::Results* results = function->func_value()->result_variables();
+      for (Function::Results::const_iterator p = results->begin();
+          p != results->end();
+          ++p)
+       {
+         Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
+         if (no == NULL)
+           go_assert(saw_errors());
+         else if (!no->is_result_variable())
+           error_at(location, "%qs is shadowed during return",
+                    (*p)->message_name().c_str());
+       }
+    }
 }
 
-// IfStat = "if" [ [ SimpleStat ] ";" ] [ Condition ]
-//             Block [ "else" Statement ] .
+// IfStmt = "if" [ SimpleStmt ";" ] Expression Block
+//          [ "else" ( IfStmt | Block ) ] .
 
 void
 Parse::if_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_IF));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
+  Location location = this->location();
   this->advance_token();
 
   this->gogo_->start_block(location);
 
+  bool saw_simple_stat = false;
   Expression* cond = NULL;
+  bool saw_send_stmt = false;
   if (this->simple_stat_may_start_here())
-    cond = this->simple_stat(false, true, NULL, NULL);
+    {
+      cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
+      saw_simple_stat = true;
+    }
   if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
     {
       // The SimpleStat is an expression statement.
@@ -3586,18 +4150,36 @@ Parse::if_stat()
     {
       if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
        this->advance_token();
-      if (!this->peek_token()->is_op(OPERATOR_LCURLY))
+      else if (saw_simple_stat)
+       {
+         if (saw_send_stmt)
+           error_at(this->location(),
+                    ("send statement used as value; "
+                     "use select for non-blocking send"));
+         else
+           error_at(this->location(),
+                    "expected %<;%> after statement in if expression");
+         if (!this->expression_may_start_here())
+           cond = Expression::make_error(this->location());
+       }
+      if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY))
+       {
+         error_at(this->location(),
+                  "missing condition in if statement");
+         cond = Expression::make_error(this->location());
+       }
+      if (cond == NULL)
        cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
     }
 
   this->gogo_->start_block(this->location());
-  source_location end_loc = this->block();
+  Location end_loc = this->block();
   Block* then_block = this->gogo_->finish_block(end_loc);
 
   // Check for the easy error of a newline before "else".
   if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
     {
-      source_location semi_loc = this->location();
+      Location semi_loc = this->location();
       if (this->advance_token()->is_keyword(KEYWORD_ELSE))
        error_at(this->location(),
                 "unexpected semicolon or newline before %<else%>");
@@ -3609,10 +4191,17 @@ Parse::if_stat()
   Block* else_block = NULL;
   if (this->peek_token()->is_keyword(KEYWORD_ELSE))
     {
-      this->advance_token();
-      // We create a block to gather the statement.
       this->gogo_->start_block(this->location());
-      this->statement(NULL);
+      const Token* token = this->advance_token();
+      if (token->is_keyword(KEYWORD_IF))
+       this->if_stat();
+      else if (token->is_op(OPERATOR_LCURLY))
+       this->block();
+      else
+       {
+         error_at(this->location(), "expected %<if%> or %<{%>");
+         this->statement(NULL);
+       }
       else_block = this->gogo_->finish_block(this->location());
     }
 
@@ -3632,18 +4221,25 @@ Parse::if_stat()
 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
 
 void
-Parse::switch_stat(const Label* label)
+Parse::switch_stat(Label* label)
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
+  Location location = this->location();
   this->advance_token();
 
   this->gogo_->start_block(location);
 
+  bool saw_simple_stat = false;
   Expression* switch_val = NULL;
+  bool saw_send_stmt;
   Type_switch type_switch;
+  bool have_type_switch_block = false;
   if (this->simple_stat_may_start_here())
-    switch_val = this->simple_stat(false, true, NULL, &type_switch);
+    {
+      switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
+                                    &type_switch);
+      saw_simple_stat = true;
+    }
   if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
     {
       // The SimpleStat is an expression statement.
@@ -3654,6 +4250,16 @@ Parse::switch_stat(const Label* label)
     {
       if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
        this->advance_token();
+      else if (saw_simple_stat)
+       {
+         if (saw_send_stmt)
+           error_at(this->location(),
+                    ("send statement used as value; "
+                     "use select for non-blocking send"));
+         else
+           error_at(this->location(),
+                    "expected %<;%> after statement in switch expression");
+       }
       if (!this->peek_token()->is_op(OPERATOR_LCURLY))
        {
          if (this->peek_token()->is_identifier())
@@ -3661,7 +4267,7 @@ Parse::switch_stat(const Label* label)
              const Token* token = this->peek_token();
              std::string identifier = token->identifier();
              bool is_exported = token->is_identifier_exported();
-             source_location id_loc = token->location();
+             Location id_loc = token->location();
 
              token = this->advance_token();
              bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
@@ -3670,14 +4276,24 @@ Parse::switch_stat(const Label* label)
                                                             id_loc));
              if (is_coloneq)
                {
-                 // This must be a TypeSwitchGuard.
-                 switch_val = this->simple_stat(false, true, NULL,
+                 // This must be a TypeSwitchGuard.  It is in a
+                 // different block from any initial SimpleStat.
+                 if (saw_simple_stat)
+                   {
+                     this->gogo_->start_block(id_loc);
+                     have_type_switch_block = true;
+                   }
+
+                 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
                                                 &type_switch);
-                 if (!type_switch.found
-                     && !switch_val->is_error_expression())
+                 if (!type_switch.found)
                    {
-                     error_at(id_loc, "expected type switch assignment");
-                     switch_val = Expression::make_error(id_loc);
+                     if (switch_val == NULL
+                         || !switch_val->is_error_expression())
+                       {
+                         error_at(id_loc, "expected type switch assignment");
+                         switch_val = Expression::make_error(id_loc);
+                       }
                    }
                }
            }
@@ -3697,13 +4313,36 @@ Parse::switch_stat(const Label* label)
 
   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
     {
-      source_location token_loc = this->location();
+      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 %<{%>");
+      else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
+       {
+         error_at(token_loc, "invalid variable name");
+         this->advance_token();
+         this->expression(PRECEDENCE_NORMAL, false, false,
+                          &type_switch.found);
+         if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
+           this->advance_token();
+         if (!this->peek_token()->is_op(OPERATOR_LCURLY))
+           {
+             if (have_type_switch_block)
+               this->gogo_->add_block(this->gogo_->finish_block(location),
+                                      location);
+             this->gogo_->add_block(this->gogo_->finish_block(location),
+                                    location);
+             return;
+           }
+         if (type_switch.found)
+           type_switch.expr = Expression::make_error(location);
+       }
       else
        {
          error_at(this->location(), "expected %<{%>");
+         if (have_type_switch_block)
+           this->gogo_->add_block(this->gogo_->finish_block(this->location()),
+                                  location);
          this->gogo_->add_block(this->gogo_->finish_block(this->location()),
                                 location);
          return;
@@ -3720,6 +4359,10 @@ Parse::switch_stat(const Label* label)
   if (statement != NULL)
     this->gogo_->add_statement(statement);
 
+  if (have_type_switch_block)
+    this->gogo_->add_block(this->gogo_->finish_block(this->location()),
+                          location);
+
   this->gogo_->add_block(this->gogo_->finish_block(this->location()),
                         location);
 }
@@ -3728,8 +4371,8 @@ Parse::switch_stat(const Label* label)
 //   "{" { ExprCaseClause } "}"
 
 Statement*
-Parse::expr_switch_body(const Label* label, Expression* switch_val,
-                       source_location location)
+Parse::expr_switch_body(Label* label, Expression* switch_val,
+                       Location location)
 {
   Switch_statement* statement = Statement::make_switch_statement(switch_val,
                                                                 location);
@@ -3737,6 +4380,7 @@ Parse::expr_switch_body(const Label* label, Expression* switch_val,
   this->push_break_statement(statement, label);
 
   Case_clauses* case_clauses = new Case_clauses();
+  bool saw_default = false;
   while (!this->peek_token()->is_op(OPERATOR_RCURLY))
     {
       if (this->peek_token()->is_eof())
@@ -3745,7 +4389,7 @@ Parse::expr_switch_body(const Label* label, Expression* switch_val,
            error_at(this->location(), "missing %<}%>");
          return NULL;
        }
-      this->expr_case_clause(case_clauses);
+      this->expr_case_clause(case_clauses, &saw_default);
     }
   this->advance_token();
 
@@ -3760,9 +4404,9 @@ Parse::expr_switch_body(const Label* label, Expression* switch_val,
 // FallthroughStat = "fallthrough" .
 
 void
-Parse::expr_case_clause(Case_clauses* clauses)
+Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
 {
-  source_location location = this->location();
+  Location location = this->location();
 
   bool is_default = false;
   Expression_list* vals = this->expr_switch_case(&is_default);
@@ -3792,6 +4436,16 @@ Parse::expr_case_clause(Case_clauses* clauses)
        this->advance_token();
     }
 
+  if (is_default)
+    {
+      if (*saw_default)
+       {
+         error_at(location, "multiple defaults in switch");
+         return;
+       }
+      *saw_default = true;
+    }
+
   if (is_default || vals != NULL)
     clauses->add(vals, is_default, statements, is_fallthrough, location);
 }
@@ -3805,7 +4459,7 @@ Parse::expr_switch_case(bool* is_default)
   if (token->is_keyword(KEYWORD_CASE))
     {
       this->advance_token();
-      return this->expression_list(NULL, false);
+      return this->expression_list(NULL, false, true);
     }
   else if (token->is_keyword(KEYWORD_DEFAULT))
     {
@@ -3827,15 +4481,22 @@ Parse::expr_switch_case(bool* is_default)
 //   "{" { TypeCaseClause } "}" .
 
 Statement*
-Parse::type_switch_body(const Label* label, const Type_switch& type_switch,
-                       source_location location)
+Parse::type_switch_body(Label* label, const Type_switch& type_switch,
+                       Location location)
 {
   Named_object* switch_no = NULL;
   if (!type_switch.name.empty())
     {
-      Variable* switch_var = new Variable(NULL, type_switch.expr, false, false,
-                                         false, type_switch.location);
-      switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
+      if (Gogo::is_sink_name(type_switch.name))
+       error_at(type_switch.location,
+                "no new variables on left side of %<:=%>");
+      else
+       {
+         Variable* switch_var = new Variable(NULL, type_switch.expr, false,
+                                             false, false,
+                                             type_switch.location);
+         switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
+       }
     }
 
   Type_switch_statement* statement =
@@ -3848,6 +4509,7 @@ Parse::type_switch_body(const Label* label, const Type_switch& type_switch,
   this->push_break_statement(statement, label);
 
   Type_case_clauses* case_clauses = new Type_case_clauses();
+  bool saw_default = false;
   while (!this->peek_token()->is_op(OPERATOR_RCURLY))
     {
       if (this->peek_token()->is_eof())
@@ -3855,7 +4517,7 @@ Parse::type_switch_body(const Label* label, const Type_switch& type_switch,
          error_at(this->location(), "missing %<}%>");
          return NULL;
        }
-      this->type_case_clause(switch_no, case_clauses);
+      this->type_case_clause(switch_no, case_clauses, &saw_default);
     }
   this->advance_token();
 
@@ -3869,9 +4531,10 @@ Parse::type_switch_body(const Label* label, const Type_switch& type_switch,
 // TypeCaseClause  = TypeSwitchCase ":" [ StatementList ] .
 
 void
-Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses)
+Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
+                       bool* saw_default)
 {
-  source_location location = this->location();
+  Location location = this->location();
 
   std::vector<Type*> types;
   bool is_default = false;
@@ -3895,7 +4558,15 @@ Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses)
          Variable* v = new Variable(type, init, false, false, false,
                                     location);
          v->set_is_type_switch_var();
-         this->gogo_->add_variable(switch_no->name(), v);
+         Named_object* no = this->gogo_->add_variable(switch_no->name(), v);
+
+         // We don't want to issue an error if the compiler
+         // introduced special variable is not used.  Instead we want
+         // to issue an error if the variable defined by the switch
+         // is not used.  That is handled via type_switch_vars_ and
+         // Parse::mark_var_used.
+         v->set_is_used();
+         this->type_switch_vars_[no] = switch_no;
        }
       this->statement_list();
       statements = this->gogo_->finish_block(this->location());
@@ -3911,7 +4582,13 @@ Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses)
 
   if (is_default)
     {
-      gcc_assert(types.empty());
+      go_assert(types.empty());
+      if (*saw_default)
+       {
+         error_at(location, "multiple defaults in type switch");
+         return;
+       }
+      *saw_default = true;
       clauses->add(NULL, false, true, statements, location);
     }
   else if (!types.empty())
@@ -3922,6 +4599,8 @@ Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses)
        clauses->add(*p, true, false, NULL, location);
       clauses->add(types.back(), false, false, statements, location);
     }
+  else
+    clauses->add(Type::make_error_type(), false, false, statements, location);
 }
 
 // TypeSwitchCase  = "case" type | "default"
@@ -3938,8 +4617,20 @@ Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
       while (true)
        {
          Type* t = this->type();
+
          if (!t->is_error_type())
            types->push_back(t);
+         else
+           {
+             this->gogo_->mark_locals_used();
+             token = this->peek_token();
+             while (!token->is_op(OPERATOR_COLON)
+                    && !token->is_op(OPERATOR_COMMA)
+                    && !token->is_op(OPERATOR_RCURLY)
+                    && !token->is_eof())
+               token = this->advance_token();
+           }
+
          if (!this->peek_token()->is_op(OPERATOR_COMMA))
            break;
          this->advance_token();
@@ -3961,15 +4652,15 @@ Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
 // SelectStat = "select" "{" { CommClause } "}" .
 
 void
-Parse::select_stat(const Label* label)
+Parse::select_stat(Label* label)
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
+  Location location = this->location();
   const Token* token = this->advance_token();
 
   if (!token->is_op(OPERATOR_LCURLY))
     {
-      source_location token_loc = token->location();
+      Location token_loc = token->location();
       if (token->is_op(OPERATOR_SEMICOLON)
          && this->advance_token()->is_op(OPERATOR_LCURLY))
        error_at(token_loc, "unexpected semicolon or newline before %<{%>");
@@ -3986,6 +4677,7 @@ Parse::select_stat(const Label* label)
   this->push_break_statement(statement, label);
 
   Select_clauses* select_clauses = new Select_clauses();
+  bool saw_default = false;
   while (!this->peek_token()->is_op(OPERATOR_RCURLY))
     {
       if (this->peek_token()->is_eof())
@@ -3993,7 +4685,7 @@ Parse::select_stat(const Label* label)
          error_at(this->location(), "expected %<}%>");
          return;
        }
-      this->comm_clause(select_clauses);
+      this->comm_clause(select_clauses, &saw_default);
     }
 
   this->advance_token();
@@ -4005,50 +4697,86 @@ Parse::select_stat(const Label* label)
   this->gogo_->add_statement(statement);
 }
 
-// CommClause = CommCase [ StatementList ] .
+// CommClause = CommCase ":" { Statement ";" } .
 
 void
-Parse::comm_clause(Select_clauses* clauses)
+Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
 {
-  source_location location = this->location();
+  Location location = this->location();
   bool is_send = false;
   Expression* channel = NULL;
   Expression* val = NULL;
+  Expression* closed = NULL;
   std::string varname;
+  std::string closedname;
   bool is_default = false;
-  bool got_case = this->comm_case(&is_send, &channel, &val, &varname,
-                                 &is_default);
+  bool got_case = this->comm_case(&is_send, &channel, &val, &closed,
+                                 &varname, &closedname, &is_default);
 
-  Block* statements = NULL;
-  Named_object* var = NULL;
-  if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
+  if (!is_send
+      && varname.empty()
+      && closedname.empty()
+      && val != NULL
+      && val->index_expression() != NULL)
+    val->index_expression()->set_is_lvalue();
+
+  if (this->peek_token()->is_op(OPERATOR_COLON))
     this->advance_token();
-  else if (this->statement_list_may_start_here())
+  else
+    error_at(this->location(), "expected colon");
+
+  this->gogo_->start_block(this->location());
+
+  Named_object* var = NULL;
+  if (!varname.empty())
     {
-      this->gogo_->start_block(this->location());
+      // FIXME: LOCATION is slightly wrong here.
+      Variable* v = new Variable(NULL, channel, false, false, false,
+                                location);
+      v->set_type_from_chan_element();
+      var = this->gogo_->add_variable(varname, v);
+    }
 
-      if (!varname.empty())
+  Named_object* closedvar = NULL;
+  if (!closedname.empty())
+    {
+      // FIXME: LOCATION is slightly wrong here.
+      Variable* v = new Variable(Type::lookup_bool_type(), NULL,
+                                false, false, false, location);
+      closedvar = this->gogo_->add_variable(closedname, v);
+    }
+
+  this->statement_list();
+
+  Block* statements = this->gogo_->finish_block(this->location());
+
+  if (is_default)
+    {
+      if (*saw_default)
        {
-         // FIXME: LOCATION is slightly wrong here.
-         Variable* v = new Variable(NULL, channel, false, false, false,
-                                    location);
-         v->set_type_from_chan_element();
-         var = this->gogo_->add_variable(varname, v);
+         error_at(location, "multiple defaults in select");
+         return;
        }
-
-      this->statement_list();
-      statements = this->gogo_->finish_block(this->location());
+      *saw_default = true;
     }
 
   if (got_case)
-    clauses->add(is_send, channel, val, var, is_default, statements, location);
+    clauses->add(is_send, channel, val, closed, var, closedvar, is_default,
+                statements, location);
+  else if (statements != NULL)
+    {
+      // Add the statements to make sure that any names they define
+      // are traversed.
+      this->gogo_->add_block(statements, location);
+    }
 }
 
-// CommCase = ( "default" | ( "case" ( SendExpr | RecvExpr) ) ) ":" .
+// CommCase   = "case" ( SendStmt | RecvStmt ) | "default" .
 
 bool
 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
-                std::string* varname, bool* is_default)
+                Expression** closed, std::string* varname,
+                std::string* closedname, bool* is_default)
 {
   const Token* token = this->peek_token();
   if (token->is_keyword(KEYWORD_DEFAULT))
@@ -4059,7 +4787,8 @@ Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
   else if (token->is_keyword(KEYWORD_CASE))
     {
       this->advance_token();
-      if (!this->send_or_recv_expr(is_send, channel, val, varname))
+      if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname,
+                                  closedname))
        return false;
     }
   else
@@ -4070,96 +4799,189 @@ Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
       return false;
     }
 
-  if (!this->peek_token()->is_op(OPERATOR_COLON))
-    {
-      error_at(this->location(), "expected colon");
-      return false;
-    }
-
-  this->advance_token();
-
   return true;
 }
 
-// SendExpr = Expression "<-" Expression .
-// RecvExpr =  [ Expression ( "=" | ":=" ) ] "<-" Expression .
+// RecvStmt   = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
+// RecvExpr   = Expression .
 
 bool
-Parse::send_or_recv_expr(bool* is_send, Expression** channel, Expression** val,
-                        std::string* varname)
+Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val,
+                        Expression** closed, std::string* varname,
+                        std::string* closedname)
 {
   const Token* token = this->peek_token();
-  source_location location = token->location();
+  bool saw_comma = false;
+  bool closed_is_id = false;
   if (token->is_identifier())
     {
+      Gogo* gogo = this->gogo_;
       std::string recv_var = token->identifier();
-      bool is_var_exported = token->is_identifier_exported();
-      if (!this->advance_token()->is_op(OPERATOR_COLONEQ))
-       this->unget_token(Token::make_identifier_token(recv_var,
-                                                      is_var_exported,
-                                                      location));
-      else
+      bool is_rv_exported = token->is_identifier_exported();
+      Location recv_var_loc = token->location();
+      token = this->advance_token();
+      if (token->is_op(OPERATOR_COLONEQ))
        {
-         if (!this->advance_token()->is_op(OPERATOR_CHANOP))
+         // case rv := <-c:
+         this->advance_token();
+         Expression* e = this->expression(PRECEDENCE_NORMAL, false, false,
+                                          NULL);
+         Receive_expression* re = e->receive_expression();
+         if (re == NULL)
            {
-             error_at(this->location(), "expected %<<-%>");
+             if (!e->is_error_expression())
+               error_at(this->location(), "expected receive expression");
              return false;
            }
+         if (recv_var == "_")
+           {
+             error_at(recv_var_loc,
+                      "no new variables on left side of %<:=%>");
+             recv_var = "blank";
+           }
          *is_send = false;
-         *varname = this->gogo_->pack_hidden_name(recv_var, is_var_exported);
-         this->advance_token();
-         *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+         *varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
+         *channel = re->channel();
          return true;
        }
+      else if (token->is_op(OPERATOR_COMMA))
+       {
+         token = this->advance_token();
+         if (token->is_identifier())
+           {
+             std::string recv_closed = token->identifier();
+             bool is_rc_exported = token->is_identifier_exported();
+             Location recv_closed_loc = token->location();
+             closed_is_id = true;
+
+             token = this->advance_token();
+             if (token->is_op(OPERATOR_COLONEQ))
+               {
+                 // case rv, rc := <-c:
+                 this->advance_token();
+                 Expression* e = this->expression(PRECEDENCE_NORMAL, false,
+                                                  false, NULL);
+                 Receive_expression* re = e->receive_expression();
+                 if (re == NULL)
+                   {
+                     if (!e->is_error_expression())
+                       error_at(this->location(),
+                                "expected receive expression");
+                     return false;
+                   }
+                 if (recv_var == "_" && recv_closed == "_")
+                   {
+                     error_at(recv_var_loc,
+                              "no new variables on left side of %<:=%>");
+                     recv_var = "blank";
+                   }
+                 *is_send = false;
+                 if (recv_var != "_")
+                   *varname = gogo->pack_hidden_name(recv_var,
+                                                     is_rv_exported);
+                 if (recv_closed != "_")
+                   *closedname = gogo->pack_hidden_name(recv_closed,
+                                                        is_rc_exported);
+                 *channel = re->channel();
+                 return true;
+               }
+
+             this->unget_token(Token::make_identifier_token(recv_closed,
+                                                            is_rc_exported,
+                                                            recv_closed_loc));
+           }
+
+         *val = this->id_to_expression(gogo->pack_hidden_name(recv_var,
+                                                              is_rv_exported),
+                                       recv_var_loc);
+         saw_comma = true;
+       }
+      else
+       this->unget_token(Token::make_identifier_token(recv_var,
+                                                      is_rv_exported,
+                                                      recv_var_loc));
     }
 
-  if (this->peek_token()->is_op(OPERATOR_CHANOP))
+  // If SAW_COMMA is false, then we are looking at the start of the
+  // send or receive expression.  If SAW_COMMA is true, then *VAL is
+  // set and we just read a comma.
+
+  Expression* e;
+  if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP))
+    e = this->expression(PRECEDENCE_NORMAL, true, true, NULL);
+  else
     {
+      // case <-c:
       *is_send = false;
       this->advance_token();
       *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+
+      // The next token should be ':'.  If it is '<-', then we have
+      // case <-c <- v:
+      // which is to say, send on a channel received from a channel.
+      if (!this->peek_token()->is_op(OPERATOR_CHANOP))
+       return true;
+
+      e = Expression::make_receive(*channel, (*channel)->location());
     }
-  else
-    {
-      Expression* left = this->expression(PRECEDENCE_CHANOP, true, true, NULL);
 
-      if (this->peek_token()->is_op(OPERATOR_EQ))
+  if (this->peek_token()->is_op(OPERATOR_EQ))
+    {
+      if (!this->advance_token()->is_op(OPERATOR_CHANOP))
        {
-         if (!this->advance_token()->is_op(OPERATOR_CHANOP))
-           {
-             error_at(this->location(), "missing %<<-%>");
-             return false;
-           }
-         *is_send = false;
-         *val = left;
-         this->advance_token();
-         *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+         error_at(this->location(), "missing %<<-%>");
+         return false;
        }
-      else if (this->peek_token()->is_op(OPERATOR_CHANOP))
+      *is_send = false;
+      this->advance_token();
+      *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+      if (saw_comma)
        {
-         *is_send = true;
-         *channel = this->verify_not_sink(left);
-         this->advance_token();
-         *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+         // case v, e = <-c:
+         // *VAL is already set.
+         if (!e->is_sink_expression())
+           *closed = e;
        }
       else
        {
-         error_at(this->location(), "expected %<<-%> or %<=%>");
-         return false;
+         // case v = <-c:
+         if (!e->is_sink_expression())
+           *val = e;
        }
+      return true;
     }
 
-  return true;
+  if (saw_comma)
+    {
+      if (closed_is_id)
+       error_at(this->location(), "expected %<=%> or %<:=%>");
+      else
+       error_at(this->location(), "expected %<=%>");
+      return false;
+    }
+
+  if (this->peek_token()->is_op(OPERATOR_CHANOP))
+    {
+      // case c <- v:
+      *is_send = true;
+      *channel = this->verify_not_sink(e);
+      this->advance_token();
+      *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+      return true;
+    }
+
+  error_at(this->location(), "expected %<<-%> or %<=%>");
+  return false;
 }
 
 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
 // Condition = Expression .
 
 void
-Parse::for_stat(const Label* label)
+Parse::for_stat(Label* label)
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
+  Location location = this->location();
   const Token* token = this->advance_token();
 
   // Open a block to hold any variables defined in the init statement
@@ -4186,11 +5008,19 @@ Parse::for_stat(const Label* label)
        {
          // We might be looking at a Condition, an InitStat, or a
          // RangeClause.
-         cond = this->simple_stat(false, true, &range_clause, NULL);
+         bool saw_send_stmt;
+         cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL);
          if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
            {
              if (cond == NULL && !range_clause.found)
-               error_at(this->location(), "parse error in for statement");
+               {
+                 if (saw_send_stmt)
+                   error_at(this->location(),
+                            ("send statement used as value; "
+                             "use select for non-blocking send"));
+                 else
+                   error_at(this->location(), "parse error in for statement");
+               }
            }
          else
            {
@@ -4239,7 +5069,7 @@ Parse::for_stat(const Label* label)
   // For_statement.
 
   this->gogo_->start_block(this->location());
-  source_location end_loc = this->block();
+  Location end_loc = this->block();
   Block* statements = this->gogo_->finish_block(end_loc);
 
   if (sfor != NULL)
@@ -4270,7 +5100,7 @@ Parse::for_stat(const Label* label)
 void
 Parse::for_clause(Expression** cond, Block** post)
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
+  go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
   this->advance_token();
   if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
     *cond = NULL;
@@ -4294,7 +5124,7 @@ Parse::for_clause(Expression** cond, Block** post)
   else
     {
       this->gogo_->start_block(this->location());
-      this->simple_stat(false, false, NULL, NULL);
+      this->simple_stat(false, NULL, NULL, NULL);
       *post = this->gogo_->finish_block(this->location());
     }
 }
@@ -4307,12 +5137,12 @@ void
 Parse::range_clause_decl(const Typed_identifier_list* til,
                         Range_clause* p_range_clause)
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
+  Location location = this->location();
 
   p_range_clause->found = true;
 
-  gcc_assert(til->size() >= 1);
+  go_assert(til->size() >= 1);
   if (til->size() > 2)
     error_at(this->location(), "too many variables for range clause");
 
@@ -4323,7 +5153,8 @@ Parse::range_clause_decl(const Typed_identifier_list* til,
   bool any_new = false;
 
   const Typed_identifier* pti = &til->front();
-  Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new);
+  Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new,
+                                   NULL, NULL);
   if (any_new && no->is_variable())
     no->var_value()->set_type_from_range_index();
   p_range_clause->index = Expression::make_var_reference(no, location);
@@ -4334,7 +5165,7 @@ Parse::range_clause_decl(const Typed_identifier_list* til,
     {
       pti = &til->back();
       bool is_new = false;
-      no = this->init_var(*pti, NULL, expr, true, true, &is_new);
+      no = this->init_var(*pti, NULL, expr, true, true, &is_new, NULL, NULL);
       if (is_new && no->is_variable())
        no->var_value()->set_type_from_range_value();
       if (is_new)
@@ -4353,11 +5184,11 @@ void
 Parse::range_clause_expr(const Expression_list* vals,
                         Range_clause* p_range_clause)
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
 
   p_range_clause->found = true;
 
-  gcc_assert(vals->size() >= 1);
+  go_assert(vals->size() >= 1);
   if (vals->size() > 2)
     error_at(this->location(), "too many variables for range clause");
 
@@ -4375,17 +5206,21 @@ Parse::range_clause_expr(const Expression_list* vals,
 // Push a statement on the break stack.
 
 void
-Parse::push_break_statement(Statement* enclosing, const Label* label)
+Parse::push_break_statement(Statement* enclosing, Label* label)
 {
-  this->break_stack_.push_back(std::make_pair(enclosing, label));
+  if (this->break_stack_ == NULL)
+    this->break_stack_ = new Bc_stack();
+  this->break_stack_->push_back(std::make_pair(enclosing, label));
 }
 
 // Push a statement on the continue stack.
 
 void
-Parse::push_continue_statement(Statement* enclosing, const Label* label)
+Parse::push_continue_statement(Statement* enclosing, Label* label)
 {
-  this->continue_stack_.push_back(std::make_pair(enclosing, label));
+  if (this->continue_stack_ == NULL)
+    this->continue_stack_ = new Bc_stack();
+  this->continue_stack_->push_back(std::make_pair(enclosing, label));
 }
 
 // Pop the break stack.
@@ -4393,7 +5228,7 @@ Parse::push_continue_statement(Statement* enclosing, const Label* label)
 void
 Parse::pop_break_statement()
 {
-  this->break_stack_.pop_back();
+  this->break_stack_->pop_back();
 }
 
 // Pop the continue stack.
@@ -4401,7 +5236,7 @@ Parse::pop_break_statement()
 void
 Parse::pop_continue_statement()
 {
-  this->continue_stack_.pop_back();
+  this->continue_stack_->pop_back();
 }
 
 // Find a break or continue statement given a label name.
@@ -4409,11 +5244,18 @@ Parse::pop_continue_statement()
 Statement*
 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
 {
+  if (bc_stack == NULL)
+    return NULL;
   for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
        p != bc_stack->rend();
        ++p)
-    if (p->second != NULL && p->second->name() == label)
-      return p->first;
+    {
+      if (p->second != NULL && p->second->name() == label)
+       {
+         p->second->set_is_used();
+         return p->first;
+       }
+    }
   return NULL;
 }
 
@@ -4422,30 +5264,33 @@ Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
 void
 Parse::break_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
+  Location location = this->location();
 
   const Token* token = this->advance_token();
   Statement* enclosing;
   if (!token->is_identifier())
     {
-      if (this->break_stack_.empty())
+      if (this->break_stack_ == NULL || this->break_stack_->empty())
        {
          error_at(this->location(),
                   "break statement not within for or switch or select");
          return;
        }
-      enclosing = this->break_stack_.back().first;
+      enclosing = this->break_stack_->back().first;
     }
   else
     {
-      enclosing = this->find_bc_statement(&this->break_stack_,
+      enclosing = this->find_bc_statement(this->break_stack_,
                                          token->identifier());
       if (enclosing == NULL)
        {
-         error_at(token->location(),
-                  ("break label %qs not associated with "
-                   "for or switch or select"),
+         // If there is a label with this name, mark it as used to
+         // avoid a useless error about an unused label.
+         this->gogo_->add_label_reference(token->identifier(),
+                                           Linemap::unknown_location(), false);
+
+         error_at(token->location(), "invalid break label %qs",
                   Gogo::message_name(token->identifier()).c_str());
          this->advance_token();
          return;
@@ -4465,7 +5310,7 @@ Parse::break_stat()
   else if (enclosing->classification() == Statement::STATEMENT_SELECT)
     label = enclosing->select_statement()->break_label();
   else
-    gcc_unreachable();
+    go_unreachable();
 
   this->gogo_->add_statement(Statement::make_break_statement(label,
                                                             location));
@@ -4476,28 +5321,32 @@ Parse::break_stat()
 void
 Parse::continue_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
+  Location location = this->location();
 
   const Token* token = this->advance_token();
   Statement* enclosing;
   if (!token->is_identifier())
     {
-      if (this->continue_stack_.empty())
+      if (this->continue_stack_ == NULL || this->continue_stack_->empty())
        {
          error_at(this->location(), "continue statement not within for");
          return;
        }
-      enclosing = this->continue_stack_.back().first;
+      enclosing = this->continue_stack_->back().first;
     }
   else
     {
-      enclosing = this->find_bc_statement(&this->continue_stack_,
+      enclosing = this->find_bc_statement(this->continue_stack_,
                                          token->identifier());
       if (enclosing == NULL)
        {
-         error_at(token->location(),
-                  "continue label %qs not associated with for",
+         // If there is a label with this name, mark it as used to
+         // avoid a useless error about an unused label.
+         this->gogo_->add_label_reference(token->identifier(),
+                                           Linemap::unknown_location(), false);
+
+         error_at(token->location(), "invalid continue label %qs",
                   Gogo::message_name(token->identifier()).c_str());
          this->advance_token();
          return;
@@ -4511,7 +5360,7 @@ Parse::continue_stat()
   else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
     label = enclosing->for_range_statement()->continue_label();
   else
-    gcc_unreachable();
+    go_unreachable();
 
   this->gogo_->add_statement(Statement::make_continue_statement(label,
                                                                location));
@@ -4522,14 +5371,15 @@ Parse::continue_stat()
 void
 Parse::goto_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
-  source_location location = this->location();
+  go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
+  Location location = this->location();
   const Token* token = this->advance_token();
   if (!token->is_identifier())
     error_at(this->location(), "expected label for goto");
   else
     {
-      Label* label = this->gogo_->add_label_reference(token->identifier());
+      Label* label = this->gogo_->add_label_reference(token->identifier(),
+                                                     location, true);
       Statement* s = Statement::make_goto_statement(label, location);
       this->gogo_->add_statement(s);
       this->advance_token();
@@ -4542,7 +5392,7 @@ void
 Parse::package_clause()
 {
   const Token* token = this->peek_token();
-  source_location location = token->location();
+  Location location = token->location();
   std::string name;
   if (!token->is_keyword(KEYWORD_PACKAGE))
     {
@@ -4576,7 +5426,7 @@ Parse::package_clause()
 void
 Parse::import_decl()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
   this->advance_token();
   this->decl(&Parse::import_spec, NULL);
 }
@@ -4587,7 +5437,7 @@ void
 Parse::import_spec(void*)
 {
   const Token* token = this->peek_token();
-  source_location location = token->location();
+  Location location = token->location();
 
   std::string local_name;
   bool is_local_name_exported = false;
@@ -4605,7 +5455,8 @@ Parse::import_spec(void*)
 
   if (!token->is_string())
     {
-      error_at(this->location(), "missing import package name");
+      error_at(this->location(), "import statement not a string");
+      this->advance_token();
       return;
     }
 
@@ -4648,6 +5499,7 @@ Parse::program()
       else
        {
          error_at(this->location(), "expected declaration");
+         this->gogo_->mark_locals_used();
          do
            this->advance_token();
          while (!this->peek_token()->is_eof()
@@ -4662,8 +5514,13 @@ Parse::program()
        token = this->advance_token();
       else if (!token->is_eof() || !saw_errors())
        {
-         error_at(this->location(),
-                  "expected %<;%> or newline after top level declaration");
+         if (token->is_op(OPERATOR_CHANOP))
+           error_at(this->location(),
+                    ("send statement used as value; "
+                     "use select for non-blocking send"));
+         else
+           error_at(this->location(),
+                    "expected %<;%> or newline after top level declaration");
          this->skip_past_error(OPERATOR_INVALID);
        }
     }
@@ -4701,6 +5558,7 @@ Parse::increment_iota()
 bool
 Parse::skip_past_error(Operator op)
 {
+  this->gogo_->mark_locals_used();
   const Token* token = this->peek_token();
   while (!token->is_op(op))
     {
@@ -4728,3 +5586,22 @@ Parse::verify_not_sink(Expression* expr)
     }
   return expr;
 }
+
+// Mark a variable as used.
+
+void
+Parse::mark_var_used(Named_object* no)
+{
+  if (no->is_variable())
+    {
+      no->var_value()->set_is_used();
+
+      // When a type switch uses := to define a variable, then for
+      // each case with a single type we introduce a new variable with
+      // the appropriate type.  When we do, if the newly introduced
+      // variable is used, then the type switch variable is used.
+      Type_switch_vars::iterator p = this->type_switch_vars_.find(no);
+      if (p != this->type_switch_vars_.end())
+       p->second->var_value()->set_is_used();
+    }
+}