representation returns the representation of the constant operand x as the basic type typ. If no such representation is possible, it returns a non-zero error code.
(x *operand, typ *Basic)
| 463 | // |
| 464 | // If no such representation is possible, it returns a non-zero error code. |
| 465 | func (check *Checker) representation(x *operand, typ *Basic) (constant.Value, errorCode) { |
| 466 | assert(x.mode == constant_) |
| 467 | v := x.val |
| 468 | if !representableConst(x.val, check, typ, &v) { |
| 469 | if isNumeric(x.typ) && isNumeric(typ) { |
| 470 | // numeric conversion : error msg |
| 471 | // |
| 472 | // integer -> integer : overflows |
| 473 | // integer -> float : overflows (actually not possible) |
| 474 | // float -> integer : truncated |
| 475 | // float -> float : overflows |
| 476 | // |
| 477 | if !isInteger(x.typ) && isInteger(typ) { |
| 478 | return nil, _TruncatedFloat |
| 479 | } else { |
| 480 | return nil, _NumericOverflow |
| 481 | } |
| 482 | } |
| 483 | return nil, _InvalidConstVal |
| 484 | } |
| 485 | return v, 0 |
| 486 | } |
| 487 | |
| 488 | func (check *Checker) invalidConversion(code errorCode, x *operand, target Type) { |
| 489 | msg := "cannot convert %s to %s" |
no test coverage detected