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

Don't emit debug info for unused variable declarations #10957

Merged

Conversation

HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented Jul 17, 2021

Fixes #6919. Fixes #9882.

More specifically, given:

x : Int32

Its type is deduced to be NoReturn after semantic analysis is completed, because it is never used; conversely, if any variable is NoReturn, then no assignments to it are possible, so it must also be unused, e.g.:

x = uninitialized NoReturn
x = raise "" # CleanupTransformer discards the assignment and retains only the raise

NoReturn variables never allocate stack space during code generation. Thus NoReturn or unused variables in a semantic context will not appear in its corresponding codegen context. This PR makes sure to ignore those variables.

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:codegen labels Jul 17, 2021
llvm_var = context.vars[name]
# If a variable is deduced to have type `NoReturn` it might not be
# allocated at all
next unless (llvm_var = context.vars[name]?)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is totally optional, but I prefer the assignment and the condition to be split. Then the comment is only relevant to the condition, not the assignment.

Copy link
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @HertzDevil 🙏

@beta-ziliani beta-ziliani added this to the 1.2.0 milestone Sep 6, 2021
@straight-shoota straight-shoota merged commit 3509155 into crystal-lang:master Sep 7, 2021
@HertzDevil HertzDevil deleted the bug/unused-var-debug-info branch September 8, 2021 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:codegen topic:compiler:debugger
Projects
None yet
5 participants