Skip to content

always @preconcurrency import Glibc/Musl/Android/Bionic/WASILibc #1175

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
Mar 4, 2025

Conversation

weissi
Copy link
Contributor

@weissi weissi commented Feb 15, 2025

Related issues:

I've got a working program

@preconcurrency import Glibc
import Foundation

await Task {
  _ = fputs("hello\n", stderr)
}.value

but alas, if swift-format reformats it, it makes it (note that the two imports are reordered)

import Foundation
@preconcurrency import Glibc

await Task {
  _ = fputs("hello\n", stderr)
}.value

which unfortunately doesn't compile

$ swift format --in-place test.swift && swiftc -swift-version 6 test.swift && echo OK
test.swift:5:24: error: reference to var 'stderr' is not concurrency-safe because it involves shared mutable state
3 | 
4 | await Task {
5 |   _ = fputs("hello\n", stderr)
  |                        `- error: reference to var 'stderr' is not concurrency-safe because it involves shared mutable state
6 | }.value
7 | 

SwiftGlibc.stderr:1:12: note: var declared here
1 | public var stderr: UnsafeMutablePointer<FILE>!
  |            `- note: var declared here

the problem is that Foundation imports Glibc/Musl but doesn't mark them @preconcurrency.


This has been used to rewrite the sources:

find Sources/ -name '*.swift' | while read -r line; do sed -r -i '' 's/^( *)(@_exported )?import (Musl|Android|Bionic|Glibc|WASILibc)/\1\2@preconcurrency import \3/g' "$line"; done

@weissi weissi force-pushed the jw-preconcurrency-glibc branch from 7c73fc2 to 662a5f1 Compare February 15, 2025 10:20
@parkera parkera requested a review from al45tair February 19, 2025 23:43
@parkera
Copy link
Contributor

parkera commented Feb 19, 2025

@swift-ci test

@al45tair
Copy link
Contributor

Interesting. I wonder whether this is really the right fix, or whether we should be using API notes to annotate stderr et al properly.

@weissi
Copy link
Contributor Author

weissi commented Feb 26, 2025

Interesting. I wonder whether this is really the right fix, or whether we should be using API notes to annotate stderr et al properly.

That seems like a pretty big project and will always miss something, no? But of course, I'd be fine with that too.

But crucially until that's done, I think this is what we should do. We can remove the @preconcurrency import once that has been completed.

@weissi
Copy link
Contributor Author

weissi commented Feb 26, 2025

@swift-ci test macos

@weissi
Copy link
Contributor Author

weissi commented Mar 4, 2025

friendly ping @parkera

@parkera parkera merged commit 601e847 into swiftlang:main Mar 4, 2025
3 checks passed
@parkera
Copy link
Contributor

parkera commented Mar 4, 2025

@weissi if this one is also for 6.1, please cherry pick

@weissi weissi deleted the jw-preconcurrency-glibc branch March 4, 2025 17:29
@weissi
Copy link
Contributor Author

weissi commented Mar 4, 2025

@weissi if this one is also for 6.1, please cherry pick

That one's already done and merged: #1194

Lukasa pushed a commit to apple/swift-nio that referenced this pull request Mar 21, 2025
### Motivation:

The non-`Darwin` libcs don't have the correct concurrency annotations.
But due to these Swift bugs, it's important that the _first_ importer
uses `@preconcurrency`:

- swiftlang/swift#79414
- swiftlang/swift#77866

### Modifications:

Much like the Foundation (& corelibs) PRs such as
swiftlang/swift-foundation#1175 , use
`@preconcurrency import` for the non-`Darwin` libcs.

### Result:

Fewer bad warnings/errors in user code.
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.

3 participants