Skip to content

Commit 85d36ed

Browse files
committed
checker: stricter type casting error messages
1 parent ede9f29 commit 85d36ed

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

vlib/v/checker/checker.v

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,13 +2665,16 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
26652665
from_type = node.expr_type
26662666
}
26672667
if !c.table.sumtype_has_variant(to_type, from_type, false) && !to_type.has_flag(.optional) {
2668-
c.error('cannot cast `$from_sym.name` to `$to_sym.name`', node.pos)
2668+
ft := c.table.type_to_str(from_type)
2669+
tt := c.table.type_to_str(to_type)
2670+
c.error('cannot cast `$ft` to `$tt`', node.pos)
26692671
}
26702672
} else if mut to_sym.info is ast.Alias && !(final_to_sym.kind == .struct_ && to_type.is_ptr()) {
26712673
if !c.check_types(from_type, to_sym.info.parent_type) && !(final_to_sym.is_int()
26722674
&& final_from_sym.kind in [.enum_, .bool, .i8, .char]) {
2673-
c.error('cannot convert type `$from_sym.name` to `$to_sym.name` (alias to `$final_to_sym.name`)',
2674-
node.pos)
2675+
ft := c.table.type_to_str(from_type)
2676+
tt := c.table.type_to_str(to_type)
2677+
c.error('cannot cast `$ft` to `$tt` (alias to `$final_to_sym.name`)', node.pos)
26752678
}
26762679
} else if to_sym.kind == .struct_ && !to_type.is_ptr()
26772680
&& !(to_sym.info as ast.Struct).is_typedef {
@@ -2686,8 +2689,8 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
26862689
node.pos)
26872690
}
26882691
} else {
2689-
type_name := c.table.type_to_str(from_type)
2690-
c.error('cannot cast `$type_name` to struct', node.pos)
2692+
ft := c.table.type_to_str(from_type)
2693+
c.error('cannot cast `$ft` to struct', node.pos)
26912694
}
26922695
} else if to_sym.kind == .interface_ {
26932696
if c.type_implements(from_type, to_type, node.pos) {
@@ -2718,8 +2721,9 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
27182721
}
27192722
} else if to_sym.kind == .byte && !final_from_sym.is_number() && !final_from_sym.is_pointer()
27202723
&& !from_type.is_ptr() && final_from_sym.kind !in [.char, .enum_, .bool] {
2721-
type_name := c.table.type_to_str(from_type)
2722-
c.error('cannot cast type `$type_name` to `byte`', node.pos)
2724+
ft := c.table.type_to_str(from_type)
2725+
tt := c.table.type_to_str(to_type)
2726+
c.error('cannot cast type `$ft` to `$tt`', node.pos)
27232727
} else if from_type.has_flag(.optional) || from_type.has_flag(.variadic) {
27242728
// variadic case can happen when arrays are converted into variadic
27252729
msg := if from_type.has_flag(.optional) { 'an optional' } else { 'a variadic' }
@@ -2734,31 +2738,34 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
27342738
} else if final_from_sym.kind == .string && final_to_sym.is_number()
27352739
&& final_to_sym.kind != .rune {
27362740
snexpr := node.expr.str()
2737-
c.error('cannot cast string to `$to_sym.name`, use `${snexpr}.${final_to_sym.name}()` instead.',
2741+
tt := c.table.type_to_str(to_type)
2742+
c.error('cannot cast string to `$tt`, use `${snexpr}.${final_to_sym.name}()` instead.',
27382743
node.pos)
27392744
}
27402745

27412746
if to_sym.kind == .rune && from_sym.is_string() {
27422747
snexpr := node.expr.str()
2743-
c.error('cannot cast `$from_sym.name` to rune, use `${snexpr}.runes()` instead.',
2744-
node.pos)
2748+
ft := c.table.type_to_str(from_type)
2749+
c.error('cannot cast `$ft` to rune, use `${snexpr}.runes()` instead.', node.pos)
27452750
}
27462751

27472752
if to_type == ast.string_type {
27482753
if from_type in [ast.byte_type, ast.bool_type] {
27492754
snexpr := node.expr.str()
2750-
c.error('cannot cast type `$from_sym.name` to string, use `${snexpr}.str()` instead.',
2755+
ft := c.table.type_to_str(from_type)
2756+
c.error('cannot cast type `$ft` to string, use `${snexpr}.str()` instead.',
27512757
node.pos)
27522758
} else if from_type.is_real_pointer() {
27532759
snexpr := node.expr.str()
2754-
c.error('cannot cast pointer type `$from_sym.name` to string, use `&byte($snexpr).vstring()` or `cstring_to_vstring($snexpr)` instead.',
2760+
ft := c.table.type_to_str(from_type)
2761+
c.error('cannot cast pointer type `$ft` to string, use `&byte($snexpr).vstring()` or `cstring_to_vstring($snexpr)` instead.',
27552762
node.pos)
27562763
} else if from_type.is_number() {
27572764
snexpr := node.expr.str()
27582765
c.error('cannot cast number to string, use `${snexpr}.str()` instead.', node.pos)
27592766
} else if from_sym.kind == .alias && final_from_sym.name != 'string' {
2760-
c.error('cannot cast type `$from_sym.name` to string, use `x.str()` instead.',
2761-
node.pos)
2767+
ft := c.table.type_to_str(from_type)
2768+
c.error('cannot cast type `$ft` to string, use `x.str()` instead.', node.pos)
27622769
} else if final_from_sym.kind == .array {
27632770
snexpr := node.expr.str()
27642771
if final_from_sym.name == '[]byte' {
@@ -2776,7 +2783,8 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
27762783
c.error('cannot cast map to string.', node.pos)
27772784
} else if final_from_sym.kind == .sum_type {
27782785
snexpr := node.expr.str()
2779-
c.error('cannot cast sumtype `$from_sym.name` to string, use `${snexpr}.str()` instead.',
2786+
ft := c.table.type_to_str(from_type)
2787+
c.error('cannot cast sumtype `$ft` to string, use `${snexpr}.str()` instead.',
27802788
node.pos)
27812789
} else if to_type != ast.string_type && from_type == ast.string_type
27822790
&& (!(to_sym.kind == .alias && final_to_sym.name == 'string')) {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
vlib/v/checker/tests/cannot_cast_to_alias.vv:6:7: error: cannot convert type `int literal` to `MyType` (alias to `string`)
2-
4 |
1+
vlib/v/checker/tests/cannot_cast_to_alias.vv:6:7: error: cannot cast `int literal` to `MyType` (alias to `string`)
2+
4 |
33
5 | fn main() {
44
6 | _ := MyType(5)
55
| ~~~~~~~~~
6-
7 | }
6+
7 | }

0 commit comments

Comments
 (0)