Skip to content

Commit

Permalink
Extract Root.decl_to_type_info to type_resolve.rs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Oct 21, 2024
1 parent 6d3e8cb commit 0559838
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
7 changes: 4 additions & 3 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::{
CallPath, Visibility,
},
namespace::{ModulePath, ModulePathBuf},
TypeId, TypeInfo,
semantic_analysis::type_resolve::{decl_to_type_info, resolve_associated_item_from_type_id},
TypeId,
};
use sway_error::{
error::CompileError,
Expand Down Expand Up @@ -878,7 +879,7 @@ impl Root {
as_trait: Option<CallPath>,
self_type: Option<TypeId>,
) -> Result<ResolvedDeclaration, ErrorEmitted> {
let type_info = self.decl_to_type_info(handler, engines, symbol, decl)?;
let type_info = decl_to_type_info(handler, engines, symbol, decl)?;
let type_id = engines
.te()
.insert(engines, type_info, symbol.span().source_id());
Expand All @@ -899,7 +900,7 @@ impl Root {
as_trait: Option<CallPath>,
self_type: Option<TypeId>,
) -> Result<ResolvedDeclaration, ErrorEmitted> {
let type_info = self.decl_to_type_info(handler, engines, symbol, decl)?;
let type_info = decl_to_type_info(handler, engines, symbol, decl)?;
let type_id = engines
.te()
.insert(engines, type_info, symbol.span().source_id());
Expand Down
33 changes: 32 additions & 1 deletion sway-core/src/semantic_analysis/type_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sway_error::{
error::CompileError,
handler::{ErrorEmitted, Handler},
};
use sway_types::{Span, Spanned};
use sway_types::{Ident, Span, Spanned};
use sway_utils::iter_prefixes;

use crate::{
Expand Down Expand Up @@ -347,3 +347,34 @@ pub fn resolve_call_path(

Ok(decl)
}

pub fn decl_to_type_info(
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(),
}))
}
}),
}
}

0 comments on commit 0559838

Please sign in to comment.