Skip to content

Handle 'import struct Foo' where Foo is a non-nominal typealias #14797

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
Feb 26, 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
8 changes: 6 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,13 @@ ERROR(decl_does_not_exist_in_module,none,
"%1 does not exist in module %2",
(/*ImportKind*/ unsigned, Identifier, Identifier))
ERROR(imported_decl_is_wrong_kind,none,
"%0 was imported as '%1', but is a "
"%select{%error|type|struct|class|enum|protocol|variable|function}2",
"%0 was imported as '%1', but is "
"%select{%error|a type|a struct|a class|an enum|a protocol|a variable|"
"a function}2",
(Identifier, StringRef, /*ImportKind*/ unsigned))
ERROR(imported_decl_is_wrong_kind_typealias,none,
"%0 %1 cannot be imported as '%2'",
(DescriptiveDeclKind, Type, StringRef))
ERROR(ambiguous_decl_in_module,none,
"ambiguous name %0 in module %1", (Identifier, Identifier))

Expand Down
5 changes: 4 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,10 @@ ImportKind ImportDecl::getBestImportKind(const ValueDecl *VD) {

case DeclKind::TypeAlias: {
Type type = cast<TypeAliasDecl>(VD)->getDeclaredInterfaceType();
return getBestImportKind(type->getAnyNominal());
auto *nominal = type->getAnyNominal();
if (!nominal)
return ImportKind::Type;
return getBestImportKind(nominal);
}

case DeclKind::Accessor:
Expand Down
46 changes: 40 additions & 6 deletions lib/Sema/NameBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ static bool isCompatibleImportKind(ImportKind expected, ImportKind actual) {
llvm_unreachable("Unhandled ImportKind in switch.");
}

static bool isNominalImportKind(ImportKind kind) {
switch (kind) {
case ImportKind::Module:
llvm_unreachable("module imports do not bring in decls");
case ImportKind::Struct:
case ImportKind::Class:
case ImportKind::Enum:
case ImportKind::Protocol:
return true;
case ImportKind::Type:
case ImportKind::Var:
case ImportKind::Func:
return false;
}
}

static const char *getImportKindString(ImportKind kind) {
switch (kind) {
case ImportKind::Module:
Expand Down Expand Up @@ -258,12 +274,30 @@ void NameBinder::addImport(
diagnose(next, diag::found_candidate);

} else if (!isCompatibleImportKind(ID->getImportKind(), *actualKind)) {
diagnose(ID, diag::imported_decl_is_wrong_kind,
declPath.front().first,
getImportKindString(ID->getImportKind()),
static_cast<unsigned>(*actualKind))
.fixItReplace(SourceRange(ID->getKindLoc()),
getImportKindString(*actualKind));
Optional<InFlightDiagnostic> emittedDiag;
if (*actualKind == ImportKind::Type &&
isNominalImportKind(ID->getImportKind())) {
assert(decls.size() == 1 &&
"if we start suggesting ImportKind::Type for, e.g., a mix of "
"structs and classes, we'll need a different message here");
assert(isa<TypeAliasDecl>(decls.front()) &&
"ImportKind::Type is only the best choice for a typealias");
auto *typealias = cast<TypeAliasDecl>(decls.front());
emittedDiag.emplace(diagnose(ID,
diag::imported_decl_is_wrong_kind_typealias,
typealias->getDescriptiveKind(),
typealias->getDeclaredInterfaceType(),
getImportKindString(ID->getImportKind())));
} else {
emittedDiag.emplace(diagnose(ID, diag::imported_decl_is_wrong_kind,
declPath.front().first,
getImportKindString(ID->getImportKind()),
static_cast<unsigned>(*actualKind)));
}

emittedDiag->fixItReplace(SourceRange(ID->getKindLoc()),
getImportKindString(*actualKind));
emittedDiag->flush();

if (decls.size() == 1)
diagnose(decls.front(), diag::decl_declared_here,
Expand Down
9 changes: 9 additions & 0 deletions test/NameBinding/Inputs/DeclsUsedWrongly.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public var x: Int = 0

public enum Choice {
case yes, no, maybeSo
}

public typealias Callback = () -> Void

public typealias Pair<T> = (T, T)
12 changes: 10 additions & 2 deletions test/NameBinding/import-specific-fixits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/ambiguous_left.swift
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/ambiguous_right.swift
// RUN: %target-swift-frontend -emit-module -o %t -I %t %S/Inputs/ambiguous.swift
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/DeclsUsedWrongly.swift

// RUN: echo "public var x = Int()" | %target-swift-frontend -module-name FooBar -emit-module -o %t -
// RUN: %target-swift-frontend -typecheck -I %t -serialize-diagnostics-path %t.dia %s -verify
// RUN: c-index-test -read-diagnostics %t.dia > %t.deserialized_diagnostics.txt 2>&1
// RUN: %FileCheck --input-file=%t.deserialized_diagnostics.txt %s
Expand All @@ -29,7 +29,15 @@ import class Swift.Int64 // expected-error {{'Int64' was imported as 'class', bu

import class Swift.Bool // expected-error {{'Bool' was imported as 'class', but is a struct}} {{8-13=struct}}

import struct FooBar.x // expected-error {{'x' was imported as 'struct', but is a variable}} {{8-14=var}}
import struct DeclsUsedWrongly.x // expected-error {{'x' was imported as 'struct', but is a variable}} {{8-14=var}}

import struct DeclsUsedWrongly.Choice // expected-error {{'Choice' was imported as 'struct', but is an enum}} {{8-14=enum}}

import struct DeclsUsedWrongly.Callback // expected-error {{type alias 'Callback' (aka '() -> ()') cannot be imported as 'struct'}} {{8-14=typealias}}
import var DeclsUsedWrongly.Callback // expected-error {{'Callback' was imported as 'var', but is a type}} {{8-11=typealias}}

import struct DeclsUsedWrongly.Pair // expected-error {{type alias 'Pair' cannot be imported as 'struct'}} {{8-14=typealias}}
import var DeclsUsedWrongly.Pair // expected-error {{'Pair' was imported as 'var', but is a type}} {{8-11=typealias}}

import struct Swift.print // expected-error {{'print' was imported as 'struct', but is a function}} {{8-14=func}}

Expand Down