Skip to content

Commit

Permalink
Added namespace inference for calling functions where the subject typ…
Browse files Browse the repository at this point in the history
…e and the function come from the same module
  • Loading branch information
IsaacShelton committed Oct 16, 2024
1 parent 710d43d commit e247e33
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ impl Display for Name {

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ResolvedName {
fs_node_id: FsNodeId,
name: Box<str>,
pub fs_node_id: FsNodeId,
pub name: Box<str>,
}

impl ResolvedName {
Expand Down
63 changes: 52 additions & 11 deletions src/resolve/function_search_ctx.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use super::{
conform::{conform_expr, to_default::conform_expr_to_default, ConformMode, Validate},
conform::{
conform_expr, to_default::conform_expr_to_default, warn_type_alias_depth_exceeded,
ConformMode, Validate,
},
expr::{PreferredType, ResolveExprCtx},
};
use crate::{
ir::FunctionRef,
name::{Name, ResolvedName},
resolved::{self, TypedExpr},
resolved::{self, TypeKind, TypedExpr},
source_files::Source,
workspace::fs::FsNodeId,
};
Expand Down Expand Up @@ -87,16 +90,54 @@ impl FunctionSearchCtx {
}

if name.namespace.is_empty() {
let mut matches = self
.imported_namespaces
.iter()
.filter_map(|namespace| {
self.available.get(&ResolvedName::new(
self.fs_node_id,
&Name::new(Some(namespace.to_string()), name.basename.clone()),
))
})
let imported_namespaces = ctx.settings.imported_namespaces.iter();

// TODO: CLEANUP: Clean up this code that gets the origin module of the callee
let subject_module_fs_node_id =
if let Some(first_type) = arguments.first().map(|arg| &arg.resolved_type) {
if let Ok(first_type) = ctx.resolved_ast.unalias(first_type) {
match &first_type.kind {
TypeKind::Structure(_, structure_ref) => Some(
ctx.resolved_ast
.structures
.get(*structure_ref)
.expect("valid struct")
.name
.fs_node_id,
),
TypeKind::Enum(_, enum_ref) => Some(
ctx.resolved_ast
.enums
.get(*enum_ref)
.expect("valid enum")
.name
.fs_node_id,
),
_ => None,
}
} else {
warn_type_alias_depth_exceeded(first_type);
None
}
} else {
None
}
.into_iter();

let mut matches = imported_namespaces
.flat_map(|namespace| ctx.settings.namespace_to_dependency.get(namespace.as_ref()))
.flatten()
.flat_map(|dependency| ctx.settings.dependency_to_module.get(dependency))
.copied()
.chain(subject_module_fs_node_id)
.unique()
.flat_map(|module_fs_node_id| {
ctx.public_functions
.get(&module_fs_node_id)
.and_then(|public| public.get(name.basename.as_ref()))
.into_iter()
.flatten()
})
.filter(|f| Self::fits(ctx, **f, arguments, source));

if let Some(found) = matches.next() {
Expand Down
11 changes: 6 additions & 5 deletions src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn resolve<'a>(
let mut type_jobs = Vec::with_capacity(ast_workspace.files.len());

for (physical_file_id, file) in ast_workspace.files.iter() {
let file_id = ast_workspace
let module_fs_node_id = ast_workspace
.get_owning_module(*physical_file_id)
.unwrap_or(*physical_file_id);

Expand All @@ -126,7 +126,7 @@ pub fn resolve<'a>(
for structure in file.structures.iter() {
let privacy = structure.privacy;
let source = structure.source;
let resolved_name = ResolvedName::new(file_id, &structure.name);
let resolved_name = ResolvedName::new(module_fs_node_id, &structure.name);

let structure_ref = resolved_ast.structures.insert(resolved::Structure {
name: resolved_name.clone(),
Expand All @@ -147,7 +147,7 @@ pub fn resolve<'a>(

let types_in_module = ctx
.types_in_modules
.entry(file_id)
.entry(module_fs_node_id)
.or_insert_with(HashMap::new);

types_in_module.insert(
Expand All @@ -164,6 +164,7 @@ pub fn resolve<'a>(

for definition in file.enums.iter() {
let enum_ref = resolved_ast.enums.insert(resolved::Enum {
name: ResolvedName::new(module_fs_node_id, &Name::plain(&definition.name)),
resolved_type: TypeKind::Unresolved.at(definition.source),
source: definition.source,
members: definition.members.clone(),
Expand All @@ -175,7 +176,7 @@ pub fn resolve<'a>(

let types_in_module = ctx
.types_in_modules
.entry(file_id)
.entry(module_fs_node_id)
.or_insert_with(HashMap::new);

types_in_module.insert(
Expand All @@ -201,7 +202,7 @@ pub fn resolve<'a>(

let types_in_module = ctx
.types_in_modules
.entry(file_id)
.entry(module_fs_node_id)
.or_insert_with(HashMap::new);

types_in_module.insert(
Expand Down
1 change: 1 addition & 0 deletions src/resolved/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub enum UnaliasError {

#[derive(Clone, Debug)]
pub struct Enum {
pub name: ResolvedName,
pub resolved_type: Type,
pub source: Source,
pub members: IndexMap<String, EnumMember>,
Expand Down

0 comments on commit e247e33

Please sign in to comment.