reports icon indicating copy to clipboard operation
reports copied to clipboard

FB13751434: Compiler fails to report errors accurately in closures

Open macmade opened this issue 1 year ago • 0 comments

  • Date: 2024-04-24
  • Resolution: Open
  • Area: Swift Compiler
  • OS: macOS
  • Type: Incorrect/Unexpected Behavior
  • Keywords: xcode, swift, error, warning, closure, compiler

Description

If a syntax error occurs inside a closure, the compiler may fail to report the error. As an example:

let strings: [ String ] = [ 0, 1, 2, 3 ].compactMap
{
    if $0 == 0
    {
        return nil
    }

    return $0
}

While the syntax error is on return $0, the compiler gives an error on the return nil statement: 'nil' is not compatible with closure result type 'String'

Which is completely garbage since we use compactMap. It looks like a syntax error inside a closure breaks compiler inference, producing invalid error messages.

If we comment out the return nil, the the compiler is finally able to give a proper error message: Cannot convert value of type 'Int' to closure result type 'String'

Files

main.swift.zip

macmade avatar Sep 25 '24 14:09 macmade