Skip to content

Commit 3c7a890

Browse files
committed
v2: more cleanup: remove infer type methods from transformer; remove lowered ast nodes from cleanc; add more tests
1 parent 5d37dd1 commit 3c7a890

5 files changed

Lines changed: 307 additions & 561 deletions

File tree

‎cmd/v2/test.v‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,186 @@ fn test_method_call_resolution() {
14391439
print_str('method call resolution: ok')
14401440
}
14411441

1442+
// ==================== Test 96: Sumtype variant inference ====================
1443+
// Exercises the variant inference logic for various expression kinds when
1444+
// wrapping values into sum types. Covers: int/f64 literals, string literals,
1445+
// struct init, function return types, and variable references.
1446+
1447+
type Val96 = f64 | int | string | Pair96
1448+
1449+
struct Pair96 {
1450+
a int
1451+
b int
1452+
}
1453+
1454+
fn make_pair96(a int, b int) Pair96 {
1455+
return Pair96{
1456+
a: a
1457+
b: b
1458+
}
1459+
}
1460+
1461+
fn make_int96() int {
1462+
return 99
1463+
}
1464+
1465+
fn wrap_int_literal96() Val96 {
1466+
return 42
1467+
}
1468+
1469+
fn wrap_float_literal96() Val96 {
1470+
return 3.14
1471+
}
1472+
1473+
fn wrap_string_literal96() Val96 {
1474+
return 'hello'
1475+
}
1476+
1477+
fn wrap_struct_init96() Val96 {
1478+
return Pair96{
1479+
a: 1
1480+
b: 2
1481+
}
1482+
}
1483+
1484+
fn wrap_fn_call96() Val96 {
1485+
return make_pair96(10, 20)
1486+
}
1487+
1488+
fn wrap_fn_call_int96() Val96 {
1489+
return make_int96()
1490+
}
1491+
1492+
fn wrap_variable96(v Val96) Val96 {
1493+
return v
1494+
}
1495+
1496+
fn test_sumtype_variant_inference() {
1497+
// 96.1 int literal wrapped in sum type
1498+
v1 := wrap_int_literal96()
1499+
if v1 is int {
1500+
assert v1 == 42
1501+
print_str('sumtype variant inference (int literal): ok')
1502+
} else {
1503+
print_str('sumtype variant inference (int literal): FAIL')
1504+
}
1505+
1506+
// 96.2 float literal wrapped in sum type
1507+
v2 := wrap_float_literal96()
1508+
if v2 is f64 {
1509+
print_str('sumtype variant inference (f64 literal): ok')
1510+
} else {
1511+
print_str('sumtype variant inference (f64 literal): FAIL')
1512+
}
1513+
1514+
// 96.3 string literal wrapped in sum type
1515+
v3 := wrap_string_literal96()
1516+
if v3 is string {
1517+
assert v3 == 'hello'
1518+
print_str('sumtype variant inference (string literal): ok')
1519+
} else {
1520+
print_str('sumtype variant inference (string literal): FAIL')
1521+
}
1522+
1523+
// 96.4 struct init wrapped in sum type
1524+
v4 := wrap_struct_init96()
1525+
if v4 is Pair96 {
1526+
assert v4.a == 1
1527+
assert v4.b == 2
1528+
print_str('sumtype variant inference (struct init): ok')
1529+
} else {
1530+
print_str('sumtype variant inference (struct init): FAIL')
1531+
}
1532+
1533+
// 96.5 function call returning struct wrapped in sum type
1534+
v5 := wrap_fn_call96()
1535+
if v5 is Pair96 {
1536+
assert v5.a == 10
1537+
assert v5.b == 20
1538+
print_str('sumtype variant inference (fn call struct): ok')
1539+
} else {
1540+
print_str('sumtype variant inference (fn call struct): FAIL')
1541+
}
1542+
1543+
// 96.6 function call returning int wrapped in sum type
1544+
v6 := wrap_fn_call_int96()
1545+
if v6 is int {
1546+
assert v6 == 99
1547+
print_str('sumtype variant inference (fn call int): ok')
1548+
} else {
1549+
print_str('sumtype variant inference (fn call int): FAIL')
1550+
}
1551+
1552+
// 96.7 variable of sum type passed through (identity)
1553+
v7_inner := Val96(7)
1554+
v7 := wrap_variable96(v7_inner)
1555+
if v7 is int {
1556+
assert v7 == 7
1557+
print_str('sumtype variant inference (variable): ok')
1558+
} else {
1559+
print_str('sumtype variant inference (variable): FAIL')
1560+
}
1561+
}
1562+
1563+
// ==================== Test 97: Array type resolution ====================
1564+
// Tests that array operations work correctly when type info comes from the checker
1565+
// (get_expr_type) rather than expression-structure inference.
1566+
1567+
fn get_test_arr97() []int {
1568+
return [10, 20, 30]
1569+
}
1570+
1571+
fn sum_arr97(arr []int) int {
1572+
mut s := 0
1573+
for v in arr {
1574+
s += v
1575+
}
1576+
return s
1577+
}
1578+
1579+
fn test_array_type_resolution() {
1580+
// 97.1 Array comparison (== uses array__eq)
1581+
a1 := [1, 2, 3]
1582+
a2 := [1, 2, 3]
1583+
a3 := [4, 5, 6]
1584+
assert a1 == a2
1585+
assert a1 != a3
1586+
print_str('array type resolution (comparison): ok')
1587+
1588+
// 97.2 Array from function return, then filter
1589+
arr := get_test_arr97()
1590+
filtered := arr.filter(it > 15)
1591+
assert filtered.len == 2
1592+
assert filtered[0] == 20
1593+
assert filtered[1] == 30
1594+
print_str('array type resolution (filter): ok')
1595+
1596+
// 97.3 Array map
1597+
doubled := arr.map(it * 2)
1598+
assert doubled.len == 3
1599+
assert doubled[0] == 20
1600+
assert doubled[1] == 40
1601+
assert doubled[2] == 60
1602+
print_str('array type resolution (map): ok')
1603+
1604+
// 97.4 Array contains
1605+
assert arr.contains(20)
1606+
assert !arr.contains(99)
1607+
print_str('array type resolution (contains): ok')
1608+
1609+
// 97.5 Array slice
1610+
sliced := arr[1..]
1611+
assert sliced.len == 2
1612+
assert sliced[0] == 20
1613+
assert sliced[1] == 30
1614+
print_str('array type resolution (slice): ok')
1615+
1616+
// 97.6 Array passed to function
1617+
total := sum_arr97(arr)
1618+
assert total == 60
1619+
print_str('array type resolution (fn arg): ok')
1620+
}
1621+
14421622
// ===================== MAIN TEST FUNCTION =====================
14431623

