diff --git a/sway-core/src/decl_engine/engine.rs b/sway-core/src/decl_engine/engine.rs index 7ea172ad7ed..8f992f119bd 100644 --- a/sway-core/src/decl_engine/engine.rs +++ b/sway-core/src/decl_engine/engine.rs @@ -9,12 +9,19 @@ use sway_types::{Named, ProgramId, Spanned}; use crate::{ concurrent_slab::ConcurrentSlab, - decl_engine::*, + decl_engine::{parsed_id::ParsedDeclId, *}, engine_threading::*, - language::ty::{ - self, TyAbiDecl, TyConfigurableDecl, TyConstantDecl, TyEnumDecl, TyFunctionDecl, - TyImplSelfOrTrait, TyStorageDecl, TyStructDecl, TyTraitDecl, TyTraitFn, TyTraitType, - TyTypeAliasDecl, + language::{ + parsed::{ + AbiDeclaration, ConfigurableDeclaration, ConstantDeclaration, EnumDeclaration, + FunctionDeclaration, ImplSelfOrTrait, StorageDeclaration, StructDeclaration, + TraitDeclaration, TraitFn, TraitTypeDeclaration, TypeAliasDeclaration, + }, + ty::{ + self, TyAbiDecl, TyConfigurableDecl, TyConstantDecl, TyDeclParsedType, TyEnumDecl, + TyFunctionDecl, TyImplSelfOrTrait, TyStorageDecl, TyStructDecl, TyTraitDecl, TyTraitFn, + TyTraitType, TyTypeAliasDecl, + }, }, }; @@ -34,6 +41,27 @@ pub struct DeclEngine { enum_slab: ConcurrentSlab, type_alias_slab: ConcurrentSlab, + function_parsed_decl_id_map: + RwLock, ParsedDeclId>>, + trait_parsed_decl_id_map: RwLock, ParsedDeclId>>, + trait_fn_parsed_decl_id_map: RwLock, ParsedDeclId>>, + trait_type_parsed_decl_id_map: + RwLock, ParsedDeclId>>, + impl_self_or_trait_parsed_decl_id_map: + RwLock, ParsedDeclId>>, + struct_parsed_decl_id_map: + RwLock, ParsedDeclId>>, + storage_parsed_decl_id_map: + RwLock, ParsedDeclId>>, + abi_parsed_decl_id_map: RwLock, ParsedDeclId>>, + constant_parsed_decl_id_map: + RwLock, ParsedDeclId>>, + configurable_parsed_decl_id_map: + RwLock, ParsedDeclId>>, + enum_parsed_decl_id_map: RwLock, ParsedDeclId>>, + type_alias_parsed_decl_id_map: + RwLock, ParsedDeclId>>, + parents: RwLock>>, } @@ -52,6 +80,32 @@ impl Clone for DeclEngine { configurable_slab: self.configurable_slab.clone(), enum_slab: self.enum_slab.clone(), type_alias_slab: self.type_alias_slab.clone(), + function_parsed_decl_id_map: RwLock::new( + self.function_parsed_decl_id_map.read().clone(), + ), + trait_parsed_decl_id_map: RwLock::new(self.trait_parsed_decl_id_map.read().clone()), + trait_fn_parsed_decl_id_map: RwLock::new( + self.trait_fn_parsed_decl_id_map.read().clone(), + ), + trait_type_parsed_decl_id_map: RwLock::new( + self.trait_type_parsed_decl_id_map.read().clone(), + ), + impl_self_or_trait_parsed_decl_id_map: RwLock::new( + self.impl_self_or_trait_parsed_decl_id_map.read().clone(), + ), + struct_parsed_decl_id_map: RwLock::new(self.struct_parsed_decl_id_map.read().clone()), + storage_parsed_decl_id_map: RwLock::new(self.storage_parsed_decl_id_map.read().clone()), + abi_parsed_decl_id_map: RwLock::new(self.abi_parsed_decl_id_map.read().clone()), + constant_parsed_decl_id_map: RwLock::new( + self.constant_parsed_decl_id_map.read().clone(), + ), + configurable_parsed_decl_id_map: RwLock::new( + self.configurable_parsed_decl_id_map.read().clone(), + ), + enum_parsed_decl_id_map: RwLock::new(self.enum_parsed_decl_id_map.read().clone()), + type_alias_parsed_decl_id_map: RwLock::new( + self.type_alias_parsed_decl_id_map.read().clone(), + ), parents: RwLock::new(self.parents.read().clone()), } } @@ -61,26 +115,40 @@ pub trait DeclEngineGet { fn get(&self, index: &I) -> Arc; } +pub trait DeclEngineGetParsedDeclId +where + T: TyDeclParsedType, +{ + fn get_parsed_decl_id(&self, decl_id: &DeclId) -> Option>; +} + pub trait DeclEngineInsert where - T: Named + Spanned, + T: Named + Spanned + TyDeclParsedType, { - fn insert(&self, decl: T) -> DeclRef>; + fn insert( + &self, + decl: T, + parsed_decl_id: Option<&ParsedDeclId>, + ) -> DeclRef>; } pub trait DeclEngineInsertArc where - T: Named + Spanned, + T: Named + Spanned + TyDeclParsedType, { - fn insert_arc(&self, decl: Arc) -> DeclRef>; + fn insert_arc( + &self, + decl: Arc, + parsed_decl_id: Option<&ParsedDeclId>, + ) -> DeclRef>; } pub trait DeclEngineReplace { fn replace(&self, index: DeclId, decl: T); } -pub trait DeclEngineIndex: - DeclEngineGet, T> + DeclEngineInsert + DeclEngineReplace +pub trait DeclEngineIndex: DeclEngineGet, T> + DeclEngineReplace where T: Named + Spanned, { @@ -115,41 +183,110 @@ decl_engine_get!(enum_slab, ty::TyEnumDecl); decl_engine_get!(type_alias_slab, ty::TyTypeAliasDecl); macro_rules! decl_engine_insert { - ($slab:ident, $decl:ty) => { + ($slab:ident, $parsed_slab:ident, $decl:ty) => { impl DeclEngineInsert<$decl> for DeclEngine { - fn insert(&self, decl: $decl) -> DeclRef> { + fn insert( + &self, + decl: $decl, + parsed_decl_id: Option<&ParsedDeclId<<$decl as TyDeclParsedType>::ParsedType>>, + ) -> DeclRef> { let span = decl.span(); - DeclRef::new( - decl.name().clone(), - DeclId::new(self.$slab.insert(decl)), - span, - ) + let decl_name = decl.name().clone(); + let decl_id = DeclId::new(self.$slab.insert(decl)); + if let Some(parsed_decl_id) = parsed_decl_id { + self.$parsed_slab + .write() + .insert(decl_id, parsed_decl_id.clone()); + } + DeclRef::new(decl_name, decl_id, span) } } impl DeclEngineInsertArc<$decl> for DeclEngine { - fn insert_arc(&self, decl: Arc<$decl>) -> DeclRef> { + fn insert_arc( + &self, + decl: Arc<$decl>, + parsed_decl_id: Option<&ParsedDeclId<<$decl as TyDeclParsedType>::ParsedType>>, + ) -> DeclRef> { let span = decl.span(); - DeclRef::new( - decl.name().clone(), - DeclId::new(self.$slab.insert_arc(decl)), - span, - ) + let decl_name = decl.name().clone(); + let decl_id = DeclId::new(self.$slab.insert_arc(decl)); + if let Some(parsed_decl_id) = parsed_decl_id { + self.$parsed_slab + .write() + .insert(decl_id, parsed_decl_id.clone()); + } + DeclRef::new(decl_name, decl_id, span) + } + } + }; +} +decl_engine_insert!( + function_slab, + function_parsed_decl_id_map, + ty::TyFunctionDecl +); +decl_engine_insert!(trait_slab, trait_parsed_decl_id_map, ty::TyTraitDecl); +decl_engine_insert!(trait_fn_slab, trait_fn_parsed_decl_id_map, ty::TyTraitFn); +decl_engine_insert!( + trait_type_slab, + trait_type_parsed_decl_id_map, + ty::TyTraitType +); +decl_engine_insert!( + impl_self_or_trait_slab, + impl_self_or_trait_parsed_decl_id_map, + ty::TyImplSelfOrTrait +); +decl_engine_insert!(struct_slab, struct_parsed_decl_id_map, ty::TyStructDecl); +decl_engine_insert!(storage_slab, storage_parsed_decl_id_map, ty::TyStorageDecl); +decl_engine_insert!(abi_slab, abi_parsed_decl_id_map, ty::TyAbiDecl); +decl_engine_insert!( + constant_slab, + constant_parsed_decl_id_map, + ty::TyConstantDecl +); +decl_engine_insert!( + configurable_slab, + configurable_parsed_decl_id_map, + ty::TyConfigurableDecl +); +decl_engine_insert!(enum_slab, enum_parsed_decl_id_map, ty::TyEnumDecl); +decl_engine_insert!( + type_alias_slab, + type_alias_parsed_decl_id_map, + ty::TyTypeAliasDecl +); + +macro_rules! decl_engine_parsed_decl_id { + ($slab:ident, $decl:ty) => { + impl DeclEngineGetParsedDeclId<$decl> for DeclEngine { + fn get_parsed_decl_id( + &self, + decl_id: &DeclId<$decl>, + ) -> Option::ParsedType>> { + let parsed_decl_id_map = self.$slab.read(); + if let Some(parsed_decl_id) = parsed_decl_id_map.get(&decl_id) { + return Some(parsed_decl_id.clone()); + } else { + None + } } } }; } -decl_engine_insert!(function_slab, ty::TyFunctionDecl); -decl_engine_insert!(trait_slab, ty::TyTraitDecl); -decl_engine_insert!(trait_fn_slab, ty::TyTraitFn); -decl_engine_insert!(trait_type_slab, ty::TyTraitType); -decl_engine_insert!(impl_self_or_trait_slab, ty::TyImplSelfOrTrait); -decl_engine_insert!(struct_slab, ty::TyStructDecl); -decl_engine_insert!(storage_slab, ty::TyStorageDecl); -decl_engine_insert!(abi_slab, ty::TyAbiDecl); -decl_engine_insert!(constant_slab, ty::TyConstantDecl); -decl_engine_insert!(configurable_slab, ty::TyConfigurableDecl); -decl_engine_insert!(enum_slab, ty::TyEnumDecl); -decl_engine_insert!(type_alias_slab, ty::TyTypeAliasDecl); + +decl_engine_parsed_decl_id!(function_parsed_decl_id_map, ty::TyFunctionDecl); +decl_engine_parsed_decl_id!(trait_parsed_decl_id_map, ty::TyTraitDecl); +decl_engine_parsed_decl_id!(trait_fn_parsed_decl_id_map, ty::TyTraitFn); +decl_engine_parsed_decl_id!(trait_type_parsed_decl_id_map, ty::TyTraitType); +decl_engine_parsed_decl_id!(impl_self_or_trait_parsed_decl_id_map, ty::TyImplSelfOrTrait); +decl_engine_parsed_decl_id!(struct_parsed_decl_id_map, ty::TyStructDecl); +decl_engine_parsed_decl_id!(storage_parsed_decl_id_map, ty::TyStorageDecl); +decl_engine_parsed_decl_id!(abi_parsed_decl_id_map, ty::TyAbiDecl); +decl_engine_parsed_decl_id!(constant_parsed_decl_id_map, ty::TyConstantDecl); +decl_engine_parsed_decl_id!(configurable_parsed_decl_id_map, ty::TyConfigurableDecl); +decl_engine_parsed_decl_id!(enum_parsed_decl_id_map, ty::TyEnumDecl); +decl_engine_parsed_decl_id!(type_alias_parsed_decl_id_map, ty::TyTypeAliasDecl); macro_rules! decl_engine_replace { ($slab:ident, $decl:ty) => { diff --git a/sway-core/src/decl_engine/id.rs b/sway-core/src/decl_engine/id.rs index dcc2391773d..24ac4aab4d7 100644 --- a/sway-core/src/decl_engine/id.rs +++ b/sway-core/src/decl_engine/id.rs @@ -5,7 +5,7 @@ use std::{fmt, hash::Hash}; use sway_types::{Named, Spanned}; -use crate::language::ty::TyTraitType; +use crate::language::ty::{TyDeclParsedType, TyTraitType}; use crate::{ decl_engine::*, engine_threading::*, @@ -231,8 +231,8 @@ impl SubstTypes for DeclId { impl DeclId where - DeclEngine: DeclEngineIndex, - T: Named + Spanned + SubstTypes + Clone, + DeclEngine: DeclEngineIndex + DeclEngineInsert + DeclEngineGetParsedDeclId, + T: Named + Spanned + SubstTypes + Clone + TyDeclParsedType, { pub(crate) fn subst_types_and_insert_new( &self, @@ -242,7 +242,7 @@ where let decl_engine = engines.de(); let mut decl = (*decl_engine.get(self)).clone(); if decl.subst(type_mapping, engines).has_changes() { - Some(decl_engine.insert(decl)) + Some(decl_engine.insert(decl, decl_engine.get_parsed_decl_id(self).as_ref())) } else { None } diff --git a/sway-core/src/decl_engine/ref.rs b/sway-core/src/decl_engine/ref.rs index 2ed681d87b9..a8d4d28041d 100644 --- a/sway-core/src/decl_engine/ref.rs +++ b/sway-core/src/decl_engine/ref.rs @@ -29,8 +29,8 @@ use crate::{ decl_engine::*, engine_threading::*, language::ty::{ - self, TyAbiDecl, TyConstantDecl, TyEnumDecl, TyFunctionDecl, TyImplSelfOrTrait, - TyStorageDecl, TyStructDecl, TyTraitDecl, TyTraitFn, TyTraitType, + self, TyAbiDecl, TyConstantDecl, TyDeclParsedType, TyEnumDecl, TyFunctionDecl, + TyImplSelfOrTrait, TyStorageDecl, TyStructDecl, TyTraitDecl, TyTraitFn, TyTraitType, }, semantic_analysis::TypeCheckContext, type_system::*, @@ -96,8 +96,8 @@ impl DeclRef> { impl DeclRef> where - DeclEngine: DeclEngineIndex, - T: Named + Spanned + SubstTypes + Clone, + DeclEngine: DeclEngineIndex + DeclEngineInsert + DeclEngineGetParsedDeclId, + T: Named + Spanned + SubstTypes + Clone + TyDeclParsedType, { pub(crate) fn subst_types_and_insert_new( &self, @@ -107,7 +107,7 @@ where let decl_engine = engines.de(); let mut decl = (*decl_engine.get(&self.id)).clone(); if decl.subst(type_mapping, engines).has_changes() { - Some(decl_engine.insert(decl)) + Some(decl_engine.insert(decl, decl_engine.get_parsed_decl_id(&self.id).as_ref())) } else { None } @@ -132,8 +132,8 @@ where impl DeclRef> where AssociatedItemDeclId: From>, - DeclEngine: DeclEngineIndex, - T: Named + Spanned + SubstTypes + Clone, + DeclEngine: DeclEngineIndex + DeclEngineInsert + DeclEngineGetParsedDeclId, + T: Named + Spanned + SubstTypes + Clone + TyDeclParsedType, { pub(crate) fn subst_types_and_insert_new_with_parent( &self, @@ -145,7 +145,7 @@ where if decl.subst(type_mapping, engines).has_changes() { Some( decl_engine - .insert(decl) + .insert(decl, decl_engine.get_parsed_decl_id(&self.id).as_ref()) .with_parent(decl_engine, self.id.into()), ) } else { diff --git a/sway-core/src/language/ty/declaration/abi.rs b/sway-core/src/language/ty/declaration/abi.rs index c147b9d270e..5ad5ea2c3dc 100644 --- a/sway-core/src/language/ty/declaration/abi.rs +++ b/sway-core/src/language/ty/declaration/abi.rs @@ -1,9 +1,14 @@ -use crate::{engine_threading::*, language::parsed, transform, type_system::*}; +use crate::{ + engine_threading::*, + language::parsed::{self, AbiDeclaration}, + transform, + type_system::*, +}; use std::hash::{Hash, Hasher}; use sway_types::{Ident, Named, Span, Spanned}; -use super::{TyTraitInterfaceItem, TyTraitItem}; +use super::{TyDeclParsedType, TyTraitInterfaceItem, TyTraitItem}; /// A [TyAbiDecl] contains the type-checked version of the parse tree's /// `AbiDeclaration`. @@ -19,6 +24,10 @@ pub struct TyAbiDecl { pub attributes: transform::AttributesMap, } +impl TyDeclParsedType for TyAbiDecl { + type ParsedType = AbiDeclaration; +} + impl EqWithEngines for TyAbiDecl {} impl PartialEqWithEngines for TyAbiDecl { fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool { diff --git a/sway-core/src/language/ty/declaration/configurable.rs b/sway-core/src/language/ty/declaration/configurable.rs index 3c3adf15216..ee9f7171dc6 100644 --- a/sway-core/src/language/ty/declaration/configurable.rs +++ b/sway-core/src/language/ty/declaration/configurable.rs @@ -10,7 +10,7 @@ use crate::{ decl_engine::{DeclId, DeclMapping, DeclRef, ReplaceDecls}, engine_threading::*, has_changes, - language::{ty::*, CallPath, Visibility}, + language::{parsed::ConfigurableDeclaration, ty::*, CallPath, Visibility}, semantic_analysis::TypeCheckContext, transform, type_system::*, @@ -29,6 +29,10 @@ pub struct TyConfigurableDecl { pub decode_fn: Option>>, } +impl TyDeclParsedType for TyConfigurableDecl { + type ParsedType = ConfigurableDeclaration; +} + impl DebugWithEngines for TyConfigurableDecl { fn fmt(&self, f: &mut fmt::Formatter<'_>, _engines: &Engines) -> fmt::Result { write!(f, "{}", self.call_path) diff --git a/sway-core/src/language/ty/declaration/constant.rs b/sway-core/src/language/ty/declaration/constant.rs index 89fd42ce83b..473eb224e57 100644 --- a/sway-core/src/language/ty/declaration/constant.rs +++ b/sway-core/src/language/ty/declaration/constant.rs @@ -10,7 +10,7 @@ use crate::{ decl_engine::{DeclMapping, ReplaceDecls}, engine_threading::*, has_changes, - language::{ty::*, CallPath, Visibility}, + language::{parsed::ConstantDeclaration, ty::*, CallPath, Visibility}, semantic_analysis::TypeCheckContext, transform, type_system::*, @@ -27,6 +27,10 @@ pub struct TyConstantDecl { pub span: Span, } +impl TyDeclParsedType for TyConstantDecl { + type ParsedType = ConstantDeclaration; +} + impl DebugWithEngines for TyConstantDecl { fn fmt(&self, f: &mut fmt::Formatter<'_>, _engines: &Engines) -> fmt::Result { write!(f, "{}", self.call_path) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 904001b1fa2..e3adb11ce4e 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -38,6 +38,14 @@ pub enum TyDecl { TypeAliasDecl(TypeAliasDecl), } +/// This trait is used to associate a typed declaration node with its +/// corresponding parsed declaration node by way of an associated type. +/// This is used by the generic code in [`DeclEngine`] related to handling +/// typed to parsed node maps. +pub trait TyDeclParsedType { + type ParsedType; +} + #[derive(Clone, Debug)] pub struct ConstantDecl { pub decl_id: DeclId, diff --git a/sway-core/src/language/ty/declaration/enum.rs b/sway-core/src/language/ty/declaration/enum.rs index 36af57315cf..20953f0940f 100644 --- a/sway-core/src/language/ty/declaration/enum.rs +++ b/sway-core/src/language/ty/declaration/enum.rs @@ -12,12 +12,14 @@ use sway_types::{Ident, Named, Span, Spanned}; use crate::{ engine_threading::*, has_changes, - language::{CallPath, Visibility}, + language::{parsed::EnumDeclaration, CallPath, Visibility}, semantic_analysis::type_check_context::MonomorphizeHelper, transform, type_system::*, }; +use super::TyDeclParsedType; + #[derive(Clone, Debug)] pub struct TyEnumDecl { pub call_path: CallPath, @@ -28,6 +30,10 @@ pub struct TyEnumDecl { pub visibility: Visibility, } +impl TyDeclParsedType for TyEnumDecl { + type ParsedType = EnumDeclaration; +} + impl Named for TyEnumDecl { fn name(&self) -> &Ident { &self.call_path.suffix diff --git a/sway-core/src/language/ty/declaration/function.rs b/sway-core/src/language/ty/declaration/function.rs index a616600bc45..f6304845c35 100644 --- a/sway-core/src/language/ty/declaration/function.rs +++ b/sway-core/src/language/ty/declaration/function.rs @@ -8,7 +8,10 @@ use sway_error::handler::{ErrorEmitted, Handler}; use crate::{ has_changes, - language::{parsed::FunctionDeclarationKind, CallPath}, + language::{ + parsed::{FunctionDeclaration, FunctionDeclarationKind}, + CallPath, + }, semantic_analysis::type_check_context::MonomorphizeHelper, transform::AttributeKind, }; @@ -58,6 +61,10 @@ pub struct TyFunctionDecl { pub kind: TyFunctionDeclKind, } +impl TyDeclParsedType for TyFunctionDecl { + type ParsedType = FunctionDeclaration; +} + impl DebugWithEngines for TyFunctionDecl { fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result { write!( diff --git a/sway-core/src/language/ty/declaration/impl_trait.rs b/sway-core/src/language/ty/declaration/impl_trait.rs index 4224d1b6a17..a50f8369bf2 100644 --- a/sway-core/src/language/ty/declaration/impl_trait.rs +++ b/sway-core/src/language/ty/declaration/impl_trait.rs @@ -3,11 +3,14 @@ use std::hash::{Hash, Hasher}; use sway_types::{Ident, Named, Span, Spanned}; use crate::{ - decl_engine::DeclRefMixedInterface, engine_threading::*, has_changes, language::CallPath, + decl_engine::DeclRefMixedInterface, + engine_threading::*, + has_changes, + language::{parsed::ImplSelfOrTrait, CallPath}, type_system::*, }; -use super::TyTraitItem; +use super::{TyDeclParsedType, TyTraitItem}; pub type TyImplItem = TyTraitItem; @@ -29,6 +32,10 @@ impl TyImplSelfOrTrait { } } +impl TyDeclParsedType for TyImplSelfOrTrait { + type ParsedType = ImplSelfOrTrait; +} + impl Named for TyImplSelfOrTrait { fn name(&self) -> &Ident { &self.trait_name.suffix diff --git a/sway-core/src/language/ty/declaration/storage.rs b/sway-core/src/language/ty/declaration/storage.rs index bc987290457..767d5728ee7 100644 --- a/sway-core/src/language/ty/declaration/storage.rs +++ b/sway-core/src/language/ty/declaration/storage.rs @@ -8,7 +8,7 @@ use sway_types::{Ident, Named, Span, Spanned}; use crate::{ engine_threading::*, - language::{ty::*, Visibility}, + language::{parsed::StorageDeclaration, ty::*, Visibility}, transform::{self}, type_system::*, Namespace, @@ -22,6 +22,10 @@ pub struct TyStorageDecl { pub storage_keyword: Ident, } +impl TyDeclParsedType for TyStorageDecl { + type ParsedType = StorageDeclaration; +} + impl Named for TyStorageDecl { fn name(&self) -> &Ident { &self.storage_keyword diff --git a/sway-core/src/language/ty/declaration/struct.rs b/sway-core/src/language/ty/declaration/struct.rs index 3ebd5c71771..d885f69c510 100644 --- a/sway-core/src/language/ty/declaration/struct.rs +++ b/sway-core/src/language/ty/declaration/struct.rs @@ -9,13 +9,15 @@ use crate::{ engine_threading::*, error::module_can_be_changed, has_changes, - language::{CallPath, Visibility}, + language::{parsed::StructDeclaration, CallPath, Visibility}, semantic_analysis::type_check_context::MonomorphizeHelper, transform, type_system::*, Namespace, }; +use super::TyDeclParsedType; + #[derive(Clone, Debug)] pub struct TyStructDecl { pub call_path: CallPath, @@ -26,6 +28,10 @@ pub struct TyStructDecl { pub attributes: transform::AttributesMap, } +impl TyDeclParsedType for TyStructDecl { + type ParsedType = StructDeclaration; +} + impl Named for TyStructDecl { fn name(&self) -> &Ident { &self.call_path.suffix diff --git a/sway-core/src/language/ty/declaration/trait.rs b/sway-core/src/language/ty/declaration/trait.rs index 91d3bc5e8a3..340c07520b3 100644 --- a/sway-core/src/language/ty/declaration/trait.rs +++ b/sway-core/src/language/ty/declaration/trait.rs @@ -13,7 +13,10 @@ use crate::{ }, engine_threading::*, has_changes, - language::{parsed, CallPath, Visibility}, + language::{ + parsed::{self, TraitDeclaration}, + CallPath, Visibility, + }, semantic_analysis::{ type_check_context::MonomorphizeHelper, TypeCheckAnalysis, TypeCheckAnalysisContext, TypeCheckFinalization, TypeCheckFinalizationContext, @@ -22,7 +25,7 @@ use crate::{ type_system::*, }; -use super::TyDecl; +use super::{TyDecl, TyDeclParsedType}; #[derive(Clone, Debug)] pub struct TyTraitDecl { @@ -38,6 +41,10 @@ pub struct TyTraitDecl { pub span: Span, } +impl TyDeclParsedType for TyTraitDecl { + type ParsedType = TraitDeclaration; +} + #[derive(Clone, Debug)] pub enum TyTraitInterfaceItem { TraitFn(DeclRefTraitFn), diff --git a/sway-core/src/language/ty/declaration/trait_fn.rs b/sway-core/src/language/ty/declaration/trait_fn.rs index d643da86fbb..deb72b137c7 100644 --- a/sway-core/src/language/ty/declaration/trait_fn.rs +++ b/sway-core/src/language/ty/declaration/trait_fn.rs @@ -8,7 +8,7 @@ use sway_types::{Ident, Named, Span, Spanned}; use crate::{ engine_threading::*, has_changes, - language::{ty::*, Purity}, + language::{parsed::TraitFn, ty::*, Purity}, semantic_analysis::type_check_context::MonomorphizeHelper, transform, type_system::*, @@ -24,6 +24,10 @@ pub struct TyTraitFn { pub attributes: transform::AttributesMap, } +impl TyDeclParsedType for TyTraitFn { + type ParsedType = TraitFn; +} + impl DebugWithEngines for TyTraitFn { fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result { write!( diff --git a/sway-core/src/language/ty/declaration/trait_type.rs b/sway-core/src/language/ty/declaration/trait_type.rs index d7d2368fcb2..515b579c133 100644 --- a/sway-core/src/language/ty/declaration/trait_type.rs +++ b/sway-core/src/language/ty/declaration/trait_type.rs @@ -5,7 +5,12 @@ use std::{ use sway_types::{Ident, Named, Span, Spanned}; -use crate::{engine_threading::*, has_changes, transform, type_system::*}; +use crate::{ + engine_threading::*, has_changes, language::parsed::TraitTypeDeclaration, transform, + type_system::*, +}; + +use super::TyDeclParsedType; #[derive(Clone, Debug)] pub struct TyTraitType { @@ -16,6 +21,10 @@ pub struct TyTraitType { pub span: Span, } +impl TyDeclParsedType for TyTraitType { + type ParsedType = TraitTypeDeclaration; +} + impl DebugWithEngines for TyTraitType { fn fmt(&self, f: &mut fmt::Formatter<'_>, _engines: &Engines) -> fmt::Result { write!(f, "{}", self.name) diff --git a/sway-core/src/language/ty/declaration/type_alias.rs b/sway-core/src/language/ty/declaration/type_alias.rs index ac72d15b322..ddb74aaca2d 100644 --- a/sway-core/src/language/ty/declaration/type_alias.rs +++ b/sway-core/src/language/ty/declaration/type_alias.rs @@ -4,11 +4,13 @@ use sway_types::{Ident, Named, Span, Spanned}; use crate::{ engine_threading::*, - language::{CallPath, Visibility}, + language::{parsed::TypeAliasDeclaration, CallPath, Visibility}, transform, type_system::*, }; +use super::TyDeclParsedType; + #[derive(Clone, Debug)] pub struct TyTypeAliasDecl { pub name: Ident, @@ -19,6 +21,10 @@ pub struct TyTypeAliasDecl { pub span: Span, } +impl TyDeclParsedType for TyTypeAliasDecl { + type ParsedType = TypeAliasDeclaration; +} + impl Named for TyTypeAliasDecl { fn name(&self) -> &Ident { &self.name diff --git a/sway-core/src/language/ty/declaration/variable.rs b/sway-core/src/language/ty/declaration/variable.rs index 87e6f808428..02adbfc408f 100644 --- a/sway-core/src/language/ty/declaration/variable.rs +++ b/sway-core/src/language/ty/declaration/variable.rs @@ -5,7 +5,7 @@ use sway_types::Ident; use crate::{ engine_threading::*, - language::ty::*, + language::{parsed::VariableDeclaration, ty::*}, semantic_analysis::{ TypeCheckAnalysis, TypeCheckAnalysisContext, TypeCheckFinalization, TypeCheckFinalizationContext, @@ -22,6 +22,10 @@ pub struct TyVariableDecl { pub type_ascription: TypeArgument, } +impl TyDeclParsedType for TyVariableDecl { + type ParsedType = VariableDeclaration; +} + impl EqWithEngines for TyVariableDecl {} impl PartialEqWithEngines for TyVariableDecl { fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool { diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs b/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs index f6c1cc1e0d1..5e1c6196964 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs @@ -4,7 +4,7 @@ use sway_error::error::CompileError; use sway_types::{Ident, Named, Span, Spanned}; use crate::{ - decl_engine::{DeclEngineInsert, DeclEngineInsertArc, DeclId}, + decl_engine::{DeclEngineGetParsedDeclId, DeclEngineInsert, DeclEngineInsertArc, DeclId}, language::ty::TyAbiDecl, namespace::{IsExtendingExistingImpl, IsImplSelf, TryInsertingTraitImplOnFailure}, semantic_analysis::{ @@ -119,7 +119,7 @@ impl ty::TyAbiDecl { } } new_interface_surface.push(ty::TyTraitInterfaceItem::TraitFn( - ctx.engines.de().insert(method.clone()), + ctx.engines.de().insert(method.clone(), Some(&decl_id)), )); method.name.clone() } @@ -127,7 +127,8 @@ impl ty::TyAbiDecl { let const_decl = engines.pe().get_constant(&decl_id).as_ref().clone(); let const_decl = ty::TyConstantDecl::type_check(handler, ctx.by_ref(), const_decl)?; - let decl_ref = ctx.engines.de().insert(const_decl.clone()); + let decl_ref = + ctx.engines.de().insert(const_decl.clone(), Some(&decl_id)); new_interface_surface .push(ty::TyTraitInterfaceItem::Constant(decl_ref.clone())); @@ -150,7 +151,8 @@ impl ty::TyAbiDecl { let type_decl = ty::TyTraitType::type_check(handler, ctx.by_ref(), type_decl)?; - let decl_ref = ctx.engines().de().insert(type_decl.clone()); + let decl_ref = + ctx.engines().de().insert(type_decl.clone(), Some(&decl_id)); new_interface_surface .push(ty::TyTraitInterfaceItem::Type(decl_ref.clone())); @@ -197,7 +199,9 @@ impl ty::TyAbiDecl { span: method.name.span(), }); } - new_items.push(TyTraitItem::Fn(ctx.engines.de().insert(method))); + new_items.push(TyTraitItem::Fn( + ctx.engines.de().insert(method, Some(&method_id)), + )); } // Compared to regular traits, we do not insert recursively methods of ABI supertraits @@ -288,12 +292,14 @@ impl ty::TyAbiDecl { } } all_items.push(TyImplItem::Fn( - ctx.engines - .de() - .insert(method.to_dummy_func( - AbiMode::ImplAbiFn(self.name.clone(), Some(self_decl_id)), - Some(type_id), - )) + decl_engine + .insert( + method.to_dummy_func( + AbiMode::ImplAbiFn(self.name.clone(), Some(self_decl_id)), + Some(type_id), + ), + None, + ) .with_parent(ctx.engines.de(), (*decl_ref.id()).into()), )); } @@ -349,21 +355,27 @@ impl ty::TyAbiDecl { } } all_items.push(TyImplItem::Fn( - ctx.engines - .de() - .insert_arc(method) + decl_engine + .insert_arc( + method, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ) .with_parent(ctx.engines.de(), (*decl_ref.id()).into()), )); } ty::TyTraitItem::Constant(decl_ref) => { let const_decl = decl_engine.get_constant(decl_ref); - all_items.push(TyImplItem::Constant( - ctx.engines.de().insert_arc(const_decl), - )); + all_items.push(TyImplItem::Constant(decl_engine.insert_arc( + const_decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ))); } ty::TyTraitItem::Type(decl_ref) => { let type_decl = decl_engine.get_type(decl_ref); - all_items.push(TyImplItem::Type(ctx.engines.de().insert_arc(type_decl))); + all_items.push(TyImplItem::Type(decl_engine.insert_arc( + type_decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ))); } } } diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/configurable.rs b/sway-core/src/semantic_analysis/ast_node/declaration/configurable.rs index f8ab7a73f67..7e318a17eb2 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/configurable.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/configurable.rs @@ -8,7 +8,7 @@ use sway_error::{ use sway_types::{style::is_screaming_snake_case, Spanned}; use crate::{ - decl_engine::{DeclEngineInsert, ReplaceDecls}, + decl_engine::{DeclEngineGetParsedDeclId, DeclEngineInsert, ReplaceDecls}, language::{ parsed::*, ty::{self, TyConfigurableDecl}, @@ -124,7 +124,8 @@ impl ty::TyConfigurableDecl { })?; let (decode_fn_ref, _, _): (crate::decl_engine::DeclRefFunction, _, _) = r?; - let mut decode_fn_decl = (*engines.de().get_function(&decode_fn_ref)).clone(); + let decode_fn_id = *decode_fn_ref.id(); + let mut decode_fn_decl = (*engines.de().get_function(&decode_fn_id)).clone(); let decl_mapping = crate::TypeParameter::gather_decl_mapping_from_trait_constraints( handler, ctx.by_ref(), @@ -135,8 +136,11 @@ impl ty::TyConfigurableDecl { decode_fn_decl.replace_decls(&decl_mapping, handler, &mut ctx)?; let decode_fn_ref = engines .de() - .insert(decode_fn_decl) - .with_parent(engines.de(), (*decode_fn_ref.id()).into()); + .insert( + decode_fn_decl, + engines.de().get_parsed_decl_id(&decode_fn_id).as_ref(), + ) + .with_parent(engines.de(), decode_fn_id.into()); (value, Some(decode_fn_ref)) } else { diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs index 8a972bc6b80..1c4b1d977b6 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -168,7 +168,9 @@ impl TyDecl { Ok(res) => res, Err(err) => return Ok(ty::TyDecl::ErrorRecovery(span, err)), }; - let typed_const_decl: ty::TyDecl = decl_engine.insert(const_decl.clone()).into(); + let typed_const_decl: ty::TyDecl = decl_engine + .insert(const_decl.clone(), Some(&decl_id)) + .into(); ctx.insert_symbol(handler, const_decl.name().clone(), typed_const_decl.clone())?; typed_const_decl } @@ -178,9 +180,9 @@ impl TyDecl { let name = decl.name.clone(); let typed_const_decl = match ty::TyConfigurableDecl::type_check(handler, ctx.by_ref(), decl) { - Ok(config_decl) => { - ty::TyDecl::from(decl_engine.insert(config_decl.clone())) - } + Ok(config_decl) => ty::TyDecl::from( + decl_engine.insert(config_decl.clone(), Some(&decl_id)), + ), Err(err) => ty::TyDecl::ErrorRecovery(span, err), }; ctx.insert_symbol(handler, name, typed_const_decl.clone())?; @@ -193,7 +195,8 @@ impl TyDecl { Ok(res) => res, Err(err) => return Ok(ty::TyDecl::ErrorRecovery(span, err)), }; - let typed_type_decl: ty::TyDecl = decl_engine.insert(type_decl.clone()).into(); + let typed_type_decl: ty::TyDecl = + decl_engine.insert(type_decl.clone(), Some(&decl_id)).into(); ctx.insert_symbol(handler, type_decl.name().clone(), typed_type_decl.clone())?; typed_type_decl } @@ -205,7 +208,7 @@ impl TyDecl { Err(err) => return Ok(ty::TyDecl::ErrorRecovery(span, err)), }; let call_path = enum_decl.call_path.clone(); - let decl: ty::TyDecl = decl_engine.insert(enum_decl).into(); + let decl: ty::TyDecl = decl_engine.insert(enum_decl, Some(&decl_id)).into(); ctx.insert_symbol(handler, call_path.suffix, decl.clone())?; decl @@ -233,7 +236,7 @@ impl TyDecl { }; let name = fn_decl.name.clone(); - let decl: ty::TyDecl = decl_engine.insert(fn_decl).into(); + let decl: ty::TyDecl = decl_engine.insert(fn_decl, Some(&decl_id)).into(); let _ = ctx.insert_symbol(handler, name, decl.clone()); decl } @@ -271,7 +274,9 @@ impl TyDecl { }); } - let decl: ty::TyDecl = decl_engine.insert(trait_decl.clone()).into(); + let decl: ty::TyDecl = decl_engine + .insert(trait_decl.clone(), Some(&decl_id)) + .into(); trait_decl .items @@ -291,6 +296,7 @@ impl TyDecl { let impl_trait_decl = match ty::TyImplSelfOrTrait::type_check_impl_self( handler, ctx.by_ref(), + &decl_id, impl_self_or_trait, ) { Ok(val) => val, @@ -386,7 +392,9 @@ impl TyDecl { IsImplSelf::No, IsExtendingExistingImpl::No, )?; - let impl_trait_decl: ty::TyDecl = decl_engine.insert(impl_trait.clone()).into(); + let impl_trait_decl: ty::TyDecl = decl_engine + .insert(impl_trait.clone(), Some(&decl_id)) + .into(); impl_trait.items.iter_mut().for_each(|item| { item.replace_implementing_type(engines, impl_trait_decl.clone()); }); @@ -403,7 +411,7 @@ impl TyDecl { } }; let call_path = decl.call_path.clone(); - let decl: ty::TyDecl = decl_engine.insert(decl).into(); + let decl: ty::TyDecl = decl_engine.insert(decl, Some(&decl_id)).into(); // insert the struct decl into namespace ctx.insert_symbol(handler, call_path.suffix, decl.clone())?; @@ -446,7 +454,7 @@ impl TyDecl { }); } - let decl: ty::TyDecl = decl_engine.insert(abi_decl.clone()).into(); + let decl: ty::TyDecl = decl_engine.insert(abi_decl.clone(), Some(&decl_id)).into(); abi_decl .items .iter_mut() @@ -554,7 +562,7 @@ impl TyDecl { attributes, storage_keyword, }; - let decl_ref = decl_engine.insert(decl); + let decl_ref = decl_engine.insert(decl, Some(&decl_id)); // insert the storage declaration into the symbols // if there already was one, return an error that duplicate storage @@ -595,7 +603,7 @@ impl TyDecl { span, }; - let decl: ty::TyDecl = decl_engine.insert(decl).into(); + let decl: ty::TyDecl = decl_engine.insert(decl, Some(&decl_id)).into(); // insert the type alias name and decl into namespace ctx.insert_symbol(handler, name, decl.clone())?; diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs index 38646cd29e6..e65406772fa 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs @@ -11,7 +11,7 @@ use sway_error::{ use sway_types::{Ident, Span, Spanned}; use crate::{ - decl_engine::*, + decl_engine::{parsed_id::ParsedDeclId, *}, engine_threading::*, language::{ parsed::*, @@ -283,6 +283,7 @@ impl TyImplSelfOrTrait { pub(crate) fn type_check_impl_self( handler: &Handler, ctx: TypeCheckContext, + parsed_decl_id: &ParsedDeclId, impl_self: ImplSelfOrTrait, ) -> Result { let ImplSelfOrTrait { @@ -395,7 +396,9 @@ impl TyImplSelfOrTrait { Ok(res) => res, Err(_) => continue, }; - new_items.push(TyImplItem::Fn(decl_engine.insert(fn_decl))); + new_items.push(TyImplItem::Fn( + decl_engine.insert(fn_decl, Some(fn_decl_id)), + )); } ImplItem::Constant(decl_id) => { let const_decl = @@ -408,7 +411,7 @@ impl TyImplSelfOrTrait { Ok(res) => res, Err(_) => continue, }; - let decl_ref = decl_engine.insert(const_decl); + let decl_ref = decl_engine.insert(const_decl, Some(decl_id)); new_items.push(TyImplItem::Constant(decl_ref.clone())); ctx.insert_symbol( @@ -430,7 +433,7 @@ impl TyImplSelfOrTrait { Ok(res) => res, Err(_) => continue, }; - let decl_ref = decl_engine.insert(type_decl); + let decl_ref = decl_engine.insert(type_decl, Some(decl_id)); new_items.push(TyImplItem::Type(decl_ref.clone())); } } @@ -489,7 +492,9 @@ impl TyImplSelfOrTrait { } } - let impl_trait_decl = decl_engine.insert(impl_trait.clone()).into(); + let impl_trait_decl = decl_engine + .insert(impl_trait.clone(), Some(parsed_decl_id)) + .into(); // First lets perform an analysis pass. // This returns a vector with ordered indexes to the items in the order that they @@ -540,7 +545,9 @@ impl TyImplSelfOrTrait { } } - let impl_trait_decl: ty::TyDecl = decl_engine.insert(impl_trait.clone()).into(); + let impl_trait_decl: ty::TyDecl = decl_engine + .insert(impl_trait.clone(), Some(parsed_decl_id)) + .into(); let mut finalizing_ctx = TypeCheckFinalizationContext::new(ctx.engines, ctx.by_ref()); @@ -789,7 +796,7 @@ fn type_check_trait_implementation( type_checklist.remove(&name); // Add this type to the "impld decls". - let decl_ref = decl_engine.insert(type_decl.clone()); + let decl_ref = decl_engine.insert(type_decl.clone(), Some(decl_id)); impld_item_refs.insert((name, implementing_for), TyTraitItem::Type(decl_ref)); let old_type_decl_info1 = TypeInfo::TraitType { @@ -854,7 +861,7 @@ fn type_check_trait_implementation( method_checklist.remove(&name); // Add this method to the "impld items". - let decl_ref = decl_engine.insert(impl_method); + let decl_ref = decl_engine.insert(impl_method, Some(impl_method_id)); impld_item_refs.insert((name, implementing_for), TyTraitItem::Fn(decl_ref)); } ImplItem::Constant(decl_id) => { @@ -877,7 +884,7 @@ fn type_check_trait_implementation( constant_checklist.remove(&name); // Add this constant to the "impld decls". - let decl_ref = decl_engine.insert(const_decl); + let decl_ref = decl_engine.insert(const_decl, Some(decl_id)); impld_item_refs.insert((name, implementing_for), TyTraitItem::Constant(decl_ref)); } ImplItem::Type(_) => {} @@ -943,7 +950,10 @@ fn type_check_trait_implementation( method.subst(&type_mapping, engines); all_items_refs.push(TyImplItem::Fn( decl_engine - .insert(method) + .insert( + method, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ) .with_parent(decl_engine, (*decl_ref.id()).into()), )); } @@ -951,12 +961,18 @@ fn type_check_trait_implementation( let mut const_decl = (*decl_engine.get_constant(decl_ref)).clone(); const_decl.replace_decls(&decl_mapping, handler, &mut ctx)?; const_decl.subst(&type_mapping, engines); - all_items_refs.push(TyImplItem::Constant(decl_engine.insert(const_decl))); + all_items_refs.push(TyImplItem::Constant(decl_engine.insert( + const_decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ))); } TyImplItem::Type(decl_ref) => { let mut type_decl = (*decl_engine.get_type(decl_ref)).clone(); type_decl.subst(&type_mapping, engines); - all_items_refs.push(TyImplItem::Type(decl_engine.insert(type_decl.clone()))); + all_items_refs.push(TyImplItem::Type(decl_engine.insert( + type_decl.clone(), + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ))); } } } diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs index 253b67ad5a4..efa7687355d 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs @@ -88,7 +88,7 @@ impl TyTraitDecl { let type_decl = engines.pe().get_trait_type(&decl_id).as_ref().clone(); let type_decl = ty::TyTraitType::type_check(handler, ctx.by_ref(), type_decl.clone())?; - let decl_ref = decl_engine.insert(type_decl.clone()); + let decl_ref = decl_engine.insert(type_decl.clone(), Some(&decl_id)); dummy_interface_surface.push(ty::TyImplItem::Type(decl_ref.clone())); new_interface_surface .push(ty::TyTraitInterfaceItem::Type(decl_ref.clone())); @@ -128,10 +128,13 @@ impl TyTraitDecl { TraitItem::TraitFn(decl_id) => { let method = engines.pe().get_trait_fn(&decl_id); let method = ty::TyTraitFn::type_check(handler, ctx.by_ref(), &method)?; - let decl_ref = decl_engine.insert(method.clone()); + let decl_ref = decl_engine.insert(method.clone(), Some(&decl_id)); dummy_interface_surface.push(ty::TyImplItem::Fn( decl_engine - .insert(method.to_dummy_func(AbiMode::NonAbi, Some(self_type))) + .insert( + method.to_dummy_func(AbiMode::NonAbi, Some(self_type)), + None, + ) .with_parent(decl_engine, (*decl_ref.id()).into()), )); new_interface_surface.push(ty::TyTraitInterfaceItem::TraitFn(decl_ref)); @@ -141,7 +144,7 @@ impl TyTraitDecl { let const_decl = engines.pe().get_constant(&decl_id).as_ref().clone(); let const_decl = ty::TyConstantDecl::type_check(handler, ctx.by_ref(), const_decl)?; - let decl_ref = ctx.engines.de().insert(const_decl.clone()); + let decl_ref = ctx.engines.de().insert(const_decl.clone(), Some(&decl_id)); new_interface_surface .push(ty::TyTraitInterfaceItem::Constant(decl_ref.clone())); @@ -199,7 +202,9 @@ impl TyTraitDecl { Some(self_type_param.type_id), ) .unwrap_or_else(|_| ty::TyFunctionDecl::error(&method)); - new_items.push(ty::TyTraitItem::Fn(decl_engine.insert(method))); + new_items.push(ty::TyTraitItem::Fn( + decl_engine.insert(method, Some(&method_decl_id)), + )); } let typed_trait_decl = ty::TyTraitDecl { @@ -352,7 +357,10 @@ impl TyTraitDecl { let name = method.name.clone(); let r = if method.subst(&type_mapping, engines).has_changes() { let new_ref = decl_engine - .insert(method) + .insert( + method, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ) .with_parent(decl_engine, (*decl_ref.id()).into()); new_ref } else { @@ -364,7 +372,10 @@ impl TyTraitDecl { let mut const_decl = (*decl_engine.get_constant(&decl_ref)).clone(); let name = const_decl.call_path.suffix.clone(); let r = if const_decl.subst(&type_mapping, engines).has_changes() { - decl_engine.insert(const_decl) + decl_engine.insert( + const_decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ) } else { decl_ref.clone() }; @@ -374,7 +385,8 @@ impl TyTraitDecl { let mut t = (*decl_engine.get_type(&decl_ref)).clone(); let name = t.name.clone(); let r = if t.subst(&type_mapping, engines).has_changes() { - decl_engine.insert(t) + decl_engine + .insert(t, decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref()) } else { decl_ref.clone() }; @@ -426,9 +438,8 @@ impl TyTraitDecl { let mut method = (*decl_engine.get_trait_fn(decl_ref)).clone(); method.subst(&type_mapping, engines); all_items.push(TyImplItem::Fn( - ctx.engines - .de() - .insert(method.to_dummy_func(AbiMode::NonAbi, Some(type_id))) + decl_engine + .insert(method.to_dummy_func(AbiMode::NonAbi, Some(type_id)), None) .with_parent(ctx.engines.de(), (*decl_ref.id()).into()), )); } @@ -457,19 +468,28 @@ impl TyTraitDecl { all_items.push(TyImplItem::Fn( ctx.engines .de() - .insert(method) - .with_parent(ctx.engines.de(), (*decl_ref.id()).into()), + .insert( + method, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ) + .with_parent(decl_engine, (*decl_ref.id()).into()), )); } ty::TyTraitItem::Constant(decl_ref) => { let mut const_decl = (*decl_engine.get_constant(decl_ref)).clone(); const_decl.subst(&type_mapping, engines); - all_items.push(TyImplItem::Constant(ctx.engines.de().insert(const_decl))); + all_items.push(TyImplItem::Constant(decl_engine.insert( + const_decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ))); } ty::TyTraitItem::Type(decl_ref) => { let mut type_decl = (*decl_engine.get_type(decl_ref)).clone(); type_decl.subst(&type_mapping, engines); - all_items.push(TyImplItem::Type(ctx.engines.de().insert(type_decl))); + all_items.push(TyImplItem::Type(decl_engine.insert( + type_decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ))); } } } diff --git a/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_scrutinee.rs b/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_scrutinee.rs index ee819ba8564..715e6e21da2 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_scrutinee.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_scrutinee.rs @@ -6,7 +6,7 @@ use sway_error::{ use sway_types::{BaseIdent, Ident, Span, Spanned}; use crate::{ - decl_engine::DeclEngineInsert, + decl_engine::{DeclEngineGetParsedDeclId, DeclEngineInsert}, language::{ parsed::*, ty::{self, StructAccessInfo, TyDecl, TyScrutinee, TyStructDecl, TyStructField}, @@ -414,7 +414,10 @@ fn type_check_struct( Ok(()) })?; - let struct_ref = decl_engine.insert(struct_decl); + let struct_ref = decl_engine.insert( + struct_decl, + decl_engine.get_parsed_decl_id(&struct_id).as_ref(), + ); let typed_scrutinee = ty::TyScrutinee { type_id: type_engine.insert( ctx.engines(), @@ -479,7 +482,7 @@ fn type_check_enum( let engines = ctx.engines(); let mut prefixes = call_path.prefixes.clone(); - let (callsite_span, mut enum_decl, call_path_decl) = match prefixes.pop() { + let (callsite_span, enum_id, call_path_decl) = match prefixes.pop() { Some(enum_name) => { let enum_callpath = CallPath { suffix: enum_name, @@ -493,12 +496,8 @@ fn type_check_enum( &enum_callpath, ctx.self_type(), )?; - let enum_ref = unknown_decl.to_enum_id(handler, ctx.engines())?; - ( - enum_callpath.span(), - (*decl_engine.get_enum(&enum_ref)).clone(), - unknown_decl, - ) + let enum_id = unknown_decl.to_enum_id(handler, ctx.engines())?; + (enum_callpath.span(), enum_id, unknown_decl) } None => { // we may have an imported variant @@ -509,11 +508,7 @@ fn type_check_enum( ctx.self_type(), )?; if let TyDecl::EnumVariantDecl(ty::EnumVariantDecl { enum_ref, .. }) = decl.clone() { - ( - call_path.suffix.span(), - (*decl_engine.get_enum(enum_ref.id())).clone(), - decl, - ) + (call_path.suffix.span(), *enum_ref.id(), decl) } else { return Err(handler.emit_err(CompileError::EnumNotFound { name: call_path.suffix.clone(), @@ -522,6 +517,7 @@ fn type_check_enum( } } }; + let mut enum_decl = (*decl_engine.get_enum(&enum_id)).clone(); let variant_name = call_path.suffix.clone(); // monomorphize the enum definition @@ -541,7 +537,7 @@ fn type_check_enum( // type check the nested scrutinee let typed_value = ty::TyScrutinee::type_check(handler, ctx, value)?; - let enum_ref = decl_engine.insert(enum_decl); + let enum_ref = decl_engine.insert(enum_decl, decl_engine.get_parsed_decl_id(&enum_id).as_ref()); let typed_scrutinee = ty::TyScrutinee { variant: ty::TyScrutineeVariant::EnumScrutinee { enum_ref: enum_ref.clone(), diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index 742b24f4fef..88a027a343a 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs @@ -1132,7 +1132,12 @@ impl ty::TyExpression { // Update `access_type` to be the type of the monomorphized struct after inserting it // into the type engine - let storage_key_struct_decl_ref = ctx.engines().de().insert(storage_key_struct_decl); + let storage_key_struct_decl_ref = decl_engine.insert( + storage_key_struct_decl, + decl_engine + .get_parsed_decl_id(&storage_key_struct_decl_ref) + .as_ref(), + ); access_type = type_engine.insert( engines, TypeInfo::Struct(*storage_key_struct_decl_ref.id()), @@ -1722,20 +1727,32 @@ impl ty::TyExpression { let method = decl_engine.get_trait_fn(decl_ref); abi_items.push(TyImplItem::Fn( decl_engine - .insert(method.to_dummy_func( - AbiMode::ImplAbiFn(abi_name.suffix.clone(), Some(*abi_ref.id())), - Some(return_type), - )) + .insert( + method.to_dummy_func( + AbiMode::ImplAbiFn( + abi_name.suffix.clone(), + Some(*abi_ref.id()), + ), + Some(return_type), + ), + None, + ) .with_parent(decl_engine, (*decl_ref.id()).into()), )); } ty::TyTraitInterfaceItem::Constant(decl_ref) => { let const_decl = decl_engine.get_constant(decl_ref); - abi_items.push(TyImplItem::Constant(decl_engine.insert_arc(const_decl))); + abi_items.push(TyImplItem::Constant(decl_engine.insert_arc( + const_decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ))); } ty::TyTraitInterfaceItem::Type(decl_ref) => { let type_decl = decl_engine.get_type(decl_ref); - abi_items.push(TyImplItem::Type(decl_engine.insert_arc(type_decl))); + abi_items.push(TyImplItem::Type(decl_engine.insert_arc( + type_decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ))); } } } diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/function_application.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/function_application.rs index 6b9c0aecbfa..ae526f6ef15 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/function_application.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/function_application.rs @@ -1,5 +1,7 @@ use crate::{ - decl_engine::{DeclEngineInsert, DeclRefFunction, ReplaceDecls}, + decl_engine::{ + engine::DeclEngineGetParsedDeclId, DeclEngineInsert, DeclRefFunction, ReplaceDecls, + }, language::{ ty::{self, TyFunctionSig}, *, @@ -97,7 +99,12 @@ pub(crate) fn instantiate_function_application( let function_is_type_check_finalized = function_decl.is_type_check_finalized; let function_is_trait_method_dummy = function_decl.is_trait_method_dummy; let new_decl_ref = decl_engine - .insert(function_decl) + .insert( + function_decl, + decl_engine + .get_parsed_decl_id(function_decl_ref.id()) + .as_ref(), + ) .with_parent(decl_engine, (*function_decl_ref.id()).into()); if method_sig.is_concrete(engines) diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs index ba2a5ac861f..04e88818497 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs @@ -1,6 +1,6 @@ use crate::{ decl_engine::{ - engine::{DeclEngineGet, DeclEngineReplace}, + engine::{DeclEngineGet, DeclEngineGetParsedDeclId, DeclEngineReplace}, DeclEngineInsert, DeclRefFunction, ReplaceDecls, UpdateConstantExpression, }, language::{ @@ -940,7 +940,10 @@ pub(crate) fn monomorphize_method( } let decl_ref = decl_engine - .insert(func_decl) + .insert( + func_decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ) .with_parent(decl_engine, (*decl_ref.id()).into()); Ok(decl_ref) diff --git a/sway-core/src/semantic_analysis/namespace/trait_map.rs b/sway-core/src/semantic_analysis/namespace/trait_map.rs index 2cd7d0dd3d6..9cc2880a73b 100644 --- a/sway-core/src/semantic_analysis/namespace/trait_map.rs +++ b/sway-core/src/semantic_analysis/namespace/trait_map.rs @@ -14,7 +14,7 @@ use sway_error::{ use sway_types::{BaseIdent, Ident, Span, Spanned}; use crate::{ - decl_engine::{DeclEngineGet, DeclEngineInsert}, + decl_engine::{DeclEngineGet, DeclEngineGetParsedDeclId, DeclEngineInsert}, engine_threading::*, language::{ parsed::ImplItem, @@ -816,7 +816,12 @@ impl TraitMap { } else { decl.subst(&type_mapping, engines); let new_ref = decl_engine - .insert(decl) + .insert( + decl, + decl_engine + .get_parsed_decl_id(decl_ref.id()) + .as_ref(), + ) .with_parent(decl_engine, decl_ref.id().into()); Some(( name, @@ -827,7 +832,10 @@ impl TraitMap { ty::TyTraitItem::Constant(decl_ref) => { let mut decl = (*decl_engine.get(decl_ref.id())).clone(); decl.subst(&type_mapping, engines); - let new_ref = decl_engine.insert(decl); + let new_ref = decl_engine.insert( + decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ); Some(( name, ResolvedTraitImplItem::Typed(TyImplItem::Constant(new_ref)), @@ -836,7 +844,10 @@ impl TraitMap { ty::TyTraitItem::Type(decl_ref) => { let mut decl = (*decl_engine.get(decl_ref.id())).clone(); decl.subst(&type_mapping, engines); - let new_ref = decl_engine.insert(decl); + let new_ref = decl_engine.insert( + decl, + decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), + ); Some(( name, ResolvedTraitImplItem::Typed(TyImplItem::Type(new_ref)), diff --git a/sway-core/src/semantic_analysis/type_check_context.rs b/sway-core/src/semantic_analysis/type_check_context.rs index b25992ba437..959499bf3f3 100644 --- a/sway-core/src/semantic_analysis/type_check_context.rs +++ b/sway-core/src/semantic_analysis/type_check_context.rs @@ -2,7 +2,7 @@ use std::collections::{HashMap, VecDeque}; use crate::{ build_config::ExperimentalFlags, - decl_engine::{DeclEngineGet, DeclEngineInsert, DeclRefFunction}, + decl_engine::{DeclEngineGet, DeclEngineGetParsedDeclId, DeclEngineInsert, DeclRefFunction}, engine_threading::*, language::{ parsed::TreeType, @@ -904,7 +904,10 @@ impl<'a> TypeCheckContext<'a> { )?; // insert the new copy in the decl engine - let new_decl_ref = decl_engine.insert(new_copy); + let new_decl_ref = decl_engine.insert( + new_copy, + decl_engine.get_parsed_decl_id(&original_id).as_ref(), + ); // create the type id from the copy type_engine.insert( @@ -931,7 +934,10 @@ impl<'a> TypeCheckContext<'a> { )?; // insert the new copy in the decl engine - let new_decl_ref = decl_engine.insert(new_copy); + let new_decl_ref = decl_engine.insert( + new_copy, + decl_engine.get_parsed_decl_id(&original_id).as_ref(), + ); // create the type id from the copy type_engine.insert( diff --git a/sway-core/src/type_system/ast_elements/binding.rs b/sway-core/src/type_system/ast_elements/binding.rs index a2bbf266f43..699a99470ba 100644 --- a/sway-core/src/type_system/ast_elements/binding.rs +++ b/sway-core/src/type_system/ast_elements/binding.rs @@ -3,7 +3,7 @@ use sway_error::handler::{ErrorEmitted, Handler}; use sway_types::{Span, Spanned}; use crate::{ - decl_engine::{DeclEngineInsert, DeclId, DeclRef}, + decl_engine::{DeclEngineGetParsedDeclId, DeclEngineInsert, DeclId, DeclRef}, engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext}, language::{ty, CallPath, QualifiedCallPath}, semantic_analysis::{type_check_context::EnforceTypeArguments, TypeCheckContext}, @@ -299,10 +299,11 @@ impl TypeCheckTypeBinding for TypeBinding { } } // Insert the new copy into the declaration engine. - let new_fn_ref = ctx - .engines - .de() - .insert(new_copy) + let new_fn_ref = decl_engine + .insert( + new_copy, + decl_engine.get_parsed_decl_id(fn_ref.id()).as_ref(), + ) .with_parent(ctx.engines.de(), fn_ref.id().into()); Ok((new_fn_ref, None, None)) } @@ -339,7 +340,10 @@ impl TypeCheckTypeBinding for TypeBinding { &self.span, )?; // Insert the new copy into the declaration engine. - let new_struct_ref = ctx.engines.de().insert(new_copy); + let new_struct_ref = decl_engine.insert( + new_copy, + decl_engine.get_parsed_decl_id(&struct_id).as_ref(), + ); let type_id = type_engine.insert( engines, TypeInfo::Struct(*new_struct_ref.id()), @@ -369,18 +373,17 @@ impl TypeCheckTypeBinding for TypeBinding { let unknown_decl = ctx.resolve_call_path_with_visibility_check(handler, &self.inner)?; // Get a new copy from the declaration engine. - let mut new_copy = if let ty::TyDecl::EnumVariantDecl(ty::EnumVariantDecl { - enum_ref, - .. - }) = &unknown_decl + let enum_id = if let ty::TyDecl::EnumVariantDecl(ty::EnumVariantDecl { enum_ref, .. }) = + &unknown_decl { - (*decl_engine.get_enum(enum_ref.id())).clone() + *enum_ref.id() } else { // Check to see if this is a enum declaration. - let enum_id = unknown_decl.to_enum_id(handler, engines)?; - (*decl_engine.get_enum(&enum_id)).clone() + unknown_decl.to_enum_id(handler, engines)? }; + let mut new_copy = (*decl_engine.get_enum(&enum_id)).clone(); + // Monomorphize the copy, in place. ctx.monomorphize( handler, @@ -390,7 +393,8 @@ impl TypeCheckTypeBinding for TypeBinding { &self.span, )?; // Insert the new copy into the declaration engine. - let new_enum_ref = ctx.engines.de().insert(new_copy); + let new_enum_ref = + decl_engine.insert(new_copy, decl_engine.get_parsed_decl_id(&enum_id).as_ref()); let type_id = type_engine.insert( engines, TypeInfo::Enum(*new_enum_ref.id()), diff --git a/sway-core/src/type_system/mod.rs b/sway-core/src/type_system/mod.rs index ea002bb87fa..85f21f9e132 100644 --- a/sway-core/src/type_system/mod.rs +++ b/sway-core/src/type_system/mod.rs @@ -84,14 +84,17 @@ fn generic_enum_resolution() { let mut call_path: CallPath = result_name.clone().into(); call_path.is_absolute = true; - let decl_ref_1 = engines.de().insert(TyEnumDecl { - call_path, - type_parameters: vec![placeholder_type_param], - variants: variant_types, - span: sp.clone(), - visibility: crate::language::Visibility::Public, - attributes: AttributesMap::default(), - }); + let decl_ref_1 = engines.de().insert( + TyEnumDecl { + call_path, + type_parameters: vec![placeholder_type_param], + variants: variant_types, + span: sp.clone(), + visibility: crate::language::Visibility::Public, + attributes: AttributesMap::default(), + }, + None, + ); let ty_1 = engines .te() .insert(&engines, TypeInfo::Enum(*decl_ref_1.id()), None); @@ -125,14 +128,17 @@ fn generic_enum_resolution() { let mut call_path: CallPath = result_name.into(); call_path.is_absolute = true; - let decl_ref_2 = engines.de().insert(TyEnumDecl { - call_path, - type_parameters: vec![type_param], - variants: variant_types.clone(), - span: sp.clone(), - visibility: crate::language::Visibility::Public, - attributes: AttributesMap::default(), - }); + let decl_ref_2 = engines.de().insert( + TyEnumDecl { + call_path, + type_parameters: vec![type_param], + variants: variant_types.clone(), + span: sp.clone(), + visibility: crate::language::Visibility::Public, + attributes: AttributesMap::default(), + }, + None, + ); let ty_2 = engines .te() .insert(&engines, TypeInfo::Enum(*decl_ref_2.id()), None); diff --git a/sway-core/src/type_system/substitute/subst_map.rs b/sway-core/src/type_system/substitute/subst_map.rs index 5d6b884d229..3d917bc5f43 100644 --- a/sway-core/src/type_system/substitute/subst_map.rs +++ b/sway-core/src/type_system/substitute/subst_map.rs @@ -1,5 +1,5 @@ use crate::{ - decl_engine::{DeclEngine, DeclEngineInsert}, + decl_engine::{DeclEngine, DeclEngineGetParsedDeclId, DeclEngineInsert}, engine_threading::{ DebugWithEngines, Engines, PartialEqWithEngines, PartialEqWithEnginesContext, }, @@ -342,8 +342,8 @@ impl TypeSubstMap { TypeInfo::UnknownGeneric { .. } => iter_for_match(engines, self, &type_info), TypeInfo::Placeholder(_) => iter_for_match(engines, self, &type_info), TypeInfo::TypeParam(_) => None, - TypeInfo::Struct(decl_ref) => { - let mut decl = (*decl_engine.get_struct(&decl_ref)).clone(); + TypeInfo::Struct(decl_id) => { + let mut decl = (*decl_engine.get_struct(&decl_id)).clone(); let mut need_to_create_new = false; for field in &mut decl.fields { if let Some(type_id) = self.find_match(field.type_argument.type_id, engines) { @@ -358,7 +358,8 @@ impl TypeSubstMap { } } if need_to_create_new { - let new_decl_ref = decl_engine.insert(decl); + let new_decl_ref = + decl_engine.insert(decl, decl_engine.get_parsed_decl_id(&decl_id).as_ref()); Some(type_engine.insert( engines, TypeInfo::Struct(*new_decl_ref.id()), @@ -368,8 +369,8 @@ impl TypeSubstMap { None } } - TypeInfo::Enum(decl_ref) => { - let mut decl = (*decl_engine.get_enum(&decl_ref)).clone(); + TypeInfo::Enum(decl_id) => { + let mut decl = (*decl_engine.get_enum(&decl_id)).clone(); let mut need_to_create_new = false; for variant in &mut decl.variants { @@ -386,7 +387,8 @@ impl TypeSubstMap { } } if need_to_create_new { - let new_decl_ref = decl_engine.insert(decl); + let new_decl_ref = + decl_engine.insert(decl, decl_engine.get_parsed_decl_id(&decl_id).as_ref()); Some(type_engine.insert( engines, TypeInfo::Enum(*new_decl_ref.id()),