Skip to content

Commit

Permalink
Remove the no-op TyReturnStatement type. (FuelLabs#3011)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyaherbert authored Oct 12, 2022
1 parent 2ec825c commit 2c3418b
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 47 deletions.
6 changes: 3 additions & 3 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,19 +1099,19 @@ fn connect_expression(
tree_type,
typed_storage_reassignment.rhs.clone().span,
),
Return(stmt) => {
Return(exp) => {
let this_index = graph.add_node("return entry".into());
for leaf in leaves {
graph.add_edge(*leaf, this_index, "".into());
}
let return_contents = connect_expression(
&stmt.expr.expression,
&exp.expression,
graph,
&[this_index],
exit_node,
"",
tree_type,
stmt.expr.span.clone(),
exp.span.clone(),
)?;
// TODO: is this right? Shouldn't we connect the return_contents leaves to the exit
// node?
Expand Down
4 changes: 1 addition & 3 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,7 @@ fn const_eval_typed_expr(
}) => fields.get(*elem_to_access_num).cloned(),
_ => None,
},
ty::TyExpressionVariant::Return(stmt) => {
const_eval_typed_expr(lookup, known_consts, &stmt.expr)
}
ty::TyExpressionVariant::Return(exp) => const_eval_typed_expr(lookup, known_consts, exp),
ty::TyExpressionVariant::ArrayIndex { .. }
| ty::TyExpressionVariant::IntrinsicFunction(_)
| ty::TyExpressionVariant::CodeBlock(_)
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ impl FnCompiler {
&storage_reassignment.rhs,
span_md_idx,
),
ty::TyExpressionVariant::Return(stmt) => {
self.compile_return_statement(context, md_mgr, stmt.expr)
ty::TyExpressionVariant::Return(exp) => {
self.compile_return_statement(context, md_mgr, *exp)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/language/ty/expression/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ impl CollectTypesMetadata for TyExpression {
));
}
}
Return(stmt) => res.append(&mut check!(
stmt.expr.collect_types_metadata(),
Return(exp) => res.append(&mut check!(
exp.collect_types_metadata(),
return err(warnings, errors),
warnings,
errors
Expand Down
9 changes: 4 additions & 5 deletions sway-core/src/language/ty/expression/expression_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use crate::{
semantic_analysis::{
ContractCallParams, ProjectionKind, TyAsmRegisterDeclaration, TyCodeBlock,
TyEnumDeclaration, TyEnumVariant, TyIntrinsicFunctionKind, TyReassignment,
TyReturnStatement, TyStorageReassignment, TyStructExpressionField, TyStructField,
VariableMutability,
TyStorageReassignment, TyStructExpressionField, TyStructField, VariableMutability,
},
type_system::*,
TyFunctionDeclaration,
Expand Down Expand Up @@ -129,7 +128,7 @@ pub enum TyExpressionVariant {
Continue,
Reassignment(Box<TyReassignment>),
StorageReassignment(Box<TyStorageReassignment>),
Return(Box<TyReturnStatement>),
Return(Box<TyExpression>),
}

// NOTE: Hash and PartialEq must uphold the invariant:
Expand Down Expand Up @@ -582,8 +581,8 @@ impl fmt::Display for TyExpressionVariant {
};
format!("storage reassignment to {}", place)
}
TyExpressionVariant::Return(stmt) => {
format!("return {}", stmt.expr)
TyExpressionVariant::Return(exp) => {
format!("return {}", *exp)
}
};
write!(f, "{}", s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ impl TyFunctionDeclaration {
let return_statements: Vec<&ty::TyExpression> = body
.contents
.iter()
.flat_map(|node| -> Vec<&TyReturnStatement> { node.gather_return_statements() })
.map(|TyReturnStatement { expr, .. }| expr)
.flat_map(|node| node.gather_return_statements())
.collect();

// unify the types of the return statements with the function return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ impl TyImplTrait {
ty::TyExpressionVariant::StorageReassignment(storage_reassignment) => {
expr_contains_get_storage_index(&storage_reassignment.rhs, access_span)?
}
ty::TyExpressionVariant::Return(stmt) => {
expr_contains_get_storage_index(&stmt.expr, access_span)?
ty::TyExpressionVariant::Return(exp) => {
expr_contains_get_storage_index(exp, access_span)?
}
};
Ok(res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ pub(crate) use self::{
use crate::{
declaration_engine::declaration_engine::*,
error::*,
language::{parsed::*, ty, *},
language::{
parsed::*,
ty::{self, TyExpression},
*,
},
semantic_analysis::*,
type_system::*,
};
Expand Down Expand Up @@ -80,7 +84,7 @@ impl ty::TyExpression {
/// do indeed return the correct type
/// This does _not_ extract implicit return statements as those are not control flow! This is
/// _only_ for explicit returns.
pub(crate) fn gather_return_statements(&self) -> Vec<&TyReturnStatement> {
pub(crate) fn gather_return_statements(&self) -> Vec<&TyExpression> {
match &self.expression {
ty::TyExpressionVariant::IfExp {
condition,
Expand Down Expand Up @@ -170,8 +174,8 @@ impl ty::TyExpression {
ty::TyExpressionVariant::EnumTag { exp } => exp.gather_return_statements(),
ty::TyExpressionVariant::UnsafeDowncast { exp, .. } => exp.gather_return_statements(),

ty::TyExpressionVariant::Return(stmt) => {
vec![stmt]
ty::TyExpressionVariant::Return(exp) => {
vec![exp]
}
// if it is impossible for an expression to contain a return _statement_ (not an
// implicit return!), put it in the pattern below.
Expand Down Expand Up @@ -364,9 +368,8 @@ impl ty::TyExpression {
warnings,
errors,
);
let stmt = TyReturnStatement { expr };
let typed_expr = ty::TyExpression {
expression: ty::TyExpressionVariant::Return(Box::new(stmt)),
expression: ty::TyExpressionVariant::Return(Box::new(expr)),
return_type: insert_type(TypeInfo::Unknown),
// FIXME: This should be Yes?
is_constant: IsConstant::No,
Expand Down
10 changes: 6 additions & 4 deletions sway-core/src/semantic_analysis/ast_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ pub mod code_block;
pub mod declaration;
pub mod expression;
pub mod mode;
mod return_statement;

use std::fmt;

pub(crate) use code_block::*;
pub use declaration::*;
pub(crate) use expression::*;
pub(crate) use mode::*;
pub(crate) use return_statement::*;

use crate::{
declaration_engine::declaration_engine::*,
error::*,
language::{parsed::*, ty, Visibility},
language::{
parsed::*,
ty::{self, TyExpression},
Visibility,
},
semantic_analysis::*,
type_system::*,
types::DeterministicallyAborts,
Expand Down Expand Up @@ -158,7 +160,7 @@ impl TyAstNode {
/// do indeed return the correct type
/// This does _not_ extract implicit return statements as those are not control flow! This is
/// _only_ for explicit returns.
pub(crate) fn gather_return_statements(&self) -> Vec<&TyReturnStatement> {
pub(crate) fn gather_return_statements(&self) -> Vec<&TyExpression> {
match &self.content {
TyAstNodeContent::ImplicitReturnExpression(ref exp) => exp.gather_return_statements(),
// assignments and reassignments can happen during control flow and can abort
Expand Down
15 changes: 0 additions & 15 deletions sway-core/src/semantic_analysis/ast_node/return_statement.rs

This file was deleted.

4 changes: 2 additions & 2 deletions sway-core/src/semantic_analysis/storage_only_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ fn expr_validate(expr: &ty::TyExpression) -> CompileResult<()> {
);
check!(expr_validate(rhs), (), warnings, errors)
}
ty::TyExpressionVariant::Return(stmt) => {
check!(expr_validate(&stmt.expr), (), warnings, errors)
ty::TyExpressionVariant::Return(exp) => {
check!(expr_validate(exp), (), warnings, errors)
}
}
ok((), warnings, errors)
Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/src/core/traverse_typed_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ fn handle_expression(expression: &ty::TyExpression, tokens: &TokenMap) {
}
handle_expression(&storage_reassignment.rhs, tokens);
}
ty::TyExpressionVariant::Return(stmt) => handle_expression(&stmt.expr, tokens),
ty::TyExpressionVariant::Return(exp) => handle_expression(exp, tokens),
}
}

Expand Down

0 comments on commit 2c3418b

Please sign in to comment.