Skip to content

Commit 93014c5

Browse files
[dev.go2go] go/go2go: avoid crashing on predeclared error type
It has no package, so we need to check for Pkg() == nil. Fixes #39878 Change-Id: I14a47c5098f97ce42dfa8cc875819f244a2b0201 Reviewed-on: https://go-review.googlesource.com/c/go/+/240013 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 1b08ace commit 93014c5

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/go/go2go/rewrite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ func (t *translator) instantiationTypes(call *ast.CallExpr) (argList []ast.Expr,
10311031
// Instantiating with a locally defined type won't work.
10321032
// Check that here.
10331033
for i, typ := range typeList {
1034-
if named, ok := typ.(*types.Named); ok {
1034+
if named, ok := typ.(*types.Named); ok && named.Obj().Pkg() != nil {
10351035
if scope := named.Obj().Parent(); scope != nil && scope != named.Obj().Pkg().Scope() {
10361036
var pos token.Pos
10371037
if haveInferred {

src/go/go2go/types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ func (t *translator) addTypePackages(typ types.Type) {
299299
t.addTypePackages(typ.Elem())
300300
case *types.Named:
301301
// This is the point of this whole method.
302-
t.typePackages[typ.Obj().Pkg()] = true
302+
if typ.Obj().Pkg() != nil {
303+
t.typePackages[typ.Obj().Pkg()] = true
304+
}
303305
case *types.TypeParam:
304306
default:
305307
panic(fmt.Sprintf("unimplemented Type %T", typ))

test/gen/g038.go2

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// compile
2+
3+
// Copyright 2020 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 main
8+
9+
func F(type T comparable)(a T) bool {
10+
return a == a
11+
}
12+
13+
func G() {
14+
F(error(nil))
15+
}

0 commit comments

Comments
 (0)