Skip to content

[Sema] Point to declaration when looking for a member type #26999

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
Sep 3, 2019
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
6 changes: 6 additions & 0 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,12 @@ static Type diagnoseUnknownType(TypeResolution resolution,
diags.diagnose(comp->getIdLoc(), diag::invalid_member_type,
comp->getIdentifier(), parentType)
.highlight(parentRange);
// Note where the type was defined, this can help diagnose if the user
// expected name lookup to find a module when there's a conflicting type.
if (auto typeDecl = parentType->getNominalOrBoundGenericNominal()) {
ctx.Diags.diagnose(typeDecl, diag::decl_declared_here,
typeDecl->getFullName());
}
}
}
return ErrorType::get(ctx);
Expand Down
2 changes: 1 addition & 1 deletion test/Generics/generic_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ var yarray : YArray = [1, 2, 3]
var xarray : XArray = [1, 2, 3]

// Type parameters can be referenced only via unqualified name lookup
struct XParam<T> {
struct XParam<T> { // expected-note{{'XParam' declared here}}
func foo(_ x: T) {
_ = x as T
}
Expand Down
22 changes: 22 additions & 0 deletions test/Sema/diag_module_conflict_with_type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %empty-directory(%t)

// Step 1. Create a module with the name ModuleType
// RUN: echo 'public struct MyStruct {}' | %target-swift-frontend -emit-module -o %t/ModuleType.swiftmodule -

// Step 2. Create a second module with a type named ModuleType
// RUN: echo 'public struct ModuleType {}' | %target-swift-frontend -emit-module -o %t/SecondModule.swiftmodule -

// Step 3. Import both modules and try to use `ModuleType.MyStruct`
// Make sure we emitted a note saying that we failed while trying to
// look up into `SecondModule.ModuleType`
// RUN: not %target-swift-frontend -typecheck %s -I %t 2>&1 | %FileCheck %s

import ModuleType
import SecondModule

public func f(_ x: ModuleType.MyStruct) {}

// CHECK: error: 'MyStruct' is not a member type of 'ModuleType'
// CHECK: SecondModule.ModuleType:1:15: note: 'ModuleType' declared here
// CHECK: public struct ModuleType {
// CHECK: ^
2 changes: 1 addition & 1 deletion test/decl/ext/extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protocol P3 {
func foo() -> Assoc
}

struct X3 : P3 {
struct X3 : P3 { // expected-note{{'X3' declared here}}
}

extension X3.Assoc { // expected-error{{'Assoc' is not a member type of 'X3'}}
Expand Down
2 changes: 1 addition & 1 deletion test/decl/nested/type_in_function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func genericFunction<T>(t: T) {
class First : Second<T>.UnknownType { }
// expected-error@-1 {{type 'First' cannot be nested in generic function 'genericFunction(t:)'}}
// expected-error@-2 {{'UnknownType' is not a member type of 'Second<T>'}}
class Second<T> : Second { }
class Second<T> : Second { } // expected-note{{'Second' declared here}}
// expected-error@-1 {{type 'Second' cannot be nested in generic function 'genericFunction(t:)'}}
// expected-error@-2 {{'Second' inherits from itself}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ struct SynthesizedClass : Codable {

// Classes which don't get synthesized Codable implementations should expose the
// appropriate CodingKeys type.
struct NonSynthesizedClass : Codable {
struct NonSynthesizedClass : Codable { // expected-note 4 {{'NonSynthesizedClass' declared here}}
// No synthesized type since we implemented both methods.
init(from decoder: Decoder) throws {}
func encode(to encoder: Encoder) throws {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ struct SynthesizedStruct : Codable {

// Structs which don't get synthesized Codable implementations should expose the
// appropriate CodingKeys type.
struct NonSynthesizedStruct : Codable {
struct NonSynthesizedStruct : Codable { // expected-note 4 {{'NonSynthesizedStruct' declared here}}
// No synthesized type since we implemented both methods.
init(from decoder: Decoder) throws {}
func encode(to encoder: Encoder) throws {}
Expand Down