Link, don't just compile, when probing compiler builtins#1052
Closed
hsbt wants to merge 1 commit into
Closed
Conversation
have_builtin_func used try_compile, so on MSVC a bare implicit declaration of __builtin_clzll passes (C4013 is only a warning unless promoted to an error), HAVE_BUILTIN___BUILTIN_CLZLL gets defined, and the parser fails to link with LNK2019 for __builtin_clzll. try_link resolves the builtin inline on GCC/Clang and fails on MSVC, giving the correct answer on every toolchain regardless of warning flags.
There was a problem hiding this comment.
Pull request overview
This PR fixes a Windows (mswin/MSVC) build failure for the JSON parser C extension by making the builtin-function probe actually link, preventing a compile-only false positive that later becomes an unresolved external at link time.
Changes:
- Switch the
have_builtin_funcprobe fromtry_compiletotry_linkso the check validates linkability on MSVC. - Ensure
HAVE_BUILTIN___BUILTIN_CLZLLis only defined when the builtin is truly usable, allowing correct fallback selection.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
|
Since 13 years ago, Ruby's config.h defines |
Member
|
Right, let's use |
matzbot
pushed a commit
to ruby/ruby
that referenced
this pull request
Jul 13, 2026
We don't need to perform out own checks. Fix: ruby/json#1052 ruby/json@db70b14321
Member
|
|
Member
Author
|
👌 Thanks! |
pull Bot
pushed a commit
to Mattlk13/json
that referenced
this pull request
Jul 13, 2026
We don't need to perform out own checks. Fix: ruby#1052
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Building the parser C extension against an official Ruby 3.3 mswin binary (
x64-mswin64_140) fails at link time with an unresolved__builtin_clzll:The root cause is in
ext/json/ext/parser/extconf.rb.have_builtin_funcprobes the builtin withtry_compile, which only compiles and never links. On MSVC__builtin_clzlldoes not exist, soint main() { __builtin_clzll(0); return 0; }merely triggersC4013('__builtin_clzll' undefined; assuming extern returning int), a warning by default. The compile succeeds,HAVE_BUILTIN___BUILTIN_CLZLLgets defined, andffc_nlz_int64inext/json/ext/vendor/fast_float_parser.hselects the__builtin_clzllbranch, which then fails to link.The fix is to use
try_linkinstead oftry_compileinhave_builtin_func. On GCC/Clang the builtin is resolved inline with no external symbol, so linking succeeds and the fast path is kept exactly as before. On MSVC there is no such symbol, so linking fails and json correctly falls back. This is the right answer on every toolchain and does not depend on any warning-as-error flag.Ruby 3.4+ mswin happens to mask this because ruby/ruby
091c7d4a54promotesC4013to an error via-we4013, so the oldtry_compileprobe already fails there. Ruby 3.3 mswin (and any config whereC4013stays a warning) still hits the false positive, and json's CI apparently runs a Ruby that already carries-we4013, which is why it slipped through. RubyInstaller (mingw/gcc) is unaffected because gcc really provides the builtin.Verification,
cl.exeonx64-mswin64_140, using the exact probe source:With the fix,
ruby extconf.rbreportschecking for __builtin_clzll()... noand the parser builds and links cleanly, with no__builtin_clzllsymbol left inparser.obj.An alternative, or additional belt-and-suspenders guard, would be to gate the
__builtin_clzllbranch infast_float_parser.hondefined(__GNUC__) || defined(__clang__). Thetry_linkchange alone fully resolves the issue, so I kept the change minimal and did not touch the header. Adding mswin Ruby 3.3 to the CI matrix would catch this class of regression, but I left that as a maintainer call rather than editing CI here.