Skip to content

Commit 762953b

Browse files
committed
cmd/compile: disable Go1.13 language features for -lang=go1.12 and below
Fixes #31747. Updates #19308. Updates #12711. Updates #29008. Updates #28493. Updates #19113. Change-Id: I76d2fdbc7698cc4e0f31b7ae24cbb4d28afbb6a3 Reviewed-on: https://go-review.googlesource.com/c/go/+/174897 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent e5f0d14 commit 762953b

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,23 +1310,55 @@ func (p *noder) binOp(op syntax.Operator) Op {
13101310
return binOps[op]
13111311
}
13121312

1313+
// checkLangCompat reports an error if the representation of a numeric
1314+
// literal is not compatible with the current language version.
1315+
func checkLangCompat(lit *syntax.BasicLit) {
1316+
s := lit.Value
1317+
if len(s) <= 2 || langSupported(1, 13) {
1318+
return
1319+
}
1320+
// len(s) > 2
1321+
if strings.Contains(s, "_") {
1322+
yyerror("underscores in numeric literals only supported as of -lang=go1.13")
1323+
return
1324+
}
1325+
if s[0] != '0' {
1326+
return
1327+
}
1328+
base := s[1]
1329+
if base == 'b' || base == 'B' {
1330+
yyerror("binary literals only supported as of -lang=go1.13")
1331+
return
1332+
}
1333+
if base == 'o' || base == 'O' {
1334+
yyerror("0o/0O-style octal literals only supported as of -lang=go1.13")
1335+
return
1336+
}
1337+
if lit.Kind != syntax.IntLit && (base == 'x' || base == 'X') {
1338+
yyerror("hexadecimal floating-point literals only supported as of -lang=go1.13")
1339+
}
1340+
}
1341+
13131342
func (p *noder) basicLit(lit *syntax.BasicLit) Val {
13141343
// TODO: Don't try to convert if we had syntax errors (conversions may fail).
13151344
// Use dummy values so we can continue to compile. Eventually, use a
13161345
// form of "unknown" literals that are ignored during type-checking so
13171346
// we can continue type-checking w/o spurious follow-up errors.
13181347
switch s := lit.Value; lit.Kind {
13191348
case syntax.IntLit:
1349+
checkLangCompat(lit)
13201350
x := new(Mpint)
13211351
x.SetString(s)
13221352
return Val{U: x}
13231353

13241354
case syntax.FloatLit:
1355+
checkLangCompat(lit)
13251356
x := newMpflt()
13261357
x.SetString(s)
13271358
return Val{U: x}
13281359

13291360
case syntax.ImagLit:
1361+
checkLangCompat(lit)
13301362
x := newMpcmplx()
13311363
x.Imag.SetString(strings.TrimSuffix(s, "i"))
13321364
return Val{U: x}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,11 @@ func typecheck1(n *Node, top int) (res *Node) {
631631
n.Type = nil
632632
return n
633633
}
634-
634+
if t.IsSigned() && !langSupported(1, 13) {
635+
yyerror("invalid operation: %v (signed shift count type %v, only supported as of -lang=go1.13)", n, r.Type)
636+
n.Type = nil
637+
return n
638+
}
635639
t = l.Type
636640
if t != nil && t.Etype != TIDEAL && !t.IsInteger() {
637641
yyerror("invalid operation: %v (shift of type %v)", n, t)

src/go/types/stdlib_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ func TestStdFixed(t *testing.T) {
180180
"issue22200b.go", // go/types does not have constraints on stack size
181181
"issue25507.go", // go/types does not have constraints on stack size
182182
"issue20780.go", // go/types does not have constraints on stack size
183+
"issue31747.go", // go/types does not have constraints on language level (-lang=go1.12) (see #31793)
183184
)
184185
}
185186

test/fixedbugs/issue31747.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// errorcheck -lang=go1.12
2+
3+
// Copyright 2019 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
// numeric literals
10+
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"
14+
_ = 0xabc // ok
15+
_ = 0x0p1 // ERROR "hexadecimal floating-point literals only supported as of -lang=go1.13"
16+
17+
_ = 0B111 // ERROR "binary"
18+
_ = 0O567 // ERROR "octal"
19+
_ = 0Xabc // ok
20+
_ = 0X0P1 // ERROR "hexadecimal floating-point"
21+
22+
_ = 1_000i // ERROR "underscores"
23+
_ = 0b111i // ERROR "binary"
24+
_ = 0o567i // ERROR "octal"
25+
_ = 0xabci // ERROR "hexadecimal floating-point"
26+
_ = 0x0p1i // ERROR "hexadecimal floating-point"
27+
)
28+
29+
// signed shift counts
30+
var (
31+
s int
32+
_ = 1 << s // ERROR "signed shift count type int, only supported as of -lang=go1.13"
33+
_ = 1 >> s // ERROR "signed shift count"
34+
)

0 commit comments

Comments
 (0)