From 1477fbae1961a615ebbfa0e60f741f5514edd438 Mon Sep 17 00:00:00 2001 From: Vassil Mladenov Date: Wed, 23 Dec 2020 14:21:18 -0800 Subject: [PATCH] Pretty print capabilities in generic subtyping errors Summary: Previously, the subtyping would proceed with standard decomposition of types and report the error, so the error would be something like `Expected \HH\Capabilities\WriteProp but got mixed`. This interrupts the error and reports the full coeffect list on both sides. Using a different code because we don't want this to be fixmeable unlike 4110. Reviewed By: DavidSnider Differential Revision: D25695994 fbshipit-source-id: 89c1e2609d20a6bdd36bc18a0100b88cba81a43f --- hphp/hack/src/errors/error_codes.ml | 1 + hphp/hack/src/errors/errors.ml | 10 ++++++++++ hphp/hack/src/errors/errors.mli | 3 +++ hphp/hack/src/oxidized/gen/error_codes.rs | 3 ++- hphp/hack/src/typing/typing_subtype.ml | 17 +++++++++++++++++ hphp/hack/test/typecheck/coeffects/pretty_subtyping.php | 7 +++++++ .../test/typecheck/coeffects/pretty_subtyping.php.exp | 6 ++++++ 7 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 hphp/hack/test/typecheck/coeffects/pretty_subtyping.php create mode 100644 hphp/hack/test/typecheck/coeffects/pretty_subtyping.php.exp diff --git a/hphp/hack/src/errors/error_codes.ml b/hphp/hack/src/errors/error_codes.ml index 4ad11b5d9ad..aa2760f97fd 100644 --- a/hphp/hack/src/errors/error_codes.ml +++ b/hphp/hack/src/errors/error_codes.ml @@ -673,6 +673,7 @@ module Typing = struct | IFCPolicyMismatch [@value 4400] | OpCoeffects [@value 4401] | ImplementsDynamic [@value 4402] (* EXTEND HERE WITH NEW VALUES IF NEEDED *) + | SubtypeCoeffects [@value 4403] [@@deriving enum, show { with_path = false }] let err_code = to_enum diff --git a/hphp/hack/src/errors/errors.ml b/hphp/hack/src/errors/errors.ml index bb56430392c..3ea95bc4ab4 100644 --- a/hphp/hack/src/errors/errors.ml +++ b/hphp/hack/src/errors/errors.ml @@ -5270,6 +5270,16 @@ let enum_inclusion_not_enum ^ " which is not an enum" ) [] +(* This is meant for subtyping functions with incompatible coeffects, i.e. + * (function ()[io]: void) unit +val coeffect_subtyping_error : + Pos.t -> string -> Pos.t -> string -> typing_error_callback -> unit + val op_coeffect_error : locally_available:string -> available_pos:Pos.t -> diff --git a/hphp/hack/src/oxidized/gen/error_codes.rs b/hphp/hack/src/oxidized/gen/error_codes.rs index db28378d37c..2439043fe3e 100644 --- a/hphp/hack/src/oxidized/gen/error_codes.rs +++ b/hphp/hack/src/oxidized/gen/error_codes.rs @@ -3,7 +3,7 @@ // This source code is licensed under the MIT license found in the // LICENSE file in the "hack" directory of this source tree. // -// @generated SignedSource<<1dfdf4ca7240e17d0d8af4af23995191>> +// @generated SignedSource<<47420593d224e913c0dd1f5666f320d0>> // // To regenerate this file, run: // hphp/hack/src/oxidized/regen.sh @@ -565,6 +565,7 @@ pub enum Typing { IFCPolicyMismatch = 4400, OpCoeffects = 4401, ImplementsDynamic = 4402, + SubtypeCoeffects = 4403, } impl TrivialDrop for Typing {} diff --git a/hphp/hack/src/typing/typing_subtype.ml b/hphp/hack/src/typing/typing_subtype.ml index 925bd3c4ed8..69122a91bd1 100644 --- a/hphp/hack/src/typing/typing_subtype.ml +++ b/hphp/hack/src/typing/typing_subtype.ml @@ -2200,6 +2200,23 @@ and simplify_subtype_params_with_variadic and simplify_subtype_implicit_params ~subtype_env { capability = sub_cap } { capability = super_cap } env = if TypecheckerOptions.any_coeffects (Env.get_tcopt env) then + let subtype_env = + { + subtype_env with + on_error = + begin + fun ?code:_ _ _ -> + let expected = Typing_coeffects.get_type sub_cap in + let got = Typing_coeffects.get_type super_cap in + Errors.coeffect_subtyping_error + (get_pos expected) + (Typing_print.coeffects env expected) + (get_pos got) + (Typing_print.coeffects env got) + subtype_env.on_error + end; + } + in match (sub_cap, super_cap) with | (CapTy sub, CapTy super) -> simplify_subtype ~subtype_env sub super env | (CapTy sub, CapDefaults _p) -> diff --git a/hphp/hack/test/typecheck/coeffects/pretty_subtyping.php b/hphp/hack/test/typecheck/coeffects/pretty_subtyping.php new file mode 100644 index 00000000000..8b22fcc75a6 --- /dev/null +++ b/hphp/hack/test/typecheck/coeffects/pretty_subtyping.php @@ -0,0 +1,7 @@ +