Skip to content

Commit ebe7fb5

Browse files
committed
Add public error, expose AccPrivateCall to haxe
and make different property access error more readable
1 parent c257f1f commit ebe7fb5

File tree

9 files changed

+61
-30
lines changed

9 files changed

+61
-30
lines changed

src/generators/genswf9.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,7 +2324,7 @@ let realize_required_accessors ctx cl =
23242324
if not (is_implemented_by_super ci) then begin
23252325
List.iter (fun cf ->
23262326
match cf.cf_kind with
2327-
| Var { v_read = (AccCall | AccNever) as read; v_write = (AccCall | AccNever) as write } ->
2327+
| Var { v_read = (AccCall | AccPrivateCall | AccNever) as read; v_write = (AccCall | AccPrivateCall | AccNever) as write } ->
23282328
begin try
23292329
let read', write', native = Hashtbl.find h cf.cf_name in
23302330
let read = if read = AccNever then read' else true in
@@ -2496,10 +2496,10 @@ let generate_class ctx c =
24962496
maybe_gen_instance_setter ctx c f acc alloc_slot
24972497
else
24982498
maybe_gen_static_setter ctx c f acc alloc_slot
2499-
| Var { v_read = (AccCall | AccNever) as read; v_write = (AccCall | AccNever) as write } when not (has_class_flag c CInterface) && not (Meta.has Meta.IsVar f.cf_meta) ->
2499+
| Var { v_read = (AccCall | AccPrivateCall | AccNever) as read; v_write = (AccCall | AccPrivateCall | AccNever) as write } when not (has_class_flag c CInterface) && not (Meta.has Meta.IsVar f.cf_meta) ->
25002500
(* if the accessor methods were defined in super classes, we still need to generate native getter/setter *)
25012501
let acc =
2502-
if read = AccCall then begin
2502+
if read = AccCall || read = AccPrivateCall then begin
25032503
try
25042504
begin
25052505
let tl = extract_param_types c.cl_params in
@@ -2524,7 +2524,7 @@ let generate_class ctx c =
25242524
acc
25252525
end else acc
25262526
in
2527-
if write = AccCall then begin
2527+
if write = AccCall || write = AccPrivateCall then begin
25282528
try
25292529
begin
25302530
let tl = extract_param_types c.cl_params in

src/typing/typeloadCheck.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ module Inheritance = struct
385385
if (has_class_field_flag f CfPublic) && not (has_class_field_flag f2 CfPublic) && not (Meta.has Meta.CompilerGenerated f.cf_meta) then
386386
display_error com ("Field " ^ f.cf_name ^ " should be public as requested by " ^ s_type_path intf.cl_path) p
387387
else if not (unify_kind ~strict:false f2.cf_kind f.cf_kind) || not (match f.cf_kind, f2.cf_kind with Var _ , Var _ -> true | Method m1, Method m2 -> mkind m1 = mkind m2 | _ -> false) then
388-
display_error com ("Field " ^ f.cf_name ^ " has different property access than in " ^ s_type_path intf.cl_path ^ " (" ^ s_kind f2.cf_kind ^ " should be " ^ s_kind f.cf_kind ^ ")") p
388+
display_error com ("Field " ^ f.cf_name ^ " has different property access than in " ^ s_type_path intf.cl_path ^ ": " ^ s_kind f2.cf_kind ^ " should be " ^ s_kind f.cf_kind) p
389389
else try
390390
let map1 = TClass.get_map_function intf params in
391391
valid_redefinition map1 map2 f2 t2 f (apply_params intf.cl_params params f.cf_type)

src/typing/typeloadFields.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1493,7 +1493,9 @@ let create_property (ctx,cctx,fctx) c f cf (get,set,t,eo) p =
14931493
display_error ctx.com (name ^ ": Custom property accessor is no longer supported, please use `set`") pset;
14941494
AccCall
14951495
) in
1496-
if (set = AccNever && get = AccNever) then raise_typing_error (name ^ ": Unsupported property combination") p;
1496+
if (set = AccNever && get = AccNever) then raise_typing_error (name ^ ": Unsupported property combination") p;
1497+
if (set = AccPrivateCall && get = AccPrivateCall && has_class_field_flag cf CfPublic) then
1498+
raise_typing_error (name ^ ": (private get, private set) property cannot be public") p;
14971499
cf.cf_kind <- Var { v_read = get; v_write = set };
14981500
if fctx.is_extern then add_class_field_flag cf CfExtern;
14991501
if List.mem_assoc AEnum f.cff_access then add_class_field_flag cf CfEnum;

