瀏覽代碼

add @:noPrivateAccess to re-enable access control within @:privateAccess (closes #3714)

Dan Korostelev 10 年之前
父節點
當前提交
f9ab409deb

+ 1 - 0
ast.ml

@@ -123,6 +123,7 @@ module Meta = struct
 		| NoExpr
 		| NoImportGlobal
 		| NoPackageRestrict
+		| NoPrivateAccess
 		| NoStack
 		| NotNull
 		| NoUsing

+ 1 - 0
common.ml

@@ -445,6 +445,7 @@ module MetaInfo = struct
 		| NoExpr -> ":noExpr",("Internally used to mark abstract fields which have no expression by design",[Internal])
 		| NoImportGlobal -> ":noImportGlobal",("Prevents a static field from being imported with import Class.*",[UsedOn TAnyField])
 		| NoPackageRestrict -> ":noPackageRestrict",("Allows a module to be accessed across all targets if found on its first type",[Internal])
+		| NoPrivateAccess -> ":noPrivateAccess",("Disallow private access to anything for the annotated expression",[UsedOn TExpr])
 		| NoStack -> ":noStack",("",[Platform Cpp])
 		| NotNull -> ":notNull",("Declares an abstract type as not accepting null values",[UsedOn TAbstract])
 		| NoUsing -> ":noUsing",("Prevents a field from being used with 'using'",[UsedOn TClassField])

+ 4 - 0
extra/CHANGES.txt

@@ -1,5 +1,9 @@
 2015-??-??: 3.2.0
 
+	New features:
+
+	all : added @:noPrivateAccess to re-enable access restrictions within @:privateAccess
+
 	Bugfixes:
 
 	all : fixed detection of @:generic classes with constructor constraints

+ 13 - 0
tests/misc/projects/Issue3714/Main.hx

@@ -0,0 +1,13 @@
+class Main {
+    static macro function m() {
+        return macro @:pos(haxe.macro.Context.currentPos()) @:noPrivateAccess A.a;
+    }
+
+	static function main() {
+        m();
+	}
+}
+
+class A {
+    static var a:Int;
+}

+ 1 - 0
tests/misc/projects/Issue3714/compile-fail.hxml

@@ -0,0 +1 @@
+-main Main

+ 1 - 0
tests/misc/projects/Issue3714/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main.hx:7: characters 8-11 : Cannot access private field a

+ 15 - 0
tests/unit/src/unit/issues/Issue3714.hx

@@ -0,0 +1,15 @@
+package unit.issues;
+
+private class A {
+    static var a:Int;
+    static function f(a:Int) {}
+}
+
+class Issue3714 extends Test {
+    function test() {
+        eq(unit.TestType.typeErrorText(A.f), "Cannot access private field f");
+        eq(unit.TestType.typeErrorText(@:privateAccess A.f), null);
+        eq(unit.TestType.typeErrorText(@:privateAccess A.f(A.a)), null);
+        eq(unit.TestType.typeErrorText(@:privateAccess A.f(@:noPrivateAccess A.a)), "Cannot access private field a");
+    }
+}

+ 3 - 0
typer.ml

@@ -3553,6 +3553,9 @@ and type_expr ctx (e,p) (with_type:with_type) =
 			| (Meta.StoredTypedExpr,_,_) ->
 				let id = match e1 with (EConst (Int s),_) -> int_of_string s | _ -> assert false in
 				get_stored_typed_expr ctx.com id
+			| (Meta.NoPrivateAccess,_,_) ->
+				ctx.meta <- List.filter (fun(m,_,_) -> m <> Meta.PrivateAccess) ctx.meta;
+				e()
 			| _ -> e()
 		in
 		ctx.meta <- old;