瀏覽代碼

Expose TVar VStatic flag in macros. (#11683)

Zeta 1 年之前
父節點
當前提交
bf8028fce7

+ 1 - 0
src/macro/macroApi.ml

@@ -1296,6 +1296,7 @@ and encode_tvar v =
 		"capture", vbool (has_var_flag v VCaptured);
 		"extra", vopt f_extra v.v_extra;
 		"meta", encode_meta v.v_meta (fun m -> v.v_meta <- m);
+		"isStatic", vbool (has_var_flag v VStatic);
 		"$", encode_unsafe (Obj.repr v);
 	]
 

+ 5 - 0
std/haxe/macro/Type.hx

@@ -770,6 +770,11 @@ typedef TVar = {
 		The metadata of the variable.
 	**/
 	public var meta(default, never):Null<MetaAccess>;
+
+	/**
+		Whether the variable is a local static variable
+	**/
+	public var isStatic(default, never):Bool;
 }
 
 /**

+ 6 - 0
tests/misc/projects/Issue11682/Main.hx

@@ -0,0 +1,6 @@
+function main() {
+	static var foo = 0;
+	myMacro(foo);
+}
+
+macro function myMacro(expr:haxe.macro.Expr):haxe.macro.Expr;

+ 10 - 0
tests/misc/projects/Issue11682/Main.macro.hx

@@ -0,0 +1,10 @@
+macro function myMacro(expr:haxe.macro.Expr):haxe.macro.Expr {
+	var texpr = haxe.macro.Context.typeExpr(expr);
+	switch texpr.expr {
+		case TLocal(tvar) if (tvar.isStatic):
+		case _:
+			haxe.macro.Context.error("Expected local static", expr.pos);
+	}
+
+	return macro null;
+}

+ 2 - 0
tests/misc/projects/Issue11682/compile.hxml

@@ -0,0 +1,2 @@
+-m Main
+--interp