Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler panics and no DCA warning when a configurable is never used #6118

Open
ironcev opened this issue Jun 13, 2024 · 1 comment · May be fixed by #6121
Open

Compiler panics and no DCA warning when a configurable is never used #6118

ironcev opened this issue Jun 13, 2024 · 1 comment · May be fixed by #6121
Assignees
Labels
compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler General compiler. Should eventually become more specific as the issue is triaged DCA Everything to do with Dead Code Analysis

Comments

@ironcev
Copy link
Member

ironcev commented Jun 13, 2024

The configurable MAX_SUPPLY is never used and optimized away but there is no dead code warning generated and that confuses the developers.

Additionally, the code compiles without warnings or errors on Sway v0.60.0 but the compiler panics on the current master:

thread 'main' panicked at sway-core/src/semantic_analysis/ast_node/declaration/configurable.rs:106:18:
called `Result::unwrap()` on an `Err` value: ErrorEmitted { _priv: () }


contract;

abi Test {
    fn test() -> bool;
}

configurable {
    MAX_SUPPLY: u64 = 3,
}

impl Test for Contract {
    fn test() -> bool {
        true
    }
}
@ironcev ironcev added compiler General compiler. Should eventually become more specific as the issue is triaged compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen DCA Everything to do with Dead Code Analysis labels Jun 13, 2024
@ironcev ironcev changed the title Compiler panics and no DCA warning when configurable is never used Compiler panics and no DCA warning when a configurable is never used Jun 13, 2024
@xunilrj xunilrj self-assigned this Jun 13, 2024
@xunilrj xunilrj linked a pull request Jun 13, 2024 that will close this issue
8 tasks
@Dr-Maxwell
Copy link

Possible Solution
Prevent Optimization of Constants:
Ensure constants like MAX_SUPPLY are not optimized away by using them in a way that the compiler recognizes as necessary. For example:
const MAX_SUPPLY: u64 = 1000;

fn main() {
println!("MAX_SUPPLY is: {}", MAX_SUPPLY); // Dummy usage to prevent optimization
// Your application logic
}
Enable Dead Code Warnings:
Ensure dead code warnings are enabled in your development environment to catch unused constants and other dead code. Add the following to Cargo.toml:
[profile.dev]
rustflags = ["-Awarnings"]
Handle Errors Properly:
Avoid using unwrap() on Result types where an Err could occur. Instead, handle errors using match or the ? operator for proper error propagation and handling.

i hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler General compiler. Should eventually become more specific as the issue is triaged DCA Everything to do with Dead Code Analysis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants