Skip to content

Commit

Permalink
Resolve alias declarations when resolving struct symbols.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Aug 4, 2024
1 parent e327345 commit f1416ed
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 9 deletions.
17 changes: 17 additions & 0 deletions sway-core/src/language/parsed/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
decl_engine::{
parsed_engine::{ParsedDeclEngine, ParsedDeclEngineGet},
parsed_id::ParsedDeclId,
DeclEngineGetParsedDeclId,
},
engine_threading::{
DebugWithEngines, DisplayWithEngines, EqWithEngines, PartialEqWithEngines,
Expand Down Expand Up @@ -142,6 +143,22 @@ impl Declaration {
) -> Result<ParsedDeclId<StructDeclaration>, ErrorEmitted> {
match self {
Declaration::StructDeclaration(decl_id) => Ok(*decl_id),
Declaration::TypeAliasDeclaration(decl_id) => {
let alias = engines.pe().get_type_alias(decl_id);
let struct_decl_id = engines.te().get(alias.ty.type_id).expect_struct(
handler,
engines,
&self.span(engines),
)?;

let parsed_decl_id = engines.de().get_parsed_decl_id(&struct_decl_id);
parsed_decl_id.ok_or_else(|| {
handler.emit_err(CompileError::InternalOwned(
"Cannot get parsed decl id from decl id".to_string(),
self.span(engines),
))
})
}
decl => Err(handler.emit_err(CompileError::DeclIsNotAStruct {
actually: decl.friendly_type_name().to_string(),
span: decl.span(engines),
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/language/ty/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ impl TyDecl {
/// Retrieves the declaration as a `DeclRef<DeclId<TyStructDecl>>`.
///
/// Returns an error if `self` is not the [TyDecl][StructDecl] variant.
pub(crate) fn to_struct_id(
pub(crate) fn to_struct_decl(
&self,
handler: &Handler,
engines: &Engines,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ where
return Some((None, None));
}

let implementing_for_decl_id = decl.to_struct_id(&Handler::default(), engines).unwrap();
let implementing_for_decl_id = decl.to_struct_decl(&Handler::default(), engines).unwrap();
let struct_decl = self.ctx.engines().de().get(&implementing_for_decl_id);

let program_id = struct_decl.span().source_id().map(|sid| sid.program_id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn type_check_struct(
let unknown_decl =
ctx.namespace()
.resolve_symbol_typed(handler, engines, &struct_name, ctx.self_type())?;
let struct_id = unknown_decl.to_struct_id(handler, ctx.engines())?;
let struct_id = unknown_decl.to_struct_decl(handler, ctx.engines())?;
let mut struct_decl = (*decl_engine.get_struct(&struct_id)).clone();

// monomorphize the struct definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ impl ty::TyExpression {
None,
)?
.expect_typed();
let storage_key_struct_decl_ref = storage_key_decl_opt.to_struct_id(handler, engines)?;
let storage_key_struct_decl_ref = storage_key_decl_opt.to_struct_decl(handler, engines)?;
let mut storage_key_struct_decl =
(*decl_engine.get_struct(&storage_key_struct_decl_ref)).clone();

Expand Down
19 changes: 18 additions & 1 deletion sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
engine_threading::*,
language::{
parsed::*,
ty::{self, TyDecl, TyTraitItem},
ty::{self, StructDecl, TyDecl, TyTraitItem},
CallPath, Visibility,
},
namespace::{ModulePath, ModulePathBuf},
Expand Down Expand Up @@ -99,6 +99,23 @@ impl ResolvedDeclaration {
}
}

pub(crate) fn to_struct_decl(
&self,
handler: &Handler,
engines: &Engines,
) -> Result<ResolvedDeclaration, ErrorEmitted> {
match self {
ResolvedDeclaration::Parsed(decl) => decl
.to_struct_decl(handler, engines)
.map(|id| ResolvedDeclaration::Parsed(Declaration::StructDeclaration(id.clone()))),
ResolvedDeclaration::Typed(decl) => decl.to_struct_decl(handler, engines).map(|id| {
ResolvedDeclaration::Typed(TyDecl::StructDecl(StructDecl {
decl_id: id.clone(),
}))
}),
}
}

pub(crate) fn visibility(&self, engines: &Engines) -> Visibility {
match self {
ResolvedDeclaration::Parsed(decl) => decl.visibility(engines.pe()),
Expand Down
11 changes: 7 additions & 4 deletions sway-core/src/type_system/ast_elements/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,14 @@ impl SymbolResolveTypeBinding<StructDeclaration> for TypeBinding<CallPath> {
let engines = ctx.engines();
// Grab the declaration.
let unknown_decl = ctx.resolve_call_path_with_visibility_check(handler, &self.inner)?;

// Check to see if this is a struct declaration.
let struct_decl_id = unknown_decl
let struct_decl = unknown_decl.to_struct_decl(handler, engines)?;
eprintln!("{:#?}", struct_decl);

Ok(struct_decl
.resolve_parsed(engines.de())
.to_struct_decl(handler, engines)?;
Ok(struct_decl_id)
.to_struct_decl(handler, engines)?)
}
}

Expand All @@ -390,7 +393,7 @@ impl TypeCheckTypeBinding<ty::TyStructDecl> for TypeBinding<CallPath> {
// Grab the declaration.
let unknown_decl = ctx.resolve_call_path_with_visibility_check(handler, &self.inner)?;
// Check to see if this is a struct declaration.
let struct_id = unknown_decl.to_struct_id(handler, engines)?;
let struct_id = unknown_decl.to_struct_decl(handler, engines)?;
// Get a new copy from the declaration engine.
let mut new_copy = (*decl_engine.get_struct(&struct_id)).clone();
// Monomorphize the copy, in place.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "alias"
source = "path+from-root-DE984AFD05391BAA"

[[package]]
name = "use-alias"
source = "member"
dependencies = ["alias"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "type_alias_basic"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library;

struct S {}
pub type Alias = S;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "type_alias_basic"
source = "path+from-root-05B061D32AED907C"

[[package]]
name = "type_alias_from_dependency"
source = "member"
dependencies = ["type_alias_basic"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "type_alias_from_dependency"
implicit-std = false

[dependencies]
type_alias_basic = { path = "../type_alias_basic" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library;

use type_alias_basic::*;

fn foo() {
let z = type_alias_basic::Alias { };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "compile"
validate_abi = false
expected_warnings = 2

0 comments on commit f1416ed

Please sign in to comment.