Skip to content

Commit dda7e96

Browse files
authored
Merge pull request #70424 from xymus/ambiguity
AST: Specify the module name in errors on ambiguities across modules
2 parents b28aa2b + 72083bf commit dda7e96

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4271,6 +4271,9 @@ WARNING(warn_use_async_alternative,none,
42714271

42724272
NOTE(found_candidate,none,
42734273
"found this candidate", ())
4274+
NOTE(found_candidate_in_module,none,
4275+
"found this candidate %select{|in module %1}0",
4276+
(bool, const ModuleDecl *))
42744277
NOTE(found_candidate_type,none,
42754278
"found candidate with type %0", (Type))
42764279

lib/Sema/ConstraintSystem.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5663,12 +5663,17 @@ bool ConstraintSystem::diagnoseAmbiguity(ArrayRef<Solution> solutions) {
56635663
case OverloadChoiceKind::Decl:
56645664
case OverloadChoiceKind::DeclViaDynamic:
56655665
case OverloadChoiceKind::DeclViaBridge:
5666-
case OverloadChoiceKind::DeclViaUnwrappedOptional:
5666+
case OverloadChoiceKind::DeclViaUnwrappedOptional: {
56675667
// FIXME: show deduced types, etc, etc.
5668-
if (EmittedDecls.insert(choice.getDecl()).second)
5669-
DE.diagnose(choice.getDecl(), diag::found_candidate);
5668+
auto decl = choice.getDecl();
5669+
if (EmittedDecls.insert(decl).second) {
5670+
auto declModule = decl->getDeclContext()->getParentModule();
5671+
bool printModuleName = declModule != DC->getParentModule();
5672+
DE.diagnose(decl, diag::found_candidate_in_module,
5673+
printModuleName, declModule);
5674+
}
56705675
break;
5671-
5676+
}
56725677
case OverloadChoiceKind::KeyPathApplication:
56735678
case OverloadChoiceKind::DynamicMemberLookup:
56745679
case OverloadChoiceKind::KeyPathDynamicMemberLookup:
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
/// Compile the exact same source file into 2 different modules.
5+
// RUN: %target-swift-frontend -emit-module -o %t/A.swiftmodule %t/Lib.swift \
6+
// RUN: -emit-module-source-info -module-name A -package-name pkg \
7+
// RUN: -enable-testing
8+
// RUN: %target-swift-frontend -emit-module -o %t/B.swiftmodule %t/Lib.swift \
9+
// RUN: -emit-module-source-info -module-name B -package-name pkg \
10+
// RUN: -enable-testing
11+
12+
// Build a client importing both modules and hitting the ambiguities.
13+
// RUN: not %target-swift-frontend -typecheck -I %t %t/Client.swift -package-name pkg 2> %t/out
14+
// RUN: cat %t/out | %FileCheck %s
15+
16+
//--- Lib.swift
17+
public func publicAmbiguity() {}
18+
package func packageAmbiguity() {}
19+
internal func internalAmbiguity() {}
20+
21+
//--- Client.swift
22+
@testable import A
23+
@testable import B
24+
25+
func foo() {
26+
publicAmbiguity()
27+
// CHECK: error: ambiguous use of 'publicAmbiguity()'
28+
// CHECK-NEXT: publicAmbiguity()
29+
// CHECK-NEXT: ^
30+
// CHECK-NEXT: Lib.swift:1:13: note: found this candidate in module 'A'
31+
// CHECK-NEXT: public func publicAmbiguity() {}
32+
// CHECK-NEXT: ^
33+
// CHECK-NEXT: Lib.swift:1:13: note: found this candidate in module 'B'
34+
// CHECK-NEXT: public func publicAmbiguity() {}
35+
// CHECK-NEXT: ^
36+
37+
packageAmbiguity()
38+
// CHECK: error: ambiguous use of 'packageAmbiguity()'
39+
// CHECK-NEXT: packageAmbiguity()
40+
// CHECK-NEXT: ^
41+
// CHECK-NEXT: Lib.swift:2:14: note: found this candidate in module 'A'
42+
// CHECK-NEXT: package func packageAmbiguity() {}
43+
// CHECK-NEXT: ^
44+
// CHECK-NEXT: Lib.swift:2:14: note: found this candidate in module 'B'
45+
// CHECK-NEXT: package func packageAmbiguity() {}
46+
// CHECK-NEXT: ^
47+
48+
internalAmbiguity()
49+
// CHECK: error: ambiguous use of 'internalAmbiguity()'
50+
// CHECK-NEXT: internalAmbiguity()
51+
// CHECK-NEXT: ^
52+
// CHECK-NEXT: Lib.swift:3:15: note: found this candidate in module 'A'
53+
// CHECK-NEXT: internal func internalAmbiguity() {}
54+
// CHECK-NEXT: ^
55+
// CHECK-NEXT: Lib.swift:3:15: note: found this candidate in module 'B'
56+
// CHECK-NEXT: internal func internalAmbiguity() {}
57+
// CHECK-NEXT: ^
58+
}

test/diagnostics/testable-printast-locations.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
ambiguous()
1010

1111
// CHECK: testable-printast-locations.swift:[[@LINE-2]]:1: error: ambiguous use of 'ambiguous()'
12-
// CHECK: ModuleA.ambiguous (internal):1:15: note: found this candidate
13-
// CHECK: ModuleB.ambiguous (internal):1:15: note: found this candidate
12+
// CHECK: ModuleA.ambiguous (internal):1:15: note: found this candidate in module 'ModuleA'
13+
// CHECK: ModuleB.ambiguous (internal):1:15: note: found this candidate in module 'ModuleB'

0 commit comments

Comments
 (0)