Skip to content

[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 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3229,7 +3229,7 @@ class ExprAvailabilityWalker : public ASTWalker {
maybeDiagParameterizedExistentialErasure(EE, Where);
}
if (auto *CC = dyn_cast<ExplicitCastExpr>(E)) {
if (!isa<CoerceExpr>(CC) &&
if (!isa<CoerceExpr>(CC) && CC->getCastType() &&
Copy link
Contributor

@xedin xedin Jun 24, 2022

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 an AbstractClosureExpr because ExprAvailabilityWalker always returns true from shouldWalkIntoClosure but:

  1. Result builders currently have multi-statement closure inference disabled due to some ordering issues related to one-way constraints;
  2. In code completion mode we always pass LeaveClosureBodiesUnchecked to the constraint system;

I wonder if something like this might reproduce the crash:

import SwiftUI

struct S<T> {
}

extension S : ExpressibleByIntegerLiteral {
  typealias IntegerLiteralType = Int8

  init(integerLiteral: IntegerLiteralType) {
  }
}

struct MyView : View {
  var body: some View {
    Group {
      EmptyView()

      test {
        let x =  #^COMPLETE^# as S<String>
        print(x)
      }
    }
  }

  func test(_: () -> Void) -> some View {
    return EmptyView()
  }
}

Copy link
Member Author

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.

Copy link
Contributor

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…

Copy link
Member Author

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 no CastType if that type is invalid. So we need to check if the type is valid (i.e. not null) before calling hasParameterizedExistential.

Copy link
Contributor

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

AFAICT it appears correct that a ExplicitCastExpr has no CastType if that type is invalid.

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

CC->getCastType()->hasParameterizedExistential()) {
SourceLoc loc = CC->getCastTypeRepr() ? CC->getCastTypeRepr()->getLoc()
: E->getLoc();
Expand Down
19 changes: 19 additions & 0 deletions validation-test/compiler_crashers_2_fixed/rdar95629905.swift
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")
}
}
}