swift icon indicating copy to clipboard operation
swift copied to clipboard

Unintentional (?) change to type inference of array literals (Swift 6 regression)

Open KeithBauerANZ opened this issue 1 year ago • 2 comments

Description

In swift 5, this compiles:

let a = 1
let x = [ a, a, nil, a ]
let y = [ a, a, nil, a ].map(\.?.magnitude)

In Swift 6, the assignment to y doesn't compile:

ArrayLiteralOfOptionals.swift:3:30: error: cannot use optional chaining on non-optional value of type 'Int'
1 | let a = 1
2 | let x = [ a, a, nil, a ]
3 | let y = [ a, a, nil, a ].map(\.?.magnitude)
  |                              `- error: cannot use optional chaining on non-optional value of type 'Int'
4 | 

ArrayLiteralOfOptionals.swift:3:17: error: 'nil' is not compatible with expected element type 'Int'
1 | let a = 1
2 | let x = [ a, a, nil, a ]
3 | let y = [ a, a, nil, a ].map(\.?.magnitude)
  |                 `- error: 'nil' is not compatible with expected element type 'Int'
4 | 

Something to do with how the special \.? keypath is handled?

Reproduction

let a = 1
let x = [ a, a, nil, a ]
let y = [ a, a, nil, a ].map(\.?.magnitude)

Expected behavior

Should probably continue to compile... don't think this is an intentional source-break?

Environment

swift-driver version: 1.111.2 Apple Swift version 6.0 (swiftlang-6.0.0.5.15 clang-1600.0.22.6) Target: arm64-apple-macosx14.0

Additional information

No response

KeithBauerANZ avatar Jul 11 '24 01:07 KeithBauerANZ

CC: @xedin

tbkka avatar Jul 11 '24 01:07 tbkka

note: the language mode appears to be unrelated. this example fails to typecheck on nightly builds using the Swift 5 language mode (from swift.godbolt.org: Swift version 6.0-dev (LLVM 96ec4bf19dbeed6, Swift 7e4425c97e8cbe4)).

jamieQ avatar Jul 11 '24 12:07 jamieQ