Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Dec 27, 2024
1 parent 70f685b commit 2ea4e57
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 41 deletions.
15 changes: 7 additions & 8 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
31 changes: 14 additions & 17 deletions vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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
Expand Down
18 changes: 4 additions & 14 deletions vlib/v/comptime/comptimeinfo.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -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('*')
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/tests/generics/generic_selector_type_test.v
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 2ea4e57

Please sign in to comment.