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

Behavior of postfix operator has been changed in Swift6.1 #80265

Closed
muukii opened this issue Mar 25, 2025 · 4 comments · Fixed by #80303
Closed

Behavior of postfix operator has been changed in Swift6.1 #80265

muukii opened this issue Mar 25, 2025 · 4 comments · Fixed by #80303
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@muukii
Copy link

muukii commented Mar 25, 2025

Description

No response

Reproduction

the below code works in Swift6.0.
but it gets errors in Swift6.1

postfix operator &>

public postfix func &> <T>(argument: consuming T) -> _FlowDownBox<T> {
  .init(consume argument)
}

public struct _FlowDownBox<Value>: ~Copyable {
  
  public var value: Value
  
  public init(_ value: consuming Value) {
    self.value = consume value
  }
  
  
  public consuming func perform<U>(_ transform: (consuming Value) throws -> U) rethrows -> U {
    try transform(value)
  }
  
}

func test() {
  
  var x: Int?
  
  x&>.perform { (v: Int?) in
    
  }
  
  x?&>.perform { (v: Int) in
    
  }
  
  var box: Box? = Box(value: 1)
  
  box?.value&>.perform { (v: Int) in
    
  }
  
}

struct Box {
  var value: Int
}

Swift 6.1

Image

Expected behavior

It should work the same way, I guess.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

No response

@muukii muukii added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 25, 2025
@airspeedswift
Copy link
Member

Shorter reduction:

extension Int {
    func f() { print("I am an Int") }
}

postfix operator §
postfix func §(argument: Int) -> Int {
    argument
}

var x: Int? = 1
x.f() // compiles in 6.0.3, in more recent compilers Value of optional type 'Int?' must be unwrapped to refer to member 'f' of wrapped base type 'Int'

@xedin
Copy link
Contributor

xedin commented Mar 26, 2025

It looks like the problem here is related to pre-check, optional evaluation expression used to be at the top but now it wraps postfix unary expression for §. cc @hamishknight and @rintaro

@hamishknight
Copy link
Contributor

This would have regressed with #76981, I can take it

@muukii
Copy link
Author

muukii commented Mar 27, 2025

@hamishknight Thank you!

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

Successfully merging a pull request may close this issue.

4 participants