Skip to content

Add explicit cast to nsec in android platforms #82139

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
Jun 11, 2025

Conversation

mhegazy
Copy link
Contributor

@mhegazy mhegazy commented Jun 10, 2025

I am seeing a build error for Android for invalid narrowing after #79139, #82154 did add a special case for non-windows, non-apple platform. but did not account for nsec implicit casting from long long to long. still seeing build errors on android.

D:/r/_work/swift-build/swift-build/SourceCache/swift/stdlib/public/Concurrency/DispatchGlobalExecutor.cpp:325:18: error: non-constant-expression cannot be narrowed from type 'long long' to 'long' in initializer list [-Wc++11-narrowing]
325 | .tv_nsec = nsec
| ^~~~

@mhegazy
Copy link
Contributor Author

mhegazy commented Jun 10, 2025

CC @phausler

struct timespec ts = {
.tv_sec = sec,
.tv_sec = static_cast<time_t>(sec),
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, doesn't this silently truncate? I do like the idea of merging the paths.

Copy link
Contributor Author

@mhegazy mhegazy Jun 10, 2025

Choose a reason for hiding this comment

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

time_t should be 64 bits on modern macos. This is why teh compiler was not complaining on the implicit cast. but yes it will truncate on Android, which i am assuming is ok for this use case.

Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't that move the time backwards then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah. i think it would truncate. but we were truncating on windows also before. @phausler, @ktoso and @grynspan thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

time_t is 64 bits on every 64-bit platform I know of (thankfully) and that includes Darwin. It could silently truncate on a 32-bit system, but I don't think those can in practical terms handle such high time_t values anyway.

Basically, yes it could truncate but by the time (2038) that becomes an issue, the platforms in question are hosed anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Note that the C standard defines timespec::tv_sec as a value of type time_t anyway, so good luck not truncating it. https://en.cppreference.com/w/c/chrono/timespec

@mhegazy mhegazy marked this pull request as ready for review June 10, 2025 15:53
@mhegazy mhegazy requested a review from ktoso as a code owner June 10, 2025 15:53
@mhegazy mhegazy force-pushed the fix_dsipatch_build_on_android branch 2 times, most recently from f5d8c14 to ed59cb4 Compare June 11, 2025 02:00
@mhegazy mhegazy force-pushed the fix_dsipatch_build_on_android branch from ed59cb4 to 43b49dd Compare June 11, 2025 02:01
@mhegazy mhegazy changed the title Add explicit cast to all platforms Add explicit cast to nsec in android platforms Jun 11, 2025
@mhegazy
Copy link
Contributor Author

mhegazy commented Jun 11, 2025

@compnerd #82154 did add a branch for non-wnidows/non-apple platform (aka linux/android). but still missed nsec cast. can we get this change in to unblock the build that is still failing.

@compnerd
Copy link
Member

@swift-ci please smoke test

@compnerd compnerd enabled auto-merge June 11, 2025 02:42
@compnerd compnerd merged commit 6b3e2cd into swiftlang:main Jun 11, 2025
3 checks passed
@mhegazy mhegazy deleted the fix_dsipatch_build_on_android branch June 11, 2025 14:33
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