Skip to content

[ClangImporter] Forward generic parameters while importing generic @c… #17164

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 2 commits into from
Jun 14, 2018

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Jun 13, 2018

…ompatibility_alias

When importing @compatibility_alias declarations check if underlying declaration
is generic and if so, forward generic environment and generic parameters (if any)
to newly created typealias declaration, otherwise there is going to be a mismatch
between type associated with typealias and its declaration which leads to crashes.

Resolves: rdar://problem/39849926

@xedin
Copy link
Contributor Author

xedin commented Jun 13, 2018

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Jun 13, 2018

@swift-ci please test source compatibility

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

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

Thanks, but this isn't the only place where the importer creates typealiases for generic types. The other is renames; see SwiftDeclConverter::importCompatibilityTypeAlias (unrelated to @compatibility_alias).

if (auto *GTD = dyn_cast<GenericTypeDecl>(typeDecl)) {
typealias->setGenericEnvironment(GTD->getGenericEnvironment());
if (GTD->isGeneric())
typealias->setGenericParams(GTD->getGenericParams()->clone(dc));
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't the typealias have to be the DeclContext?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean GenericContext? I'm checking for GenericTypeDecl here because typeDecl already is a TypeDecl so not sure if it makes sense to check lower than that?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, sorry, I mean in the call to clone.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, sure, I'll change that!

@@ -60,8 +60,12 @@
@interface SwiftNameTestErrorSub : SwiftNameTestError
@end

@interface SwiftGenericNameTest<T> : NSObject
Copy link
Contributor

Choose a reason for hiding this comment

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

Please include a test where T is constrained (either to a class or to a protocol or both).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think particular case T is constrained to NSObject, I have a test which tried to pass Int to it and fails, but I'll add more too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added one more class which has T constrained to NSNumber *

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it's just constrained to AnyObject. NSNumber is good enough!

@xedin xedin force-pushed the rdar-39849926 branch 2 times, most recently from 197f2d7 to 9315b55 Compare June 13, 2018 21:09
@xedin
Copy link
Contributor Author

xedin commented Jun 13, 2018

@jrose-apple I've made changes to importCompatibilityTypeAlias and fixed clone to use GTD instead of dc, WDYT?

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

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

Tests look good, code still a bit off!

if (auto *GTD = dyn_cast<GenericTypeDecl>(typeDecl)) {
typealias->setGenericEnvironment(GTD->getGenericEnvironment());
if (GTD->isGeneric())
typealias->setGenericParams(GTD->getGenericParams()->clone(GTD));
Copy link
Contributor

Choose a reason for hiding this comment

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

This is still wrong; it's supposed to be the new DeclContext, not the old one. That's typealias.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I get it now, sorry - it has to use typealias because that's the scope for the new generic parameters, sorry about this...

if (GTD && !isa<ProtocolDecl>(GTD)) {
alias->setGenericEnvironment(GTD->getGenericEnvironment());
if (GTD->isGeneric())
alias->setGenericParams(GTD->getGenericParams()->clone(GTD));
Copy link
Contributor

Choose a reason for hiding this comment

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

(here too)

xedin added 2 commits June 13, 2018 14:59
…ompatibility_alias

When importing @compatibility_alias declarations check if underlying declaration
is generic and if so, forward generic environment and generic parameters (if any)
to newly created typealias declaration, otherwise there is going to be a mismatch
between type associated with typealias and its declaration which leads to crashes.

Resolves: rdar://problem/39849926
While trying to import declaration which requires renaming forward
generic parameters (if any) to newly created typealias.
@xedin
Copy link
Contributor Author

xedin commented Jun 13, 2018

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Jun 13, 2018

@swift-ci please test source compatibility

@xedin
Copy link
Contributor Author

xedin commented Jun 13, 2018

@swift-ci please test macOS platform

1 similar comment
@xedin
Copy link
Contributor Author

xedin commented Jun 14, 2018

@swift-ci please test macOS platform

@swiftlang swiftlang deleted a comment from swift-ci Jun 14, 2018
@xedin
Copy link
Contributor Author

xedin commented Jun 14, 2018

@swift-ci please test source compatibility

@xedin xedin merged commit 2be766d into swiftlang:master Jun 14, 2018
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