Skip to content

Commit 2eb6f2a

Browse files
tritaoJoshuaBatty
andauthored
Refactor trait map and module symbol resolution code to allow usage for 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 <joshpbatty@gmail.com>
1 parent 35df2a7 commit 2eb6f2a

29 files changed

+636
-238
lines changed

sway-core/src/ir_generation/const_eval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ pub(crate) fn compile_const_decl(
130130
None => None,
131131
};
132132
let const_decl = match decl {
133-
Ok(decl) => match decl {
133+
Ok(decl) => match decl.expect_typed() {
134134
ty::TyDecl::ConstantDecl(ty::ConstantDecl { decl_id, .. }) => {
135-
Some((*env.engines.de().get_constant(decl_id)).clone())
135+
Some((*env.engines.de().get_constant(&decl_id)).clone())
136136
}
137137
_otherwise => const_decl.cloned(),
138138
},

sway-core/src/language/parsed/declaration.rs

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use r#enum::*;
1717
pub use r#struct::*;
1818
pub use r#trait::*;
1919
pub use storage::*;
20+
use sway_types::Spanned;
2021
pub use type_alias::*;
2122
pub use variable::*;
2223

@@ -48,4 +49,24 @@ impl Declaration {
4849
false
4950
}
5051
}
52+
53+
#[allow(dead_code)]
54+
fn span(&self, engines: &Engines) -> sway_types::Span {
55+
use Declaration::*;
56+
let pe = engines.pe();
57+
match self {
58+
VariableDeclaration(decl_id) => pe.get_variable(decl_id).span(),
59+
FunctionDeclaration(decl_id) => pe.get_function(decl_id).span(),
60+
TraitDeclaration(decl_id) => pe.get_trait(decl_id).span(),
61+
StructDeclaration(decl_id) => pe.get_struct(decl_id).span(),
62+
EnumDeclaration(decl_id) => pe.get_enum(decl_id).span(),
63+
ImplTrait(decl_id) => pe.get_impl_trait(decl_id).span(),
64+
ImplSelf(decl_id) => pe.get_impl_self(decl_id).span(),
65+
AbiDeclaration(decl_id) => pe.get_abi(decl_id).span(),
66+
ConstantDeclaration(decl_id) => pe.get_constant(decl_id).span(),
67+
StorageDeclaration(decl_id) => pe.get_storage(decl_id).span(),
68+
TypeAliasDeclaration(decl_id) => pe.get_type_alias(decl_id).span(),
69+
TraitTypeDeclaration(decl_id) => pe.get_trait_type(decl_id).span(),
70+
}
71+
}
5172
}

sway-core/src/language/parsed/declaration/abi.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{decl_engine::parsed_id::ParsedDeclId, transform};
22

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

5-
use sway_types::{ident::Ident, span::Span};
5+
use sway_types::{ident::Ident, span::Span, Named, Spanned};
66

77
/// An `abi` declaration, which declares an interface for a contract
88
/// to implement or for a caller to use to call a contract.
@@ -18,3 +18,15 @@ pub struct AbiDeclaration {
1818
pub(crate) span: Span,
1919
pub attributes: transform::AttributesMap,
2020
}
21+
22+
impl Named for AbiDeclaration {
23+
fn name(&self) -> &sway_types::BaseIdent {
24+
&self.name
25+
}
26+
}
27+
28+
impl Spanned for AbiDeclaration {
29+
fn span(&self) -> sway_types::Span {
30+
self.span.clone()
31+
}
32+
}

sway-core/src/language/parsed/declaration/constant.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
language::{parsed::Expression, Visibility},
44
transform, Engines, TypeArgument,
55
};
6-
use sway_types::{Ident, Span};
6+
use sway_types::{Ident, Named, Span, Spanned};
77

