Skip to content

[5.9] Fix SILCombine miscompile for discard on non-copyable types #68119

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

Merged
merged 1 commit into from
Aug 24, 2023

Conversation

atrick
Copy link
Contributor

@atrick atrick commented Aug 24, 2023

Fixes rdar://113214179 (Raw layout types don't adhere to discard self)

main PR: #68110

// swift-frontend ./rawdeinit.swift -enable-experimental-feature RawLayout \ // -enable-builtin-module -emit-sil -o rawdeinit.sil

import Builtin

@_rawLayout(like: T)
public struct Cell<T>: ~Copyable {
  var ptr: UnsafeMutablePointer<T> {
    .init(Builtin.unprotectedAddressOfBorrow(self))
  }

  init(_ initialValue: T) {
    ptr.initialize(to: initialValue)
  }

  deinit {
    ptr.deinitialize(count: 1)
  }

  public consuming func get() -> T {
    let value = ptr.move()
    discard self // SILCombine inserts a destroy of self here
    return value
  }
}

(cherry picked from commit a946922)

Fixes rdar://113214179 (Raw layout types don't adhere to discard self)

// swift-frontend ./rawdeinit.swift -enable-experimental-feature RawLayout \
// -enable-builtin-module -emit-sil -o rawdeinit.sil

import Builtin

@_rawLayout(like: T)
public struct Cell<T>: ~Copyable {
  var ptr: UnsafeMutablePointer<T> {
    .init(Builtin.unprotectedAddressOfBorrow(self))
  }

  init(_ initialValue: T) {
    ptr.initialize(to: initialValue)
  }

  deinit {
    ptr.deinitialize(count: 1)
  }

  public consuming func get() -> T {
    let value = ptr.move()
    discard self // SILCombine inserts a destroy of self here
    return value
  }
}

(cherry picked from commit a946922)
@atrick atrick requested a review from a team as a code owner August 24, 2023 15:56
@atrick
Copy link
Contributor Author

atrick commented Aug 24, 2023

@swift-ci test

@atrick atrick merged commit fb3675e into swiftlang:release/5.9 Aug 24, 2023
@atrick atrick deleted the 5.9-fix-silcombine-moveonly branch August 24, 2023 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants