Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Jul 2, 2024
1 parent d91626c commit 1dc038e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 82 deletions.
141 changes: 63 additions & 78 deletions crates/red_knot_module_resolver/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(unsafe_code)]
use std::iter::FusedIterator;
use std::ops::Deref;

use ruff_db::file_system::{FileSystemPath, FileSystemPathBuf};
use ruff_db::vfs::VfsPath;
Expand All @@ -14,102 +13,34 @@ use crate::Db;
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct ExtraPath(FileSystemPath);

impl ExtraPath {
#[must_use]
fn new_unchecked(path: &FileSystemPath) -> &Self {
// SAFETY: ExtraPath is marked as #[repr(transparent)] so the conversion from a
// *const FileSystemPath to a *const ExtraPath is valid.
unsafe { &*(path as *const FileSystemPath as *const Self) }
}
}

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub(crate) struct ExtraPathBuf(FileSystemPathBuf);

impl Deref for ExtraPathBuf {
type Target = ExtraPath;

fn deref(&self) -> &Self::Target {
ExtraPath::new_unchecked(&self.0)
}
}

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct FirstPartyPath(FileSystemPath);

impl FirstPartyPath {
#[must_use]
fn new_unchecked(path: &FileSystemPath) -> &Self {
// SAFETY: FirstPartyPath is marked as #[repr(transparent)] so the conversion from a
// *const FileSystemPath to a *const FirstPartyPath is valid.
unsafe { &*(path as *const FileSystemPath as *const Self) }
}
}

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub(crate) struct FirstPartyPathBuf(FileSystemPathBuf);

impl Deref for FirstPartyPathBuf {
type Target = FirstPartyPath;

fn deref(&self) -> &Self::Target {
FirstPartyPath::new_unchecked(&self.0)
}
}

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct StandardLibraryPath(FileSystemPath);

impl StandardLibraryPath {
#[must_use]
fn new_unchecked(path: &FileSystemPath) -> &Self {
// SAFETY: StandardLibraryPath is marked as #[repr(transparent)] so the conversion from a
// *const FileSystemPath to a *const StandardLibraryPath is valid.
unsafe { &*(path as *const FileSystemPath as *const Self) }
}
}

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub(crate) struct StandardLibraryPathBuf(FileSystemPathBuf);

impl Deref for StandardLibraryPathBuf {
type Target = StandardLibraryPath;

fn deref(&self) -> &Self::Target {
StandardLibraryPath::new_unchecked(&self.0)
}
}

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct SitePackagesPath(FileSystemPath);

impl SitePackagesPath {
#[must_use]
fn new_unchecked(path: &FileSystemPath) -> &Self {
// SAFETY: SitePackagesPath is marked as #[repr(transparent)] so the conversion from a
// *const FileSystemPath to a *const SitePackagesPath is valid.
unsafe { &*(path as *const FileSystemPath as *const Self) }
}
}

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub(crate) struct SitePackagesPathBuf(FileSystemPathBuf);

impl Deref for SitePackagesPathBuf {
type Target = SitePackagesPath;

fn deref(&self) -> &Self::Target {
SitePackagesPath::new_unchecked(&self.0)
}
}

/// Enumeration of the different kinds of search paths type checkers are expected to support.
///
/// N.B. Although we don't implement `Ord` for this enum, they are ordered in terms of the
Expand Down Expand Up @@ -158,6 +89,7 @@ impl ModuleResolutionPath {
inner.push(component);
}

#[must_use]
pub(crate) fn extra(path: FileSystemPathBuf) -> Option<Self> {
if path
.extension()
Expand All @@ -169,10 +101,12 @@ impl ModuleResolutionPath {
}
}

#[must_use]
fn extra_unchecked(path: FileSystemPathBuf) -> Self {
Self::Extra(ExtraPathBuf(path))
}

#[must_use]
pub(crate) fn first_party(path: FileSystemPathBuf) -> Option<Self> {
if path
.extension()
Expand All @@ -184,10 +118,12 @@ impl ModuleResolutionPath {
}
}

#[must_use]
fn first_party_unchecked(path: FileSystemPathBuf) -> Self {
Self::FirstParty(FirstPartyPathBuf(path))
}