88
#[derive(Debug, Clone)]
99
pub struct ConstantDeclaration {
@@ -16,6 +16,18 @@ pub struct ConstantDeclaration {
1616
pub span: Span,
1717
}
1818

19+
impl Named for ConstantDeclaration {
20+
fn name(&self) -> &sway_types::BaseIdent {
21+
&self.name
22+
}
23+
}
24+
25+
impl Spanned for ConstantDeclaration {
26+
fn span(&self) -> sway_types::Span {
27+
self.span.clone()
28+
}
29+
}
30+
1931
impl DebugWithEngines for ConstantDeclaration {
2032
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, _engines: &Engines) -> std::fmt::Result {
2133
f.write_fmt(format_args!("{}", self.name))

sway-core/src/language/parsed/declaration/enum.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{language::Visibility, transform, type_system::*};
2-
use sway_types::{ident::Ident, span::Span};
2+
use sway_types::{ident::Ident, span::Span, Named, Spanned};
33

44
#[derive(Debug, Clone)]
55
pub struct EnumDeclaration {
@@ -11,6 +11,18 @@ pub struct EnumDeclaration {
1111
pub visibility: Visibility,
1212
}
1313

14+
impl Named for EnumDeclaration {
15+
fn name(&self) -> &sway_types::BaseIdent {
16+
&self.name
17+
}
18+
}
19+
20+
impl Spanned for EnumDeclaration {
21+
fn span(&self) -> sway_types::Span {
22+
self.span.clone()
23+
}
24+
}
25+
1426
#[derive(Debug, Clone)]
1527
pub struct EnumVariant {
1628
pub name: Ident,

sway-core/src/language/parsed/declaration/function.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
transform::{self, AttributeKind},
55
type_system::*,
66
};
7-
use sway_types::{ident::Ident, span::Span};
7+
use sway_types::{ident::Ident, span::Span, Named, Spanned};
88

99
#[derive(Debug, Clone)]
1010
pub enum FunctionDeclarationKind {
@@ -35,6 +35,18 @@ impl DebugWithEngines for FunctionDeclaration {
3535
}
3636
}
3737

38+
impl Named for FunctionDeclaration {
39+
fn name(&self) -> &sway_types::BaseIdent {
40+
&self.name
41+
}
42+
}
43+
44+
impl Spanned for FunctionDeclaration {
45+
fn span(&self) -> sway_types::Span {
46+
self.span.clone()
47+
}
48+
}
49+
3850
#[derive(Debug, Clone)]
3951
pub struct FunctionParameter {
4052
pub name: Ident,

sway-core/src/language/parsed/declaration/impl_trait.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
type_system::TypeArgument, Engines, TypeParameter,
55
};
66

7-
use sway_types::span::Span;
7+
use sway_types::{span::Span, Named, Spanned};
88

99
#[derive(Debug, Clone)]
1010
pub enum ImplItem {
@@ -13,6 +13,16 @@ pub enum ImplItem {
1313
Type(ParsedDeclId<TraitTypeDeclaration>),
1414
}
1515

16+
impl ImplItem {
17+
pub fn span(&self, engines: &Engines) -> Span {
18+
match self {
19+
ImplItem::Fn(id) => engines.pe().get_function(id).span(),
20+
ImplItem::Constant(id) => engines.pe().get_constant(id).span(),
21+
ImplItem::Type(id) => engines.pe().get_trait_type(id).span(),
22+
}
23+
}
24+
}
25+
1626
impl DebugWithEngines for ImplItem {
1727
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, engines: &Engines) -> std::fmt::Result {
1828
match self {
@@ -43,6 +53,18 @@ pub struct ImplTrait {
4353
pub(crate) block_span: Span,
4454
}
4555

56+
impl Named for ImplTrait {
57+
fn name(&self) -> &sway_types::BaseIdent {
58+
&self.trait_name.suffix
59+
}
60+
}
61+
62+
impl Spanned for ImplTrait {
63+
fn span(&self) -> sway_types::Span {
64+
self.block_span.clone()
65+
}
66+
}
67+
4668
impl DebugWithEngines for ImplTrait {
4769
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, engines: &Engines) -> std::fmt::Result {
4870
f.write_fmt(format_args!(
@@ -64,6 +86,12 @@ pub struct ImplSelf {
6486
pub(crate) block_span: Span,
6587
}
6688

89+
impl Spanned for ImplSelf {
90+
fn span(&self) -> sway_types::Span {
91+
self.block_span.clone()
92+
}
93+
}
94+
6795
impl DebugWithEngines for ImplSelf {
6896
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, engines: &Engines) -> std::fmt::Result {
6997
f.write_fmt(format_args!(

sway-core/src/language/parsed/declaration/storage.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{language::parsed::Expression, transform, type_system::*};
2-
use sway_types::{ident::Ident, span::Span};
2+
use sway_types::{ident::Ident, span::Span, Spanned};
33

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

14+
impl Spanned for StorageDeclaration {
15+
fn span(&self) -> sway_types::Span {
16+
self.span.clone()
17+
}
18+
}
19+
1420
/// An individual field in a storage declaration.
1521
/// A type annotation _and_ initializer value must be provided. The initializer value must be a
1622
/// constant expression. For now, that basically means just a literal, but as constant folding

sway-core/src/language/parsed/declaration/struct.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{language::Visibility, transform, type_system::TypeParameter, TypeArgument};
2-
use sway_types::{ident::Ident, span::Span};
2+
use sway_types::{ident::Ident, span::Span, Named, Spanned};
33

44
#[derive(Debug, Clone)]
55
pub struct StructDeclaration {
@@ -11,6 +11,18 @@ pub struct StructDeclaration {
1111
pub(crate) span: Span,
1212
}
1313

14+
impl Named for StructDeclaration {
15+
fn name(&self) -> &sway_types::BaseIdent {
16+
&self.name
17+
}
18+
}
19+
20+
impl Spanned for StructDeclaration {
21+
fn span(&self) -> sway_types::Span {
22+
self.span.clone()
23+
}
24+
}
25+
1426
#[derive(Debug, Clone)]
1527
pub struct StructField {
1628
pub visibility: Visibility,

sway-core/src/language/parsed/declaration/trait.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
type_system::*,
1111
};
1212
use sway_error::handler::ErrorEmitted;
13-
use sway_types::{ident::Ident, span::Span, Spanned};
13+
use sway_types::{ident::Ident, span::Span, Named, Spanned};
1414

1515
#[derive(Debug, Clone)]
1616
pub enum TraitItem {
@@ -33,6 +33,18 @@ pub struct TraitDeclaration {
3333
pub span: Span,
3434
}
3535

36+
impl Named for TraitDeclaration {
37+
fn name(&self) -> &sway_types::BaseIdent {
38+
&self.name
39+
}
40+
}
41+
42+
impl Spanned for TraitDeclaration {
43+
fn span(&self) -> sway_types::Span {
44+
self.span.clone()
45+
}
46+
}
47+
3648
#[derive(Debug, Clone)]
3749
pub struct Supertrait {
3850
pub name: CallPath,
@@ -86,6 +98,18 @@ pub struct TraitTypeDeclaration {
8698
pub span: Span,
8799
}
88100

101+
impl Named for TraitTypeDeclaration {
102+
fn name(&self) -> &sway_types::BaseIdent {
103+
&self.name
104+
}
105+
}
106+
107+
impl Spanned for TraitTypeDeclaration {
108+
fn span(&self) -> sway_types::Span {
109+
self.span.clone()
110+
}
111+
}
112+
89113
impl DebugWithEngines for TraitTypeDeclaration {
90114
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, _engines: &Engines) -> std::fmt::Result {
91115
f.write_fmt(format_args!("{}", self.name))

sway-core/src/language/parsed/declaration/type_alias.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{language::Visibility, transform, type_system::*};
22

3-
use sway_types::{ident::Ident, span::Span};
3+
use sway_types::{ident::Ident, span::Span, Named, Spanned};
44

55
#[derive(Debug, Clone)]
66
pub struct TypeAliasDeclaration {
@@ -10,3 +10,15 @@ pub struct TypeAliasDeclaration {
1010
pub visibility: Visibility,
1111
pub span: Span,
1212
}
13+
14+
impl Named for TypeAliasDeclaration {
15+
fn name(&self) -> &sway_types::BaseIdent {
16+
&self.name
17+
}
18+
}
19+
20+
impl Spanned for TypeAliasDeclaration {
21+
fn span(&self) -> sway_types::Span {
22+
self.span.clone()
23+
}
24+
}

sway-core/src/language/parsed/declaration/variable.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use sway_types::{Named, Spanned};
2+
13
use crate::{language::parsed::Expression, Ident, TypeArgument};
24

35
#[derive(Debug, Clone)]
@@ -7,3 +9,15 @@ pub struct VariableDeclaration {
79
pub body: Expression, // will be codeblock variant
810
pub is_mutable: bool,
911
}
12+
13+
impl Named for VariableDeclaration {
14+
fn name(&self) -> &sway_types::BaseIdent {
15+
&self.name
16+
}
17+
}
18+
19+
impl Spanned for VariableDeclaration {
20+
fn span(&self) -> sway_types::Span {
21+
self.name.span()
22+
}
23+
}

sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,12 @@ impl TyDecl {
230230
for supertrait in trait_decl.supertraits.iter_mut() {
231231
let _ = ctx
232232
.namespace()
233-
.resolve_call_path(handler, engines, &supertrait.name, ctx.self_type())
233+
.resolve_call_path_typed(
234+
handler,
235+
engines,
236+
&supertrait.name,
237+
ctx.self_type(),
238+
)
234239
.map(|supertrait_decl| {
235240
if let ty::TyDecl::TraitDecl(ty::TraitDecl {
236241
name: supertrait_name,
@@ -273,7 +278,7 @@ impl TyDecl {
273278
// we insert its methods with a prefix
274279
let emp_vec = vec![];
275280
let impl_trait_items = if let Ok(ty::TyDecl::TraitDecl { .. }) =
276-
ctx.namespace().resolve_call_path(
281+
ctx.namespace().resolve_call_path_typed(
277282
&Handler::default(),
278283
engines,
279284
&impl_trait.trait_name,
@@ -392,7 +397,12 @@ impl TyDecl {
392397
for supertrait in abi_decl.supertraits.iter_mut() {
393398
let _ = ctx
394399
.namespace()
395-
.resolve_call_path(handler, engines, &supertrait.name, ctx.self_type())
400+
.resolve_call_path_typed(
401+
handler,
402+
engines,
403+
&supertrait.name,
404+
ctx.self_type(),
405+
)
396406
.map(|supertrait_decl| {
397407
if let ty::TyDecl::TraitDecl(ty::TraitDecl {
398408
name: supertrait_name,

0 commit comments

Comments
 (0)