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

[ASTScope] Re-enable checkSourceRangeBeforeAddingChild #80339

Merged
merged 7 commits into from
Mar 29, 2025

Conversation

hamishknight
Copy link
Contributor

It seems like this was accidentally disabled in #63023. This requires fixing a few different cases to avoid hitting the assertion, mostly around macro expansions. Attribute scopes on extensions are the only remaining case I'm aware of that hit this assertion, I've disabled checking them for now since it requires reworking how we handle extension scopes (in particular generic parameter lookup for extensions). I'll try and get to that in a follow-up PR.

Such bindings may have a local decl context, but their scope shouldn't
extend past the `@abi` attribute.
Rather than looking at their DeclContext, look at the location of
their storage. This allows us to differentiate accessors added by
macro expansions vs accessors that are attached to storage that's
in a macro expansion.
`visitParsedAccessors` currently iterates over any non-implicit
accessor that happens to be present. This means we end up with
accessors added by macro expansions if the expansion happens before
the ASTScope expansion. Make sure we avoid adding such accessors to
the ASTScope tree, they should instead use their own tree. Ideally
we'd better formalize `visitParsedAccessors` such that it only ever
visits non-expansion accessors, but I'm leaving that for another day.
Make sure it receives a SourceRange that covers the entire body,
since that's what it replaces.
Given we're working with CharSourceRanges, the end location is past
the end of the actual range, so when checking if e.g a function body
macro is contained within the function decl, we fail since the end
location is past the closing `}`. Check the start location instead,
which is part of the range.
Update the logic to correctly handle replacement ranges, and re-enable
the assertion. For now, carve out an exception for attributes on
extensions, I'll try and tackle those in a follow-up.
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight
Copy link
Contributor Author

@swift-ci please test source compatibility

@hamishknight
Copy link
Contributor Author

@swift-ci please SourceKit stress test

Comment on lines +839 to +843
->originalSourceRange.getStart();
SourceLoc secondLocInLCA = secondMismatch == secondAncestors.end()
? secondLoc
: sourceMgr.getGeneratedSourceInfo(*secondMismatch)
->originalSourceRange.getEnd();
->originalSourceRange.getStart();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hborla This effectively reverts 8c9eeb2, AFAIK that shouldn't be necessary with 60da121, let me know if there's a case I'm missing though

Comment on lines +1014 to +1021
// For accessors, the storage decl is the more accurate thing to check
// since the entire property/subscript could be macro-generated, in which
// case the accessor shouldn't be considered "added by macro expansion".
if (auto *accessor = dyn_cast<AccessorDecl>(this)) {
auto storageLoc = accessor->getStorage()->getLoc();
if (storageLoc.isValid())
return mod->getSourceFileContainingLocation(storageLoc);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could split this logic out into a separate method specific to AccessorDecl, but it seems to me that this is the behavior that clients would expect for isInMacroExpansionInContext, even if it's not strictly based on the DeclContext

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I agree that what you have here is the intended behavior of isInMacroExpansionInContext

Use the same approach as `ModuleFileSharedCore::fatal`, ensuring the
verification error ends up in the crash log.
@hamishknight
Copy link
Contributor Author

@swift-ci please smoke test

@hamishknight
Copy link
Contributor Author

Figured we also ought to be printing the verification error in a pretty stack trace rather than stderr, such that it's picked up in crash logs, pushed a commit for that

Copy link
Member

@hborla hborla 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!

@hamishknight hamishknight enabled auto-merge March 28, 2025 21:43
@hamishknight hamishknight merged commit b63ed2b into swiftlang:main Mar 29, 2025
3 checks passed
@hamishknight hamishknight deleted the macroscope branch March 29, 2025 01:01
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