14441624
fn main() {
@@ -4991,5 +5171,18 @@ fn main() {
49915171
print_str('--- Test 95: Method call resolution ---')
49925172
test_method_call_resolution()
49935173

5174+
// ==================== 96. SUMTYPE VARIANT INFERENCE ====================
5175+
// Tests that sum type wrapping works for various expression kinds:
5176+
// literals, struct init, function calls, and variables.
5177+
// This exercises the inlined variant inference logic in wrap_sumtype_value_transformed.
5178+
print_str('--- Test 96: Sumtype variant inference ---')
5179+
test_sumtype_variant_inference()
5180+
5181+
// ==================== 97. ARRAY TYPE RESOLUTION ====================
5182+
// Tests that array operations are correctly resolved using checker type info:
5183+
// array comparisons, filter/map, string interpolation of arrays, and slicing.
5184+
print_str('--- Test 97: Array type resolution ---')
5185+
test_array_type_resolution()
5186+
49945187
print_str('=== All tests completed ===')
49955188
}

‎vlib/v2/gen/cleanc/cleanc_test.v‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module cleanc
2+
3+
fn test_c_string_literal_content_to_c_single_line() {
4+
out := c_string_literal_content_to_c('hello')
5+
assert out == '"hello"'
6+
}
7+
8+
fn test_c_string_literal_content_to_c_multiline() {
9+
out := c_string_literal_content_to_c('hello\nworld')
10+
assert out == '"hello\\n"\n"world"'
11+
}
12+
13+
fn test_c_string_literal_content_to_c_trailing_newline() {
14+
out := c_string_literal_content_to_c('hello\n')
15+
assert out == '"hello\\n"\n""'
16+
}
17+
18+
fn test_c_string_literal_content_to_c_escapes_quote() {
19+
out := c_string_literal_content_to_c('say "hello"')
20+
assert out == '"say \\"hello\\""'
21+
}
22+
23+
fn test_c_string_literal_content_to_c_preserves_percent_placeholders() {
24+
out := c_string_literal_content_to_c('"%s"')
25+
assert out == '"\\"%s\\""'
26+
}

0 commit comments

Comments
 (0)