Skip to content

Commit

Permalink
Fixes DCA unused struct warning. (FuelLabs#4238)
Browse files Browse the repository at this point in the history
## Description
The issue was that a struct that is used as an accessed field of a
public struct results in unused struct warning.

The solution was to add to the DCA graph an edge from the access fields
to the parent struct.

DCA graph before fix:

![3683_before](https://user-images.githubusercontent.com/1188095/223726504-2dc36d96-adf6-464f-b413-af4e7c928647.png)

DCA graph after fix:

![3683_after](https://user-images.githubusercontent.com/1188095/223726534-326e29f6-a590-427c-9c1a-f6175891e6c1.png)


Closes FuelLabs#3683

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

Co-authored-by: João Matos <[email protected]>
  • Loading branch information
esdrubal and tritao authored Mar 8, 2023
1 parent 0dcae3d commit 0cafc16
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,14 @@ fn connect_expression<'eng: 'cfg, 'cfg>(
graph.add_edge(*leaf, this_ix, "".into());
}
graph.add_edge(this_ix, field_ix, "".into());

if let Some(struct_node_ix) = graph
.namespace
.find_struct_decl(resolved_type_of_parent.suffix.as_str())
{
graph.add_edge(this_ix, *struct_node_ix, "".into());
}

Ok(vec![this_ix])
}
AsmExpression { registers, .. } => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'struct_field_no_warning'
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"
implicit-std = false
license = "Apache-2.0"
name = "struct_field_no_warning"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
script;

struct CallData {
id: u64,
}

pub struct Proposal {
call_data: CallData,
}

fn main(p: Proposal) -> u64 {
p.call_data.id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
category = "compile"

0 comments on commit 0cafc16

Please sign in to comment.