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

[SR-3721] Unused global variable detection #46306

Open
swift-ci opened this issue Jan 25, 2017 · 3 comments
Open

[SR-3721] Unused global variable detection #46306

swift-ci opened this issue Jan 25, 2017 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-3721
Radar None
Original Reporter KingOfBrian (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI
Assignee None
Priority Medium

md5: 206c77cf9a5b3c05ea02fd224ebd6539

Issue Description:

Swift does not report un-used global variables, or recommend let modifications for un-mutated vars. This feature would be very valuable for swift as a scripting language, where it is common to have a lot of top level code.

@swift-ci
Copy link
Collaborator Author

Comment by Brian (JIRA)

I just made a PR for SR-2115 which reported that unused warnings in top level statements was not working. In there I disabled top level variable warning because walking for all global variable usage was a bit more complicated. I would like to fix this too, but there were a number of red flags that made it seem worth talking through my approach to see what scenarios I'm missing.

  • Split the diagnostics from `VarDeclUsageChecker` into a `VarDeclScopeDiagnostic` class, and have the VDUC use a VDSD instead. Move the reporting mechanism from the destructor to a method.

  • Add a `VarDeclScopeDiagnostic` to `ASTContext`. `VarDeclUsageChecker` will fall back to this scope if the variable isn't defined in the current scope.

  • Add some scope intelligence to VDSD where anything marked `public` would be considered R|W. Add this as a fixit to global variables.

  • Trigger the module-wide diagnostics after all files are walked and type checked correctly.

@swift-ci
Copy link
Collaborator Author

Comment by Brian (JIRA)

I wanted two mention another issue with the `VarDeclUsageChecker`.

func outerFunction() {
  var noWarning: String? = "due to innerFunction not being typechecked"
  func innerFunction() {
    var warningGenerated: String? = ""
  }
}

There's another bug I was looking at that I can't find, where only the active condition in a `#if` statement would be checked, and the inactive blocks mark any decl ref as read. I think if an inactive block was type checked and didn't have any errors, the checker could be less conservative. Not positive this is important enough though.

@rnapier
Copy link

rnapier commented Jul 14, 2017

Would (and should) implementing this allow unused globals to be optimized out? Currently, even under Whole Module Optimization, _main will initialize global let constants that are never used. For example, the following code:

let x = 1
func main() {}

Even with -O -wmo, this generates:

_main:
00000001000015a0        pushq   %rbp
00000001000015a1        movq    %rsp, %rbp
00000001000015a4        movq    $0x1, 0x3021(%rip)
00000001000015af        xorl    %eax, %eax
00000001000015b1        popq    %rbp
00000001000015b2        retq

The movq $0x1 should be possible to optimize away.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation
Projects
None yet
Development

No branches or pull requests

2 participants