exprInternal contains the core of type checking of expressions. Must only be called by rawExpr.
(x *operand, e Expr, hint Type)
| 1262 | // Must only be called by rawExpr. |
| 1263 | // |
| 1264 | func (check *Checker) exprInternal(x *operand, e Expr, hint Type) exprKind { |
| 1265 | // make sure x has a valid state in case of bailout |
| 1266 | // (was issue 5770) |
| 1267 | x.mode = invalid |
| 1268 | x.typ = Typ[Invalid] |
| 1269 | |
| 1270 | switch e := e.(type) { |
| 1271 | case nil: |
| 1272 | unreachable() |
| 1273 | |
| 1274 | case *BadExpr: |
| 1275 | goto Error // error was reported before |
| 1276 | |
| 1277 | case *Name: |
| 1278 | check.ident(x, e, nil, false) |
| 1279 | |
| 1280 | case *DotsType: |
| 1281 | // dots are handled explicitly where they are legal |
| 1282 | // (array composite literals and parameter lists) |
| 1283 | check.error(e, "invalid use of '...'") |
| 1284 | goto Error |
| 1285 | |
| 1286 | case *BasicLit: |
| 1287 | if e.Bad { |
| 1288 | goto Error // error reported during parsing |
| 1289 | } |
| 1290 | switch e.Kind { |
| 1291 | case IntLit, FloatLit, ImagLit: |
| 1292 | check.langCompat(e) |
| 1293 | // The max. mantissa precision for untyped numeric values |
| 1294 | // is 512 bits, or 4048 bits for each of the two integer |
| 1295 | // parts of a fraction for floating-point numbers that are |
| 1296 | // represented accurately in the go/constant package. |
| 1297 | // Constant literals that are longer than this many bits |
| 1298 | // are not meaningful; and excessively long constants may |
| 1299 | // consume a lot of space and time for a useless conversion. |
| 1300 | // Cap constant length with a generous upper limit that also |
| 1301 | // allows for separators between all digits. |
| 1302 | const limit = 10000 |
| 1303 | if len(e.Value) > limit { |
| 1304 | check.errorf(e, "excessively long constant: %s... (%d chars)", e.Value[:10], len(e.Value)) |
| 1305 | goto Error |
| 1306 | } |
| 1307 | } |
| 1308 | x.setConst(e.Kind, e.Value) |
| 1309 | if x.mode == invalid { |
| 1310 | // The parser already establishes syntactic correctness. |
| 1311 | // If we reach here it's because of number under-/overflow. |
| 1312 | // TODO(gri) setConst (and in turn the go/constant package) |
| 1313 | // should return an error describing the issue. |
| 1314 | check.errorf(e, "malformed constant: %s", e.Value) |
| 1315 | goto Error |
| 1316 | } |
| 1317 | |
| 1318 | case *FuncLit: |
| 1319 | if sig, ok := check.typ(e.Type).(*Signature); ok { |
| 1320 | obj := NewFuncLit(e.Pos(), check.pkg, sig) |
| 1321 | obj.body = e.Body |
no test coverage detected