This is an archive of the discontinued LLVM Phabricator instance.

[Clang][Sema] Fix invalid redefinition error in if/switch/for statement
ClosedPublic

Authored by junaire on Apr 15 2022, 12:38 AM.

Details

Summary

Because lambda will create its own scope, so we shouldn't consider variables
defined with the same name as redefinition error, if the new one is in a
lambda.

After this patch, clang is supposed to accept code below:

void foo() {

for (int x = [] { int x = 0; return x; }(); ;) ;

}

Fixes https://github.com/llvm/llvm-project/issues/54913

Diff Detail

Event Timeline

junaire created this revision.Apr 15 2022, 12:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 15 2022, 12:38 AM
junaire requested review of this revision.Apr 15 2022, 12:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 15 2022, 12:38 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
erichkeane accepted this revision.Apr 15 2022, 6:12 AM
This revision is now accepted and ready to land.Apr 15 2022, 6:12 AM

Hi @erichkeane , thanks for accepting this! But do you think we need to add a release note about it? (I'm a totally beginner so sometimes I'm confused about when we should do it.) Also, Do you have any concerns about the wording? In fact, I am pretty embarrassed by the current wording in my commit message...

Ah, right, we SHOULD have a release note, please add one and I'll help review the wording. Commit message is fine for a commit message, particularly with a good release note.

junaire updated this revision to Diff 423079.Apr 15 2022, 6:32 AM

Add a release note.

Ah, right, we SHOULD have a release note, please add one and I'll help review the wording. Commit message is fine for a commit message, particularly with a good release note.

Thanks! I have updated it :)

junaire added a comment.EditedApr 15 2022, 6:39 AM

I also find the code below really awkward:

S->getParent()->getFlags() & Scope::ControlScope

I find that we have helpers like Scope::isBlockScope() Scope::isTryScope() and etc, but no Scope::isControlScope and Scope::isFnTryCatchScope

I will add these helpers as a NFC patch after this one, that won't need a release note or review right?

I will add these helpers as a NFC patch after this one, that won't need a release note or review right?

I found that awkward as well, and almost requested that you fix this, but decided against it. I'm ok with this being an NFC patch. It would be nice to grep for all uses of the getFlags that compares with ControlScope and FnTryCatchScope to see if we can replace those uses as well.

clang/docs/ReleaseNotes.rst
122

I think this isn't accurate, right? I thought this had to do with lambdas alone? Is the following accurate?

`Clang should no longer incorrectly diagnose a variable declaration inside of a lambda expression inside the scope of a if/while/for/switch init statement as a redeclaration. This fixes ...'.

junaire updated this revision to Diff 423085.Apr 15 2022, 6:46 AM

Fix bad wording.

erichkeane accepted this revision.Apr 15 2022, 6:47 AM
erichkeane added inline comments.
clang/docs/ReleaseNotes.rst
123

Sorry, re-read it:

Clang should no longer incorrectly diagnose a variable declaration inside of a lambda expression that shares the name of a variable in a containing if/while/for/switch init statement as a redeclaration.

Feel free to commit with that wording.

junaire marked an inline comment as done.Apr 15 2022, 6:49 AM
junaire added inline comments.
clang/docs/ReleaseNotes.rst
122

I think this isn't accurate, right? I thought this had to do with lambdas alone? Is the following accurate?

`Clang should no longer incorrectly diagnose a variable declaration inside of a lambda expression inside the scope of a if/while/for/switch init statement as a redeclaration. This fixes ...'.

Thanks for the suggestion, It's my fault, I really shouldn't be sleeping in my English grammar class...

junaire updated this revision to Diff 423086.Apr 15 2022, 6:50 AM
junaire marked an inline comment as done.

Update release note.

This revision was landed with ongoing or failed builds.Apr 15 2022, 6:55 AM
This revision was automatically updated to reflect the committed changes.

Hi I think this patch is the root cause of https://github.com/llvm/llvm-project/issues/54968

The issue first appears in our builders here:
https://luci-milo.appspot.com/ui/p/fuchsia/builders/ci/clang_toolchain.ci.core.x64-release/b8816803488685972465/overview

based on the blamelist (https://luci-milo.appspot.com/ui/p/fuchsia/builders/ci/clang_toolchain.ci.core.x64-release/b8816803488685972465/blamelist) I'm fairly certain that this change is responsible.

I'm guessing that there is some additional case the new branch condition is missing that causes the assertion failure to manifest.

If this will be hard to fix, can you revert this patch until one is ready?