Skip to content

Commit

Permalink
fix: compiler crash when compiling for iOS
Browse files Browse the repository at this point in the history
Summary
=======

Fix a compiler crash when compiling any program for iOS with threading
enabled. Only the iOS target was affected.

Details
=======

Due to `StrTab` ordering, within the `system` module, all calls to
`sizeof` outside of generic routines resolve to the `sizeof(x: T)`
symbol, foregoing overload resolution since `sizeof` is an "eager"
magic.

If called with the symbol of a type that has an unknown size (which
happens in a `when defined(ios)` guarded part of `syslocks`), the first
parameter's type of the callee reaching `mirgen` is a `tyGenericParam`,
and thus `mirgen.genArgs` attempts to translate the `skType` symbol
like a normal expression, triggering an assertion.

Always using overload resolution for `sizeof` would result in
unnecessary generic instantiations, so `mirgen` now uses dedicated
handling for the `mSizeOf` magic, always translating argument as a
type, thus fixing the crash.
  • Loading branch information
zerbina committed Aug 30, 2024
1 parent d393260 commit 41281f0
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions compiler/mir/mirgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,12 @@ proc genMagic(c: var TCtx, n: PNode; m: TMagic) =
c.emitOperandTree it, sink=true
else:
genCall(c, n)
of mSizeOf, mAlignOf:
# a late-resolved sizeof/alignof. Both have two variants, and they're
# normalized here
c.buildMagicCall m, rtyp:
# skip the surrounding typedesc
c.emitByVal typeLit(c.typeToMir(n[1].typ.skipTypes({tyTypeDesc})))

# arithmetic operations:
of mAddI, mSubI, mMulI, mDivI, mModI, mPred, mSucc:
Expand Down Expand Up @@ -1134,14 +1140,6 @@ proc genMagic(c: var TCtx, n: PNode; m: TMagic) =

else:
genCall(c, n)
of mAlignOf:
# instances of the magic inserted by ``liftdestructors`` and ``alignof(x)``
# calls where ``x`` is of an imported type with unknown alignment reach
# here. The code-generators only care about the types in both cases, so
# that's what we emit
c.buildMagicCall m, rtyp:
# skip the surrounding typedesc
c.emitByVal typeLit(c.typeToMir(n[1].typ.skipTypes({tyTypeDesc})))
of mGetTypeInfoV2:
if n[0].typ == nil:
# the compiler-generated version always uses a type as the argument
Expand Down

0 comments on commit 41281f0

Please sign in to comment.