Skip to content

Commit

Permalink
fix regression with imported distinct types (#1418)
Browse files Browse the repository at this point in the history
## Summary

Marking a `distinct T` type as imported resulted in multiple C
definitions for `T`, if `T` is a non-numeric, non-pointer type. This is
now fixed.

Fixes #1417.

## Details

When translating imported types, `mirtypes` always created a new type
symbol for the imported type's underlying type. For `tyAlias` and
`tyDistinct` types (or any other type kind in the `Skip` set), this
resulted in a duplicate of the underlying type symbol being created.
Since MIR types are identified by ID, both look separate to `cgen`, and
thus a C definition is emitted for each.

`handleImportedTypes` now goes through the caching mechanism for the
underlying type if its not the type marked with `.importc`, fixing the
issue.
  • Loading branch information
zerbina authored Aug 15, 2024
1 parent a1decd8 commit 4a02aaa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 5 additions & 3 deletions compiler/mir/mirtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1085,11 +1085,13 @@ proc handleImported(env: var TypeEnv, t: PType): TypeId =
if t.sym != nil and sfImportc in t.sym.flags:
# an imported type. It's wrapped in a ``tkImported``, referencing the
# underlying type
let base = typeSymToMir(env):
let base =
if t.kind in Skip:
t.lastSon.skipIrrelevant()
env.add t.lastSon.skipIrrelevant()
else:
t
# create a type symbol without going through the cache or ``.importc``
# handling
typeSymToMir(env, t)

discard getSize(env.config, t) # compute the sizes, alignments, and offsets

Expand Down
15 changes: 15 additions & 0 deletions tests/ccgbugs/timport_distinct.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
discard """
description: '''
Regression test for importing ``distinct`` types resulting in duplicate C
type definitions
'''
"""

type
Object = object
Imported {.importc: "int", nodecl.} = distinct Object

var x: Object
var y: Imported

# two type definitions for `Object` were emitted

0 comments on commit 4a02aaa

Please sign in to comment.