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

[SR-16009] MainActor static-let initialization not enforced on main thread. #58270

Closed
kavon opened this issue Mar 17, 2022 · 7 comments
Closed
Assignees
Labels
actor isolation Feature → concurrency: Actor isolation bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself concurrency Feature: umbrella label for concurrency language features let & var Feature: constant and variable declarations static declarations Feature → declarations: Static declarations swift 5.9 unexpected behavior Bug: Unexpected behavior or incorrect output

Comments

@kavon
Copy link
Contributor

kavon commented Mar 17, 2022

Previous ID SR-16009
Radar rdar://problem/90436577
Original Reporter @kavon
Type Bug
Environment

swift-driver version: 1.26.9 Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Concurrency
Assignee @kavon
Priority Medium

md5: f6b9b747f9f450df4eddd1f7c0fe8e4f

cloned from:

Issue Description:

@MainActor 
struct Bar {
  init() { 
    print("bar init is main", Thread.isMainThread) 
  } 
  func barCall() { 
    print("bar call is main", Thread.isMainThread) 
  } 
} 

@MainActor 
struct Foo { 
  @MainActor 
  static let global = Foo() 

  init() { 
    print("foo init is main", Thread.isMainThread) 
    let b = Bar() 
    b.barCall() 
  } 

  func fooCall() { 
    print("foo call is main", Thread.isMainThread) 
  } 

} 

Task.detached { 
  await Foo.global.fooCall() 
} 
// prints: 
// foo init is main false 
// bar init is main false 
// bar call is main false 
// foo call is main true

While fooCall() is enforced on the main thread, the initialization of the static variable is not, which breaks the contract of @MainActor.

@kavon
Copy link
Contributor Author

kavon commented Mar 17, 2022

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@moreindirection
Copy link

This is still present in Swift 5.8

@AnthonyLatsis AnthonyLatsis added actor isolation Feature → concurrency: Actor isolation swift 5.9 unexpected behavior Bug: Unexpected behavior or incorrect output let & var Feature: constant and variable declarations static declarations Feature → declarations: Static declarations labels Apr 22, 2023
@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Apr 22, 2023

Revision update: Swift version 5.9-dev (LLVM 91080e65df791d6, Swift c1d5118)

@bradleymackey
Copy link
Contributor

This is still present in Swift 5.9

@hborla
Copy link
Member

hborla commented Nov 3, 2023

Fixed by #69598 and #69616

@hborla hborla closed this as completed Nov 3, 2023
@moreindirection
Copy link

This is great news - thank you for fixing!

When will this fix be available? Swift 5.10?

@bradleymackey
Copy link
Contributor

bradleymackey commented Nov 6, 2023

@moreindirection If #69607 gets merged it will be in 5.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actor isolation Feature → concurrency: Actor isolation bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself concurrency Feature: umbrella label for concurrency language features let & var Feature: constant and variable declarations static declarations Feature → declarations: Static declarations swift 5.9 unexpected behavior Bug: Unexpected behavior or incorrect output
Projects
None yet
Development

No branches or pull requests

5 participants