From 66981dcca6221a5bf9c5d4aeb4440bcc6f3bb844 Mon Sep 17 00:00:00 2001 From: Lucian Wischik Date: Wed, 6 Oct 2021 10:53:21 -0700 Subject: [PATCH] FILENAME 4/4 - naming_sqlite.rs uses NameType not NameKind Summary: My goal is to have a clean implementation of the FILENAME action all the way from Naming_provider through to decl-service-worker. There are two types in ocaml, `Naming_types.name_kind` which has structure and `FileInfo.name_type` which doesn't. The ocaml codebase uses both, mostly through historical legacy of code which (pointlessly) uses `Naming_types.kind_of_type`. The decl service doesn't need that legacy. I've switched over to using `file_info::NameType` everywhere in the decl service. Indeed, the only consumer of it was Naming_provider.ml, which always wanted a NameType, so all the intermediate uses of NameKind were just a waste. I also moved `impl FromSql for SqliteNameKind` in naming_sqlite/datatypes.rs into `impl FromSql for NameType` in oxidized/file_info_impl.rs. It feels a little bit awkward that a really tiny atomic type like file_info should depend upon rusqlite, but only a little bit. Differential Revision: D31326773 fbshipit-source-id: 1acd6394f10dd11a4e19179be2a0373cac8ece8a --- hphp/hack/Cargo.lock | 1 + hphp/hack/src/oxidized/Cargo.toml | 1 + hphp/hack/src/oxidized/manual/file_info_impl.rs | 42 +++++++++++++--------- hphp/hack/src/oxidized/manual/mod.rs | 1 - hphp/hack/src/oxidized/manual/naming_types_impl.rs | 32 ----------------- 5 files changed, 28 insertions(+), 49 deletions(-) delete mode 100644 hphp/hack/src/oxidized/manual/naming_types_impl.rs diff --git a/hphp/hack/Cargo.lock b/hphp/hack/Cargo.lock index 7bafff31618..b4deb46b76b 100644 --- a/hphp/hack/Cargo.lock +++ b/hphp/hack/Cargo.lock @@ -2834,6 +2834,7 @@ dependencies = [ "ocamlrep", "ocamlrep_derive", "pretty_assertions", + "rusqlite", "serde", "static_assertions", ] diff --git a/hphp/hack/src/oxidized/Cargo.toml b/hphp/hack/src/oxidized/Cargo.toml index c56ceb75eac..6296d56e1d5 100644 --- a/hphp/hack/src/oxidized/Cargo.toml +++ b/hphp/hack/src/oxidized/Cargo.toml @@ -17,6 +17,7 @@ itertools = "0.10.1" no_pos_hash = { path = "../utils/no_pos_hash" } ocamlrep = { path = "../ocamlrep" } ocamlrep_derive = { path = "../ocamlrep_derive" } +rusqlite = { version = "0.23", features = ["backup", "blob"] } serde = { version = "1.0.126", features = ["derive", "rc"] } static_assertions = "1.1.0" diff --git a/hphp/hack/src/oxidized/manual/file_info_impl.rs b/hphp/hack/src/oxidized/manual/file_info_impl.rs index b0a703f999e..8710de2b0b1 100644 --- a/hphp/hack/src/oxidized/manual/file_info_impl.rs +++ b/hphp/hack/src/oxidized/manual/file_info_impl.rs @@ -3,11 +3,13 @@ // This source code is licensed under the MIT license found in the // LICENSE file in the "hack" directory of this source tree. -use std::convert::{From, TryFrom}; +use std::convert::From; use crate::gen::file_info::Mode; use crate::gen::file_info::NameType; use crate::gen::naming_types::KindOfType; +use rusqlite::types::FromSql; +use rusqlite::types::{FromSqlError, FromSqlResult, ValueRef}; impl Mode { pub fn from_string(s: &str) -> Option { @@ -19,21 +21,6 @@ impl Mode { } } -impl TryFrom for NameType { - type Error = String; - - fn try_from(kind: u32) -> Result { - match kind { - 0 => Ok(NameType::Fun), - 1 => Ok(NameType::Class), - 2 => Ok(NameType::RecordDef), - 3 => Ok(NameType::Typedef), - 4 => Ok(NameType::Const), - _ => Err(format!("Out of range for NameType: {}", kind)), - } - } -} - impl From for NameType { fn from(kind: KindOfType) -> Self { match kind { @@ -43,3 +30,26 @@ impl From for NameType { } } } + +impl FromSql for NameType { + fn column_result(value: ValueRef<'_>) -> FromSqlResult { + match value { + ValueRef::Integer(i) => { + if i == NameType::Fun as i64 { + Ok(NameType::Fun) + } else if i == NameType::Const as i64 { + Ok(NameType::Const) + } else if i == NameType::Class as i64 { + Ok(NameType::Class) + } else if i == NameType::Typedef as i64 { + Ok(NameType::Typedef) + } else if i == NameType::RecordDef as i64 { + Ok(NameType::RecordDef) + } else { + Err(FromSqlError::OutOfRange(i)) + } + } + _ => Err(FromSqlError::InvalidType), + } + } +} diff --git a/hphp/hack/src/oxidized/manual/mod.rs b/hphp/hack/src/oxidized/manual/mod.rs index 0f56a8040be..61ed13fb059 100644 --- a/hphp/hack/src/oxidized/manual/mod.rs +++ b/hphp/hack/src/oxidized/manual/mod.rs @@ -19,7 +19,6 @@ pub mod i_set; pub mod local_id; pub mod local_id_map; pub mod namespace_env_impl; -pub mod naming_types_impl; pub mod phase_map; pub mod pos; pub mod pos_span_raw; diff --git a/hphp/hack/src/oxidized/manual/naming_types_impl.rs b/hphp/hack/src/oxidized/manual/naming_types_impl.rs deleted file mode 100644 index 60d99652823..00000000000 --- a/hphp/hack/src/oxidized/manual/naming_types_impl.rs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) Facebook, Inc. and its affiliates. -// -// This source code is licensed under the MIT license found in the -// LICENSE file in the "hack" directory of this source tree. - -use std::convert::TryFrom; - -use crate::file_info::NameType; -use crate::gen::naming_types::{KindOfType, NameKind}; - -impl TryFrom for NameKind { - type Error = String; - - fn try_from(kind: i64) -> Result { - // Must be kept in sync with naming_types.ml and namekind_to_i64 - // name_type.ml is where the numerical values are defined, and NameType in rust is - // generated from it, and all other places including namkind_to_i64 just use it. - if kind == (NameType::Class as i64) { - Ok(NameKind::TypeKind(KindOfType::TClass)) - } else if kind == (NameType::Typedef as i64) { - Ok(NameKind::TypeKind(KindOfType::TTypedef)) - } else if kind == (NameType::RecordDef as i64) { - Ok(NameKind::TypeKind(KindOfType::TRecordDef)) - } else if kind == (NameType::Fun as i64) { - Ok(NameKind::FunKind) - } else if kind == (NameType::Const as i64) { - Ok(NameKind::ConstKind) - } else { - Err("oops".to_owned()) - } - } -} -- 2.11.4.GIT