Skip to content

Commit 63b4e67

Browse files
committed
feat(compiler): add compliance test for struct value init clone
Verifies that initializing a struct value via a composite literal and assigning it correctly invokes the .clone() method to maintain Go's value semantics, as described in DESIGN.md. Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent 6b5bea6 commit 63b4e67

4 files changed

Lines changed: 69 additions & 0 deletions

File tree

compliance/COMPLIANCE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The following tests are currently implemented in the `/compliance/tests` directo
2222
* **`simple/`**: A basic test covering simple struct definition, field access, method calls, and `println`. (Likely overlaps with others, could be a general integration test).
2323
* **`simple_deref_assignment/`**: Tests simple assignment involving pointer dereferencing (`*ptr`), ensuring value copying.
2424
* **`struct_field_access/`**: Verifies accessing fields of struct values and struct pointers.
25+
* **`struct_value_init_clone/`**: Checks struct initialization via composite literal (`T{...}`) and subsequent assignment, ensuring `.clone()` is used for value semantics.
2526
* **`value_type_copy_behavior/`**: Focuses specifically on demonstrating that assigning struct values creates independent copies (value semantics).
2627

2728
Each test should have only three files:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
p1: 1 2
2+
p1 after p2 mod: 1 2
3+
p2: 10 2
4+
v after p3 mod: 3 4
5+
p3: 3 40
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
type Point struct {
4+
X int
5+
Y int
6+
}
7+
8+
func main() {
9+
// Initialize directly
10+
p1 := Point{X: 1, Y: 2}
11+
println("p1:", p1.X, p1.Y)
12+
13+
// Assign to another variable (should trigger clone)
14+
p2 := p1
15+
p2.X = 10 // Modify the copy
16+
17+
// Print both to show they are independent
18+
println("p1 after p2 mod:", p1.X, p1.Y)
19+
println("p2:", p2.X, p2.Y)
20+
21+
// Initialize via variable assignment
22+
v := Point{X: 3, Y: 4}
23+
p3 := v // Should trigger clone
24+
p3.Y = 40 // Modify the copy
25+
26+
println("v after p3 mod:", v.X, v.Y)
27+
println("p3:", p3.X, p3.Y)
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Generated file based on struct_value_init_clone.go
2+
// Updated when compliance tests are re-run, DO NOT EDIT!
3+
4+
import * as goscript from "@go/builtin";
5+
6+
class Point {
7+
public X: number = 0;
8+
public Y: number = 0;
9+
10+
constructor(init?: Partial<Point>) { if (init) Object.assign(this, init as any); }
11+
public clone(): Point { return Object.assign(Object.create(Point.prototype) as Point, this); }
12+
}
13+
14+
export function main(): void {
15+
// Initialize directly
16+
let p1 = new Point({ X: 1, Y: 2 }).clone()
17+
console.log("p1:", p1.X, p1.Y)
18+
19+
// Assign to another variable (should trigger clone)
20+
let p2 = p1.clone()
21+
p2.X = 10 // Modify the copy
22+
23+
// Print both to show they are independent
24+
console.log("p1 after p2 mod:", p1.X, p1.Y)
25+
console.log("p2:", p2.X, p2.Y)
26+
27+
// Initialize via variable assignment
28+
let v = new Point({ X: 3, Y: 4 }).clone()
29+
let p3 = v.clone() // Should trigger clone
30+
p3.Y = 40 // Modify the copy
31+
32+
console.log("v after p3 mod:", v.X, v.Y)
33+
console.log("p3:", p3.X, p3.Y)
34+
}
35+

0 commit comments

Comments
 (0)