Skip to content

Commit c6cc483

Browse files
tdakkotadominikh
authored andcommitted
go/ir: do not panic on generic type slice expression
Closes: gh-1270 Closes: gh-1271 [via git-merge-pr] Co-authored-by: Dominik Honnef <[email protected]> (cherry picked from commit 67bbe3d)
1 parent b345e26 commit c6cc483

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

go/ir/builder.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -669,17 +669,24 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value {
669669
}
670670

671671
case *ast.SliceExpr:
672-
var low, high, max Value
673672
var x Value
674-
switch typeutil.CoreType(fn.Pkg.typeOf(e.X)).Underlying().(type) {
675-
case *types.Array:
676-
// Potentially escaping.
677-
x = b.addr(fn, e.X, true).address(fn)
678-
case *types.Basic, *types.Slice, *types.Pointer: // *array
673+
if core := typeutil.CoreType(fn.Pkg.typeOf(e.X)); core != nil {
674+
switch core.Underlying().(type) {
675+
case *types.Array:
676+
// Potentially escaping.
677+
x = b.addr(fn, e.X, true).address(fn)
678+
case *types.Basic, *types.Slice, *types.Pointer: // *array
679+
x = b.expr(fn, e.X)
680+
default:
681+
panic("unreachable")
682+
}
683+
} else {
684+
// We're indexing a string | []byte. Note that other combinations such as []byte | [4]byte are currently not
685+
// allowed by the language.
679686
x = b.expr(fn, e.X)
680-
default:
681-
panic("unreachable")
682687
}
688+
689+
var low, high, max Value
683690
if e.High != nil {
684691
high = b.expr(fn, e.High)
685692
}

0 commit comments

Comments
 (0)