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

Using borrowing On String Causes Compiler Error #69252

Closed
thecoolwinter opened this issue Oct 18, 2023 · 1 comment
Closed

Using borrowing On String Causes Compiler Error #69252

thecoolwinter opened this issue Oct 18, 2023 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@thecoolwinter
Copy link

Description

When using the borrowing parameter on a String in an enum initializer the compiler reports the following error:

Copy of noncopyable typed value. This is a compiler bug. Please file a bug with a small example of the bug

I've attached a code sample demonstrating this behavior in the next section. This error occurs if the scenario is on the same enum with a static function borrowing a String or a regular function.

Steps to reproduce

Compiling the following code snippet will produce the error using Swift 5.9:

public enum LineEnding: String {
    /// The default unix `\n` character
    case lineFeed = "\n"
    /// MacOS line ending `\r` character
    case carriageReturn = "\r"
    /// Windows line ending sequence `\r\n`
    case carriageReturnLineFeed = "\r\n"

    /// Initialize a line ending from a line string.
    /// - Parameter line: The line to use
    public init?(line: borrowing String) {
        guard let lastChar = line.last, // ❌ Copy of noncopyable typed value. This is a compiler bug. Please file a bug with a small example of the bug
              let lineEnding = LineEnding(rawValue: String(lastChar)) else { return nil }
        self = lineEnding
    }

    // These also cause the same error:
    static func makeLineEnding(_ line: borrowing String) -> LineEnding? {
         guard let lastChar = line.last, // ❌ ...
         ...
    }

    func makeLineEnding(_ line: borrowing String) -> LineEnding? {
        guard let lastChar = line.last, // ❌ ...
         ...
    }
}

Expected behavior

The initializer, static function, or member function should be able to borrow a String value to ensure the value isn't copied.

Environment

  • swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
  • Xcode 15.0, Build version 15A240d
  • Target: arm64-apple-macosx13.0
@thecoolwinter thecoolwinter added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Oct 18, 2023
nate-chandler added a commit to nate-chandler/swift that referenced this issue Jun 12, 2024
@nate-chandler
Copy link
Contributor

Regression test added in #74361 .

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

No branches or pull requests

2 participants