std/haxe/display/JsonModuleTypes.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ enum abstract JsonVarAccessKind<T>(String) {
213213
var AccNever;
214214
var AccResolve;
215215
var AccCall;
216+
var AccPrivateCall;
216217
var AccInline;
217218
var AccRequire:JsonVarAccessKind<{require:String, message:Null<String>}>;
218219
var AccCtor;

std/haxe/macro/Type.hx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,11 @@ enum VarAccess {
651651
**/
652652
AccCall;
653653

654+
/**
655+
Access through private accessor function (`private get`, `private set`).
656+
**/
657+
AccPrivateCall;
658+
654659
/**
655660
Inline access (`inline`).
656661
**/

std/haxe/macro/TypeTools.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class TypeTools {
4747
case AccNever: "never";
4848
case AccResolve: throw "Invalid TAnonymous";
4949
case AccCall: getOrSet;
50+
case AccPrivateCall: 'private $getOrSet';
5051
case AccInline: "default";
5152
case AccRequire(_, _): "default";
5253
}

tests/misc/projects/Issue3053/Main.hx

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
typedef MainPrivateType = {
1+
typedef BadType = {
2+
// should error but not yet
23
var foo(private get, private set):Int;
4+
var foo2(never, never):Int;
35
}
46

5-
typedef MainType = {
7+
typedef FooPrivateGetType = {
8+
var foo(private get, set):Int;
9+
}
10+
11+
typedef FooType = {
612
var foo(get, set):Int;
713
}
814

@@ -11,14 +17,14 @@ class Main {
1117
final main = new Main();
1218
main.foo = 1;
1319

14-
var privateObj:MainPrivateType = main;
15-
var obj:MainType = main; // err, should be allowed?
20+
var privateObj:FooPrivateGetType = main;
21+
var obj:FooType = main; // err, should be allowed?
1622

1723
privateObj = obj;
1824
obj = privateObj; // err
1925
}
2026

21-
public var foo(private get, private set):Int;
27+
public var foo(private get, set):Int;
2228

2329
function get_foo():Int {
2430
return 0;
@@ -28,6 +34,16 @@ class Main {
2834
return v;
2935
}
3036

37+
public var notAllowed(private get, private set):Int; // err
38+
39+
function set_notAllowed(value:Int):Int {
40+
throw new haxe.exceptions.NotImplementedException();
41+
}
42+
43+
function get_notAllowed():Int {
44+
throw new haxe.exceptions.NotImplementedException();
45+
}
46+
3147
public function new() {
3248
foo = 1;
3349
foo;
@@ -41,7 +57,7 @@ class Main {
4157
rect.width;
4258

4359
final shape:Shape = rect;
44-
shape.width = 1; // err
60+
shape.width = 1;
4561
shape.width; // err
4662

4763
final bar = new Bar();
@@ -55,11 +71,14 @@ class Main {
5571
bar.width; // err
5672

5773
bar.defaultNull = 1; // err
74+
75+
final child = new Child();
76+
@:privateAccess child.width = 1;
5877
}
5978
}
6079

6180
interface Shape {
62-
var width(private get, private set):Int;
81+
var width(private get, set):Int;
6382
}
6483

6584
interface PublicShape {
@@ -85,7 +104,7 @@ class Rect implements Shape {
85104
}
86105

87106
public function new() {}
88-
public var width(get, private set):Int;
107+
public var width(get, private set):Int; // err
89108

90109
function set_width(value:Int):Int {
91110
return 0;
@@ -108,7 +127,7 @@ class Bar {
108127
return value;
109128
}
110129

111-
public var width(private get, private set):Int;
130+
var width(private get, private set):Int;
112131

113132
function set_width(value:Int):Int {
114133
return value;
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
Main.hx:37: characters 3-17 : This property cannot be accessed for writing
2-
Main.hx:40: characters 3-13 : This property cannot be accessed for writing
3-
Main.hx:44: characters 3-14 : This property cannot be accessed for writing
4-
Main.hx:45: characters 3-14 : This property cannot be accessed for reading
5-
Main.hx:49: characters 3-24 : This property cannot be accessed for writing
6-
Main.hx:54: characters 3-12 : This property cannot be accessed for writing
7-
Main.hx:55: characters 3-12 : This property cannot be accessed for reading
8-
Main.hx:57: characters 7-18 : This expression cannot be accessed for writing
9-
Main.hx:15: characters 3-27 : Main should be MainType
10-
Main.hx:15: characters 3-27 : ... Inconsistent access for field foo : (private get,private set) should be (get,set)
11-
Main.hx:18: characters 3-19 : MainPrivateType should be MainType
12-
Main.hx:18: characters 3-19 : ... Inconsistent access for field foo : (private get,private set) should be (get,set)
13-
Main.hx:70: characters 13-18 : Field width has different property access than in PublicShape ((get,private set) should be (get,set))
1+
Main.hx:37: characters 2-54 : notAllowed: (private get, private set) property cannot be public
2+
Main.hx:53: characters 3-17 : This property cannot be accessed for writing
3+
Main.hx:56: characters 3-13 : This property cannot be accessed for writing
4+
Main.hx:61: characters 3-14 : This property cannot be accessed for reading
5+
Main.hx:65: characters 3-24 : This property cannot be accessed for writing
6+
Main.hx:70: characters 7-12 : Cannot access private field width
7+
Main.hx:70: characters 3-12 : This property cannot be accessed for writing
8+
Main.hx:71: characters 7-12 : Cannot access private field width
9+
Main.hx:71: characters 3-12 : This property cannot be accessed for reading
10+
Main.hx:73: characters 7-18 : This expression cannot be accessed for writing
11+
Main.hx:21: characters 3-26 : Main should be FooType
12+
Main.hx:21: characters 3-26 : ... Inconsistent getter for field foo : private get should be get
13+
Main.hx:24: characters 3-19 : FooPrivateGetType should be FooType
14+
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)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
InterfaceFields.hx:1: characters 7-22 : Field missing needed by IFace is missing
2-
InterfaceFields.hx:5: characters 18-27 : Field wrongKind has different property access than in IFace (method should be var)
3-
InterfaceFields.hx:4: characters 13-24 : Field wrongAccess has different property access than in IFace ((never,null) should be (default,null))
2+
InterfaceFields.hx:5: characters 18-27 : Field wrongKind has different property access than in IFace: method should be var
3+
InterfaceFields.hx:4: characters 13-24 : Field wrongAccess has different property access than in IFace: (never,null) should be (default,null)

0 commit comments

Comments
 (0)