Commit bf59aafd authored by Daniel Morsing's avatar Daniel Morsing

cmd/gc: Give better line numbers for errors in composite literals.

Credit to Russ for suggesting this fix.

Fixes #3925.

R=golang-dev, franciscossouza, rsc
CC=golang-dev
https://golang.org/cl/6920051
parent 11d96dd7
...@@ -54,7 +54,7 @@ static void fixlbrace(int); ...@@ -54,7 +54,7 @@ static void fixlbrace(int);
%type <node> stmt ntype %type <node> stmt ntype
%type <node> arg_type %type <node> arg_type
%type <node> case caseblock %type <node> case caseblock
%type <node> compound_stmt dotname embed expr complitexpr %type <node> compound_stmt dotname embed expr complitexpr bare_complitexpr
%type <node> expr_or_type %type <node> expr_or_type
%type <node> fndcl hidden_fndcl fnliteral %type <node> fndcl hidden_fndcl fnliteral
%type <node> for_body for_header for_stmt if_header if_stmt non_dcl_stmt %type <node> for_body for_header for_stmt if_header if_stmt non_dcl_stmt
...@@ -974,6 +974,29 @@ keyval: ...@@ -974,6 +974,29 @@ keyval:
$$ = nod(OKEY, $1, $3); $$ = nod(OKEY, $1, $3);
} }
bare_complitexpr:
expr
{
// These nodes do not carry line numbers.
// Since a composite literal commonly spans several lines,
// the line number on errors may be misleading.
// Introduce a wrapper node to give the correct line.
$$ = $1;
switch($$->op) {
case ONAME:
case ONONAME:
case OTYPE:
case OPACK:
case OLITERAL:
$$ = nod(OPAREN, $$, N);
}
}
| '{' start_complit braced_keyval_list '}'
{
$$ = $2;
$$->list = $3;
}
complitexpr: complitexpr:
expr expr
| '{' start_complit braced_keyval_list '}' | '{' start_complit braced_keyval_list '}'
...@@ -1761,7 +1784,7 @@ keyval_list: ...@@ -1761,7 +1784,7 @@ keyval_list:
{ {
$$ = list1($1); $$ = list1($1);
} }
| complitexpr | bare_complitexpr
{ {
$$ = list1($1); $$ = list1($1);
} }
...@@ -1769,7 +1792,7 @@ keyval_list: ...@@ -1769,7 +1792,7 @@ keyval_list:
{ {
$$ = list($1, $3); $$ = list($1, $3);
} }
| keyval_list ',' complitexpr | keyval_list ',' bare_complitexpr
{ {
$$ = list($1, $3); $$ = list($1, $3);
} }
......
This diff is collapsed.
/* A Bison parser, made by GNU Bison 2.6.5. */ /* A Bison parser, made by GNU Bison 2.5. */
/* Bison interface for Yacc-like parsers in C /* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. Copyright (C) 1984, 1989-1990, 2000-2011 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -30,15 +30,6 @@ ...@@ -30,15 +30,6 @@
This special exception was added by the Free Software Foundation in This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */ version 2.2 of Bison. */
#ifndef YY_YY_Y_TAB_H_INCLUDED
# define YY_YY_Y_TAB_H_INCLUDED
/* Enabling traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int yydebug;
#endif
/* Tokens. */ /* Tokens. */
#ifndef YYTOKENTYPE #ifndef YYTOKENTYPE
...@@ -150,10 +141,12 @@ extern int yydebug; ...@@ -150,10 +141,12 @@ extern int yydebug;
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE typedef union YYSTYPE
{ {
/* Line 2042 of yacc.c */
/* Line 2068 of yacc.c */
#line 28 "go.y" #line 28 "go.y"
Node* node; Node* node;
...@@ -164,8 +157,9 @@ typedef union YYSTYPE ...@@ -164,8 +157,9 @@ typedef union YYSTYPE
int i; int i;
/* Line 2042 of yacc.c */
#line 169 "y.tab.h" /* Line 2068 of yacc.c */
#line 163 "y.tab.h"
} YYSTYPE; } YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1 # define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */ # define yystype YYSTYPE /* obsolescent; will be withdrawn */
...@@ -174,18 +168,4 @@ typedef union YYSTYPE ...@@ -174,18 +168,4 @@ typedef union YYSTYPE
extern YYSTYPE yylval; extern YYSTYPE yylval;
#ifdef YYPARSE_PARAM
#if defined __STDC__ || defined __cplusplus
int yyparse (void *YYPARSE_PARAM);
#else
int yyparse ();
#endif
#else /* ! YYPARSE_PARAM */
#if defined __STDC__ || defined __cplusplus
int yyparse (void);
#else
int yyparse ();
#endif
#endif /* ! YYPARSE_PARAM */
#endif /* !YY_YY_Y_TAB_H_INCLUDED */
...@@ -71,6 +71,6 @@ static struct { ...@@ -71,6 +71,6 @@ static struct {
112, LNAME, 112, LNAME,
"nested func not allowed", "nested func not allowed",
639, ';', 641, ';',
"else must be followed by if or statement block" "else must be followed by if or statement block"
}; };
// errorcheck
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Issue 3925: wrong line number for error message "missing key in map literal"
// also a test for correct line number in other malformed composite literals.
package foo
var _ = map[string]string{
"1": "2",
"3", "4", // ERROR "missing key"
}
var _ = []string{
"foo",
"bar",
20, // ERROR "cannot use"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment