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
This commit is contained in:
Daniel Morsing 2012-12-12 16:43:54 +01:00
Родитель 11d96dd7f5
Коммит bf59aafddc
5 изменённых файлов: 1935 добавлений и 1567 удалений

Просмотреть файл

@ -54,7 +54,7 @@ static void fixlbrace(int);
%type <node> stmt ntype
%type <node> arg_type
%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> fndcl hidden_fndcl fnliteral
%type <node> for_body for_header for_stmt if_header if_stmt non_dcl_stmt
@ -974,6 +974,29 @@ keyval:
$$ = 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:
expr
| '{' start_complit braced_keyval_list '}'
@ -1761,7 +1784,7 @@ keyval_list:
{
$$ = list1($1);
}
| complitexpr
| bare_complitexpr
{
$$ = list1($1);
}
@ -1769,7 +1792,7 @@ keyval_list:
{
$$ = list($1, $3);
}
| keyval_list ',' complitexpr
| keyval_list ',' bare_complitexpr
{
$$ = list($1, $3);
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -1,8 +1,8 @@
/* 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
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
it under the terms of the GNU General Public License as published by
@ -30,15 +30,6 @@
This special exception was added by the Free Software Foundation in
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. */
#ifndef YYTOKENTYPE
@ -150,10 +141,12 @@ extern int yydebug;
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
/* Line 2042 of yacc.c */
/* Line 2068 of yacc.c */
#line 28 "go.y"
Node* node;
@ -164,8 +157,9 @@ typedef union YYSTYPE
int i;
/* Line 2042 of yacc.c */
#line 169 "y.tab.h"
/* Line 2068 of yacc.c */
#line 163 "y.tab.h"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
@ -174,18 +168,4 @@ typedef union YYSTYPE
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 {
112, LNAME,
"nested func not allowed",
639, ';',
641, ';',
"else must be followed by if or statement block"
};

Просмотреть файл

@ -0,0 +1,23 @@
// 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"
}