Skip to content

Conversation

j-hui
Copy link
Contributor

@j-hui j-hui commented Oct 14, 2025

Prior to this patch, we eagerly imported template type arguments. A consequence of doing so is that we over-instantiate (potentially unused) type arguments, which causes unnecessary errors.

The only apparent use we have for these type arguments are to check whether they are unsafe, so that we can mark the instantiated type as unsafe as well... even if the instantiated type does not use its unsafe template type argument at all.

Note that using un-instantiatable types in template type arguments is supported in C++. The test case included in this patch validates this behavior, for both missing member and incomplete type errors.

Note, also, that as of this patch, dumping the module interface of CxxModule actually causes swift-ide-test to emit compiler errors, since it tries to instantiate the invalid types MissingMember and IncompleteField. However, these errors are simply swallowed by swift-ide-test, so they should be harmless, though we should probably get rid of them entirely in future work.

rdar://145238539

Prior to this patch, we eagerly imported template type arguments.
A consequence of doing so is that we over-instantiate (potentially
unused) type arguments, which causes unnecessary errors.

The only apparent use we have for these type arguments are to check
whether they are unsafe, so that we can mark the instantiated type as
unsafe as well... even if the instantiated type does not use its unsafe
template type argument at all.

Note that using un-instantiatable types in template type arguments is
supported in C++. The test case included in this patch validates this
behavior, for both missing member and incomplete type errors.

Note, also, that as of this patch, dumping the module interface of
CxxModule actually causes swift-ide-test to emit compiler errors, since
it tries to instantiate the invalid types MissingMember<Empty> and
IncompleteField<Incomplete>. However, these errors are simply swallowed
by swift-ide-test, so they should be harmless, though we should probably
get rid of them entirely in future work.

rdar://145238539
@j-hui
Copy link
Contributor Author

j-hui commented Oct 14, 2025

@swift-ci please test

@hnrklssn
Copy link
Contributor

Do I understand correctly that when the template is instantiated, if the template argument is used inside the class, it is still imported at that point?

Do we want to also remove the logic for marking templated types as unsafe if the type parameter is unsafe? Does it risk being inconsistent, depending on if full importing is triggered or not?

@Xazax-hun
Copy link
Contributor

Xazax-hun commented Oct 14, 2025

Do we want to also remove the logic for marking templated types as unsafe if the type parameter is unsafe?

I think we don't want to do that.

Doing so preserves ClangImporter's behavior first introduced in
f12b48a, but do so without relying on
importing the type argument (since that can lead to template
over-instantiation).

This patch also promotes the logic of hasUnsafeType() to a standalone
SimpleRequest, to provide a QualType-typed entry point for evaluating
the safety of a Clang entity.
@j-hui
Copy link
Contributor Author

j-hui commented Oct 14, 2025

I do believe we want to consider a type unsafe even if the unsafe type argument is unused, because that matches what Swift is doing and we want the two models to be at least roughly in sync.

I didn't realize this was the safety model in Swift! I added back the behavior, and checked in some test code to ensure this is the case.

@j-hui
Copy link
Contributor Author

j-hui commented Oct 14, 2025

@swift-ci please test

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.

This looks great, thanks a lot! I have one minor suggestion inline.

@j-hui
Copy link
Contributor Author

j-hui commented Oct 15, 2025

@swift-ci Please test

@j-hui j-hui enabled auto-merge October 15, 2025 19:33
@j-hui j-hui merged commit dc4ff24 into swiftlang:main Oct 16, 2025
5 checks passed
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.

3 participants