-
Notifications
You must be signed in to change notification settings - Fork 10.5k
One last round of ASTScope cleanups #34232
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
One last round of ASTScope cleanups #34232
Conversation
b38f7f4
to
09fa595
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff! Worth landing ASAP. Take my comments as fodder for any subsequent PRs you want to do.
lib/AST/DeclContext.cpp
Outdated
#ifndef NDEBUG | ||
auto checkSourceRange = [&](Decl *prev, Decl *next) { | ||
if (!member->getDeclContext()->getParentSourceFile()) | ||
return; | ||
|
||
auto shouldSkip = [](Decl *d) { | ||
if (isa<VarDecl>(d) || isa<EnumElementDecl>(d) || isa<IfConfigDecl>(d)) | ||
return true; | ||
|
||
if (d->isImplicit()) | ||
return true; | ||
|
||
return false; | ||
}; | ||
|
||
if (shouldSkip(prev) || shouldSkip(next)) | ||
return; | ||
|
||
SourceLoc prevEnd = prev->getEndLoc(); | ||
SourceLoc nextStart = next->getStartLoc(); | ||
|
||
if (!prevEnd.isValid() || !nextStart.isValid()) | ||
return; | ||
|
||
if (getASTContext().SourceMgr.isBeforeInBuffer(prevEnd, nextStart)) | ||
return; | ||
|
||
llvm::errs() << "Source ranges out of order in addMember():\n"; | ||
prev->dump(llvm::errs()); | ||
next->dump(llvm::errs()); | ||
abort(); | ||
}; | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great to see the assertion here! A couple of thoughts:
-
A lambda inside the function vs a separate function:
In favor of the lambda is that it is only called here. In favor of a separate function is thataddMemberSilently
would be a bit more readable without the lambda in the middle. Also the data flow intocheckSourceRange
gets clearer when forced to pass in data explicitly. (Obviously I lean towards the latter.) -
Does it make sense to generalize the order checking to any Decl? The details would be different for brace statements, but it would be nice to reify the commonality of needing source ranges to be ordered.
BTW, great to check the invariant right at the insertion point!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I think some "why" comments could help here. Why check the ranges? Why skip over vars, enums, etc.?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add some more comments, thanks.
I'll also see about adding a similar assertion to SourceFile::addTopLevelDecl() and BraceStmt::setBody() (and hopefully share code), but I'll do that in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Thank you.
09fa595
to
4b0d39c
Compare
@swift-ci Please smoke test |
@swift-ci Please test source compatibility |
@swift-ci Please smoke test Linux |
@swift-ci Please clean smoke test Linux |
@swift-ci Please smoke test Linux |
@swift-ci Please test source compatibility |
Builds upon #34226.