Skip to content

Commit

Permalink
Adds SubstTypesContext with optional subst_codeblocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
esdrubal committed Aug 26, 2024
1 parent e4c907e commit 52b34c5
Show file tree
Hide file tree
Showing 37 changed files with 407 additions and 236 deletions.
54 changes: 27 additions & 27 deletions sway-core/src/decl_engine/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ where
}

impl SubstTypes for DeclId<TyFunctionDecl> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
let decl_engine = engines.de();
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
let decl_engine = ctx.engines.de();
let mut decl = (*decl_engine.get(self)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
decl_engine.replace(*self, decl);
HasChanges::Yes
} else {
Expand All @@ -155,10 +155,10 @@ impl SubstTypes for DeclId<TyFunctionDecl> {
}
}
impl SubstTypes for DeclId<TyTraitDecl> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
let decl_engine = engines.de();
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
let decl_engine = ctx.engines.de();
let mut decl = (*decl_engine.get(self)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
decl_engine.replace(*self, decl);
HasChanges::Yes
} else {
Expand All @@ -167,10 +167,10 @@ impl SubstTypes for DeclId<TyTraitDecl> {
}
}
impl SubstTypes for DeclId<TyTraitFn> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
let decl_engine = engines.de();
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
let decl_engine = ctx.engines.de();
let mut decl = (*decl_engine.get(self)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
decl_engine.replace(*self, decl);
HasChanges::Yes
} else {
Expand All @@ -179,10 +179,10 @@ impl SubstTypes for DeclId<TyTraitFn> {
}
}
impl SubstTypes for DeclId<TyImplSelfOrTrait> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
let decl_engine = engines.de();
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
let decl_engine = ctx.engines.de();
let mut decl = (*decl_engine.get(self)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
decl_engine.replace(*self, decl);
HasChanges::Yes
} else {
Expand All @@ -191,10 +191,10 @@ impl SubstTypes for DeclId<TyImplSelfOrTrait> {
}
}
impl SubstTypes for DeclId<TyStructDecl> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
let decl_engine = engines.de();
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
let decl_engine = ctx.engines.de();
let mut decl = (*decl_engine.get(self)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
decl_engine.replace(*self, decl);
HasChanges::Yes
} else {
Expand All @@ -203,10 +203,10 @@ impl SubstTypes for DeclId<TyStructDecl> {
}
}
impl SubstTypes for DeclId<TyEnumDecl> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
let decl_engine = engines.de();
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
let decl_engine = ctx.engines.de();
let mut decl = (*decl_engine.get(self)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
decl_engine.replace(*self, decl);
HasChanges::Yes
} else {
Expand All @@ -215,10 +215,10 @@ impl SubstTypes for DeclId<TyEnumDecl> {
}
}
impl SubstTypes for DeclId<TyTypeAliasDecl> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
let decl_engine = engines.de();
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
let decl_engine = ctx.engines.de();
let mut decl = (*decl_engine.get(self)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
decl_engine.replace(*self, decl);
HasChanges::Yes
} else {
Expand All @@ -228,10 +228,10 @@ impl SubstTypes for DeclId<TyTypeAliasDecl> {
}

impl SubstTypes for DeclId<TyTraitType> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
let decl_engine = engines.de();
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
let decl_engine = ctx.engines.de();
let mut decl = (*decl_engine.get(self)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
decl_engine.replace(*self, decl);
HasChanges::Yes
} else {
Expand All @@ -248,11 +248,11 @@ where
pub(crate) fn subst_types_and_insert_new(
&self,
type_mapping: &TypeSubstMap,
engines: &Engines,
ctx: &SubstTypesContext,
) -> Option<DeclRef<Self>> {
let decl_engine = engines.de();
let decl_engine = ctx.engines.de();
let mut decl = (*decl_engine.get(self)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
Some(decl_engine.insert(decl, decl_engine.get_parsed_decl_id(self).as_ref()))
} else {
None
Expand Down
26 changes: 13 additions & 13 deletions sway-core/src/decl_engine/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ where
pub(crate) fn subst_types_and_insert_new(
&self,
type_mapping: &TypeSubstMap,
engines: &Engines,
ctx: &SubstTypesContext,
) -> Option<Self> {
let decl_engine = engines.de();
if type_mapping.source_ids_contains_concrete_type(engines)
|| !decl_engine.get(&self.id).is_concrete(engines)
let decl_engine = ctx.engines.de();
if type_mapping.source_ids_contains_concrete_type(ctx.engines)
|| !decl_engine.get(&self.id).is_concrete(ctx.engines)
{
let mut decl = (*decl_engine.get(&self.id)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
Some(decl_engine.insert(decl, decl_engine.get_parsed_decl_id(&self.id).as_ref()))
} else {
None
Expand Down Expand Up @@ -144,14 +144,14 @@ where
pub(crate) fn subst_types_and_insert_new_with_parent(
&self,
type_mapping: &TypeSubstMap,
engines: &Engines,
ctx: &SubstTypesContext,
) -> Option<Self> {
let decl_engine = engines.de();
if type_mapping.source_ids_contains_concrete_type(engines)
|| !decl_engine.get(&self.id).is_concrete(engines)
let decl_engine = ctx.engines.de();
if type_mapping.source_ids_contains_concrete_type(ctx.engines)
|| !decl_engine.get(&self.id).is_concrete(ctx.engines)
{
let mut decl = (*decl_engine.get(&self.id)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
Some(
decl_engine
.insert(decl, decl_engine.get_parsed_decl_id(&self.id).as_ref())
Expand Down Expand Up @@ -269,10 +269,10 @@ where
DeclEngine: DeclEngineIndex<T>,
T: Named + Spanned + SubstTypes + Clone,
{
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
let decl_engine = engines.de();
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
let decl_engine = ctx.engines.de();
let mut decl = (*decl_engine.get(&self.id)).clone();
if decl.subst(type_mapping, engines).has_changes() {
if decl.subst(type_mapping, ctx).has_changes() {
decl_engine.replace(self.id, decl);
HasChanges::Yes
} else {
Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/language/ty/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl DebugWithEngines for TyAstNode {
}

impl SubstTypes for TyAstNode {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
match self.content {
TyAstNodeContent::Declaration(ref mut decl) => decl.subst(type_mapping, engines),
TyAstNodeContent::Expression(ref mut expr) => expr.subst(type_mapping, engines),
TyAstNodeContent::Declaration(ref mut decl) => decl.subst(type_mapping, ctx),
TyAstNodeContent::Expression(ref mut expr) => expr.subst(type_mapping, ctx),
TyAstNodeContent::SideEffect(_) | TyAstNodeContent::Error(_, _) => HasChanges::No,
}
}
Expand Down
8 changes: 6 additions & 2 deletions sway-core/src/language/ty/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ impl HashWithEngines for TyCodeBlock {
}

impl SubstTypes for TyCodeBlock {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
self.contents.subst(type_mapping, engines)
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
if ctx.subst_codeblocks {
self.contents.subst(type_mapping, ctx)
} else {
HasChanges::No
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions sway-core/src/language/ty/declaration/configurable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ impl Spanned for TyConfigurableDecl {
}

impl SubstTypes for TyConfigurableDecl {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
has_changes! {
self.return_type.subst(type_mapping, engines);
self.type_ascription.subst(type_mapping, engines);
self.value.subst(type_mapping, engines);
self.return_type.subst(type_mapping, ctx);
self.type_ascription.subst(type_mapping, ctx);
self.value.subst(type_mapping, ctx);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions sway-core/src/language/ty/declaration/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ impl IsConcrete for TyConstantDecl {
}

impl SubstTypes for TyConstantDecl {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
has_changes! {
self.return_type.subst(type_mapping, engines);
self.type_ascription.subst(type_mapping, engines);
self.value.subst(type_mapping, engines);
self.return_type.subst(type_mapping, ctx);
self.type_ascription.subst(type_mapping, ctx);
self.value.subst(type_mapping, ctx);
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions sway-core/src/language/ty/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,33 +250,33 @@ impl HashWithEngines for TyDecl {
}

impl SubstTypes for TyDecl {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
match self {
TyDecl::VariableDecl(ref mut var_decl) => var_decl.subst(type_mapping, engines),
TyDecl::VariableDecl(ref mut var_decl) => var_decl.subst(type_mapping, ctx),
TyDecl::FunctionDecl(FunctionDecl {
ref mut decl_id, ..
}) => decl_id.subst(type_mapping, engines),
}) => decl_id.subst(type_mapping, ctx),
TyDecl::TraitDecl(TraitDecl {
ref mut decl_id, ..
}) => decl_id.subst(type_mapping, engines),
}) => decl_id.subst(type_mapping, ctx),
TyDecl::StructDecl(StructDecl {
ref mut decl_id, ..
}) => decl_id.subst(type_mapping, engines),
}) => decl_id.subst(type_mapping, ctx),
TyDecl::EnumDecl(EnumDecl {
ref mut decl_id, ..
}) => decl_id.subst(type_mapping, engines),
}) => decl_id.subst(type_mapping, ctx),
TyDecl::EnumVariantDecl(EnumVariantDecl {
ref mut enum_ref, ..
}) => enum_ref.subst(type_mapping, engines),
}) => enum_ref.subst(type_mapping, ctx),
TyDecl::ImplSelfOrTrait(ImplSelfOrTrait {
ref mut decl_id, ..
}) => decl_id.subst(type_mapping, engines),
}) => decl_id.subst(type_mapping, ctx),
TyDecl::TypeAliasDecl(TypeAliasDecl {
ref mut decl_id, ..
}) => decl_id.subst(type_mapping, engines),
}) => decl_id.subst(type_mapping, ctx),
TyDecl::TraitTypeDecl(TraitTypeDecl {
ref mut decl_id, ..
}) => decl_id.subst(type_mapping, engines),
}) => decl_id.subst(type_mapping, ctx),
// generics in an ABI is unsupported by design
TyDecl::AbiDecl(_)
| TyDecl::ConstantDecl(_)
Expand Down
10 changes: 5 additions & 5 deletions sway-core/src/language/ty/declaration/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ impl HashWithEngines for TyEnumDecl {
}

impl SubstTypes for TyEnumDecl {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
has_changes! {
self.variants.subst(type_mapping, engines);
self.type_parameters.subst(type_mapping, engines);
self.variants.subst(type_mapping, ctx);
self.type_parameters.subst(type_mapping, ctx);
}
}
}
Expand Down Expand Up @@ -186,7 +186,7 @@ impl OrdWithEngines for TyEnumVariant {
}

impl SubstTypes for TyEnumVariant {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
self.type_argument.subst_inner(type_mapping, engines)
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
self.type_argument.subst_inner(type_mapping, ctx)
}
}
16 changes: 8 additions & 8 deletions sway-core/src/language/ty/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ impl HashWithEngines for TyFunctionDecl {
}

impl SubstTypes for TyFunctionDecl {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
has_changes! {
self.type_parameters.subst(type_mapping, engines);
self.parameters.subst(type_mapping, engines);
self.return_type.subst(type_mapping, engines);
self.body.subst(type_mapping, engines);
self.implementing_for_typeid.subst(type_mapping, engines);
self.type_parameters.subst(type_mapping, ctx);
self.parameters.subst(type_mapping, ctx);
self.return_type.subst(type_mapping, ctx);
self.body.subst(type_mapping, ctx);
self.implementing_for_typeid.subst(type_mapping, ctx);
}
}
}
Expand Down Expand Up @@ -518,8 +518,8 @@ impl HashWithEngines for TyFunctionParameter {
}

impl SubstTypes for TyFunctionParameter {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
self.type_argument.type_id.subst(type_mapping, engines)
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
self.type_argument.type_id.subst(type_mapping, ctx)
}
}

Expand Down
8 changes: 4 additions & 4 deletions sway-core/src/language/ty/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ impl HashWithEngines for TyImplSelfOrTrait {
}

impl SubstTypes for TyImplSelfOrTrait {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
has_changes! {
self.impl_type_parameters.subst(type_mapping, engines);
self.implementing_for.subst_inner(type_mapping, engines);
self.items.subst(type_mapping, engines);
self.impl_type_parameters.subst(type_mapping, ctx);
self.implementing_for.subst_inner(type_mapping, ctx);
self.items.subst(type_mapping, ctx);
}
}
}
10 changes: 5 additions & 5 deletions sway-core/src/language/ty/declaration/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ impl HashWithEngines for TyStructDecl {
}

impl SubstTypes for TyStructDecl {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
has_changes! {
self.fields.subst(type_mapping, engines);
self.type_parameters.subst(type_mapping, engines);
self.fields.subst(type_mapping, ctx);
self.type_parameters.subst(type_mapping, ctx);
}
}
}
Expand Down Expand Up @@ -279,7 +279,7 @@ impl OrdWithEngines for TyStructField {
}

impl SubstTypes for TyStructField {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges {
self.type_argument.subst_inner(type_mapping, engines)
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
self.type_argument.subst_inner(type_mapping, ctx)
}
}
Loading

0 comments on commit 52b34c5

Please sign in to comment.