Skip to content

Commit

Permalink
Make sure global const delcarations are not misreported as unused. (F…
Browse files Browse the repository at this point in the history
…uelLabs#1537)

ConstantDeclarations aren't actually a thing as they're now all just
VariableDeclarations with 'pub const' mutability.  This needed to be
accounted for in the dead code analysis.
  • Loading branch information
otrho authored May 13, 2022
1 parent 197e640 commit 6171a79
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
TypedEnumDeclaration, TypedExpression, TypedExpressionVariant,
TypedFunctionDeclaration, TypedReassignment, TypedReturnStatement,
TypedStructDeclaration, TypedStructExpressionField, TypedTraitDeclaration,
TypedVariableDeclaration, TypedWhileLoop,
TypedVariableDeclaration, TypedWhileLoop, VariableMutability,
},
TypeCheckedStorageReassignment, TypedAstNode, TypedAstNodeContent, TypedParseTree,
},
Expand Down Expand Up @@ -338,15 +338,27 @@ fn connect_declaration(
) -> Result<Vec<NodeIndex>, CompileError> {
use TypedDeclaration::*;
match decl {
VariableDeclaration(TypedVariableDeclaration { body, .. }) => connect_expression(
&body.expression,
graph,
&[entry_node],
exit_node,
"variable instantiation",
tree_type,
body.clone().span,
),
VariableDeclaration(TypedVariableDeclaration {
name,
body,
is_mutable,
..
}) => {
if matches!(is_mutable, VariableMutability::ExportedConst) {
graph.namespace.insert_constant(name.clone(), entry_node);
Ok(leaves.to_vec())
} else {
connect_expression(
&body.expression,
graph,
&[entry_node],
exit_node,
"variable instantiation",
tree_type,
body.clone().span,
)
}
}
ConstantDeclaration(TypedConstantDeclaration { name, .. }) => {
graph.namespace.insert_constant(name.clone(), entry_node);
Ok(leaves.to_vec())
Expand Down

0 comments on commit 6171a79

Please sign in to comment.