Skip to content

Commit

Permalink
Fix DCA warnings for log intrinsic and stdlib function (FuelLabs#3538)
Browse files Browse the repository at this point in the history
This adds special handling for the log intrinsic, consuming the fields
of all structs used inside the inner expressions.

It also does the same for functions external to the current analysis, to
solve the same thing for the stdlib log function, which could lead to
some false negatives when it comes to the unused fields warning, but
it's the best we can do for now without considerable effort refactoring
the DCA engine to work inter-proceduraly/module.

Closes FuelLabs#3012.

Co-authored-by: Mohammad Fawaz <[email protected]>
  • Loading branch information
tritao and mohammadfawaz authored Dec 8, 2022
1 parent 8dc9afa commit b444422
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 4 deletions.
95 changes: 91 additions & 4 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions sway-core/src/control_flow_analysis/flow_graph/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ impl ControlFlowNamespace {
}
}

pub(crate) fn get_struct(&self, ident: &Ident) -> Option<&StructNamespaceEntry> {
self.struct_namespace.get(ident.as_str())
}
pub(crate) fn insert_struct(
&mut self,
struct_name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'log'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "log"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
script;

struct Foo {
value: u64
}

fn main() -> u64 {
__log(Foo {value: 0});
0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "compile"

# not: $()value: u64
# not: $()This struct field is never accessed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-E6171879CB601FD9'

[[package]]
name = 'log_stdlib'
source = 'member'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-E6171879CB601FD9'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "log_stdlib"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
script;

use std::logging::log;

struct Foo {
value: u64
}

fn main() -> u64 {
log(Foo {value: 0});
0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "compile"

# not: $()value: u64
# not: $()This struct field is never accessed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'unused_fields'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "unused_fields"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
script;

dep utils;
use utils::Foo;


struct Bar {
value: u64
}

fn internal_fn(s: Bar) {

}

fn main() -> u64 {
internal_fn(Bar {value: 0});
utils::external_fn(Foo {value: 0});
0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
library utils;

pub struct Foo {
value: u64
}

pub fn external_fn(s: Foo) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
category = "compile"

# check: $()struct Bar {
# check: $()This struct field is never accessed.

# not: $()value: u64
# not: $()This struct field is never accessed.

0 comments on commit b444422

Please sign in to comment.