ソースを参照

deal with unqualified extern `@:enum` abstract constructors (closes #4862)

Simon Krajewski 9 年 前
コミット
4db4a19771
2 ファイル変更8 行追加34 行削除
  1. 6 33
      matcher.ml
  2. 2 1
      tests/unit/src/unit/issues/Issue4862.hx

+ 6 - 33
matcher.ml

@@ -416,39 +416,10 @@ let to_pattern ctx e t =
 			end
 		| EConst(Ident s) ->
 			begin try
-				let ec = match follow t with
-					| TEnum(en,pl) ->
-						let ef = try
-							PMap.find s en.e_constrs
-						with Not_found when not (is_lower_ident s) ->
-							error (string_error s en.e_names ("Expected constructor for enum " ^ (s_type_path en.e_path))) p
-						in
-						(match ef.ef_type with
-							| TFun (args,_) ->
-								let msg = Printf.sprintf "Enum constructor %s.%s requires parameters %s"
-									(s_type_path en.e_path)
-									ef.ef_name
-									(String.concat ", " (List.map (fun (n,_,t) -> n ^ ":" ^ (s_type t)) args))
-								in
-								error msg p
-							| _ -> ());
-						let et = mk (TTypeExpr (TEnumDecl en)) (TAnon { a_fields = PMap.empty; a_status = ref (EnumStatics en) }) p in
-						mk (TField (et,FEnum (en,ef))) (apply_params en.e_params pl ef.ef_type) p
-					| TAbstract({a_impl = Some c} as a,_) when Meta.has Meta.Enum a.a_meta ->
-						let cf = PMap.find s c.cl_statics in
-						Type.unify (follow cf.cf_type) t;
-						let e = begin match cf.cf_expr with
-						| Some ({eexpr = TConst c | TCast({eexpr = TConst c},None)} as e) -> e
-						| _ -> raise Not_found
-						end in
-						e
-					| _ ->
-						let old = ctx.untyped in
-						ctx.untyped <- true;
-						let e = try type_expr ctx e (WithType t) with _ -> ctx.untyped <- old; raise Not_found in
-						ctx.untyped <- old;
-						e
-				in
+				let old = ctx.in_call_args in
+				ctx.in_call_args <- true; (* Not really, but it does exactly what we want here. *)
+				let ec = try type_expr ctx e (WithType t) with _ -> ctx.in_call_args <- old; raise Not_found in
+				ctx.in_call_args <- old;
 				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)) ->
@@ -465,6 +436,8 @@ let to_pattern ctx e t =
 						mk_con_pat (CConst c) [] t p
 					| TTypeExpr mt ->
 						mk_type_pat ctx mt t p
+					| TField(_,FStatic({cl_extern = true},({cf_kind = Var {v_write = AccNever}} as cf))) ->
+						mk_con_pat (CExpr ec) [] cf.cf_type p
 					| _ ->
 						raise Not_found);
 			with Not_found ->

+ 2 - 1
tests/unit/src/unit/issues/Issue4862.hx

@@ -19,9 +19,10 @@ class Issue4862 extends Test {
         eq(200, a);
         var b = HttpStatus.NotFound;
         eq(404, b);
-        // TODO: support unqualified matching
         t(switch (a) { case HttpStatus.Ok: true; default: false; });
         t(switch (b) { case HttpStatus.NotFound: true; default: false; });
+        t(switch (a) { case Ok: true; default: false; });
+        t(switch (b) { case NotFound: true; default: false; });
     }
     #end
 }