Skip to content

Commit

Permalink
Refactor trait map and module symbol resolution code to allow usage f…
Browse files Browse the repository at this point in the history
…or parsed declarations (#5810)

## Description

This PR unifies the codepaths in trait map and name resolution code so
they can work for both parsed and typed declarations.
It does this by introducing wrapper types for the output of resolving of
names.

This is an intermediate step to being able to use this code on the new
symbol resolving pass.

While this adds a little bit of minor bloat to the code, I think it's an
acceptable further step to be able to do re-use the code in steps before
type-checking.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Joshua Batty <[email protected]>
  • Loading branch information
tritao and JoshuaBatty committed Apr 3, 2024
1 parent 35df2a7 commit 2eb6f2a
Show file tree
Hide file tree
Showing 29 changed files with 636 additions and 238 deletions.
4 changes: 2 additions & 2 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ pub(crate) fn compile_const_decl(
None => None,
};
let const_decl = match decl {
Ok(decl) => match decl {
Ok(decl) => match decl.expect_typed() {
ty::TyDecl::ConstantDecl(ty::ConstantDecl { decl_id, .. }) => {
Some((*env.engines.de().get_constant(decl_id)).clone())
Some((*env.engines.de().get_constant(&decl_id)).clone())
}
_otherwise => const_decl.cloned(),
},
Expand Down
21 changes: 21 additions & 0 deletions sway-core/src/language/parsed/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub use r#enum::*;
pub use r#struct::*;
pub use r#trait::*;
pub use storage::*;
use sway_types::Spanned;
pub use type_alias::*;
pub use variable::*;

Expand Down Expand Up @@ -48,4 +49,24 @@ impl Declaration {
false
}
}

#[allow(dead_code)]
fn span(&self, engines: &Engines) -> sway_types::Span {
use Declaration::*;
let pe = engines.pe();
match self {
VariableDeclaration(decl_id) => pe.get_variable(decl_id).span(),
FunctionDeclaration(decl_id) => pe.get_function(decl_id).span(),
TraitDeclaration(decl_id) => pe.get_trait(decl_id).span(),
StructDeclaration(decl_id) => pe.get_struct(decl_id).span(),
EnumDeclaration(decl_id) => pe.get_enum(decl_id).span(),
ImplTrait(decl_id) => pe.get_impl_trait(decl_id).span(),
ImplSelf(decl_id) => pe.get_impl_self(decl_id).span(),
AbiDeclaration(decl_id) => pe.get_abi(decl_id).span(),
ConstantDeclaration(decl_id) => pe.get_constant(decl_id).span(),
StorageDeclaration(decl_id) => pe.get_storage(decl_id).span(),
TypeAliasDeclaration(decl_id) => pe.get_type_alias(decl_id).span(),
TraitTypeDeclaration(decl_id) => pe.get_trait_type(decl_id).span(),
}
}
}
14 changes: 13 additions & 1 deletion sway-core/src/language/parsed/declaration/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{decl_engine::parsed_id::ParsedDeclId, transform};

use super::{FunctionDeclaration, Supertrait, TraitItem};

use sway_types::{ident::Ident, span::Span};
use sway_types::{ident::Ident, span::Span, Named, Spanned};

/// An `abi` declaration, which declares an interface for a contract
/// to implement or for a caller to use to call a contract.
Expand All @@ -18,3 +18,15 @@ pub struct AbiDeclaration {
pub(crate) span: Span,
pub attributes: transform::AttributesMap,
}

impl Named for AbiDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
}
}

impl Spanned for AbiDeclaration {
fn span(&self) -> sway_types::Span {
self.span.clone()
}
}
14 changes: 13 additions & 1 deletion sway-core/src/language/parsed/declaration/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
language::{parsed::Expression, Visibility},
transform, Engines, TypeArgument,
};
use sway_types::{Ident, Span};
use sway_types::{Ident, Named, Span, Spanned};

#[derive(Debug, Clone)]
pub struct ConstantDeclaration {
Expand All @@ -16,6 +16,18 @@ pub struct ConstantDeclaration {
pub span: Span,
}

impl Named for ConstantDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
}
}

impl Spanned for ConstantDeclaration {
fn span(&self) -> sway_types::Span {
self.span.clone()
}
}

