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

Immediately receiving "error: fatalError" with "The compiler is unable to type-check this expression in reasonable time" #69621

Open
art-divin opened this issue Nov 2, 2023 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@art-divin
Copy link
Contributor

art-divin commented Nov 2, 2023

Description

While tinkering with swift-syntax I have changed the signature of the following method:

 private func visitImpl<NodeType: SyntaxProtocol>(
    _ node: Syntax,
    _ nodeType: NodeType.Type,
    _ visit: (NodeType) -> SyntaxVisitorContinueKind,
    _ visitPost: (NodeType) -> Void
  ) {
    let node = node.cast(NodeType.self)
    let needsChildren = (visit(node) == .visitChildren)
    // Avoid calling into visitChildren if possible.
    if needsChildren && !node.raw.layoutView!.children.isEmpty {
      visitChildren(node)
    }
    visitPost(node)
  }

to variant without generics:

private func visitImpl(
    _ node: Syntax,
    _ nodeType: SyntaxProtocol.Type,
    _ visit: (SyntaxProtocol) -> SyntaxVisitorContinueKind,
    _ visitPost: (SyntaxProtocol) -> Void
  ) {
    let node = node.cast(nodeType)
    let needsChildren = (visit(node) == .visitChildren)
    // Avoid calling into visitChildren if possible.
    if needsChildren && !node.raw.layoutView!.children.isEmpty {
      visitChildren(node)
    }
    visitPost(node)
  }

I then immediately receive "error: fatalError" with "The compiler is unable to type-check this expression in reasonable time"

Steps to reproduce

  1. clone https://github.com/art-divin/swift-syntax/ and checkout branch "resolve-compilation-time-issues" with commit 16c35c5ffb529185adf1af8f0d3fe38c96b89ecb
  2. cd into cloned directory
  3. swift build to see error: fatalError or use Xcode to see The compiler is unable to type-check this expression in reasonable time

Expected behavior

Code should compile

Environment

  • Swift compiler version info swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
  • Xcode version info Xcode 15.0.1 (Build version 15A507)
  • Deployment target: Target: arm64-apple-macosx14.0
@art-divin art-divin added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Nov 2, 2023
@art-divin
Copy link
Contributor Author

👋🏻

Update

I have looked into it, and the error happens because there are too many overrides of the method, thus the condition in ConstraintSystem.cpp on line 4361:

if (isTooComplex(viable))
      return SolutionResult::forTooComplex(getTooComplexRange());

fails and returns that error.

In my opinion, the error is not the one developer can expect, i.e. the message should state that what TypeChecker says in the log:

...
Constraint restrictions:
      AccessorDeclListSyntax.Type to any SyntaxProtocol.Type is [metatype-to-existential-metatype]
    Trailing closure matching:
      locator@0x140eff900 [UnresolvedDot@/Users/art-divin/Documents/Projects/SwiftTypeChecker/swift-syntax/Sources/SwiftSyntax/generated/SyntaxVisitor.swift:7069:12 → apply argument]: forward
      locator@0x140effa90 [UnresolvedDot@/Users/art-divin/Documents/Projects/SwiftTypeChecker/swift-syntax/Sources/SwiftSyntax/generated/SyntaxVisitor.swift:7069:61 → apply argument]: forward
      locator@0x119199510 [UnresolvedDot@/Users/art-divin/Documents/Projects/SwiftTypeChecker/swift-syntax/Sources/SwiftSyntax/generated/SyntaxVisitor.swift:7069:73 → apply argument]: forward
      locator@0x1191ae608 [Call@/Users/art-divin/Documents/Projects/SwiftTypeChecker/swift-syntax/Sources/SwiftSyntax/generated/SyntaxVisitor.swift:7069:12 → apply argument]: forward
    Fixes:
      [fix: allow function type mismatch] @ locator@0x11a88f2c8 [Call@/Users/art-divin/Documents/Projects/SwiftTypeChecker/swift-syntax/Sources/SwiftSyntax/generated/SyntaxVisitor.swift:7069:12 → apply argument → comparing call argument #2 to parameter #2]
...

Meaning that there were no candidates found in overrides which satisfy such type constraint:
AccessorDeclListSyntax.Type to any SyntaxProtocol.Type

@art-divin
Copy link
Contributor Author

👋🏻 Hi,

Update

After setting unsigned ExpressionTimeoutThreshold = 600 * 1024; unsigned SolverMemoryThreshold = std::numeric_limits<unsigned int>::max(); and unsigned SolverBindingThreshold = std::numeric_limits<unsigned int>::max();, I was able to let the compiler run as long as it needed to calm down and generate the expected error, i.e. the error I would expect to see in Xcode as a developer, rather than the "reasonable time" error (Kind::TooComplex).

SyntaxVisitor.swift:7069:12: error: no 'visit' candidates produce the expected type '(any SyntaxProtocol) -> SyntaxVisitorContinueKind' for parameter #2
      self.visitImpl2($0, AccessorDeclListSyntax.self, self.visit, self.visitPost)

I am unsure how to make it that with the existing constraints for SolverMemoryThreshold (512 * 1024 * 1024), SolverBindingThreshold (1024 * 1024), ExpressionTimeoutThreshold (600), the compiler would still generate the expected error rather that "timing out" due to the number of overloaded functions.

@art-divin
Copy link
Contributor Author

art-divin commented Nov 26, 2023

👋🏻 Update

I have found that SolverMemoryThreshold is the limiting factor, and so, with the number of overrides - 558 - CSSolver.cpp cannot find neither the resolution, nor the correct error message - that no candidates with the types were found. If to set SolverMemoryThreshold to std::numeric_limits<unsigned int>::max();, then the process ends without the SolutionResult::TooComplex result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant