]> Cypherpunks repositories - gostls13.git/commitdiff
gc: use correct line number for EOF syntax error
authorAnthony Martin <ality@pbrane.org>
Thu, 26 Apr 2012 09:57:23 +0000 (02:57 -0700)
committerAnthony Martin <ality@pbrane.org>
Thu, 26 Apr 2012 09:57:23 +0000 (02:57 -0700)
I also added some debugging code that's turned
on with -xx.

Fixes #3392.

R=rsc, lvd
CC=golang-dev
https://golang.org/cl/5909058

src/cmd/gc/lex.c
src/cmd/gc/subr.c
test/fixedbugs/bug435.go [new file with mode: 0644]

index e71fd3848e7fd200fb3ba768bdfffa0ce30f8b10..6dc36e47c017576517772b56f3b4b9eebd28d3c9 100644 (file)
@@ -1531,9 +1531,7 @@ getc(void)
        if(c != 0) {
                curio.peekc = curio.peekc1;
                curio.peekc1 = 0;
-               if(c == '\n' && pushedio.bin == nil)
-                       lexlineno++;
-               return c;
+               goto check;
        }
        
        if(curio.bin == nil) {
@@ -1543,6 +1541,7 @@ getc(void)
        } else
                c = Bgetc(curio.bin);
 
+check:
        switch(c) {
        case 0:
                if(curio.bin != nil) {
index 681c023a01800ece7ef03c9d7e2bba4b69c007d8..9542b2e718d5218f8ea331f2db3344e8896531ba 100644 (file)
@@ -142,6 +142,11 @@ yyerror(char *fmt, ...)
                if(debug['x'])  
                        print("yyerror: yystate=%d yychar=%d\n", yystate, yychar);
 
+               // An unexpected EOF caused a syntax error. Use the previous
+               // line number since getc generated a fake newline character.
+               if(curio.eofnl)
+                       lexlineno = prevlineno;
+
                // only one syntax error per line
                if(lastsyntax == lexlineno)
                        return;
diff --git a/test/fixedbugs/bug435.go b/test/fixedbugs/bug435.go
new file mode 100644 (file)
index 0000000..e6af452
--- /dev/null
@@ -0,0 +1,15 @@
+// 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.
+
+// Test that a syntax error caused by an unexpected EOF
+// gives an error message with the correct line number.
+//
+// https://code.google.com/p/go/issues/detail?id=3392
+
+package main
+
+func foo() {
+       bar(1, // ERROR "unexpected"