chs: fix GHC 9.2+ prim ambiguity in c2hs-processed source files#86
Open
mgajda wants to merge 1 commit into
Open
chs: fix GHC 9.2+ prim ambiguity in c2hs-processed source files#86mgajda wants to merge 1 commit into
mgajda wants to merge 1 commit into
Conversation
c2hs processes .chs files with its own C preprocessor, in which
`__GLASGOW_HASKELL__` is not defined. Bare guards of the form
#if __GLASGOW_HASKELL__ < N
therefore evaluate to `0 < N` (TRUE), so local bridge definitions of
`wordToWord64#`, `word64ToWord#`, and `wordToWord8#` are emitted
unconditionally into the generated .hs output.
GHC 9.2 added these operations to `GHC.Prim` / `GHC.Base` / `GHC.Exts`,
so on GHC 9.2+ the generated code contains a duplicate definition that
the compiler correctly rejects as ambiguous:
Ambiguous occurrence `wordToWord64#'
It could refer to either `GHC.Base.wordToWord64#'
or `Foreign.CUDA.Driver.Stream.wordToWord64#'
Fix: rewrite bare guards as
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ < N
`defined(__GLASGOW_HASKELL__)` evaluates to FALSE under c2hs, suppressing
the local definition there. GHC's own CPP pass sees a defined macro and
evaluates the version comparison normally.
Affected files: Driver/Stream.chs, Driver/Marshal.chs, Driver/Module/Query.chs
Tested: c2hs 0.28.8, GHC 9.4.7 and 9.6.x, CUDA 12.0, Ubuntu 24.04.
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.
Branch:
fix/ghc94-prim-guardsCommit:
chs: fix GHC 9.2+ prim ambiguity in c2hs-processed source filesBuilding with GHC 9.2 or later fails with ambiguous occurrence errors:
The .chs files guard local bridge definitions with
#if __GLASGOW_HASKELL__ < N. c2hs processes .chs files with its own C preprocessor, in which__GLASGOW_HASKELL__is undefined (evaluates to0). The guard is therefore always TRUE, and the local definitions ofwordToWord64#,word64ToWord#, andwordToWord8#are emitted unconditionally into the generated .hs output. GHC 9.2+ exports these same operations fromGHC.Base/GHC.Exts, so the compiler sees a duplicate and correctly rejects it.The fix changes bare guards of the form
#if __GLASGOW_HASKELL__ < Nto#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ < N. Under c2hs,defined(__GLASGOW_HASKELL__)is FALSE so the local definition is stripped. GHC's own CPP pass sees a defined macro and evaluates the version check normally.Affected:
Driver/Stream.chs,Driver/Marshal.chs,Driver/Module/Query.chs.Tested: c2hs 0.28.8, GHC 9.4.7, CUDA 12.0, Ubuntu 24.04.