浏览代码

concatenate strings while creating match patterns (fixed issue #1732)

Simon Krajewski 12 年之前
父节点
当前提交
cc733f62e2
共有 2 个文件被更改,包括 5 次插入2 次删除
  1. 2 1
      matcher.ml
  2. 3 1
      optimizer.ml

+ 2 - 1
matcher.ml

@@ -326,7 +326,7 @@ let to_pattern ctx e t =
 			mk_con_pat (CConst c) [] t p
 		| EField _ ->
 			let e = type_expr ctx e (WithType t) in
-			let e = match Optimizer.make_constant_expression ctx e with Some e -> e | None -> e in
+			let e = match Optimizer.make_constant_expression ctx ~concat_strings:true e with Some e -> e | None -> e in
 			(match e.eexpr with
 			| TConst c -> mk_con_pat (CConst c) [] t p
 			| TTypeExpr mt -> mk_con_pat (CType mt) [] t p
@@ -415,6 +415,7 @@ let to_pattern ctx e t =
 						ctx.untyped <- old;
 						e
 				in
+				let ec = match Optimizer.make_constant_expression ctx ~concat_strings:true ec with Some e -> e | None -> ec in
 				(match ec.eexpr with
 					| TField (_,FEnum (en,ef)) ->
 						begin try unify_raise ctx ec.etype t ec.epos with Error (Unify _,_) -> raise Not_found end;

+ 3 - 1
optimizer.ml

@@ -950,11 +950,13 @@ let rec reduce_loop ctx e =
 let reduce_expression ctx e =
 	if ctx.com.foptimize then reduce_loop ctx e else e
 
-let rec make_constant_expression ctx e =
+let rec make_constant_expression ctx ?(concat_strings=false) e =
 	let e = reduce_loop ctx e in
 	match e.eexpr with
 	| TConst _ -> Some e
 	| TBinop ((OpAdd|OpSub|OpMult|OpDiv|OpMod) as op,e1,e2) -> (match make_constant_expression ctx e1,make_constant_expression ctx e2 with
+		| Some ({eexpr = TConst (TString s1)}), Some ({eexpr = TConst (TString s2)}) when concat_strings ->
+			Some (mk (TConst (TString (s1 ^ s2))) ctx.com.basic.tstring (punion e1.epos e2.epos))
 		| Some e1, Some e2 -> Some (mk (TBinop(op, e1, e2)) e.etype e.epos)
 		| _ -> None)
 	| TParenthesis e -> Some e