From d38c05aa9cbee842a5d38b3d4dc4749dce978950 Mon Sep 17 00:00:00 2001 From: Sam Goldman Date: Mon, 22 Jun 2015 22:28:43 -1000 Subject: [PATCH] Don't create default constructor for subclasses This behavior prevented classes from correctly inheriting the constructor function from their superclass. Fixes #214 --- src/typing/type_inference_js.ml | 15 +++++++++------ tests/class_subtyping/class_subtyping.exp | 2 ++ tests/generics/generics.exp | 6 +++++- tests/generics/generics.js | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/typing/type_inference_js.ml b/src/typing/type_inference_js.ml index 831abf6759a..32f3f747899 100644 --- a/src/typing/type_inference_js.ml +++ b/src/typing/type_inference_js.ml @@ -4578,14 +4578,17 @@ and body_loc = Ast.Statement.FunctionDeclaration.(function ) (* Makes signatures for fields and methods in a class. *) -and mk_signature cx reason_c c_type_params_map body = Ast.Class.( +and mk_signature cx reason_c c_type_params_map superClass body = Ast.Class.( let _, { Body.body = elements } = body in (* In case there is no constructor, we create one. *) - let default_methods = - SMap.singleton "constructor" - (replace_reason "default constructor" reason_c, [], SMap.empty, - ([], [], VoidT.t, SMap.empty, SMap.empty)) + let default_methods = match superClass with + | None -> + SMap.singleton "constructor" + (replace_reason "default constructor" reason_c, [], SMap.empty, + ([], [], VoidT.t, SMap.empty, SMap.empty)) + | Some _ -> + SMap.empty in (* NOTE: We used to mine field declarations from field assignments in a constructor as a convenience, but it was not worth it: often, all that did @@ -4765,7 +4768,7 @@ and mk_class cx loc reason_c = Ast.Class.(function { id=_; body; superClass; (* fields: { f: X }, methods_: { m(x: Y): X } *) let sfields, smethods_, fields, methods_ = - mk_signature cx reason_c type_params_map body + mk_signature cx reason_c type_params_map superClass body in let id = Flow_js.mk_nominal cx in diff --git a/tests/class_subtyping/class_subtyping.exp b/tests/class_subtyping/class_subtyping.exp index 6bd9a0ac8f2..5f0de1821f9 100644 --- a/tests/class_subtyping/class_subtyping.exp +++ b/tests/class_subtyping/class_subtyping.exp @@ -1,4 +1,6 @@ +test2.js:7:14,20: constructor call +Error: test2.js:7:18,18: C This type is incompatible with test2.js:7:8,10: B diff --git a/tests/generics/generics.exp b/tests/generics/generics.exp index d5a2149e434..b50494f0f88 100644 --- a/tests/generics/generics.exp +++ b/tests/generics/generics.exp @@ -7,6 +7,10 @@ generics.js:18:18,18: number This type is incompatible with generics.js:19:7,12: string +generics.js:27:9,15: undefined (too few arguments, expected default/rest parameters) +This type is incompatible with +generics.js:28:7,12: string + generics.js:28:22,22: number This type is incompatible with generics.js:28:7,12: string @@ -24,4 +28,4 @@ Incorrect number of type parameters (expected 0) generics.js:43:10,25: Incorrect number of type parameters (expected 0) -Found 7 errors +Found 8 errors diff --git a/tests/generics/generics.js b/tests/generics/generics.js index 7c69d8b39b9..603f3aebb83 100644 --- a/tests/generics/generics.js +++ b/tests/generics/generics.js @@ -24,7 +24,7 @@ class E extends C { set(x:X):X { /*return x;*/ this.x = x; return /*this.x; */this.get(); } } -var e = new E(); +var e = new E(); // error: too few arguments to inherited constructor var x:string = e.set(0); class F { }