Selaa lähdekoodia

[pattern matcher] collect parent class fields so they can be matched against (closes #3507)

Simon Krajewski 10 vuotta sitten
vanhempi
commit
0e1a82e64f
2 muutettua tiedostoa jossa 45 lisäystä ja 1 poistoa
  1. 10 1
      matcher.ml
  2. 35 0
      tests/unit/src/unit/issues/Issue3507.hx

+ 10 - 1
matcher.ml

@@ -506,7 +506,16 @@ let to_pattern ctx e t =
 				| TAnon {a_fields = fields} ->
 				| TAnon {a_fields = fields} ->
 					fields
 					fields
 				| TInst(c,tl) ->
 				| TInst(c,tl) ->
-					PMap.map (fun cf -> {cf with cf_type = apply_params c.cl_params tl (monomorphs cf.cf_params cf.cf_type)}) c.cl_fields
+					let fields = ref PMap.empty in
+					let rec loop c tl =
+						begin match c.cl_super with
+							| Some (csup,tlsup) -> loop csup (List.map (apply_params c.cl_params tl) tlsup)
+							| None -> ()
+						end;
+						PMap.iter (fun n cf -> fields := PMap.add n {cf with cf_type = apply_params c.cl_params tl (monomorphs cf.cf_params cf.cf_type)} !fields) c.cl_fields
+					in
+					loop c tl;
+					!fields
 				| TAbstract({a_impl = Some c} as a,tl) ->
 				| TAbstract({a_impl = Some c} as a,tl) ->
 					let fields = List.fold_left (fun acc cf ->
 					let fields = List.fold_left (fun acc cf ->
 						if Meta.has Meta.Impl cf.cf_meta then
 						if Meta.has Meta.Impl cf.cf_meta then

+ 35 - 0
tests/unit/src/unit/issues/Issue3507.hx

@@ -0,0 +1,35 @@
+package unit.issues;
+
+private class A<T1> {
+	public var t1:T1;
+	public function new(t1:T1) {
+		this.t1 = t1;
+	}
+}
+
+private class B<T1, T2> extends A<T1> {
+	public var t2:T2;
+	public function new(t1:T1, t2:T2) {
+		super(t1);
+		this.t2 = t2;
+	}
+}
+
+private class C<T1, T2, T3> extends B<T1, T2> {
+	public var t3:T3;
+	public function new(t1:T1, t2:T2, t3:T3) {
+		super(t1, t2);
+		this.t3 = t3;
+	}
+}
+
+class Issue3507 extends Test {
+	function test() {
+		var c = new C("foo", 12, false);
+		var s = switch (c) {
+			case { t1:'foo', t2:12, t3:false } : "ok";
+			case _: "not ok";
+		}
+		eq("ok", s);
+	}
+}