Skip to content

Conversation

Rajveer100
Copy link
Contributor

Resolves #80261

When assigning properties for non-copyable structs globally or in final class, a destroy instruction is incorrectly placed in the basic block causing compiler crash.

…r `UncheckedTakeEnumDataAddrInst`

preventing use after destroy for `~Copyable` structs in special cases.

Resolves swiftlang#80261

When assigning properties for non-copyable structs globally or in final class,
a destroy instruction is incorrectly placed in the basic block causing compiler crash.
@Rajveer100 Rajveer100 requested a review from kavon as a code owner April 23, 2025 12:32
@Rajveer100
Copy link
Contributor Author

Rajveer100 commented Apr 23, 2025

Let me know if this isn't the right place to handle. Both cases provided by @YOCKOW do not crash after this change:

1:

struct NonCopyable: ~Copyable {
  var value: Int = 0
}

var nc: NonCopyable? = NonCopyable()
nc?.value = 1

2:

struct NonCopyable: ~Copyable {
  var value: Int = 0
}

final class Holder { // The compiler doesn't crash if this is a noncopyable struct.
  var nc: NonCopyable? = NonCopyable()

  func f() {
    nc?.value = 1
  }
}

@Rajveer100
Copy link
Contributor Author

Sharing my thought process, referring from #80261 (comment):

I think that the problem is for optionals, when we are in a potentially global context (or final class), and there isn't any outer basic block, a consume is assumed by the checker causing destroy instruction to be inserted way early.

In this case, we are consuming before assigning the property for the non-copyable struct.

@YOCKOW YOCKOW requested review from meg-gupta and eeckstein April 24, 2025 03:36
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.

Combination of ~Copyable + optional chaining + assignment crashes the compiler.
1 participant