Commit 7ebb96a4 authored by Didier Spezia's avatar Didier Spezia Committed by Russ Cox

cmd/compile/internal/gc: fix panic in Type Stringer

The following code:

func n() {(interface{int})}

generates:

3: interface contains embedded non-interface int
3: type %!v(PANIC=runtime error: invalid memory address or nil pointer dereference) is not an expression

It is because the corresponding symbol (Sym field in Type object)
is nil, resulting in a panic in typefmt.

Just skip the symbol if it is nil, so that the error message becomes:

3: interface contains embedded non-interface int
3: type interface { int } is not an expression

Fixes #11614

Change-Id: I219ae7eb01edca264fad1d4a1bd261d026294b00
Reviewed-on: https://go-review.googlesource.com/14015Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
parent 423b0cc2
......@@ -579,9 +579,14 @@ func typefmt(t *Type, flag int) string {
buf.WriteString("interface {")
for t1 := t.Type; t1 != nil; t1 = t1.Down {
buf.WriteString(" ")
if exportname(t1.Sym.Name) {
switch {
case t1.Sym == nil:
// Check first that a symbol is defined for this type.
// Wrong interface definitions may have types lacking a symbol.
break
case exportname(t1.Sym.Name):
buf.WriteString(Sconv(t1.Sym, obj.FmtShort))
} else {
default:
buf.WriteString(Sconv(t1.Sym, obj.FmtUnsigned))
}
buf.WriteString(Tconv(t1.Type, obj.FmtShort))
......
// errorcheck
// Copyright 2015 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 incorrect expressions involving wrong anonymous interface
// do not generate panics in Type Stringer.
// Does not compile.
package main
type I interface {
int // ERROR "interface contains embedded non-interface int"
}
func n() {
(I) // ERROR "type I is not an expression"
}
func m() {
(interface{int}) // ERROR "interface contains embedded non-interface int" "type interface { int } is not an expression"
}
func main() {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment