Skip to content

[ClangImporter] Look through bounds attributes for template matching #82076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025

Conversation

hnrklssn
Copy link
Contributor

@hnrklssn hnrklssn commented Jun 7, 2025

When instantiating templated functions with pointers to the templated type, the ClangImporter does not strip type sugar. This strips type sugar for bounds attributes, to make sure that they import the same regardless of whether they are parsed or not.

rdar://151041990

@hnrklssn
Copy link
Contributor Author

hnrklssn commented Jun 7, 2025

@swift-ci please smoke test

@hnrklssn
Copy link
Contributor Author

hnrklssn commented Jun 7, 2025

These functions all contain this type of pattern:

if (isa<clang::PointerType>(paramTy) &&
             isa<clang::TemplateTypeParmType>(paramTy->getPointeeType())) {

My hunch is that they actually ought to use something like this (or equivalent with getAsAdjusted):

if (paramTy->getAs<clang::PointerType>() &&
             paramTy->getAs<clang::PointerType>()->getPointeeType()->getAs<clang::TemplateTypeParmType>() {

But I don't want to introduce unnecessary risk this late in the release cycle, so I elected to only strip the type sugar we know we need to strip, in case it affects other things. Especially since it seems like this template logic is all a bit of a hack anyways that eventually needs to be replaced; e.g. this doesn't seem to get imported:

template <class T>
void foo(T **p);

CC @egorzhdan in case you have more context

@Xazax-hun
Copy link
Contributor

My hunch is that they actually ought to use something like this

I have a similar hunch, we probably want to ignore all type sugar as these do not change how template instantiation work. Could you open a rdar to make sure we clean this up?

@hnrklssn
Copy link
Contributor Author

hnrklssn commented Jun 9, 2025

My hunch is that they actually ought to use something like this

I have a similar hunch, we probably want to ignore all type sugar as these do not change how template instantiation work. Could you open a rdar to make sure we clean this up?

Created rdar://152835871 ([ClangImporter] Ignore all type sugar for template instantiation) and rdar://152835941 ([ClangImporter] Generalize type matching for function template instantiation)

@hnrklssn hnrklssn force-pushed the skip-bounds-safety-types2 branch from d192deb to b209224 Compare June 10, 2025 14:48
@hnrklssn
Copy link
Contributor Author

@swift-ci please smoke test

When instantiating templated functions with pointers to the templated
type, the ClangImporter does not strip type sugar. This strips type
sugar for bounds attributes, to make sure that they import the same
regardless of whether they are parsed or not.

rdar://151041990
@hnrklssn hnrklssn force-pushed the skip-bounds-safety-types2 branch from b209224 to bd714f5 Compare June 10, 2025 16:12
@hnrklssn
Copy link
Contributor Author

@swift-ci please smoke test

@hnrklssn hnrklssn merged commit 00afb8b into swiftlang:main Jun 11, 2025
3 checks passed
hnrklssn added a commit to hnrklssn/swift that referenced this pull request Jun 11, 2025
…ypes2

When instantiating templated functions with pointers to the templated type, the ClangImporter does not strip type sugar. This strips type sugar for bounds attributes, to make sure that they import the same regardless of whether they are parsed or not.

rdar://151041990
(cherry picked from commit 00afb8b)
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.

2 participants