Skip to content

Commit bc9babb

Browse files
committed
Diagnostics: Use public module name in diagnostics
1 parent 214da85 commit bc9babb

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

lib/AST/DiagnosticEngine.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,9 @@ static void formatDiagnosticArgument(StringRef Modifier,
799799
// Figure out the name we want to print.
800800
DeclName name;
801801
if (includeName) {
802-
if (auto VD = dyn_cast<ValueDecl>(D))
802+
if (auto MD = dyn_cast<ModuleDecl>(D))
803+
name = MD->getPublicModuleName(/*mustBeVisible=*/true);
804+
else if (auto VD = dyn_cast<ValueDecl>(D))
803805
name = VD->getName();
804806
else if (auto PGD = dyn_cast<PrecedenceGroupDecl>(D))
805807
name = PGD->getName();
@@ -1371,7 +1373,9 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
13711373
// build the name of the buffer.
13721374
SmallVector<StringRef, 4> nameComponents;
13731375
while (dc) {
1374-
nameComponents.push_back(cast<ModuleDecl>(dc)->getName().str());
1376+
auto publicName = cast<ModuleDecl>(dc)->
1377+
getPublicModuleName(/*mustBeVisible*/true);
1378+
nameComponents.push_back(publicName.str());
13751379
dc = dc->getParent();
13761380
}
13771381

test/Sema/public-module-name.swift

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file --leading-lines %s %t
3+
4+
/// Build the libraries.
5+
// RUN: %target-swift-frontend %t/LibCore.swift \
6+
// RUN: -emit-module-path %t/LibCore.swiftmodule \
7+
// RUN: -emit-module-interface-path %t/LibCore.swiftinterface \
8+
// RUN: -enable-library-evolution -swift-version 6 \
9+
// RUN: -public-module-name Lib
10+
// RUN: %target-swift-typecheck-module-from-interface(%t/LibCore.swiftinterface)
11+
12+
// RUN: %target-swift-frontend %t/LibMiddle.swift -I %t \
13+
// RUN: -emit-module-path %t/LibMiddle.swiftmodule \
14+
// RUN: -emit-module-interface-path %t/LibMiddle.swiftinterface \
15+
// RUN: -enable-library-evolution -swift-version 6 \
16+
// RUN: -public-module-name Lib
17+
// RUN: %target-swift-typecheck-module-from-interface(%t/LibMiddle.swiftinterface) -I %t
18+
19+
// RUN: %target-swift-frontend %t/Lib.swift -I %t \
20+
// RUN: -emit-module-path %t/Lib.swiftmodule \
21+
// RUN: -emit-module-interface-path %t/Lib.swiftinterface \
22+
// RUN: -enable-library-evolution -swift-version 6
23+
// RUN: %target-swift-typecheck-module-from-interface(%t/Lib.swiftinterface) -I %t
24+
25+
// RUN: %target-swift-frontend %t/LibUnrelated.swift -I %t \
26+
// RUN: -emit-module-path %t/LibUnrelated.swiftmodule \
27+
// RUN: -enable-library-evolution -swift-version 6
28+
29+
/// Check flag in swiftinterface
30+
// RUN: cat %t/LibCore.swiftinterface | %FileCheck --check-prefix=CHECK-FLAG %s
31+
// RUN: cat %t/LibMiddle.swiftinterface | %FileCheck --check-prefix=CHECK-FLAG %s
32+
// CHECK-FLAG: -public-module-name Lib
33+
34+
/// Build clients against binary swiftmodules.
35+
/// First errors in files, then diagnostics in other files.
36+
// RUN: %target-swift-frontend -typecheck %t/ClientPublic.swift -o %t -I %t \
37+
// RUN: -enable-library-evolution -swift-version 6 \
38+
// RUN: -verify
39+
// RUN: not %target-swift-frontend -typecheck %t/ClientPublic.swift -o %t -I %t \
40+
// RUN: -enable-library-evolution -swift-version 6 \
41+
// RUN: -diagnostic-style llvm \
42+
// RUN: 2>&1 | %FileCheck %t/ClientPublic.swift
43+
44+
// RUN: %target-swift-frontend -typecheck %t/ClientMiddle.swift -o %t -I %t \
45+
// RUN: -enable-library-evolution -swift-version 6 \
46+
// RUN: -verify
47+
// RUN: not %target-swift-frontend -typecheck %t/ClientMiddle.swift -o %t -I %t \
48+
// RUN: -enable-library-evolution -swift-version 6 \
49+
// RUN: 2>&1 | %FileCheck %t/ClientMiddle.swift
50+
51+
/// Test more diagnostics referencing modules.
52+
// RUN: %target-swift-frontend -typecheck %t/ClientAccessLevelOnImports.swift -o %t -I %t \
53+
// RUN: -enable-library-evolution -swift-version 6 \
54+
// RUN: -verify
55+
56+
/// Build client against textual swiftinterfaces.
57+
// RUN: rm %t/LibCore.swiftmodule %t/LibMiddle.swiftmodule %t/Lib.swiftmodule
58+
// RUN: %target-swift-frontend -typecheck %t/ClientPublic.swift -I %t \
59+
// RUN: -enable-library-evolution -swift-version 6 \
60+
// RUN: -verify
61+
// RUN: not %target-swift-frontend -typecheck %t/ClientPublic.swift -o %t -I %t \
62+
// RUN: -enable-library-evolution -swift-version 6 \
63+
// RUN: -diagnostic-style llvm \
64+
// RUN: 2>&1 | %FileCheck %t/ClientPublic.swift
65+
66+
// RUN: %target-swift-frontend -typecheck %t/ClientMiddle.swift -o %t -I %t \
67+
// RUN: -enable-library-evolution -swift-version 6 \
68+
// RUN: -verify
69+
// RUN: not %target-swift-frontend -typecheck %t/ClientMiddle.swift -o %t -I %t \
70+
// RUN: -enable-library-evolution -swift-version 6 \
71+
// RUN: 2>&1 | %FileCheck %t/ClientMiddle.swift
72+
73+
// RUN: %target-swift-frontend -typecheck %t/ClientAccessLevelOnImports.swift -o %t -I %t \
74+
// RUN: -enable-library-evolution -swift-version 6 \
75+
// RUN: -verify
76+
77+
//--- LibCore.swift
78+
public func ambiguous() {}
79+
public func coreFunc() {}
80+
81+
//--- LibMiddle.swift
82+
import LibCore
83+
public func ambiguous() {}
84+
85+
//--- Lib.swift
86+
@_exported import LibCore
87+
@_exported import LibMiddle
88+
89+
//--- LibUnrelated.swift
90+
public func ambiguous() {}
91+
92+
//--- ClientPublic.swift
93+
import Lib
94+
import LibUnrelated
95+
96+
ambiguous() // expected-error {{ambiguous use of 'ambiguous()'}}
97+
// CHECK-NOT: LibCore
98+
// CHECK-NOT: LibMiddle
99+
// CHECK: LibUnrelated.ambiguous:1:13: note: found this candidate in module 'LibUnrelated'
100+
// CHECK: Lib.ambiguous:1:13: note: found this candidate in module 'Lib'
101+
// CHECK: Lib.ambiguous:1:13: note: found this candidate in module 'Lib'
102+
103+
//--- ClientMiddle.swift
104+
import LibCore
105+
import LibMiddle
106+
107+
ambiguous() // expected-error {{ambiguous use of 'ambiguous()'}}
108+
// CHECK: LibCore.ambiguous:1:13: note: found this candidate in module 'LibCore'
109+
// CHECK: LibMiddle.ambiguous:1:13: note: found this candidate in module 'LibMiddle'
110+
111+
//--- ClientAccessLevelOnImports.swift
112+
internal import Lib // expected-note {{global function 'coreFunc()' imported as 'internal' from 'Lib' here}}
113+
114+
@inlinable
115+
public func foo() {
116+
coreFunc() // expected-error {{global function 'coreFunc()' is internal and cannot be referenced from an '@inlinable' function}}
117+
}

0 commit comments

Comments
 (0)