Skip to content

[IDE] Avoid uses of isBeforeInBuffer in TypeCheckASTNodeAtLocRequest #81028

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 8 commits into from
May 14, 2025

Conversation

hamishknight
Copy link
Contributor

@hamishknight hamishknight commented Apr 23, 2025

Use the higher level APIs on SourceManager that handle locations in parent vs child buffers. This requires a couple of AST node source range fixes such that e.g the implicit function for a defer statement and for loop iterator vars have valid source ranges.

This then allows us to fix walkToDeclPre such that we don't set the found DeclContext unless the location is actually within that decl (the location here may well be in a separate buffer as we may have a replaced function body). This is the more principled fix for #80985.

@hamishknight hamishknight changed the title [Completion] A couple of SourceRange fixes [DNM] [Completion] A couple of SourceRange fixes Apr 23, 2025
@hamishknight hamishknight force-pushed the fix-completion-sourceranges branch from 73a7115 to 392339c Compare April 24, 2025 14:11
@hamishknight hamishknight force-pushed the fix-completion-sourceranges branch 2 times, most recently from 291ba9a to a99b66f Compare May 8, 2025 18:04
@hamishknight hamishknight force-pushed the fix-completion-sourceranges branch 2 times, most recently from 8768c53 to f6ece61 Compare May 12, 2025 11:47
@hamishknight hamishknight changed the title [DNM] [Completion] A couple of SourceRange fixes [IDE] Avoid uses of isBeforeInBuffer in TypeCheckASTNodeAtLocRequest May 12, 2025
@hamishknight hamishknight force-pushed the fix-completion-sourceranges branch from f6ece61 to f68b2bf Compare May 12, 2025 12:03
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight hamishknight marked this pull request as ready for review May 12, 2025 12:04
@hamishknight
Copy link
Contributor Author

Almost all clients would crash for an invalid location, let's
always assert.
Handle PatternBindingDecls with missing var locations, which can
happen for loop iterator vars, and FuncDecls with missing name and
func locations, which can happen for `defer`. Also while here make
sure we set the source location of a parser-produced ErrorExpr.
This wasn't really sound since it could result in source ranges that
have different buffers for the start and end loc. Instead, adjust
the parser logic to look at the brace range.
These don't appear to be necessary, let's mirror the logic in
`getStartLoc` and remove them.
Use the higher level APIs on SourceManager that handle locations in
parent vs child buffers. This then allows us to fix `walkToDeclPre`
such that we don't set the found DeclContext unless the location is
actually within that decl (here the location may well be in a
separate buffer as we may have a replaced function body).
Previously we could end up in cases where we pick the wrong
DeclContext in `TypeCheckASTNodeAtLocRequest` since we previously
weren't checking source ranges, which could result in skipping the
type-checking of an outer closure. Now that we correctly pick the
DeclContext, we should no longer hit that case, so we should be able
to fall into `getTypeForCompletion` and assert that the solution has
a type. Also while here let's upgrade that assert to a
`CONDITIONAL_ASSERT`.
@hamishknight hamishknight force-pushed the fix-completion-sourceranges branch from f68b2bf to d00f45a Compare May 14, 2025 10:49
@hamishknight
Copy link
Contributor Author

@swift-ci please smoke test

@hamishknight hamishknight enabled auto-merge May 14, 2025 13:06
@hamishknight hamishknight merged commit 93c37b4 into swiftlang:main May 14, 2025
3 checks passed
@hamishknight hamishknight deleted the fix-completion-sourceranges branch May 14, 2025 15:49
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