2
0
Эх сурвалжийг харах

Ensure generic class names are valid identifiers (#6968)

Ben Morris 7 жил өмнө
parent
commit
47264de1d1

+ 3 - 1
src/typing/typeload.ml

@@ -3784,9 +3784,11 @@ let make_generic ctx ps pt p =
 	in
 	let name =
 		String.concat "_" (List.map2 (fun (s,_) t ->
+			let rec subst s = "_" ^ string_of_int (Char.code (String.get (Str.matched_string s) 0)) ^ "_" in
+			let ident_safe = Str.global_substitute (Str.regexp "[^a-zA-Z0-9_]") subst in
 			let s_type_path_underscore (p,s) = match p with [] -> s | _ -> String.concat "_" p ^ "_" ^ s in
 			let rec loop top t = match follow t with
-				| TInst(c,tl) -> (s_type_path_underscore c.cl_path) ^ (loop_tl tl)
+				| TInst(c,tl) -> (ident_safe (s_type_path_underscore c.cl_path)) ^ (loop_tl tl)
 				| TEnum(en,tl) -> (s_type_path_underscore en.e_path) ^ (loop_tl tl)
 				| TAbstract(a,tl) -> (s_type_path_underscore a.a_path) ^ (loop_tl tl)
 				| _ when not top -> "_" (* allow unknown/incompatible types as type parameters to retain old behavior *)

+ 16 - 0
tests/unit/src/unit/issues/Issue6952.hx

@@ -0,0 +1,16 @@
+package unit.issues;
+
+@:generic class Issue6952TestClass<@:const T> {
+	public function new() {}
+	public function foo() {
+		trace(T);
+	}
+}
+
+class Issue6952 extends Test {
+
+	function test() {
+		var x = new Issue6952TestClass<"hello world">();
+		x.foo();
+	}
+}