Files
swift-mirror/test/SILGen/member_chains.swift
Hamish Knight e30e5a7539 [Sema] Fix optional chaining behavior with postfix operators
Postfix operators can further be chained within an optional binding
chain, so we need to make sure they're handled in
`getMemberChainSubExpr`. Unresolved member chains still don't allow
them, so we need to add a new `kind` parameter to differentiate the
behavior here.

rdar://147826988
2025-03-26 15:02:42 +00:00

37 lines
1.0 KiB
Swift

// RUN: %target-swift-emit-silgen -verify %s
struct ImplicitMembers: Equatable {
static var implicit = ImplicitMembers()
static var optional: ImplicitMembers? = ImplicitMembers()
static func createOptional() -> ImplicitMembers? {
ImplicitMembers()
}
var another: ImplicitMembers { ImplicitMembers() }
var anotherOptional: ImplicitMembers? { ImplicitMembers() }
}
// Make sure we can SILGen these without issue.
postfix operator ^
postfix func ^ (_ lhs: ImplicitMembers) -> Int { 0 }
extension Int {
func foo() {}
var optionalMember: Int? { 0 }
}
// https://github.com/swiftlang/swift/issues/80265
// Make sure optional chaining looks through postfix operators.
var x: ImplicitMembers?
let _ = x?^.foo()
let _ = x?^.optionalMember?.foo()
let _ = x?.another^.optionalMember?.foo()
// Make sure the unresolved member chain extends up to the postfix operator,
// but the optional chain covers the entire expr.
let _ = .optional?^.foo()
let _ = .createOptional()?^.foo()
let _ = .implicit.anotherOptional?^.foo()