1 // Copyright (c) Meta Platforms, Inc. and affiliates.
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
6 #![allow(unused_imports, unused_variables, dead_code)]
7 use crate::inference_env::InferenceEnv;
9 use ty::local::{Ty, Ty_, Tyvar};
10 use ty::reason::Reason;
12 pub fn remove_tyvar_from_upper_bound<R: Reason>(
13 env: &mut InferenceEnv<R>,
17 remove_tyvar_from_upper_bound_help(env, tv, ty).unwrap_or_else(|| Ty::mixed(R::none()))
20 fn remove_tyvar_from_upper_bound_help<R: Reason>(
21 env: &mut InferenceEnv<R>,
26 let ty = env.resolve_ty(ty);
28 Tvar(tv2) if tv == *tv2 => None,
30 let ety2 = remove_tyvar_from_upper_bound_help(env, tv, ty2)?;
31 Some(Ty::option(ty.reason().clone(), ety2))
36 .map(|ty| remove_tyvar_from_upper_bound_help(env, tv, ty))
37 .collect::<Option<_>>()?;
38 Some(Ty::union(ty.reason().clone(), tys_out))
44 pub fn remove_tyvar_from_lower_bound<R: Reason>(
45 env: &mut InferenceEnv<R>,
49 remove_tyvar_from_lower_bound_help(env, tv, ty).unwrap_or_else(|| Ty::nothing(R::none()))
52 fn remove_tyvar_from_lower_bound_help<R: Reason>(
53 env: &mut InferenceEnv<R>,
58 let ty = env.resolve_ty(ty);
60 Tvar(tv2) if tv == *tv2 => None,
64 .filter_map(|ty| remove_tyvar_from_lower_bound_help(env, tv, ty))
66 Some(Ty::union(ty.reason().clone(), tys_out))
77 use ty::reason::NReason;
78 use utils::core::IdentGen;
81 fn test_remove_upper() {
82 let mut env = InferenceEnv::default();
83 let gen = IdentGen::new();
85 let tv_0: Tyvar = gen.make().into();
86 let ty_v0 = Ty::var(NReason::none(), tv_0);
87 let tv_1: Tyvar = gen.make().into();
88 let ty_v1 = Ty::var(NReason::none(), tv_1);
89 let ty_no_v0 = Ty::option(
94 Ty::int(NReason::none()),
95 Ty::union(NReason::none(), vec![Ty::float(NReason::none()), ty_v1]),
99 let ty_with_v0 = Ty::option(
104 Ty::int(NReason::none()),
105 Ty::union(NReason::none(), vec![Ty::float(NReason::none()), ty_v0]),
110 assert!(remove_tyvar_from_upper_bound_help(&mut env, tv_0, &ty_no_v0).is_some());
111 assert!(remove_tyvar_from_upper_bound_help(&mut env, tv_0, &ty_with_v0).is_none());