Skip to content

Commit

Permalink
Correctly display DCA warnings and fix some incorrect warnings that s…
Browse files Browse the repository at this point in the history
…howed up (FuelLabs#2230)
  • Loading branch information
mohammadfawaz authored Jul 6, 2022
1 parent c002df4 commit 4ba140a
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 51 deletions.
60 changes: 47 additions & 13 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;

use crate::{
parse_tree::{CallPath, Visibility},
semantic_analysis::{
Expand All @@ -10,20 +9,16 @@ use crate::{
TypedStructDeclaration, TypedStructExpressionField, TypedTraitDeclaration,
TypedVariableDeclaration, TypedWhileLoop, VariableMutability,
},
TypeCheckedStorageReassignment, TypedAstNode, TypedAstNodeContent, TypedImplTrait,
TypedIntrinsicFunctionKind,
TypeCheckedStorageReassignment, TypedAsmRegisterDeclaration, TypedAstNode,
TypedAstNodeContent, TypedImplTrait, TypedIntrinsicFunctionKind, TypedStorageDeclaration,
},
type_engine::{resolve_type, TypeInfo},
CompileError, CompileWarning, Ident, TreeType, Warning,
};
use petgraph::{prelude::NodeIndex, visit::Dfs};
use std::collections::BTreeSet;
use sway_types::{span::Span, Spanned};

use crate::semantic_analysis::TypedStorageDeclaration;

use petgraph::prelude::NodeIndex;
use petgraph::visit::Dfs;

impl ControlFlowGraph {
pub(crate) fn find_dead_code(&self) -> Vec<CompileWarning> {
// Dead code is code that has no path to the entry point.
Expand Down Expand Up @@ -100,7 +95,7 @@ impl ControlFlowGraph {
.filter(|CompileWarning { span, .. }| {
// if any other warnings contain a span which completely covers this one, filter
// out this one.
all_warnings.iter().any(
!all_warnings.iter().any(
|CompileWarning {
span: other_span, ..
}| {
Expand Down Expand Up @@ -191,6 +186,16 @@ impl ControlFlowGraph {
TypedAstNodeContent::Declaration(TypedDeclaration::ImplTrait { .. }),
..
}) => true,
ControlFlowGraphNode::ProgramNode(TypedAstNode {
content:
TypedAstNodeContent::Declaration(TypedDeclaration::ConstantDeclaration(
TypedConstantDeclaration {
visibility: Visibility::Public,
..
},
)),
..
}) => true,
_ => false,
})
.collect(),
Expand Down Expand Up @@ -887,12 +892,35 @@ fn connect_expression(
graph.add_edge(this_ix, field_ix, "".into());
Ok(vec![this_ix])
}
AsmExpression { .. } => {
let asm_node = graph.add_node("Inline asm".into());
AsmExpression { registers, .. } => {
let asm_node_entry = graph.add_node("Inline asm entry".into());
let asm_node_exit = graph.add_node("Inline asm exit".into());
for leaf in leaves {
graph.add_edge(*leaf, asm_node, "".into());
graph.add_edge(*leaf, asm_node_entry, "".into());
}
Ok(vec![asm_node])

let mut current_leaf = vec![asm_node_entry];
for TypedAsmRegisterDeclaration { initializer, .. } in registers {
current_leaf = match initializer {
Some(initializer) => connect_expression(
&initializer.expression,
graph,
&current_leaf,
exit_node,
"asm block argument initialization",
tree_type,
initializer.clone().span,
)?,
None => current_leaf,
}
}

// connect the final field to the exit
for leaf in current_leaf {
graph.add_edge(leaf, asm_node_exit, "".into());
}

Ok(vec![asm_node_exit])
}
Tuple { fields } => {
let entry = graph.add_node("tuple entry".into());
Expand Down Expand Up @@ -1192,6 +1220,12 @@ fn construct_dead_code_warning_from_node(node: &TypedAstNode) -> Option<CompileW
content: TypedAstNodeContent::Declaration(TypedDeclaration::AbiDeclaration { .. }),
..
} => return None,
// We handle storage fields individually. There is no need to emit any warnings for the
// storage declaration itself.
TypedAstNode {
content: TypedAstNodeContent::Declaration(TypedDeclaration::StorageDeclaration { .. }),
..
} => return None,
TypedAstNode {
content: TypedAstNodeContent::Declaration(..),
span,
Expand Down
4 changes: 1 addition & 3 deletions sway-core/src/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,9 +774,7 @@ fn item_const_to_constant_declaration(
None => TypeInfo::Unknown,
},
value: expr_to_expression(ec, item_const.expr)?,
//visibility: pub_token_opt_to_visibility(item_const.visibility),
// FIXME: you have to lie here or else the tests fail.
visibility: Visibility::Public,
visibility: pub_token_opt_to_visibility(item_const.visibility),
})
}

Expand Down
12 changes: 2 additions & 10 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
error::CompileError,
parse_tree::Visibility,
semantic_analysis::{
declaration::ProjectionKind, namespace, TypedAstNode, TypedAstNodeContent,
TypedConstantDeclaration, TypedDeclaration, TypedExpression, TypedExpressionVariant,
Expand Down Expand Up @@ -47,15 +46,8 @@ pub fn compile_const_decl(
TypedDeclaration::ConstantDeclaration(TypedConstantDeclaration {
name,
value,
visibility,
}) => {
// XXX Do we really only add public constants?
if !env.public_only || matches!(visibility, Visibility::Public) {
Some((name, value))
} else {
None
}
}
..
}) => Some((name, value)),
_otherwise => None,
};
if let Some((name, value)) = decl_name_value {
Expand Down
10 changes: 0 additions & 10 deletions sway-lib-core/src/ops.sw
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,6 @@ trait OrdEq: Ord + Eq {
}
}

trait OrdEq: Ord + Eq {
} {
fn ge(self, other: Self) -> bool {
self.gt(other) || self.eq(other)
}
fn le(self, other: Self) -> bool {
self.lt(other) || self.eq(other)
}
}

impl OrdEq for u64 {
}
impl OrdEq for u32 {
Expand Down
4 changes: 2 additions & 2 deletions sway-lib-std/src/constants.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ library constants;

use ::contract_id::ContractId;

const BASE_ASSET_ID = ~ContractId::from(ZERO_B256);
const ZERO_B256 = 0x0000000000000000000000000000000000000000000000000000000000000000;
pub const BASE_ASSET_ID = ~ContractId::from(ZERO_B256);
pub const ZERO_B256 = 0x0000000000000000000000000000000000000000000000000000000000000000;
18 changes: 9 additions & 9 deletions sway-lib-std/src/tx.sw
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ const TX_SCRIPT_START_OFFSET = 10352;
const TX_ID_OFFSET = 0;

// Input types
const INPUT_COIN = 0u8;
const INPUT_CONTRACT = 1u8;
const INPUT_MESSAGE = 2u8;
pub const INPUT_COIN = 0u8;
pub const INPUT_CONTRACT = 1u8;
pub const INPUT_MESSAGE = 2u8;

// Output types
const OUTPUT_COIN = 0u8;
const OUTPUT_CONTRACT = 1u8;
const OUTPUT_MESSAGE = 2u8;
const OUTPUT_CHANGE = 3u8;
const OUTPUT_VARIABLE = 4u8;
const OUTPUT_CONTRACT_CREATED = 5u8;
pub const OUTPUT_COIN = 0u8;
pub const OUTPUT_CONTRACT = 1u8;
pub const OUTPUT_MESSAGE = 2u8;
pub const OUTPUT_CHANGE = 3u8;
pub const OUTPUT_VARIABLE = 4u8;
pub const OUTPUT_CONTRACT_CREATED = 5u8;

/// Get the transaction type.
pub fn tx_type() -> u8 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
library earth;

const MAN = 5;
pub const MAN = 5;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library heaven;

const UNKNOWN_DEITY_VALUE = 0;
const MONKEYS_GONE_HERE = true;
pub const UNKNOWN_DEITY_VALUE = 0;
pub const MONKEYS_GONE_HERE = true;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
library hell;

const THE_DEVIL = 6;
pub const THE_DEVIL = 6;

0 comments on commit 4ba140a

Please sign in to comment.