#[must_use]
pub(crate) fn standard_library(path: FileSystemPathBuf) -> Option<Self> {
if path.extension().map_or(true, |ext| ext == "pyi") {
Some(Self::standard_library_unchecked(path))
Expand All @@ -196,14 +132,17 @@ impl ModuleResolutionPath {
}
}

#[must_use]
pub(crate) fn stdlib_from_typeshed_root(typeshed_root: &FileSystemPath) -> Option<Self> {
Self::standard_library(typeshed_root.join(FileSystemPath::new("stdlib")))
}

#[must_use]
fn standard_library_unchecked(path: FileSystemPathBuf) -> Self {
Self::StandardLibrary(StandardLibraryPathBuf(path))
}

#[must_use]
pub(crate) fn site_packages(path: FileSystemPathBuf) -> Option<Self> {
if path
.extension()
Expand All @@ -215,31 +154,38 @@ impl ModuleResolutionPath {
}
}

#[must_use]
fn site_packages_unchecked(path: FileSystemPathBuf) -> Self {
Self::SitePackages(SitePackagesPathBuf(path))
}

#[must_use]
pub(crate) fn is_regular_package(&self, db: &dyn Db) -> bool {
ModuleResolutionPathRef::from(self).is_regular_package(db)
}

#[must_use]
pub(crate) fn is_directory(&self, db: &dyn Db) -> bool {
ModuleResolutionPathRef::from(self).is_directory(db)
}

#[must_use]
pub(crate) fn with_pyi_extension(&self) -> Self {
ModuleResolutionPathRef::from(self).with_pyi_extension()
}

#[must_use]
pub(crate) fn with_py_extension(&self) -> Option<Self> {
ModuleResolutionPathRef::from(self).with_py_extension()
}

#[cfg(test)]
#[must_use]
pub(crate) fn join(&self, component: &(impl AsRef<FileSystemPath> + ?Sized)) -> Self {
ModuleResolutionPathRef::from(self).join(component)
}

#[must_use]
pub(crate) fn as_file_system_path_buf(&self) -> &FileSystemPathBuf {
match self {
Self::Extra(ExtraPathBuf(path)) => path,
Expand Down Expand Up @@ -308,12 +254,14 @@ impl PartialEq<ModuleResolutionPath> for FileSystemPath {
}

impl AsRef<FileSystemPathBuf> for ModuleResolutionPath {
#[inline]
fn as_ref(&self) -> &FileSystemPathBuf {
self.as_file_system_path_buf()
}
}

impl AsRef<FileSystemPath> for ModuleResolutionPath {
#[inline]
fn as_ref(&self) -> &FileSystemPath {
ModuleResolutionPathRef::from(self).as_file_system_path()
}
Expand All @@ -328,6 +276,7 @@ pub(crate) enum ModuleResolutionPathRef<'a> {
}

impl<'a> ModuleResolutionPathRef<'a> {
#[must_use]
pub(crate) fn extra(path: &'a (impl AsRef<FileSystemPath> + ?Sized)) -> Option<Self> {
let path = path.as_ref();
if path
Expand All @@ -340,10 +289,14 @@ impl<'a> ModuleResolutionPathRef<'a> {
}
}

#[must_use]
fn extra_unchecked(path: &'a (impl AsRef<FileSystemPath> + ?Sized)) -> Self {
Self::Extra(ExtraPath::new_unchecked(path.as_ref()))
// SAFETY: ExtraPath is marked as #[repr(transparent)] so the conversion from a
// *const FileSystemPath to a *const ExtraPath is valid.
Self::Extra(unsafe { &*(path.as_ref() as *const FileSystemPath as *const ExtraPath) })
}

#[must_use]
pub(crate) fn first_party(path: &'a (impl AsRef<FileSystemPath> + ?Sized)) -> Option<Self> {
let path = path.as_ref();
if path
Expand All @@ -356,25 +309,38 @@ impl<'a> ModuleResolutionPathRef<'a> {
}
}

#[must_use]
fn first_party_unchecked(path: &'a (impl AsRef<FileSystemPath> + ?Sized)) -> Self {
Self::FirstParty(FirstPartyPath::new_unchecked(path.as_ref()))
// SAFETY: FirstPartyPath is marked as #[repr(transparent)] so the conversion from a
// *const FileSystemPath to a *const FirstPartyPath is valid.
Self::FirstParty(unsafe {
&*(path.as_ref() as *const FileSystemPath as *const FirstPartyPath)
})
}

#[must_use]
pub(crate) fn standard_library(
path: &'a (impl AsRef<FileSystemPath> + ?Sized),
) -> Option<Self> {
let path = path.as_ref();
// Unlike other variants, only `.pyi` extensions are permitted
if path.extension().map_or(true, |ext| ext == "pyi") {
Some(Self::standard_library_unchecked(path))
} else {
None
}
}

#[must_use]
fn standard_library_unchecked(path: &'a (impl AsRef<FileSystemPath> + ?Sized)) -> Self {
Self::StandardLibrary(StandardLibraryPath::new_unchecked(path.as_ref()))
// SAFETY: StandardLibraryPath is marked as #[repr(transparent)] so the conversion from a
// *const FileSystemPath to a *const StandardLibraryPath is valid.
Self::StandardLibrary(unsafe {
&*(path.as_ref() as *const FileSystemPath as *const StandardLibraryPath)
})
}

#[must_use]
pub(crate) fn site_packages(path: &'a (impl AsRef<FileSystemPath> + ?Sized)) -> Option<Self> {
let path = path.as_ref();
if path
Expand All @@ -387,10 +353,16 @@ impl<'a> ModuleResolutionPathRef<'a> {
}
}

#[must_use]
fn site_packages_unchecked(path: &'a (impl AsRef<FileSystemPath> + ?Sized)) -> Self {
Self::SitePackages(SitePackagesPath::new_unchecked(path.as_ref()))
// SAFETY: SitePackagesPath is marked as #[repr(transparent)] so the conversion from a
// *const FileSystemPath to a *const SitePackagesPath is valid.
Self::SitePackages(unsafe {
&*(path.as_ref() as *const FileSystemPath as *const SitePackagesPath)
})
}

#[must_use]
pub(crate) fn is_directory(&self, db: &dyn Db) -> bool {
match self {
Self::Extra(ExtraPath(path)) => db.file_system().is_directory(path),
Expand All @@ -414,6 +386,7 @@ impl<'a> ModuleResolutionPathRef<'a> {
}
}

#[must_use]
pub(crate) fn is_regular_package(&self, db: &dyn Db) -> bool {
match self {
Self::Extra(ExtraPath(fs_path))
Expand Down Expand Up @@ -444,6 +417,7 @@ impl<'a> ModuleResolutionPathRef<'a> {
}
}

#[must_use]
pub(crate) fn parent(&self) -> Option<Self> {
Some(match self {
Self::Extra(ExtraPath(path)) => Self::extra_unchecked(path.parent()?),
Expand All @@ -457,6 +431,7 @@ impl<'a> ModuleResolutionPathRef<'a> {
})
}

#[must_use]
fn ends_with_dunder_init(&self) -> bool {
match self {
Self::Extra(ExtraPath(path))
Expand All @@ -468,6 +443,7 @@ impl<'a> ModuleResolutionPathRef<'a> {
}
}

#[must_use]
fn sans_dunder_init(self) -> Self {
if self.ends_with_dunder_init() {
self.parent().unwrap_or_else(|| match self {
Expand All @@ -481,6 +457,7 @@ impl<'a> ModuleResolutionPathRef<'a> {
}
}

#[must_use]
pub(crate) fn as_module_name(&self) -> Option<ModuleName> {
let mut parts_iter = match self.sans_dunder_init() {
Self::Extra(ExtraPath(path)) => ModulePartIterator::from_fs_path(path),
Expand All @@ -503,6 +480,7 @@ impl<'a> ModuleResolutionPathRef<'a> {
}
}

#[must_use]
pub(crate) fn with_pyi_extension(&self) -> ModuleResolutionPath {
match self {
Self::Extra(ExtraPath(path)) => {
Expand All @@ -520,6 +498,7 @@ impl<'a> ModuleResolutionPathRef<'a> {
}
}

#[must_use]
pub(crate) fn with_py_extension(&self) -> Option<ModuleResolutionPath> {
match self {
Self::Extra(ExtraPath(path)) => Some(ModuleResolutionPath::extra_unchecked(
Expand Down Expand Up @@ -581,12 +560,18 @@ impl<'a> From<&'a ModuleResolutionPath> for ModuleResolutionPathRef<'a> {
#[inline]
fn from(value: &'a ModuleResolutionPath) -> Self {
match value {
ModuleResolutionPath::Extra(path) => ModuleResolutionPathRef::Extra(path),
ModuleResolutionPath::FirstParty(path) => ModuleResolutionPathRef::FirstParty(path),
ModuleResolutionPath::StandardLibrary(path) => {
ModuleResolutionPathRef::StandardLibrary(path)
ModuleResolutionPath::Extra(ExtraPathBuf(path)) => {
ModuleResolutionPathRef::extra_unchecked(path)
}
ModuleResolutionPath::FirstParty(FirstPartyPathBuf(path)) => {
ModuleResolutionPathRef::first_party_unchecked(path)
}
ModuleResolutionPath::StandardLibrary(StandardLibraryPathBuf(path)) => {
ModuleResolutionPathRef::standard_library_unchecked(path)
}
ModuleResolutionPath::SitePackages(SitePackagesPathBuf(path)) => {
ModuleResolutionPathRef::site_packages_unchecked(path)
}
ModuleResolutionPath::SitePackages(path) => ModuleResolutionPathRef::SitePackages(path),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ mod tests {
use red_knot_module_resolver::{
set_module_resolution_settings, ModuleResolutionSettings, SupportedPyVersion,
};
use ruff_db::file_system::FileSystemPath;
use ruff_db::file_system::FileSystemPathBuf;
use ruff_db::parsed::parsed_module;
use ruff_db::vfs::system_path_to_file;

Expand All @@ -531,7 +531,7 @@ mod tests {
ModuleResolutionSettings {
target_version: SupportedPyVersion::Py38,
extra_paths: vec![],
workspace_root: FileSystemPath::new("/src").to_path_buf(),
workspace_root: FileSystemPathBuf::from("/src"),
site_packages: None,
custom_typeshed: None,
},
Expand Down
Loading

0 comments on commit 1dc038e

Please sign in to comment.