-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[TypeChecker] Don’t crash if a ExplicitCastExpr doesn’t have a cast type #59664
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
validation-test/compiler_crashers_2_fixed/rdar95629905.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// RUN: %target-typecheck-verify-swift | ||
|
||
@resultBuilder | ||
struct ViewBuilder { | ||
static func buildBlock(_ x: Int) -> Int { x } | ||
} | ||
|
||
func test(_: () -> Void) -> Int { | ||
return 42 | ||
} | ||
|
||
struct MyView { | ||
@ViewBuilder var body: Int { | ||
test { | ||
"ab" is Unknown // expected-error{{cannot find type 'Unknown' in scope}} | ||
print("x") | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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 took a quick look at this and I might have an idea about what is going on. Based on the stacktrace -
diagnoseStmtAvailability
walks into anAbstractClosureExpr
becauseExprAvailabilityWalker
always returnstrue
fromshouldWalkIntoClosure
but:LeaveClosureBodiesUnchecked
to the constraint system;I wonder if something like this might reproduce the crash:
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.
That example doesn’t reproduce the crash for me. AFAICT closures aren’t walked
https://github.com/apple/swift/blob/ba42be5fcf2d663a589a3f5e4efe0ffcffb101fa/lib/Sema/MiscDiagnostics.cpp#L64-L65
I very much suspect that this issue will be resolved fundamentally when we get rid of
LeaveClosureBodiesUnchecked
and I’m wondering if it makes sense to hunt for a reproducing test case now since the fix seems pretty straight-forward.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.
The check you pointed out would skip walking into some decls in the closures, not the closures themselves though. I think this issue needs to be addressed in a different way by adjusting the check in
ExprAvailabilityWalker
, otherwise it would just crash in some other place…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 managed to find a reproducer that’s not code completion specific. With this reproducer at hand, I would argue that the fix is correct because AFAICT it appears correct that a
ExplicitCastExpr
has noCastType
if that type is invalid. So we need to check if the type is valid (i.e. not null) before callinghasParameterizedExistential
.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.
How did an expression with incorrect cast end up getting past to a syntactic diagnostics?
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 don't know for sure if that makes sense in this case, but some other places I saw where the type was invalid the expected was to have an
ErrorType
instead of a null type. But again not sure if that is the case here...