Skip to content

Commit

Permalink
cmd/compile: fix ICE due to bad rune width
Browse files Browse the repository at this point in the history
It was possible that

    var X interface{} = 'x'

could cause a compilation failure due to having not calculated rune's
width yet. typecheck.go normally calculates the width of things, but
it doesn't for implicit conversions to default type. We already
compute the width of all of the standard numeric types in universe.go,
but we failed to calculate it for the rune alias type. So we could
later crash if the code never otherwise explicitly mentioned 'rune'.

While here, explicitly compute widths for 'byte' and 'error' for
consistency.

Fixes #29350.

Change-Id: Ifedd4899527c983ee5258dcf75aaf635b6f812f8
Reviewed-on: https://go-review.googlesource.com/c/155380
Reviewed-by: Josh Bleecher Snyder <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
mdempsky committed Dec 20, 2018
1 parent 8ff04a9 commit 706b54b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/cmd/compile/internal/gc/universe.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ func lexinit1() {
types.Errortype.Sym = s
types.Errortype.Orig = makeErrorInterface()
s.Def = asTypesNode(typenod(types.Errortype))
dowidth(types.Errortype)

// We create separate byte and rune types for better error messages
// rather than just creating type alias *types.Sym's for the uint8 and
Expand All @@ -401,13 +402,15 @@ func lexinit1() {
types.Bytetype.Sym = s
s.Def = asTypesNode(typenod(types.Bytetype))
asNode(s.Def).Name = new(Name)
dowidth(types.Bytetype)

// rune alias
s = builtinpkg.Lookup("rune")
types.Runetype = types.New(TINT32)
types.Runetype.Sym = s
s.Def = asTypesNode(typenod(types.Runetype))
asNode(s.Def).Name = new(Name)
dowidth(types.Runetype)

// backend-dependent builtin types (e.g. int).
for _, s := range typedefs {
Expand Down
9 changes: 9 additions & 0 deletions test/fixedbugs/issue29350.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// compile

// Copyright 2018 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

var X interface{} = 'x'

0 comments on commit 706b54b

Please sign in to comment.