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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4966,6 +4966,12 @@ namespace {
Impl.importSourceLoc(decl->getLocation()),
/*genericparams=*/nullptr, dc);

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

typealias->setUnderlyingType(Impl.getSugaredTypeReference(typeDecl));
return typealias;
}
Expand Down Expand Up @@ -5182,18 +5188,6 @@ Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
if (!typeDecl)
return nullptr;

// Deliberately use an UnboundGenericType to avoid having to translate over
// generic parameters.
Type underlyingType;
if (auto *underlyingAlias = dyn_cast<TypeAliasDecl>(typeDecl)) {
if (underlyingAlias->isGeneric())
underlyingType = underlyingAlias->getUnboundGenericType();
else
underlyingType = Impl.getSugaredTypeReference(underlyingAlias);
} else {
underlyingType = cast<NominalTypeDecl>(typeDecl)->getDeclaredType();
}

auto dc = Impl.importDeclContextOf(decl,
compatibilityName.getEffectiveContext());
if (!dc)
Expand All @@ -5204,7 +5198,15 @@ Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
decl, AccessLevel::Public, Impl.importSourceLoc(decl->getLocStart()),
SourceLoc(), compatibilityName.getDeclName().getBaseIdentifier(),
Impl.importSourceLoc(decl->getLocation()), /*generic params*/nullptr, dc);
alias->setUnderlyingType(underlyingType);

auto *GTD = dyn_cast<GenericTypeDecl>(typeDecl);
if (GTD && !isa<ProtocolDecl>(GTD)) {
alias->setGenericEnvironment(GTD->getGenericEnvironment());
if (GTD->isGeneric())
alias->setGenericParams(GTD->getGenericParams()->clone(alias));
}

alias->setUnderlyingType(Impl.getSugaredTypeReference(typeDecl));

// Record that this is the official version of this declaration.
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}] = alias;
Expand Down
4 changes: 2 additions & 2 deletions test/APINotes/versioned-objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func testRenamedGeneric() {

class SwiftClass {}

// CHECK-DIAGS-3:[[@LINE+1]]:{{[0-9]+}}: error: 'RenamedGeneric' requires that 'SwiftClass' inherit from 'Base'
// CHECK-DIAGS-3:[[@LINE+1]]:{{[0-9]+}}: error: 'OldRenamedGeneric' requires that 'SwiftClass' inherit from 'Base'
let _: OldRenamedGeneric<SwiftClass> = RenamedGeneric<SwiftClass>()
// CHECK-DIAGS-4:[[@LINE-1]]:{{[0-9]+}}: error: 'RenamedGeneric' requires that 'SwiftClass' inherit from 'Base'
// CHECK-DIAGS-4:[[@LINE-1]]:{{[0-9]+}}: error: 'OldRenamedGeneric' requires that 'SwiftClass' inherit from 'Base'

// CHECK-DIAGS-3:[[@LINE+1]]:{{[0-9]+}}: error: 'RenamedGeneric' requires that 'SwiftClass' inherit from 'Base'
let _: RenamedGeneric<SwiftClass> = OldRenamedGeneric<SwiftClass>()
Expand Down
8 changes: 8 additions & 0 deletions test/ClangImporter/Inputs/custom-modules/ObjCIRExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@
@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!

@end

@interface SwiftConstrGenericNameTest<T:NSNumber *> : NSNumber
@end

int global_int SWIFT_NAME(GlobalInt);

@compatibility_alias SwiftNameAlias SwiftNameTest;
@compatibility_alias SwiftGenericNameAlias SwiftGenericNameTest;
@compatibility_alias SwiftConstrGenericNameAlias SwiftConstrGenericNameTest;

#pragma clang assume_nonnull end
23 changes: 23 additions & 0 deletions test/ClangImporter/generic_compatibility_alias.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %empty-directory(%t)
// RUN: %build-clang-importer-objc-overlays
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -module-name generic_compat_alias -I %S/Inputs/custom-modules -typecheck -verify %s

// REQUIRES: objc_interop
// REQUIRES: OS=macosx

import ObjectiveC
import Foundation
import ObjCIRExtras

func foo(_: SwiftConstrGenericNameAlias<String>) {
// expected-error@-1 {{'SwiftConstrGenericNameAlias' requires that 'String' inherit from 'NSNumber'}}
// expected-note@-2 {{requirement specified as 'T' : 'NSNumber' [with T = String]}}
}

func faz(_: SwiftGenericNameAlias<Int>) {
// expected-error@-1 {{'SwiftGenericNameAlias' requires that 'Int' be a class type}}
// expected-note@-2 {{requirement specified as 'T' : 'AnyObject' [with T = Int]}}
}

func bar(_: SwiftGenericNameAlias<NSNumber>) {} // Ok
func baz<T: AnyObject>(_: SwiftGenericNameAlias<T>) {} // Ok
12 changes: 12 additions & 0 deletions test/ClangImporter/objc_ir.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ func testCompatibilityAliasMangling(obj: SwiftNameAlias) {
// CHECK: call void @llvm.dbg.declare(metadata %TSo13SwiftNameTestC** {{%.+}}, metadata ![[SWIFT_NAME_ALIAS_VAR:[0-9]+]], metadata !DIExpression())
}

func testGenericCompatibilityAliasMangling(generic_obj: SwiftGenericNameAlias<NSNumber>) {
// CHECK: call void @llvm.dbg.declare(metadata %TSo20SwiftGenericNameTestCySo8NSNumberCG** {{%.+}}, metadata ![[SWIFT_GENERIC_NAME_ALIAS_VAR:[0-9]+]], metadata !DIExpression())
}

func testConstrGenericCompatibilityAliasMangling(constr_generic_obj: SwiftConstrGenericNameAlias<NSNumber>) {
// CHECK: call void @llvm.dbg.declare(metadata %TSo26SwiftConstrGenericNameTestCySo8NSNumberCG** {{%.+}}, metadata ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_VAR:[0-9]+]], metadata !DIExpression())
}

// CHECK: linkonce_odr hidden {{.*}} @"$SSo1BC3intABSgs5Int32V_tcfcTO"
// CHECK: load i8*, i8** @"\01L_selector(initWithInt:)"
Expand All @@ -347,3 +354,8 @@ func testCompatibilityAliasMangling(obj: SwiftNameAlias) {
// CHECK: ![[SWIFT_NAME_ALIAS_VAR]] = !DILocalVariable(name: "obj", arg: 1, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[SWIFT_NAME_ALIAS_TYPE:[0-9]+]])
// CHECK: ![[SWIFT_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$SSo14SwiftNameAliasaD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, baseType: !{{[0-9]+}})

// CHECK: ![[SWIFT_GENERIC_NAME_ALIAS_VAR]] = !DILocalVariable(name: "generic_obj", arg: 1, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[SWIFT_GENERIC_NAME_ALIAS_TYPE:[0-9]+]])
// CHECK: ![[SWIFT_GENERIC_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$SSo21SwiftGenericNameAliasaySo8NSNumberCGD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, baseType: !{{[0-9]+}})

// CHECK: ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_VAR]] = !DILocalVariable(name: "constr_generic_obj", arg: 1, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_TYPE:[0-9]+]])
// CHECK: ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$SSo27SwiftConstrGenericNameAliasaySo8NSNumberCGD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, baseType: !{{[0-9]+}})