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

fix Issue 17807 - Spurious dead code warnings on enum and static variables. #7120

Merged
merged 1 commit into from
Sep 15, 2017

Conversation

tgehr
Copy link
Contributor

@tgehr tgehr commented Sep 4, 2017

Local declarations (except variable declarations that are neither enum nor static) have no associated code, and therefore should not give dead code warnings.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @tgehr! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.

Some tips to help speed things up:

  • smaller, focused PRs are easier to review than big ones

  • try not to mix up refactoring or style changes with bug fixes or feature enhancements

  • provide helpful commit messages explaining the rationale behind each change

Bear in mind that large or tricky changes may require multiple rounds of review and revision.

Please see CONTRIBUTING.md for more information.

Bugzilla references

Auto-close Bugzilla Description
17807 Spurious dead code warnings on enum and static variables.

@@ -612,6 +617,8 @@ class DeclarationExp : public Expression

Expression *syntaxCopy();

virtual bool hasCode();
Copy link
Member

Choose a reason for hiding this comment

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

Indentation

Copy link
Member

Choose a reason for hiding this comment

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

And virtual shouldn't be needed.

{
if (auto vd = declaration.isVarDeclaration())
{
return !(vd.storage_class & (STCmanifest | STCstatic));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you elaborate why the compiler should not complain about static variables? They do not generate code but take up space in the data segment, so a warning seems reasonable nevertheless?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Control flow passing by the static variable declaration does not do anything. Your argument justifies warnings for unused static variables, but not static variables that control flow does not reach.

Copy link
Member

Choose a reason for hiding this comment

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

@tgehr I agree.

Copy link
Contributor

@jpf91 jpf91 Sep 6, 2017

Choose a reason for hiding this comment

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

OK, but is there actually a case where a static variable declaration is not reached by control flow and the variable is still accessible from some place reached by code flow?

void main()
{
    return;

    {
        static int x;
    }
}

My line of thought goes like this: If the declaration is unreachable, all code in the scope of the declaration following the declaration is unreachable as well. The variable is only accessible in the same scope (or a nested scope) defined after the variable declaration. So, such a variable seems to be always unused/used only by dead code.

OTOH int x = void; also does not generate any code, so I don't really understand the passing by the static variable declaration does not do anything point. Or are you just arguing that these cases are still invalid, but should produce a variable unused message instead of the statement not reachable message?

EDIT: I see hasCode is used not only for the warning. So hasCode should indeed return false for static variable declarations. Maybe void initializers should also be handled then.

@WalterBright
Copy link
Member

Trailing whitespace in expression.h is what is preventing this from passing the autotester. Please fix. I use the tool detab to do that.

return 0;
}
default: y=x+S.x+foo();
static foreach(i;1..5)
Copy link
Member

Choose a reason for hiding this comment

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

Interestingly, if you remove static here, there's a ICE in the backend. Has a bug report been raised for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch. I couldn't find an existing issue. I have opened a new issue here: https://issues.dlang.org/show_bug.cgi?id=17834
(However, this bug will disappear once the deprecation messages are turned into compilation errors.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants