Skip to content

Commit

Permalink
Use Rc::clone(&ptr) syntax instead of ptr.clone()
Browse files Browse the repository at this point in the history
Change to use the idiomatic syntax preferred by the official
Rust book.
  • Loading branch information
cburgdorf committed Nov 18, 2020
1 parent c9e7352 commit 5de8db5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
29 changes: 16 additions & 13 deletions semantics/src/namespace/scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,12 @@ impl BlockScope {
loop {
parent = match parent {
BlockScopeParent::Block(ref scope) => {
last_block_scope = scope.clone();
last_block_scope = Rc::clone(&scope);
scope.borrow().parent.clone()
}
BlockScopeParent::Contract(ref scope) => return (scope.clone(), last_block_scope),
BlockScopeParent::Contract(ref scope) => {
return (Rc::clone(&scope), last_block_scope)
}
}
}
}
Expand Down Expand Up @@ -258,13 +260,14 @@ mod tests {
};
use crate::namespace::types::Base;
use fe_parser::span::Span;
use std::rc::Rc;

#[test]
fn test_scope_resolution_on_first_level_block_scope() {
let module_scope = ModuleScope::new();
let contract_scope = ContractScope::new(module_scope);
let block_scope_1 =
BlockScope::from_contract_scope(Span::new(0, 0), contract_scope.clone());
BlockScope::from_contract_scope(Span::new(0, 0), Rc::clone(&contract_scope));
assert_eq!(block_scope_1, block_scope_1.borrow().function_scope());
assert_eq!(contract_scope, block_scope_1.borrow().contract_scope());
}
Expand All @@ -274,11 +277,11 @@ mod tests {
let module_scope = ModuleScope::new();
let contract_scope = ContractScope::new(module_scope);
let block_scope_1 =
BlockScope::from_contract_scope(Span::new(0, 0), contract_scope.clone());
BlockScope::from_contract_scope(Span::new(0, 0), Rc::clone(&contract_scope));
let block_scope_2 = BlockScope::from_block_scope(
Span::new(0, 0),
BlockScopeType::IfElse,
block_scope_1.clone(),
Rc::clone(&block_scope_1),
);
assert_eq!(block_scope_1, block_scope_2.borrow().function_scope());
assert_eq!(contract_scope, block_scope_2.borrow().contract_scope());
Expand All @@ -289,7 +292,7 @@ mod tests {
let module_scope = ModuleScope::new();
let contract_scope = ContractScope::new(module_scope);
let block_scope_1 =
BlockScope::from_contract_scope(Span::new(0, 0), contract_scope.clone());
BlockScope::from_contract_scope(Span::new(0, 0), Rc::clone(&contract_scope));
block_scope_1
.borrow_mut()
.add_base("some_thing".to_string(), Base::Bool);
Expand All @@ -304,11 +307,11 @@ mod tests {
let module_scope = ModuleScope::new();
let contract_scope = ContractScope::new(module_scope);
let block_scope_1 =
BlockScope::from_contract_scope(Span::new(0, 0), contract_scope.clone());
BlockScope::from_contract_scope(Span::new(0, 0), Rc::clone(&contract_scope));
let block_scope_2 = BlockScope::from_block_scope(
Span::new(0, 0),
BlockScopeType::IfElse,
block_scope_1.clone(),
Rc::clone(&block_scope_1),
);
block_scope_1
.borrow_mut()
Expand All @@ -324,11 +327,11 @@ mod tests {
let module_scope = ModuleScope::new();
let contract_scope = ContractScope::new(module_scope);
let block_scope_1 =
BlockScope::from_contract_scope(Span::new(0, 0), contract_scope.clone());
BlockScope::from_contract_scope(Span::new(0, 0), Rc::clone(&contract_scope));
let block_scope_2 = BlockScope::from_block_scope(
Span::new(0, 0),
BlockScopeType::IfElse,
block_scope_1.clone(),
Rc::clone(&block_scope_1),
);
block_scope_2
.borrow_mut()
Expand All @@ -341,7 +344,7 @@ mod tests {
let module_scope = ModuleScope::new();
let contract_scope = ContractScope::new(module_scope);
let block_scope_1 =
BlockScope::from_contract_scope(Span::new(0, 0), contract_scope.clone());
BlockScope::from_contract_scope(Span::new(0, 0), Rc::clone(&contract_scope));
assert_eq!(
true,
block_scope_1
Expand All @@ -360,7 +363,7 @@ mod tests {
let block_scope_2 = BlockScope::from_block_scope(
Span::new(0, 0),
BlockScopeType::IfElse,
block_scope_1.clone(),
Rc::clone(&block_scope_1),
);
assert_eq!(
true,
Expand All @@ -380,7 +383,7 @@ mod tests {
let block_scope_3 = BlockScope::from_block_scope(
Span::new(0, 0),
BlockScopeType::Loop,
block_scope_2.clone(),
Rc::clone(&block_scope_2),
);
assert_eq!(
true,
Expand Down
2 changes: 1 addition & 1 deletion semantics/src/traversal/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ fn expr_call(
.iter()
// Side effect: Performs semantic analysis on each call arg and adds its attributes to
// the context
.map(|argument| call_arg(scope.clone(), context.clone(), argument))
.map(|argument| call_arg(Rc::clone(&scope), Rc::clone(&context), argument))
.collect::<Result<_, _>>()?;

if let fe::Expr::Attribute { value: _, attr } = &func.node {
Expand Down
16 changes: 8 additions & 8 deletions semantics/src/traversal/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ fn if_statement(
or_else,
} => {
let body_scope =
BlockScope::from_block_scope(stmt.span, BlockScopeType::IfElse, scope.clone());
traverse_statements(body_scope, context.clone(), body)?;
BlockScope::from_block_scope(stmt.span, BlockScopeType::IfElse, Rc::clone(&scope));
traverse_statements(body_scope, Rc::clone(&context), body)?;
let or_else_scope =
BlockScope::from_block_scope(stmt.span, BlockScopeType::IfElse, scope.clone());
traverse_statements(or_else_scope, context.clone(), or_else)?;
BlockScope::from_block_scope(stmt.span, BlockScopeType::IfElse, Rc::clone(&scope));
traverse_statements(or_else_scope, Rc::clone(&context), or_else)?;
verify_is_boolean(scope, context, test)
}
_ => unreachable!(),
Expand All @@ -236,8 +236,8 @@ fn while_loop(
unimplemented!();
}
let body_scope =
BlockScope::from_block_scope(stmt.span, BlockScopeType::Loop, scope.clone());
traverse_statements(body_scope, context.clone(), body)?;
BlockScope::from_block_scope(stmt.span, BlockScopeType::Loop, Rc::clone(&scope));
traverse_statements(body_scope, Rc::clone(&context), body)?;
verify_is_boolean(scope, context, test)
}
_ => unreachable!(),
Expand Down Expand Up @@ -291,7 +291,7 @@ fn assert(
stmt: &Spanned<fe::FuncStmt>,
) -> Result<(), SemanticError> {
if let fe::FuncStmt::Assert { test, msg } = &stmt.node {
verify_is_boolean(scope.clone(), context.clone(), test)?;
verify_is_boolean(Rc::clone(&scope), Rc::clone(&context), test)?;
if let Some(msg) = msg {
// TODO: type check for a string once strings are supported
let _msg_attributes = expressions::expr(scope, context, msg)?;
Expand Down Expand Up @@ -329,7 +329,7 @@ fn func_return(
stmt: &Spanned<fe::FuncStmt>,
) -> Result<(), SemanticError> {
if let fe::FuncStmt::Return { value: Some(value) } = &stmt.node {
let attributes = expressions::expr(scope.clone(), context.clone(), value)?;
let attributes = expressions::expr(Rc::clone(&scope), Rc::clone(&context), value)?;

match context
.borrow()
Expand Down

0 comments on commit 5de8db5

Please sign in to comment.