Skip to content

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

Merged
merged 6 commits into from
Oct 10, 2020

Conversation

slavapestov
Copy link
Contributor

Builds upon #34226.

@slavapestov slavapestov force-pushed the misc-astscope-cleanups branch from b38f7f4 to 09fa595 Compare October 8, 2020 03:33
Copy link
Contributor

@davidungar davidungar left a 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.

Comment on lines 821 to 875
#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
Copy link
Contributor

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:

  1. 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 that addMemberSilently would be a bit more readable without the lambda in the middle. Also the data flow into checkSourceRange gets clearer when forced to pass in data explicitly. (Obviously I lean towards the latter.)

  2. 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!

Copy link
Contributor

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.?

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great! Thank you.

@slavapestov slavapestov force-pushed the misc-astscope-cleanups branch from 09fa595 to 4b0d39c Compare October 8, 2020 20:17
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test Linux

@slavapestov
Copy link
Contributor Author

@swift-ci Please clean smoke test Linux

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test Linux

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@slavapestov slavapestov merged commit b13ce50 into swiftlang:main Oct 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants