Skip to content

Commit

Permalink
Remove clones from scoped namespace usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Feb 21, 2024
1 parent a213a2d commit db5140c
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 246 deletions.
30 changes: 13 additions & 17 deletions sway-core/src/semantic_analysis/ast_node/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,22 @@ use crate::{
impl ty::TyCodeBlock {
pub(crate) fn type_check(
handler: &Handler,
mut ctx: TypeCheckContext,
ctx: TypeCheckContext,
code_block: &CodeBlock,
) -> Result<Self, ErrorEmitted> {
// Create a temp namespace for checking within the code block scope.
let mut code_block_namespace = ctx.namespace().clone();
let evaluated_contents = code_block
.contents
.iter()
.filter_map(|node| {
ctx.by_ref()
.scoped(&mut code_block_namespace, |ctx| {
ty::TyAstNode::type_check(handler, ctx, node.clone())
})
.ok()
})
.collect::<Vec<ty::TyAstNode>>();
ctx.scoped(|mut ctx| {
let evaluated_contents = code_block
.contents
.iter()
.filter_map(|node| {
ty::TyAstNode::type_check(handler, ctx.by_ref(), node.clone()).ok()
})
.collect::<Vec<ty::TyAstNode>>();

Ok(ty::TyCodeBlock {
contents: evaluated_contents,
whole_block_span: code_block.whole_block_span.clone(),
Ok(ty::TyCodeBlock {
contents: evaluated_contents,
whole_block_span: code_block.whole_block_span.clone(),
})
})
}

Expand Down
3 changes: 1 addition & 2 deletions sway-core/src/semantic_analysis/ast_node/declaration/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ impl ty::TyAbiDecl {
let self_type_id = self_type_param.type_id;

// A temporary namespace for checking within this scope.
let mut abi_namespace = ctx.namespace().clone();
ctx.with_abi_mode(AbiMode::ImplAbiFn(name.clone(), None))
.with_self_type(Some(self_type_id))
.scoped(&mut abi_namespace, |mut ctx| {
.scoped(|mut ctx| {
// Insert the "self" type param into the namespace.
self_type_param.insert_self_type_into_namespace(handler, ctx.by_ref());

Expand Down
3 changes: 1 addition & 2 deletions sway-core/src/semantic_analysis/ast_node/declaration/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ impl ty::TyEnumDecl {
} = decl;

// create a namespace for the decl, used to create a scope for generics
let mut decl_namespace = ctx.namespace().clone();
ctx.scoped(&mut decl_namespace, |mut ctx| {
ctx.scoped(|mut ctx| {
// Type check the type parameters.
let new_type_parameters = TypeParameter::type_check_type_params(
handler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ impl ty::TyFunctionDecl {
}

// create a namespace for the function
let mut fn_namespace = ctx.namespace().clone();
ctx.by_ref()
.with_purity(*purity)
.with_const_shadowing_mode(ConstShadowingMode::Sequential)
.disallow_functions()
.scoped(&mut fn_namespace, |mut ctx| {
.scoped(|mut ctx| {
// Type check the type parameters.
let new_type_parameters = TypeParameter::type_check_type_params(
handler,
Expand Down Expand Up @@ -171,12 +170,11 @@ impl ty::TyFunctionDecl {
ty_fn_decl: &mut Self,
) -> Result<Self, ErrorEmitted> {
// create a namespace for the function
let mut fn_namespace = ctx.namespace().clone();
ctx.by_ref()
.with_purity(ty_fn_decl.purity)
.with_const_shadowing_mode(ConstShadowingMode::Sequential)
.disallow_functions()
.scoped(&mut fn_namespace, |mut ctx| {
.scoped(|mut ctx| {
let FunctionDeclaration { body, .. } = fn_decl;

let ty::TyFunctionDecl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ impl TyImplTrait {
let self_type_id = self_type_param.type_id;

// create a namespace for the impl
let mut impl_namespace = ctx.namespace().clone();
ctx.by_ref()
.with_const_shadowing_mode(ConstShadowingMode::ItemStyle)
.with_self_type(Some(self_type_id))
.allow_functions()
.scoped(&mut impl_namespace, |mut ctx| {
.scoped(|mut ctx| {
// Type check the type parameters
let new_impl_type_parameters = TypeParameter::type_check_type_params(
handler,
Expand Down Expand Up @@ -292,10 +291,9 @@ impl TyImplTrait {
let engines = ctx.engines();

// create the namespace for the impl
let mut impl_namespace = ctx.namespace().clone();
ctx.with_const_shadowing_mode(ConstShadowingMode::ItemStyle)
.allow_functions()
.scoped(&mut impl_namespace, |mut ctx| {
.scoped(|mut ctx| {
// Create a new type parameter for the "self type".
let self_type_param =
TypeParameter::new_self_type(engines, implementing_for.span());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ impl ty::TyStructDecl {
} = decl;

// create a namespace for the decl, used to create a scope for generics
let mut decl_namespace = ctx.namespace().clone();
ctx.scoped(&mut decl_namespace, |mut ctx| {
ctx.scoped(|mut ctx| {
// Type check the type parameters.
let new_type_parameters = TypeParameter::type_check_type_params(
handler,
Expand Down
Loading

0 comments on commit db5140c

Please sign in to comment.