Skip to content

Commit

Permalink
Extract Root.resolve_associated_item_from_type_id to `type_resolve.…
Browse files Browse the repository at this point in the history
…rs`.
  • Loading branch information
tritao committed Oct 21, 2024
1 parent 0559838 commit 3a38601
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 77 deletions.
79 changes: 4 additions & 75 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use std::fmt;
use super::{
module::{mod_is_ancestor, Module},
trait_map::TraitMap,
Ident, ResolvedTraitImplItem,
Ident,
};
use crate::{
decl_engine::{DeclEngine, DeclRef},
engine_threading::*,
language::{
parsed::*,
ty::{self, StructDecl, TyDecl, TyTraitItem},
ty::{self, StructDecl, TyDecl},
CallPath, Visibility,
},
namespace::{ModulePath, ModulePathBuf},
Expand Down Expand Up @@ -905,43 +905,11 @@ impl Root {
.te()
.insert(engines, type_info, symbol.span().source_id());

self.resolve_associated_item_from_type_id(
resolve_associated_item_from_type_id(
handler, engines, module, symbol, type_id, as_trait, self_type,
)
}

fn decl_to_type_info(
&self,
handler: &Handler,
engines: &Engines,
symbol: &Ident,
decl: ResolvedDeclaration,
) -> Result<TypeInfo, ErrorEmitted> {
match decl {
ResolvedDeclaration::Parsed(_decl) => todo!(),
ResolvedDeclaration::Typed(decl) => Ok(match decl.clone() {
ty::TyDecl::StructDecl(struct_ty_decl) => TypeInfo::Struct(struct_ty_decl.decl_id),
ty::TyDecl::EnumDecl(enum_ty_decl) => TypeInfo::Enum(enum_ty_decl.decl_id),
ty::TyDecl::TraitTypeDecl(type_decl) => {
let type_decl = engines.de().get_type(&type_decl.decl_id);
if type_decl.ty.is_none() {
return Err(handler.emit_err(CompileError::Internal(
"Trait type declaration has no type",
symbol.span(),
)));
}
(*engines.te().get(type_decl.ty.clone().unwrap().type_id)).clone()
}
_ => {
return Err(handler.emit_err(CompileError::SymbolNotFound {
name: symbol.clone(),
span: symbol.span(),
}))
}
}),
}
}

#[allow(clippy::too_many_arguments)]
fn resolve_associated_type_from_type_id(
&self,
Expand All @@ -953,50 +921,11 @@ impl Root {
as_trait: Option<CallPath>,
self_type: Option<TypeId>,
) -> Result<ResolvedDeclaration, ErrorEmitted> {
let item_decl = self.resolve_associated_item_from_type_id(
let item_decl = resolve_associated_item_from_type_id(
handler, engines, module, symbol, type_id, as_trait, self_type,
)?;
Ok(item_decl)
}

#[allow(clippy::too_many_arguments)]
fn resolve_associated_item_from_type_id(
&self,
handler: &Handler,
engines: &Engines,
module: &Module,
symbol: &Ident,
type_id: TypeId,
as_trait: Option<CallPath>,
self_type: Option<TypeId>,
) -> Result<ResolvedDeclaration, ErrorEmitted> {
let type_id = if engines.te().get(type_id).is_self_type() {
if let Some(self_type) = self_type {
self_type
} else {
return Err(handler.emit_err(CompileError::Internal(
"Self type not provided.",
symbol.span(),
)));
}
} else {
type_id
};
let item_ref = module
.current_items()
.implemented_traits
.get_trait_item_for_type(handler, engines, symbol, type_id, as_trait)?;
match item_ref {
ResolvedTraitImplItem::Parsed(_item) => todo!(),
ResolvedTraitImplItem::Typed(item) => match item {
TyTraitItem::Fn(fn_ref) => Ok(ResolvedDeclaration::Typed(fn_ref.into())),
TyTraitItem::Constant(const_ref) => {
Ok(ResolvedDeclaration::Typed(const_ref.into()))
}
TyTraitItem::Type(type_ref) => Ok(ResolvedDeclaration::Typed(type_ref.into())),
},
}
}
}

impl From<Module> for Root {
Expand Down
43 changes: 41 additions & 2 deletions sway-core/src/semantic_analysis/type_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ use sway_types::{Ident, Span, Spanned};
use sway_utils::iter_prefixes;

use crate::{
language::{ty::TyTraitItem, CallPath, QualifiedCallPath},
language::{
ty::{self, TyTraitItem},
CallPath, QualifiedCallPath,
},
monomorphization::type_decl_opt_to_type_id,
namespace::{ModulePath, ResolvedDeclaration, ResolvedTraitImplItem},
namespace::{Module, ModulePath, ResolvedDeclaration, ResolvedTraitImplItem},
type_system::SubstTypes,
EnforceTypeArguments, Engines, Namespace, SubstTypesContext, TypeId, TypeInfo,
};
Expand Down Expand Up @@ -378,3 +381,39 @@ pub fn decl_to_type_info(
}),
}
}

#[allow(clippy::too_many_arguments)]
pub fn resolve_associated_item_from_type_id(
handler: &Handler,
engines: &Engines,
module: &Module,
symbol: &Ident,
type_id: TypeId,
as_trait: Option<CallPath>,
self_type: Option<TypeId>,
) -> Result<ResolvedDeclaration, ErrorEmitted> {
let type_id = if engines.te().get(type_id).is_self_type() {
if let Some(self_type) = self_type {
self_type
} else {
return Err(handler.emit_err(CompileError::Internal(
"Self type not provided.",
symbol.span(),
)));
}
} else {
type_id
};
let item_ref = module
.current_items()
.implemented_traits
.get_trait_item_for_type(handler, engines, symbol, type_id, as_trait)?;
match item_ref {
ResolvedTraitImplItem::Parsed(_item) => todo!(),
ResolvedTraitImplItem::Typed(item) => match item {
TyTraitItem::Fn(fn_ref) => Ok(ResolvedDeclaration::Typed(fn_ref.into())),
TyTraitItem::Constant(const_ref) => Ok(ResolvedDeclaration::Typed(const_ref.into())),
TyTraitItem::Type(type_ref) => Ok(ResolvedDeclaration::Typed(type_ref.into())),
},
}
}

0 comments on commit 3a38601

Please sign in to comment.