Skip to content

Commit 27fc32f

Browse files
committed
cmd/compile: better error message for language version errors
Fixes #33753. Updates #31747. Change-Id: Icc42b23405ead4f7f17b0ffa3611405454b6b271 Reviewed-on: https://go-review.googlesource.com/c/go/+/198491 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 64e598f commit 27fc32f

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

src/cmd/compile/internal/gc/noder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,23 +1327,23 @@ func checkLangCompat(lit *syntax.BasicLit) {
13271327
}
13281328
// len(s) > 2
13291329
if strings.Contains(s, "_") {
1330-
yyerror("underscores in numeric literals only supported as of -lang=go1.13")
1330+
yyerrorv("go1.13", "underscores in numeric literals")
13311331
return
13321332
}
13331333
if s[0] != '0' {
13341334
return
13351335
}
13361336
base := s[1]
13371337
if base == 'b' || base == 'B' {
1338-
yyerror("binary literals only supported as of -lang=go1.13")
1338+
yyerrorv("go1.13", "binary literals")
13391339
return
13401340
}
13411341
if base == 'o' || base == 'O' {
1342-
yyerror("0o/0O-style octal literals only supported as of -lang=go1.13")
1342+
yyerrorv("go1.13", "0o/0O-style octal literals")
13431343
return
13441344
}
13451345
if lit.Kind != syntax.IntLit && (base == 'x' || base == 'X') {
1346-
yyerror("hexadecimal floating-point literals only supported as of -lang=go1.13")
1346+
yyerrorv("go1.13", "hexadecimal floating-point literals")
13471347
}
13481348
}
13491349

src/cmd/compile/internal/gc/subr.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ func yyerrorl(pos src.XPos, format string, args ...interface{}) {
154154
}
155155
}
156156

157+
func yyerrorv(lang string, format string, args ...interface{}) {
158+
what := fmt.Sprintf(format, args...)
159+
yyerrorl(lineno, "%s requires %s or later (-lang was set to %s; check go.mod)", what, lang, flag_lang)
160+
}
161+
157162
func yyerror(format string, args ...interface{}) {
158163
yyerrorl(lineno, format, args...)
159164
}

src/cmd/compile/internal/gc/typecheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ func typecheck1(n *Node, top int) (res *Node) {
604604
return n
605605
}
606606
if t.IsSigned() && !langSupported(1, 13) {
607-
yyerror("invalid operation: %v (signed shift count type %v, only supported as of -lang=go1.13)", n, r.Type)
607+
yyerrorv("go1.13", "invalid operation: %v (signed shift count type %v)", n, r.Type)
608608
n.Type = nil
609609
return n
610610
}

test/fixedbugs/issue31747.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ package p
88

99
// numeric literals
1010
const (
11-
_ = 1_000 // ERROR "underscores in numeric literals only supported as of -lang=go1.13"
12-
_ = 0b111 // ERROR "binary literals only supported as of -lang=go1.13"
13-
_ = 0o567 // ERROR "0o/0O-style octal literals only supported as of -lang=go1.13"
11+
_ = 1_000 // ERROR "underscores in numeric literals requires go1.13 or later \(-lang was set to go1.12; check go.mod\)"
12+
_ = 0b111 // ERROR "binary literals requires go1.13 or later"
13+
_ = 0o567 // ERROR "0o/0O-style octal literals requires go1.13 or later"
1414
_ = 0xabc // ok
15-
_ = 0x0p1 // ERROR "hexadecimal floating-point literals only supported as of -lang=go1.13"
15+
_ = 0x0p1 // ERROR "hexadecimal floating-point literals requires go1.13 or later"
1616

1717
_ = 0B111 // ERROR "binary"
1818
_ = 0O567 // ERROR "octal"
@@ -29,6 +29,6 @@ const (
2929
// signed shift counts
3030
var (
3131
s int
32-
_ = 1 << s // ERROR "signed shift count type int, only supported as of -lang=go1.13"
32+
_ = 1 << s // ERROR "invalid operation: 1 << s \(signed shift count type int\) requires go1.13 or later"
3333
_ = 1 >> s // ERROR "signed shift count"
3434
)

0 commit comments

Comments
 (0)