Skip to content

Cannot assign self inside ~Copyable struct initializer under certain circumstances #83780

@dblsaiko

Description

@dblsaiko

Description

With certain struct configurations, assigning self inside of an initializer of a ~Copyable struct gives a seemingly nonsensical error in the initializer that assigns self:

error: conditional initialization or destruction of noncopyable types is not supported; this variable must be consistently in an initialized or uninitialized state through every code path

Reproduction

public struct NonCopyable2: ~Copyable {
  let dummy: Int
  var string: String?

  init(dummy: Int, string: String?) {
    self.dummy = dummy
    self.string = string
  }

  init() {
    self = NonCopyable2(dummy: 0, string: "")
  }
}
/Users/saiko/Src/nucom/Sources/NucomSupport/Lexer/GrammarFile.swift:154:3: error: conditional initialization or destruction of noncopyable types is not supported; this variable must be consistently in an initialized or uninitialized state through every code path
152 |   }
153 | 
154 |   init() {
    |   `- error: conditional initialization or destruction of noncopyable types is not supported; this variable must be consistently in an initialized or uninitialized state through every code path
155 |     self = NonCopyable2(dummy: 0, string: "")
156 |   }

Changing either the ‘var’ to ‘let’, the ‘String?’ to ‘Optional<String>’ (!), the ‘String’ to ‘Int’, or removing the ‘dummy’ member clears the error.

Expected behavior

The code compiles

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)
Target: arm64-apple-macosx16.0

Additional information

Encountered trying to work around #83779.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions