Skip to content

Commit

Permalink
Refactor type resolve to deal with ResolvedDeclaration.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Oct 18, 2024
1 parent 029c593 commit ab48ec9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 deletions.
4 changes: 2 additions & 2 deletions sway-core/src/semantic_analysis/type_check_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ impl<'a> TypeCheckContext<'a> {
&self,
handler: &Handler,
call_path: &CallPath,
) -> Result<ty::TyDecl, ErrorEmitted> {
) -> Result<ResolvedDeclaration, ErrorEmitted> {
resolve_call_path(
handler,
self.engines(),
Expand All @@ -694,7 +694,7 @@ impl<'a> TypeCheckContext<'a> {
&mut self,
handler: &Handler,
qualified_call_path: &QualifiedCallPath,
) -> Result<ty::TyDecl, ErrorEmitted> {
) -> Result<ResolvedDeclaration, ErrorEmitted> {
resolve_qualified_call_path(
handler,
self.engines(),
Expand Down
36 changes: 14 additions & 22 deletions sway-core/src/semantic_analysis/type_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ use sway_types::{Span, Spanned};
use sway_utils::iter_prefixes;

use crate::{
language::{
ty::{self, TyTraitItem},
CallPath, QualifiedCallPath,
},
language::{ty::TyTraitItem, CallPath, QualifiedCallPath},
monomorphization::type_decl_opt_to_type_id,
namespace::{ModulePath, ResolvedTraitImplItem},
namespace::{ModulePath, ResolvedDeclaration, ResolvedTraitImplItem},
type_system::SubstTypes,
EnforceTypeArguments, Engines, Namespace, SubstTypesContext, TypeId, TypeInfo,
};
Expand Down Expand Up @@ -52,7 +49,6 @@ pub fn resolve_type(
&qualified_call_path.clone().to_call_path(handler)?,
self_type,
)
.map(|decl| decl.expect_typed())
.ok()
} else {
resolve_qualified_call_path(
Expand Down Expand Up @@ -228,7 +224,7 @@ pub fn resolve_qualified_call_path(
qualified_call_path: &QualifiedCallPath,
self_type: Option<TypeId>,
subst_ctx: &SubstTypesContext,
) -> Result<ty::TyDecl, ErrorEmitted> {
) -> Result<ResolvedDeclaration, ErrorEmitted> {
let type_engine = engines.te();
if let Some(qualified_path_root) = qualified_call_path.clone().qualified_path_root {
let root_type_id = match &&*type_engine.get(qualified_path_root.ty.type_id) {
Expand Down Expand Up @@ -275,18 +271,15 @@ pub fn resolve_qualified_call_path(
_ => None,
};

namespace
.root
.resolve_call_path_and_root_type_id(
handler,
engines,
&namespace.root.module,
root_type_id,
as_trait_opt,
&qualified_call_path.call_path,
self_type,
)
.map(|decl| decl.expect_typed())
namespace.root.resolve_call_path_and_root_type_id(
handler,
engines,
&namespace.root.module,
root_type_id,
as_trait_opt,
&qualified_call_path.call_path,
self_type,
)
} else {
resolve_call_path(
handler,
Expand Down Expand Up @@ -314,11 +307,10 @@ pub fn resolve_call_path(
mod_path: &ModulePath,
call_path: &CallPath,
self_type: Option<TypeId>,
) -> Result<ty::TyDecl, ErrorEmitted> {
) -> Result<ResolvedDeclaration, ErrorEmitted> {
let (decl, mod_path) = namespace
.root
.resolve_call_path_and_mod_path(handler, engines, mod_path, call_path, self_type)?;
let decl = decl.expect_typed();

// In case there is no mod path we don't need to check visibility
if mod_path.is_empty() {
Expand All @@ -344,7 +336,7 @@ pub fn resolve_call_path(
}

// check the visibility of the symbol itself
if !decl.visibility(engines.de()).is_public() {
if !decl.visibility(engines).is_public() {
handler.emit_err(CompileError::ImportPrivateSymbol {
name: call_path.suffix.clone(),
span: call_path.suffix.span(),
Expand Down
21 changes: 15 additions & 6 deletions sway-core/src/type_system/ast_elements/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ impl TypeCheckTypeBinding<ty::TyFunctionDecl> for TypeBinding<CallPath> {
let decl_engine = ctx.engines.de();
let engines = ctx.engines();
// Grab the declaration.
let unknown_decl = ctx.resolve_call_path_with_visibility_check(handler, &self.inner)?;
let unknown_decl = ctx
.resolve_call_path_with_visibility_check(handler, &self.inner)?
.expect_typed();
// Check to see if this is a fn declaration.
let fn_ref = unknown_decl.to_fn_ref(handler, ctx.engines())?;
// Get a new copy from the declaration engine.
Expand Down Expand Up @@ -389,7 +391,9 @@ impl TypeCheckTypeBinding<ty::TyStructDecl> for TypeBinding<CallPath> {
let decl_engine = ctx.engines.de();
let engines = ctx.engines();
// Grab the declaration.
let unknown_decl = ctx.resolve_call_path_with_visibility_check(handler, &self.inner)?;
let unknown_decl = ctx
.resolve_call_path_with_visibility_check(handler, &self.inner)?
.expect_typed();
// Check to see if this is a struct declaration.
let struct_id = unknown_decl.to_struct_decl(handler, engines)?;
// Get a new copy from the declaration engine.
Expand Down Expand Up @@ -433,7 +437,9 @@ impl TypeCheckTypeBinding<ty::TyEnumDecl> for TypeBinding<CallPath> {
let decl_engine = ctx.engines.de();
let engines = ctx.engines();
// Grab the declaration.
let unknown_decl = ctx.resolve_call_path_with_visibility_check(handler, &self.inner)?;
let unknown_decl = ctx
.resolve_call_path_with_visibility_check(handler, &self.inner)?
.expect_typed();

// Get a new copy from the declaration engine.
let enum_id = if let ty::TyDecl::EnumVariantDecl(ty::EnumVariantDecl { enum_ref, .. }) =
Expand Down Expand Up @@ -474,8 +480,9 @@ impl TypeBinding<QualifiedCallPath> {
ctx: &mut TypeCheckContext,
) -> Result<DeclRef<DeclId<ty::TyConstantDecl>>, ErrorEmitted> {
// Grab the declaration.
let unknown_decl =
ctx.resolve_qualified_call_path_with_visibility_check(handler, &self.inner)?;
let unknown_decl = ctx
.resolve_qualified_call_path_with_visibility_check(handler, &self.inner)?
.expect_typed();

// Check to see if this is a const declaration.
let const_ref = unknown_decl.to_const_ref(handler, ctx.engines())?;
Expand All @@ -498,7 +505,9 @@ impl TypeCheckTypeBinding<ty::TyConstantDecl> for TypeBinding<CallPath> {
ErrorEmitted,
> {
// Grab the declaration.
let unknown_decl = ctx.resolve_call_path_with_visibility_check(handler, &self.inner)?;
let unknown_decl = ctx
.resolve_call_path_with_visibility_check(handler, &self.inner)?
.expect_typed();

// Check to see if this is a const declaration.
let const_ref = unknown_decl.to_const_ref(handler, ctx.engines())?;
Expand Down
29 changes: 15 additions & 14 deletions sway-core/src/type_system/monomorphization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use sway_types::{Ident, Span, Spanned};
use crate::{
decl_engine::{engine::DeclEngineGetParsedDeclId, DeclEngineInsert},
language::{
ty::{self, TyDecl},
ty::{self},
CallPath,
},
namespace::ModulePath,
namespace::{ModulePath, ResolvedDeclaration},
semantic_analysis::type_resolve::resolve_type,
type_system::ast_elements::create_type_id::CreateTypeId,
EnforceTypeArguments, Engines, Namespace, SubstTypes, SubstTypesContext, TypeArgument, TypeId,
Expand Down Expand Up @@ -219,7 +219,7 @@ pub(crate) fn type_decl_opt_to_type_id(
handler: &Handler,
engines: &Engines,
namespace: &Namespace,
type_decl_opt: Option<TyDecl>,
type_decl_opt: Option<ResolvedDeclaration>,
call_path: &CallPath,
span: &Span,
enforce_type_arguments: EnforceTypeArguments,
Expand All @@ -231,10 +231,10 @@ pub(crate) fn type_decl_opt_to_type_id(
let decl_engine = engines.de();
let type_engine = engines.te();
Ok(match type_decl_opt {
Some(ty::TyDecl::StructDecl(ty::StructDecl {
Some(ResolvedDeclaration::Typed(ty::TyDecl::StructDecl(ty::StructDecl {
decl_id: original_id,
..
})) => {
}))) => {
// get the copy from the declaration engine
let mut new_copy = (*decl_engine.get_struct(&original_id)).clone();

Expand Down Expand Up @@ -265,10 +265,10 @@ pub(crate) fn type_decl_opt_to_type_id(
new_decl_ref.span().source_id(),
)
}
Some(ty::TyDecl::EnumDecl(ty::EnumDecl {
Some(ResolvedDeclaration::Typed(ty::TyDecl::EnumDecl(ty::EnumDecl {
decl_id: original_id,
..
})) => {
}))) => {
// get the copy from the declaration engine
let mut new_copy = (*decl_engine.get_enum(&original_id)).clone();

Expand Down Expand Up @@ -299,22 +299,23 @@ pub(crate) fn type_decl_opt_to_type_id(
new_decl_ref.span().source_id(),
)
}
Some(ty::TyDecl::TypeAliasDecl(ty::TypeAliasDecl {
Some(ResolvedDeclaration::Typed(ty::TyDecl::TypeAliasDecl(ty::TypeAliasDecl {
decl_id: original_id,
..
})) => {
}))) => {
let new_copy = decl_engine.get_type_alias(&original_id);

// TODO: monomorphize the copy, in place, when generic type aliases are
// supported

new_copy.create_type_id(engines)
}
Some(ty::TyDecl::GenericTypeForFunctionScope(ty::GenericTypeForFunctionScope {
type_id,
..
})) => type_id,
Some(ty::TyDecl::TraitTypeDecl(ty::TraitTypeDecl { decl_id })) => {
Some(ResolvedDeclaration::Typed(ty::TyDecl::GenericTypeForFunctionScope(
ty::GenericTypeForFunctionScope { type_id, .. },
))) => type_id,
Some(ResolvedDeclaration::Typed(ty::TyDecl::TraitTypeDecl(ty::TraitTypeDecl {
decl_id,
}))) => {
let decl_type = decl_engine.get_type(&decl_id);

if let Some(ty) = &decl_type.ty {
Expand Down

0 comments on commit ab48ec9

Please # to comment.