impl DebugWithEngines for ConstantDeclaration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, _engines: &Engines) -> std::fmt::Result {
f.write_fmt(format_args!("{}", self.name))
Expand Down
14 changes: 13 additions & 1 deletion sway-core/src/language/parsed/declaration/enum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{language::Visibility, transform, type_system::*};
use sway_types::{ident::Ident, span::Span};
use sway_types::{ident::Ident, span::Span, Named, Spanned};

#[derive(Debug, Clone)]
pub struct EnumDeclaration {
Expand All @@ -11,6 +11,18 @@ pub struct EnumDeclaration {
pub visibility: Visibility,
}

impl Named for EnumDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
}
}

impl Spanned for EnumDeclaration {
fn span(&self) -> sway_types::Span {
self.span.clone()
}
}

#[derive(Debug, Clone)]
pub struct EnumVariant {
pub name: Ident,
Expand Down
14 changes: 13 additions & 1 deletion sway-core/src/language/parsed/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
transform::{self, AttributeKind},
type_system::*,
};
use sway_types::{ident::Ident, span::Span};
use sway_types::{ident::Ident, span::Span, Named, Spanned};

#[derive(Debug, Clone)]
pub enum FunctionDeclarationKind {
Expand Down Expand Up @@ -35,6 +35,18 @@ impl DebugWithEngines for FunctionDeclaration {
}
}

impl Named for FunctionDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
}
}

impl Spanned for FunctionDeclaration {
fn span(&self) -> sway_types::Span {
self.span.clone()
}
}

#[derive(Debug, Clone)]
pub struct FunctionParameter {
pub name: Ident,
Expand Down
30 changes: 29 additions & 1 deletion sway-core/src/language/parsed/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
type_system::TypeArgument, Engines, TypeParameter,
};

use sway_types::span::Span;
use sway_types::{span::Span, Named, Spanned};

