Skip to content

Commit 26531cd

Browse files
committed
macro test
1 parent ebe7fb5 commit 26531cd

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

tests/misc/projects/Issue3053/Main.hx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class Main {
7272

7373
bar.defaultNull = 1; // err
7474

75+
bar.age;
76+
@:bypassAccessor bar.age = 1;
77+
@:privateAccess bar.age = 1;
78+
bar.age = 1; // err
79+
7580
final child = new Child();
7681
@:privateAccess child.width = 1;
7782
}
@@ -115,6 +120,7 @@ class Rect implements Shape {
115120
}
116121
}
117122

123+
@:build(PropertyMacro.addIntProperty("age"))
118124
class Bar {
119125
public function new() {
120126
width = 2;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import haxe.macro.Context;
2+
import haxe.macro.Expr;
3+
4+
class PropertyMacro {
5+
public static macro function addIntProperty(name:String):Array<Field> {
6+
final fields = Context.getBuildFields();
7+
8+
final privateField:Field = {
9+
name: name,
10+
access: [APublic],
11+
meta: [{
12+
name: ":isVar",
13+
pos: Context.currentPos()
14+
}],
15+
kind: FProp("get", "private set", macro : Int, null),
16+
pos: Context.currentPos()
17+
};
18+
19+
final getterMethod = {
20+
name: "get_" + name,
21+
access: [],
22+
kind: FFun({
23+
args: [],
24+
ret: macro : Int,
25+
expr: macro return this.$name
26+
}),
27+
pos: Context.currentPos()
28+
};
29+
30+
final setterMethod = {
31+
name: "set_" + name,
32+
access: [],
33+
kind: FFun({
34+
args: [{ name: "value", type: macro : Int }],
35+
ret: macro : Int,
36+
expr: macro return this.$name = value
37+
}),
38+
pos: Context.currentPos()
39+
};
40+
41+
fields.push(privateField);
42+
fields.push(getterMethod);
43+
fields.push(setterMethod);
44+
45+
return fields;
46+
}
47+
}

tests/misc/projects/Issue3053/compile-fail.hxml.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ Main.hx:70: characters 3-12 : This property cannot be accessed for writing
88
Main.hx:71: characters 7-12 : Cannot access private field width
99
Main.hx:71: characters 3-12 : This property cannot be accessed for reading
1010
Main.hx:73: characters 7-18 : This expression cannot be accessed for writing
11+
Main.hx:78: characters 3-10 : This property cannot be accessed for writing
1112
Main.hx:21: characters 3-26 : Main should be FooType
1213
Main.hx:21: characters 3-26 : ... Inconsistent getter for field foo : private get should be get
1314
Main.hx:24: characters 3-19 : FooPrivateGetType should be FooType
1415
Main.hx:24: characters 3-19 : ... Inconsistent getter for field foo : private get should be get
15-
Main.hx:107: characters 13-18 : Field width has different property access than in Shape: (get,private set) should be (private get,set)
16-
Main.hx:89: characters 13-18 : Field width has different property access than in PublicShape: (get,private set) should be (get,set)
16+
Main.hx:112: characters 13-18 : Field width has different property access than in Shape: (get,private set) should be (private get,set)
17+
Main.hx:94: characters 13-18 : Field width has different property access than in PublicShape: (get,private set) should be (get,set)

0 commit comments

Comments
 (0)