Skip to content

Conversation

@hnrklssn
Copy link
Member

A Swift module only imports a type definition once. This means that some clang modules may have function signatures imported with UnsafePointer<qux> even if qux is only forward declared in that module (which would normally be imported as OpaquePointer), because some Swift file in the module imported the clang module with the definition, so ClangImporter sees the definition. The macro expansion only imports the imports of the clang module it belongs to however, so namelookup for qux fails. This patch detects when this will result in an error and avoids attaching the macro in those cases.

rdar://165864358

@hnrklssn
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@hnrklssn
Copy link
Member Author

@swift-ci please smoke test

A Swift module only imports a type definition once. This means that some
clang modules may have function signatures imported with
`UnsafePointer<qux>` even if `qux` is only forward declared in that
module (which would normally be imported as `OpaquePointer`), because
some Swift file in the module imported the clang module with the
definition, so ClangImporter sees the definition. The macro expansion
only imports the imports of the clang module it belongs to however, so
namelookup for `qux` fails. This patch detects when this will result in
an error and avoids attaching the macro in those cases.

rdar://165864358
Implicit decls don't have an owning module, since multiple modules could
instantiate them on demand. Since no implicit functions currently have
any need for safe wrappers we can simply detect this and early exit. For
templated functions, we need to fetch the owning module from the
instantiation.
@hnrklssn
Copy link
Member Author

@swift-ci please smoke test

This fixes an assert when signatures contain
`struct forward_declared_t **`, and makes sure that
`struct foo { struct forward_declared_t *bar; }` does *not* block
swiftification, since `@_SwiftifyImport` does not need to access the
struct field.
@hnrklssn
Copy link
Member Author

@swift-ci please smoke test

Since decls with no module are imported to __ObjC this is not an issue,
because __ObjC is always implicitly imported. Added test case that would
previously assert.
@hnrklssn
Copy link
Member Author

@swift-ci please smoke test

@hnrklssn hnrklssn marked this pull request as ready for review December 19, 2025 01:28
Copy link
Contributor

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me, one question inline.

Action walkToTypePre(Type ty) override {
DLOG("Walking type:\n");
LLVM_DEBUG(DUMP(ty));
if (Type PointeeTy = ConcretePointeeType(ty)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we ever run into trouble with UnsafeBufferPointer and co?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in, could we ever encounter an UnsafeBufferPointer in the imported signature? Not that I know of

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't looked into the specifics of the implementation, but the approach looks good, thanks!

@hnrklssn
Copy link
Member Author

@swift-ci please smoke test windows platform

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants