Skip to content

Commit

Permalink
Don't create default constructor for subclasses
Browse files Browse the repository at this point in the history
This behavior prevented classes from correctly inheriting the
constructor function from their superclass.

Fixes facebook#214
  • Loading branch information
samwgoldman committed Jun 23, 2015
1 parent 729c2af commit d38c05a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/typing/type_inference_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -4765,7 +4768,7 @@ and mk_class cx loc reason_c = Ast.Class.(function { id=_; body; superClass;

(* fields: { f: X }, methods_: { m<Y: X>(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
Expand Down
2 changes: 2 additions & 0 deletions tests/class_subtyping/class_subtyping.exp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion tests/generics/generics.exp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion tests/generics/generics.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class E<X> extends C<X> {
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<X> { }
Expand Down

0 comments on commit d38c05a

Please sign in to comment.