-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[cxx-interop] Fix crash when importing C++ forward-declared template specializations in typedefs #84186
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
fahadnayyar
merged 2 commits into
swiftlang:main
from
fahadnayyar:fnayyar/rdar-147595723
Sep 30, 2025
+118
−0
Merged
[cxx-interop] Fix crash when importing C++ forward-declared template specializations in typedefs #84186
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
test/Interop/Cxx/templates/Inputs/ForwardDeclaredSpecialization.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#ifndef FORWARD_DECLARED_SPECIALIZATION_H | ||
#define FORWARD_DECLARED_SPECIALIZATION_H | ||
|
||
// Basic template definition | ||
template <typename T> | ||
struct BasicTemplate { | ||
T value; | ||
}; | ||
|
||
// Case 1: Forward-declared specialization (should NOT import) | ||
template <> | ||
struct BasicTemplate<int>; | ||
typedef BasicTemplate<int> ForwardDeclaredInt; | ||
|
||
// Case 2: Complete specialization (should import successfully) | ||
template <> | ||
struct BasicTemplate<double> { | ||
double value; | ||
double getValue() const { return value; } | ||
}; | ||
typedef BasicTemplate<double> CompleteDouble; | ||
|
||
// Case 3: Specialization defined after typedef (should import) | ||
template <> | ||
struct BasicTemplate<float>; | ||
typedef BasicTemplate<float> FloatTypedef; | ||
|
||
template <> | ||
struct BasicTemplate<float> { | ||
float value; | ||
}; | ||
|
||
// Case 4: For comparison - forward-declared non-templated struct (have same behavior) | ||
struct ForwardDeclaredStruct; | ||
typedef ForwardDeclaredStruct ForwardDeclaredStructType; | ||
|
||
// Case 5: Complete non-templated struct (imports successfully) | ||
struct CompleteStruct { | ||
int value; | ||
}; | ||
typedef CompleteStruct CompleteStructType; | ||
|
||
// Template for partial specialization test | ||
template <typename T, typename U> | ||
struct PartialTemplate { | ||
T first; | ||
U second; | ||
}; | ||
|
||
// Case 6: Forward-declared partial specialization (should NOT import - same as explicit) | ||
template <typename T> | ||
struct PartialTemplate<T*, int>; // Forward declaration only | ||
typedef PartialTemplate<double*, int> ForwardDeclaredPartial; | ||
|
||
// Case 7: Complete partial specialization (should import successfully) | ||
template <typename T> | ||
struct PartialTemplate<T*, double> { | ||
T* ptr; | ||
double value; | ||
}; | ||
typedef PartialTemplate<int*, double> CompletePartial; | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
test/Interop/Cxx/templates/forward-declared-specialization.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// RUN: %target-typecheck-verify-swift -I %S/Inputs -cxx-interoperability-mode=default | ||
|
||
import ForwardDeclaredSpecialization | ||
|
||
func testForwardDeclaredSpecialization(_ param: ForwardDeclaredInt) { | ||
// expected-error@-1 {{cannot find type 'ForwardDeclaredInt' in scope}} | ||
} | ||
|
||
func testCompleteSpecialization(_ param: CompleteDouble) { | ||
let _ = param.getValue() | ||
} | ||
|
||
func testSpecializationDefinedAfter(_ param: FloatTypedef) { | ||
let _ = param.value | ||
} | ||
|
||
func testForwardDeclaredStruct(_ param: ForwardDeclaredStructType) { | ||
// expected-error@-1 {{cannot find type 'ForwardDeclaredStructType' in scope}} | ||
} | ||
|
||
func testCompleteStruct(_ param: CompleteStructType) { | ||
let _ = param.value | ||
} | ||
|
||
func testForwardDeclaredPartial(_ param: ForwardDeclaredPartial) { | ||
// expected-error@-1 {{cannot find type 'ForwardDeclaredPartial' in scope}} | ||
} | ||
|
||
func testCompletePartial(_ param: CompletePartial) { | ||
let _ = param.ptr | ||
let _ = param.value | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: it’s not clear what the behavior is the same as. Better to make it explicit imo:
This suggestion isn’t blocking, i.e., no need to change the comment and be forced to re-run CI.