diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index fb8a4d71ccc1d1..1f39d9a326f2a3 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1625,14 +1625,13 @@ pub mut: @[minify] pub struct CastExpr { pub mut: - arg Expr // `n` in `string(buf, n)` - typ Type // `string` - expr Expr // `buf` in `string(buf, n)` and `&Type(buf)` - typname string // `&Type` in `&Type(buf)` - expr_type Type // `byteptr`, the type of the `buf` expression - has_arg bool // true for `string(buf, n)`, false for `&Type(buf)` - pos token.Pos - is_generated bool // true when added by Checker + arg Expr // `n` in `string(buf, n)` + typ Type // `string` + expr Expr // `buf` in `string(buf, n)` and `&Type(buf)` + typname string // `&Type` in `&Type(buf)` + expr_type Type // `byteptr`, the type of the `buf` expression + has_arg bool // true for `string(buf, n)`, false for `&Type(buf)` + pos token.Pos } @[minify] diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 4340385a0693ad..21825362df54f2 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -84,12 +84,11 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { if left_type in [ast.f32_type_idx, ast.f64_type_idx] && right_type == ast.float_literal_type { defer { node.right = ast.CastExpr{ - expr: node.right - typ: left_type - typname: c.table.get_type_name(left_type) - expr_type: right_type - pos: node.right.pos() - is_generated: true + expr: node.right + typ: left_type + typname: c.table.get_type_name(left_type) + expr_type: right_type + pos: node.right.pos() } } } @@ -100,11 +99,10 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { if right_type in [ast.f32_type_idx, ast.f64_type_idx] && left_type == ast.float_literal_type { defer { node.left = ast.CastExpr{ - expr: node.left - typ: right_type - typname: c.table.get_type_name(right_type) - expr_type: left_type - is_generated: true + expr: node.left + typ: right_type + typname: c.table.get_type_name(right_type) + expr_type: left_type } } } @@ -734,12 +732,11 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { node = ast.InfixExpr{ left: ast.CastExpr{ - expr: node.left - typ: modified_left_type - typname: c.table.type_str(modified_left_type) - expr_type: left_type - pos: node.pos - is_generated: true + expr: node.left + typ: modified_left_type + typname: c.table.type_str(modified_left_type) + expr_type: left_type + pos: node.pos } left_type: left_type op: .right_shift diff --git a/vlib/v/comptime/comptimeinfo.v b/vlib/v/comptime/comptimeinfo.v index e61a2f8ddb87d0..a46e82a7463b2f 100644 --- a/vlib/v/comptime/comptimeinfo.v +++ b/vlib/v/comptime/comptimeinfo.v @@ -64,14 +64,8 @@ pub fn (mut ct ComptimeInfo) is_comptime(node ast.Expr) bool { ast.SelectorExpr { return node.expr is ast.Ident && node.expr.ct_expr } - ast.CastExpr { - if node.is_generated { - if node.expr is ast.InfixExpr { - return ct.is_comptime(node.expr.left) || ct.is_comptime(node.expr.right) - } - return ct.is_comptime(node.expr) - } - return false + ast.InfixExpr { + return ct.is_comptime(node.left) || ct.is_comptime(node.right) } ast.ParExpr { return ct.is_comptime(node.expr) @@ -146,10 +140,8 @@ pub fn (mut ct ComptimeInfo) get_type_or_default(node ast.Expr, default_typ ast. ast.ParExpr { return ct.get_type_or_default(node.expr, default_typ) } - ast.CastExpr { - if node.is_generated { - return ct.get_type_or_default(node.expr, default_typ) - } + ast.InfixExpr { + return ct.get_type_or_default(node.left, default_typ) } else { return default_typ @@ -215,8 +207,6 @@ pub fn (mut ct ComptimeInfo) get_type(node ast.Expr) ast.Type { nltype := ct.get_type(node.left) nltype_unwrapped := ct.resolver.unwrap_generic(nltype) return ct.table.value_type(nltype_unwrapped) - } else if node is ast.SelectorExpr && ct.is_comptime(node.expr) { - println('>>> ${node.expr}') } return ast.void_type } diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 8458b6890cdd3e..9f1888f1e54fda 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -1189,11 +1189,11 @@ fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) { || g.file.is_translated) if needs_cast { typ_str := if g.comptime.is_comptime(node.left) { - g.styp(g.comptime.get_type(node.left)) + g.styp(g.comptime.get_type_or_default(node.left, node.promoted_type)) } else { g.styp(node.promoted_type) } - g.write('(${typ_str}/*${node.left}*/)(') + g.write('(${typ_str})(') } if node.left_type.is_ptr() && node.left.is_auto_deref_var() && !node.right_type.is_pointer() { g.write('*') diff --git a/vlib/v/tests/generics/generic_selector_type_test.v b/vlib/v/tests/generics/generic_selector_type_test.v new file mode 100644 index 00000000000000..6643b4cef0e3e7 --- /dev/null +++ b/vlib/v/tests/generics/generic_selector_type_test.v @@ -0,0 +1,12 @@ +import math.vec + +type UnusedType = vec.Vec3[f32] + +fn (n UnusedType) unused_function() f32 { + return n.mul_scalar(2).magnitude() +} + +fn test_main() { + assert vec.Vec3[f32]{0.5, 0.5, 0.5}.magnitude() == f32(0.8660254) + assert vec.Vec3[f32]{1.5, 1.5, 1.5}.magnitude() == f32(2.598076) +}