Skip to content

Fix SILCombine miscompile for discard on non-copyable types #68110

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)

// 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
  }
}

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
  }
}
@atrick
Copy link
Contributor Author

atrick commented Aug 24, 2023

@swift-ci test

Copy link
Contributor

@nate-chandler nate-chandler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@atrick atrick merged commit 5dd2cf7 into swiftlang:main Aug 24, 2023
@atrick atrick deleted the fix-silcombine-moveonly branch August 24, 2023 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants