Skip to content

Commit

Permalink
cmd/compile: disable Go1.13 language features for -lang=go1.12 and below
Browse files Browse the repository at this point in the history
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 <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
  • Loading branch information
griesemer committed May 2, 2019
1 parent e5f0d14 commit 762953b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/cmd/compile/internal/gc/noder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1310,23 +1310,55 @@ func (p *noder) binOp(op syntax.Operator) Op {
return binOps[op]
}

// checkLangCompat reports an error if the representation of a numeric
// literal is not compatible with the current language version.
func checkLangCompat(lit *syntax.BasicLit) {
s := lit.Value
if len(s) <= 2 || langSupported(1, 13) {
return
}
// len(s) > 2
if strings.Contains(s, "_") {
yyerror("underscores in numeric literals only supported as of -lang=go1.13")
return
}
if s[0] != '0' {
return
}
base := s[1]
if base == 'b' || base == 'B' {
yyerror("binary literals only supported as of -lang=go1.13")
return
}
if base == 'o' || base == 'O' {
yyerror("0o/0O-style octal literals only supported as of -lang=go1.13")
return
}
if lit.Kind != syntax.IntLit && (base == 'x' || base == 'X') {
yyerror("hexadecimal floating-point literals only supported as of -lang=go1.13")
}
}

func (p *noder) basicLit(lit *syntax.BasicLit) Val {
// TODO: Don't try to convert if we had syntax errors (conversions may fail).
// Use dummy values so we can continue to compile. Eventually, use a
// form of "unknown" literals that are ignored during type-checking so
// we can continue type-checking w/o spurious follow-up errors.
switch s := lit.Value; lit.Kind {
case syntax.IntLit:
checkLangCompat(lit)
x := new(Mpint)
x.SetString(s)
return Val{U: x}

case syntax.FloatLit:
checkLangCompat(lit)
x := newMpflt()
x.SetString(s)
return Val{U: x}

case syntax.ImagLit:
checkLangCompat(lit)
x := newMpcmplx()
x.Imag.SetString(strings.TrimSuffix(s, "i"))
return Val{U: x}
Expand Down
6 changes: 5 additions & 1 deletion src/cmd/compile/internal/gc/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,11 @@ func typecheck1(n *Node, top int) (res *Node) {
n.Type = nil
return n
}

if t.IsSigned() && !langSupported(1, 13) {
yyerror("invalid operation: %v (signed shift count type %v, only supported as of -lang=go1.13)", n, r.Type)
n.Type = nil
return n
}
t = l.Type
if t != nil && t.Etype != TIDEAL && !t.IsInteger() {
yyerror("invalid operation: %v (shift of type %v)", n, t)
Expand Down
1 change: 1 addition & 0 deletions src/go/types/stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func TestStdFixed(t *testing.T) {
"issue22200b.go", // go/types does not have constraints on stack size
"issue25507.go", // go/types does not have constraints on stack size
"issue20780.go", // go/types does not have constraints on stack size
"issue31747.go", // go/types does not have constraints on language level (-lang=go1.12) (see #31793)
)
}

Expand Down
34 changes: 34 additions & 0 deletions test/fixedbugs/issue31747.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// errorcheck -lang=go1.12

// Copyright 2019 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.

package p

// numeric literals
const (
_ = 1_000 // ERROR "underscores in numeric literals only supported as of -lang=go1.13"
_ = 0b111 // ERROR "binary literals only supported as of -lang=go1.13"
_ = 0o567 // ERROR "0o/0O-style octal literals only supported as of -lang=go1.13"
_ = 0xabc // ok
_ = 0x0p1 // ERROR "hexadecimal floating-point literals only supported as of -lang=go1.13"

_ = 0B111 // ERROR "binary"
_ = 0O567 // ERROR "octal"
_ = 0Xabc // ok
_ = 0X0P1 // ERROR "hexadecimal floating-point"

_ = 1_000i // ERROR "underscores"
_ = 0b111i // ERROR "binary"
_ = 0o567i // ERROR "octal"
_ = 0xabci // ERROR "hexadecimal floating-point"
_ = 0x0p1i // ERROR "hexadecimal floating-point"
)

// signed shift counts
var (
s int
_ = 1 << s // ERROR "signed shift count type int, only supported as of -lang=go1.13"
_ = 1 >> s // ERROR "signed shift count"
)

0 comments on commit 762953b

Please sign in to comment.