swift-foundation icon indicating copy to clipboard operation
swift-foundation copied to clipboard

URL.init(string:) handling of empty strings has changed in Swift 6

Open rnro opened this issue 1 year ago • 1 comments

Initializing a URL from an empty string in Swift 6 on Linux now returns a non-nil URL value. In earlier Swift versions and on macOS it returns nil.

On Linux Swift 5.10:

  1> import Foundation
  2> let u = Foundation.URL(string: "")
  [snip]
  3> print(u)
nil

On Linux Swift 6.0.1:

  1> import Foundation
  2> let u = Foundation.URL(string: "")
  [snip]
  3> print(u)
Optional()

rnro avatar Oct 03 '24 10:10 rnro

This is an expected behavior change when updating URL to RFC 3986. A URL always contains a path, and it may be empty. "" is considered a valid relative path, and something that can be resolved against a base URL. The RFC lists resolving "" against a base URL as an example in section 5.4.1. We'll monitor bincompat from this change.

jrflat avatar Oct 03 '24 17:10 jrflat