Skip to content

Commit 2062ee8

Browse files
author
Harlan Haskins
authored
Merge pull request #26999 from harlanhaskins/my-internal-conflict
[Sema] Point to declaration when looking for a member type
2 parents 3b15fae + fb54651 commit 2062ee8

File tree

7 files changed

+33
-5
lines changed

7 files changed

+33
-5
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,12 @@ static Type diagnoseUnknownType(TypeResolution resolution,
12231223
diags.diagnose(comp->getIdLoc(), diag::invalid_member_type,
12241224
comp->getIdentifier(), parentType)
12251225
.highlight(parentRange);
1226+
// Note where the type was defined, this can help diagnose if the user
1227+
// expected name lookup to find a module when there's a conflicting type.
1228+
if (auto typeDecl = parentType->getNominalOrBoundGenericNominal()) {
1229+
ctx.Diags.diagnose(typeDecl, diag::decl_declared_here,
1230+
typeDecl->getFullName());
1231+
}
12261232
}
12271233
}
12281234
return ErrorType::get(ctx);

test/Generics/generic_types.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ var yarray : YArray = [1, 2, 3]
189189
var xarray : XArray = [1, 2, 3]
190190

191191
// Type parameters can be referenced only via unqualified name lookup
192-
struct XParam<T> {
192+
struct XParam<T> { // expected-note{{'XParam' declared here}}
193193
func foo(_ x: T) {
194194
_ = x as T
195195
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// Step 1. Create a module with the name ModuleType
4+
// RUN: echo 'public struct MyStruct {}' | %target-swift-frontend -emit-module -o %t/ModuleType.swiftmodule -
5+
6+
// Step 2. Create a second module with a type named ModuleType
7+
// RUN: echo 'public struct ModuleType {}' | %target-swift-frontend -emit-module -o %t/SecondModule.swiftmodule -
8+
9+
// Step 3. Import both modules and try to use `ModuleType.MyStruct`
10+
// Make sure we emitted a note saying that we failed while trying to
11+
// look up into `SecondModule.ModuleType`
12+
// RUN: not %target-swift-frontend -typecheck %s -I %t 2>&1 | %FileCheck %s
13+
14+
import ModuleType
15+
import SecondModule
16+
17+
public func f(_ x: ModuleType.MyStruct) {}
18+
19+
// CHECK: error: 'MyStruct' is not a member type of 'ModuleType'
20+
// CHECK: SecondModule.ModuleType:1:15: note: 'ModuleType' declared here
21+
// CHECK: public struct ModuleType {
22+
// CHECK: ^

test/decl/ext/extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protocol P3 {
103103
func foo() -> Assoc
104104
}
105105

106-
struct X3 : P3 {
106+
struct X3 : P3 { // expected-note{{'X3' declared here}}
107107
}
108108

109109
extension X3.Assoc { // expected-error{{'Assoc' is not a member type of 'X3'}}

test/decl/nested/type_in_function.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func genericFunction<T>(t: T) {
129129
class First : Second<T>.UnknownType { }
130130
// expected-error@-1 {{type 'First' cannot be nested in generic function 'genericFunction(t:)'}}
131131
// expected-error@-2 {{'UnknownType' is not a member type of 'Second<T>'}}
132-
class Second<T> : Second { }
132+
class Second<T> : Second { } // expected-note{{'Second' declared here}}
133133
// expected-error@-1 {{type 'Second' cannot be nested in generic function 'genericFunction(t:)'}}
134134
// expected-error@-2 {{'Second' inherits from itself}}
135135
}

test/decl/protocol/special/coding/class_codable_member_type_lookup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ struct SynthesizedClass : Codable {
174174

175175
// Classes which don't get synthesized Codable implementations should expose the
176176
// appropriate CodingKeys type.
177-
struct NonSynthesizedClass : Codable {
177+
struct NonSynthesizedClass : Codable { // expected-note 4 {{'NonSynthesizedClass' declared here}}
178178
// No synthesized type since we implemented both methods.
179179
init(from decoder: Decoder) throws {}
180180
func encode(to encoder: Encoder) throws {}

test/decl/protocol/special/coding/struct_codable_member_type_lookup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ struct SynthesizedStruct : Codable {
174174

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

0 commit comments

Comments
 (0)