#[derive(Debug, Clone)]
pub enum ImplItem {
Expand All @@ -13,6 +13,16 @@ pub enum ImplItem {
Type(ParsedDeclId<TraitTypeDeclaration>),
}

impl ImplItem {
pub fn span(&self, engines: &Engines) -> Span {
match self {
ImplItem::Fn(id) => engines.pe().get_function(id).span(),
ImplItem::Constant(id) => engines.pe().get_constant(id).span(),
ImplItem::Type(id) => engines.pe().get_trait_type(id).span(),
}
}
}

impl DebugWithEngines for ImplItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, engines: &Engines) -> std::fmt::Result {
match self {
Expand Down Expand Up @@ -43,6 +53,18 @@ pub struct ImplTrait {
pub(crate) block_span: Span,
}

impl Named for ImplTrait {
fn name(&self) -> &sway_types::BaseIdent {
&self.trait_name.suffix
}
}

impl Spanned for ImplTrait {
fn span(&self) -> sway_types::Span {
self.block_span.clone()
}
}

impl DebugWithEngines for ImplTrait {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, engines: &Engines) -> std::fmt::Result {
f.write_fmt(format_args!(
Expand All @@ -64,6 +86,12 @@ pub struct ImplSelf {
pub(crate) block_span: Span,
}

impl Spanned for ImplSelf {
fn span(&self) -> sway_types::Span {
self.block_span.clone()
}
}

impl DebugWithEngines for ImplSelf {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, engines: &Engines) -> std::fmt::Result {
f.write_fmt(format_args!(
Expand Down
8 changes: 7 additions & 1 deletion sway-core/src/language/parsed/declaration/storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{language::parsed::Expression, transform, type_system::*};
use sway_types::{ident::Ident, span::Span};
use sway_types::{ident::Ident, span::Span, Spanned};

#[derive(Debug, Clone)]
/// A declaration of contract storage. Only valid within contract contexts.
Expand All @@ -11,6 +11,12 @@ pub struct StorageDeclaration {
pub storage_keyword: Ident,
}

impl Spanned for StorageDeclaration {
fn span(&self) -> sway_types::Span {
self.span.clone()
}
}

/// An individual field in a storage declaration.
/// A type annotation _and_ initializer value must be provided. The initializer value must be a
/// constant expression. For now, that basically means just a literal, but as constant folding
Expand Down
14 changes: 13 additions & 1 deletion sway-core/src/language/parsed/declaration/struct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{language::Visibility, transform, type_system::TypeParameter, TypeArgument};
use sway_types::{ident::Ident, span::Span};
use sway_types::{ident::Ident, span::Span, Named, Spanned};

#[derive(Debug, Clone)]
pub struct StructDeclaration {
Expand All @@ -11,6 +11,18 @@ pub struct StructDeclaration {
pub(crate) span: Span,
}

impl Named for StructDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
}
}

impl Spanned for StructDeclaration {
fn span(&self) -> sway_types::Span {
self.span.clone()
}
}

#[derive(Debug, Clone)]
pub struct StructField {
pub visibility: Visibility,
Expand Down
26 changes: 25 additions & 1 deletion sway-core/src/language/parsed/declaration/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
type_system::*,
};
use sway_error::handler::ErrorEmitted;
use sway_types::{ident::Ident, span::Span, Spanned};
use sway_types::{ident::Ident, span::Span, Named, Spanned};

#[derive(Debug, Clone)]
pub enum TraitItem {
Expand All @@ -33,6 +33,18 @@ pub struct TraitDeclaration {
pub span: Span,
}

impl Named for TraitDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
}
}

impl Spanned for TraitDeclaration {
fn span(&self) -> sway_types::Span {
self.span.clone()
}
}

#[derive(Debug, Clone)]
pub struct Supertrait {
pub name: CallPath,
Expand Down Expand Up @@ -86,6 +98,18 @@ pub struct TraitTypeDeclaration {
pub span: Span,
}

impl Named for TraitTypeDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
}
}

impl Spanned for TraitTypeDeclaration {
fn span(&self) -> sway_types::Span {
self.span.clone()
}
}

impl DebugWithEngines for TraitTypeDeclaration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, _engines: &Engines) -> std::fmt::Result {
f.write_fmt(format_args!("{}", self.name))
Expand Down
14 changes: 13 additions & 1 deletion sway-core/src/language/parsed/declaration/type_alias.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{language::Visibility, transform, type_system::*};

use sway_types::{ident::Ident, span::Span};
use sway_types::{ident::Ident, span::Span, Named, Spanned};

#[derive(Debug, Clone)]
pub struct TypeAliasDeclaration {
Expand All @@ -10,3 +10,15 @@ pub struct TypeAliasDeclaration {
pub visibility: Visibility,
pub span: Span,
}

impl Named for TypeAliasDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
}
}

impl Spanned for TypeAliasDeclaration {
fn span(&self) -> sway_types::Span {
self.span.clone()
}
}
14 changes: 14 additions & 0 deletions sway-core/src/language/parsed/declaration/variable.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use sway_types::{Named, Spanned};

use crate::{language::parsed::Expression, Ident, TypeArgument};

#[derive(Debug, Clone)]
Expand All @@ -7,3 +9,15 @@ pub struct VariableDeclaration {
pub body: Expression, // will be codeblock variant
pub is_mutable: bool,
}

impl Named for VariableDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
}
}

impl Spanned for VariableDeclaration {
fn span(&self) -> sway_types::Span {
self.name.span()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,12 @@ impl TyDecl {
for supertrait in trait_decl.supertraits.iter_mut() {
let _ = ctx
.namespace()
.resolve_call_path(handler, engines, &supertrait.name, ctx.self_type())
.resolve_call_path_typed(
handler,
engines,
&supertrait.name,
ctx.self_type(),
)
.map(|supertrait_decl| {
if let ty::TyDecl::TraitDecl(ty::TraitDecl {
name: supertrait_name,
Expand Down Expand Up @@ -273,7 +278,7 @@ impl TyDecl {
// we insert its methods with a prefix
let emp_vec = vec![];
let impl_trait_items = if let Ok(ty::TyDecl::TraitDecl { .. }) =
ctx.namespace().resolve_call_path(
ctx.namespace().resolve_call_path_typed(
&Handler::default(),
engines,
&impl_trait.trait_name,
Expand Down Expand Up @@ -392,7 +397,12 @@ impl TyDecl {
for supertrait in abi_decl.supertraits.iter_mut() {
let _ = ctx
.namespace()
.resolve_call_path(handler, engines, &supertrait.name, ctx.self_type())
.resolve_call_path_typed(
handler,
engines,
&supertrait.name,
ctx.self_type(),
)
.map(|supertrait_decl| {
if let ty::TyDecl::TraitDecl(ty::TraitDecl {
name: supertrait_name,
Expand Down
Loading

0 comments on commit 2eb6f2a

Please sign in to comment.