Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array(n).fill(value) isn't typechecked #7887

Open
julienw opened this issue Jul 4, 2019 · 5 comments · May be fixed by #7917
Open

Array(n).fill(value) isn't typechecked #7887

julienw opened this issue Jul 4, 2019 · 5 comments · May be fixed by #7917
Labels
bug Has PR Library definitions Issues or pull requests about core library definitions Typing: soundness No false negatives (type checker claims that there is no error in the incorrect program)

Comments

@julienw
Copy link
Contributor

julienw commented Jul 4, 2019

With this code, Flow doesn't find the error:

type Plop = {
  prop: string[],
};

var a: Plop = {
  prop: Array(5).fill(1),
}

I believe this is because the Array constructor returns an array of any items in

static (...values:Array<any>): Array<any>;
.

try link

@julienw
Copy link
Contributor Author

julienw commented Jul 4, 2019

Interestingly it works fine with new Array :)

@canova
Copy link

canova commented Jul 4, 2019

ECMA says Array function is the same with the array constructor. I don't know the codebase but probably we are missing that function or there is something with its type.
See the spec (section 15.4.1, The Array Constructor Called as a Function)

@goodmind
Copy link
Contributor

goodmind commented Jul 4, 2019

Array constructor is defined in OCaml code for some reason

@goodmind
Copy link
Contributor

goodmind commented Jul 4, 2019

flow/src/typing/statement.ml

Lines 3068 to 3115 in f796b60

| New {
New.callee = callee_loc, Identifier (id_loc, ({ Ast.Identifier.name= "Array" as n; comments= _ } as name));
targs;
arguments
} -> (
let targts = Option.map targs (fun (loc, args) ->
loc, (convert_tparam_instantiations cx SMap.empty args)
) in
let args = Core_list.map ~f:(expression_or_spread cx) arguments in
let result = match targts, args with
| Some (loc, ([t], [elem_t])), [Arg argt, arg] -> Ok (Some (loc, elem_t, t), argt, arg)
| None, [Arg argt, arg] -> Ok (None, argt, arg)
| None, _ -> Error (Error_message.EUseArrayLiteral loc)
| Some _, _ ->
Error Error_message.(ECallTypeArity {
call_loc = loc;
is_new = true;
reason_arity = Reason.(locationless_reason (RType n));
expected_arity = 1;
})
in
match result with
| Ok (targ_t, arg_t, arg) ->
let reason = mk_reason (RCustom "new Array(..)") loc in
let length_reason =
replace_reason_const (RCustom "array length") reason in
Flow.flow_t cx (arg_t, DefT (length_reason, bogus_trust (), NumT AnyLiteral));
let t, targs = match targ_t with
| Some (loc, ast, ExplicitArg t) -> t, Some (loc, [ast])
| Some (_, _, ImplicitArg _)
| None ->
let element_reason =
replace_reason_const (RCustom "array element") reason in
Tvar.mk cx element_reason, None
in
let id_t = identifier cx name callee_loc in
(* TODO - tuple_types could be undefined x N if given a literal *)
(loc, DefT (reason, bogus_trust (), ArrT (ArrayAT (t, None)))),
New { New.
callee = (callee_loc, id_t), Identifier ((id_loc, id_t), name);
targs;
arguments = [arg];
}
| Error err ->
Flow.add_output cx err;
Tast_utils.error_mapper#expression ex
)

@goodmind
Copy link
Contributor

goodmind commented Jul 4, 2019

Not sure why it's not possible to typecheck it like this instead of doing this kind of hardcoding
https://flow.org/try/#0CYUwxgNghgTiAEkoGdnwLIE8CCMZUwB4AVAPngG8AoeRAewDtkAXGAVzGbpgAoGAueAzYBbAEYgYASkG58RECIAOzTKQDcNek1YcuvAHRHYAc2SDiAbQC6M+HIIkNWrSyjMAlmHh9Bw8ZJ2DgrKqs60bp7ePEYGpubwVrayeI5kmgC+LmCMLPAAZnR08AC8GDipmDwArFJUOTrwYrCl5cE8AIwANPAATD0AzHX1ucwFRQAMrQwgAO5tlTV1DXnNMFNlM-NY7d19g1JAA
this error just doesn't make sense

13: const bar0 = new Array(1, 2, 3)                 
^ Use an array literal instead of `new Array(...)`.

@mvitousek mvitousek added Typing: soundness No false negatives (type checker claims that there is no error in the incorrect program) and removed needs triage labels Jul 4, 2019
@goodmind goodmind linked a pull request Jul 10, 2019 that will close this issue
@SamChou19815 SamChou19815 added the Library definitions Issues or pull requests about core library definitions label Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Has PR Library definitions Issues or pull requests about core library definitions Typing: soundness No false negatives (type checker claims that there is no error in the incorrect program)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants