瀏覽代碼

Disallow case string interpolation (#10496)

RblSb 3 年之前
父節點
當前提交
a9fbf53ca3

+ 9 - 0
src/typing/matcher.ml

@@ -335,6 +335,15 @@ module Pattern = struct
 				pctx.in_reification <- old;
 				e
 			| EConst((Ident ("false" | "true") | Int _ | String _ | Float _) as ct) ->
+				begin match ct with
+					| String (value,kind) when kind = Ast.SSingleQuotes ->
+						let e = ctx.g.do_format_string ctx value p in
+						begin match e with
+							| EBinop _, p -> typing_error "String interpolation is not allowed in case patterns" p;
+							| _ -> ()
+						end;
+					| _ -> ()
+				end;
 				let p = pos e in
 				let e = Texpr.type_constant ctx.com.basic ct p in
 				unify_expected e.etype;

+ 12 - 0
tests/misc/projects/Issue9163/Main.hx

@@ -0,0 +1,12 @@
+class Main {
+	static function main() {
+		var foo = '5';
+		var bar = '5';
+		var isEqual = switch (bar) {
+			case 'foo$ foo': true;
+			case '\\ foo': true;
+			case '$foo': true;
+			default: false;
+		}
+	}
+}

+ 2 - 0
tests/misc/projects/Issue9163/compile-fail.hxml

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

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

@@ -0,0 +1 @@
+Main.hx:8: characters 10-14 : String interpolation is not allowed in case patterns