Skip to content

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

@kavon

Description

@kavon
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.

Metadata

Metadata

Assignees

Labels

actor isolationFeature → concurrency: Actor isolationbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconcurrencyFeature: umbrella label for concurrency language featureslet & varFeature: constant and variable declarationsstatic declarationsFeature → declarations: Static declarationsswift 5.9unexpected behaviorBug: Unexpected behavior or incorrect output

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions