@@ -1125,6 +1125,16 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
1125
1125
}
1126
1126
1127
1127
case * Array :
1128
+ // Prevent crash if the array referred to is not yet set up.
1129
+ // This is a stop-gap solution; a better approach would use the mechanism of
1130
+ // Checker.ident (typexpr.go) using a path of types. But that would require
1131
+ // passing the path everywhere (all expression-checking methods, not just
1132
+ // type expression checking), and we're not set up for that (quite possibly
1133
+ // an indication that cycle detection needs to be rethought). Was issue #18643.
1134
+ if utyp .elem == nil {
1135
+ check .error (e .Pos (), "illegal cycle in type declaration" )
1136
+ goto Error
1137
+ }
1128
1138
n := check .indexedElts (e .Elts , utyp .elem , utyp .len )
1129
1139
// If we have an "open" [...]T array, set the length now that we know it
1130
1140
// and record the type for [...] (usually done by check.typExpr which is
@@ -1135,9 +1145,21 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
1135
1145
}
1136
1146
1137
1147
case * Slice :
1148
+ // Prevent crash if the slice referred to is not yet set up.
1149
+ // See analogous comment for *Array.
1150
+ if utyp .elem == nil {
1151
+ check .error (e .Pos (), "illegal cycle in type declaration" )
1152
+ goto Error
1153
+ }
1138
1154
check .indexedElts (e .Elts , utyp .elem , - 1 )
1139
1155
1140
1156
case * Map :
1157
+ // Prevent crash if the map referred to is not yet set up.
1158
+ // See analogous comment for *Array.
1159
+ if utyp .key == nil || utyp .elem == nil {
1160
+ check .error (e .Pos (), "illegal cycle in type declaration" )
1161
+ goto Error
1162
+ }
1141
1163
visited := make (map [interface {}][]Type , len (e .Elts ))
1142
1164
for _ , e := range e .Elts {
1143
1165
kv , _ := e .(* ast.KeyValueExpr )
0 commit comments