Skip to content

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Oct 11, 2025

Summary

Fix an internal issue with MIR type translation of recursive
imported types.

Details

In the MIR type IR, imported types use a dedicated type kind
(tkImported), which has the "to be treated as" type as the element/
base.

Due to unconditionally updating the PType->MIR mapping when
translating importced object types, usages of the imported types
within their own body (via a pointer/ref indirection) used the
tkStruct symbol, not the tkImported symbol.

type Foreign {.importc: "...".} = object
  field: ptr Foreign
  # ^^ the field had type `(tkPtr (tkStruct ...))`,
  # not `(tkPtr (tkImported ...))`

This led to internal type mismatches between the actual and computed
type of MIR access expression, but since the type of access expressions
is never recomputed at the moment, the bug had no user-facing effect.

To fix the bug, handleImported now adds a symbol and registers a
mapping right away, with creation of a MIR type symbol for the
"internal" type of importced types leaving the mapping as is.

In the MIR type IR, imported types use a dedicated type kind
(`tkImported`), which has the "to be treated as" type as the element/
base.

Due to unconditionally updating the `PType`->MIR mapping when
translating `importc`ed object types meant that usages of the imported
types within its own body (via a pointer/ref indirection) used the
`tkStruct` symbol, not the `tkImported` symbol.

```nim
type Foreign {.importc: "...".} = object
  field: ptr Foreign
  # ^^ the field had type `(tkPtr (tkStruct ...))`,
  # not `(tkPtr (tkImported ...))`
```

To fix this, `handleImported` now adds a symbol and registers a
mapping right away, with creation of a MIR type symbol for the
"internal" type of `importc`ed types leaving the mapping as is.
@zerbina zerbina added this to the MIR phase milestone Oct 11, 2025
@zerbina zerbina added bug Something isn't working compiler/backend Related to backend system of the compiler labels Oct 11, 2025
@zerbina zerbina moved this to In Progress in Rework the mid- and backend Oct 11, 2025
@saem
Copy link
Collaborator

saem commented Oct 13, 2025

/merge

Copy link

Merge requested by: @saem

Contents after the first section break of the PR description has been removed and preserved below:


Notes for Reviewers

@chore-runner chore-runner bot added this pull request to the merge queue Oct 13, 2025
Merged via the queue into nim-works:devel with commit 2e1da11 Oct 13, 2025
44 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in Rework the mid- and backend Oct 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working compiler/backend Related to backend system of the compiler

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants