Skip to content

Commit 99c3802

Browse files
Merge pull request swiftlang#78669 from AnthonyLatsis/andromeda
[NFC] Add some unit tests for diagnostic behaviors
2 parents 5a28475 + 87cf117 commit 99c3802

File tree

10 files changed

+143
-17
lines changed

10 files changed

+143
-17
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ ERROR(error_no_group_info,none,
4747
NOTE(brace_stmt_suggest_do,none,
4848
"did you mean to use a 'do' statement?", ())
4949

50-
// `error_in_future_swift_version` does not have a group because warnings of this kind
51-
// inherit the group from the wrapped error.
52-
WARNING(error_in_future_swift_version,none,
50+
// This does not have a group because warnings of this kind inherit the group
51+
// from the wrapped error.
52+
WARNING(error_in_swift_lang_mode,none,
5353
"%0; this is an error in the Swift %1 language mode",
5454
(DiagnosticInfo *, unsigned))
5555

56-
// `error_in_a_future_swift_version` does not have a group because warnings of this kind
57-
// inherit the group from the wrapped error.
58-
WARNING(error_in_a_future_swift_version,none,
56+
// This does not have a group because warnings of this kind inherit the group
57+
// from the wrapped error.
58+
WARNING(error_in_a_future_swift_lang_mode,none,
5959
"%0; this will be an error in a future Swift language mode",
6060
(DiagnosticInfo *))
6161

include/swift/AST/EducationalNotes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ EDUCATIONAL_NOTES(result_builder_missing_build_array,
8686
EDUCATIONAL_NOTES(multiple_inheritance,
8787
"multiple-inheritance.md")
8888

89-
EDUCATIONAL_NOTES(error_in_future_swift_version,
89+
EDUCATIONAL_NOTES(error_in_swift_lang_mode,
9090
"error-in-future-swift-version.md")
9191

9292
#undef EDUCATIONAL_NOTES

include/swift/Sema/ConstraintSystem.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1753,8 +1753,12 @@ class Solution {
17531753
return *getOverloadChoiceIfAvailable(locator);
17541754
}
17551755

1756-
/// Retrieve the overload choice associated with the given
1756+
/// Retrieve the overload choice for the callee associated with the given
17571757
/// locator.
1758+
SelectedOverload getCalleeOverloadChoice(ConstraintLocator *locator) const;
1759+
1760+
/// Retrieve the overload choice associated with the given
1761+
/// locator, if any.
17581762
std::optional<SelectedOverload>
17591763
getOverloadChoiceIfAvailable(ConstraintLocator *locator) const {
17601764
auto known = overloadChoices.find(locator);
@@ -1763,6 +1767,11 @@ class Solution {
17631767
return std::nullopt;
17641768
}
17651769

1770+
/// Retrieve the overload choice for the callee associated with the given
1771+
/// locator, if any.
1772+
std::optional<SelectedOverload>
1773+
getCalleeOverloadChoiceIfAvailable(ConstraintLocator *locator) const;
1774+
17661775
std::optional<SyntacticElementTarget>
17671776
getTargetFor(SyntacticElementTargetKey key) const;
17681777

lib/AST/DiagnosticEngine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ InFlightDiagnostic::limitBehaviorUntilSwiftVersion(
431431
// wrapIn will result in the behavior of the wrapping diagnostic.
432432
if (limit >= DiagnosticBehavior::Warning) {
433433
if (majorVersion > 6) {
434-
wrapIn(diag::error_in_a_future_swift_version);
434+
wrapIn(diag::error_in_a_future_swift_lang_mode);
435435
} else {
436-
wrapIn(diag::error_in_future_swift_version, majorVersion);
436+
wrapIn(diag::error_in_swift_lang_mode, majorVersion);
437437
}
438438
}
439439

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ Solution::resolveConcreteDeclRef(ValueDecl *decl,
188188
return ConcreteDeclRef(decl, subst);
189189
}
190190

191+
SelectedOverload
192+
Solution::getCalleeOverloadChoice(ConstraintLocator *locator) const {
193+
return getOverloadChoice(getCalleeLocator(locator));
194+
}
195+
196+
std::optional<SelectedOverload>
197+
Solution::getCalleeOverloadChoiceIfAvailable(ConstraintLocator *locator) const {
198+
return getOverloadChoiceIfAvailable(getCalleeLocator(locator));
199+
}
191200

192201
ConstraintLocator *Solution::getCalleeLocator(ConstraintLocator *locator,
193202
bool lookThroughApply) const {

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9364,10 +9364,9 @@ bool DefaultExprTypeMismatch::diagnoseAsError() {
93649364
emitDiagnostic(diag::cannot_convert_default_value_type_to_argument_type,
93659365
getFromType(), getToType(), paramIdx);
93669366

9367-
auto overload = getCalleeOverloadChoiceIfAvailable(locator);
9368-
assert(overload);
9367+
auto overload = getSolution().getCalleeOverloadChoice(locator);
93699368

9370-
auto *PD = getParameterList(overload->choice.getDecl())->get(paramIdx);
9369+
auto *PD = getParameterList(overload.choice.getDecl())->get(paramIdx);
93719370

93729371
auto note = emitDiagnosticAt(PD->getLoc(), diag::default_value_declared_here);
93739372

lib/Sema/CSDiagnostics.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ class FailureDiagnostic {
149149
return S.getOverloadChoiceIfAvailable(locator);
150150
}
151151

152-
/// Retrieve overload choice resolved for a callee for the anchor
153-
/// of a given locator.
152+
/// Retrieve the overload choice for the callee associated with the given
153+
/// locator, if any.
154154
std::optional<SelectedOverload>
155155
getCalleeOverloadChoiceIfAvailable(ConstraintLocator *locator) const {
156-
return getOverloadChoiceIfAvailable(S.getCalleeLocator(locator));
156+
return S.getCalleeOverloadChoiceIfAvailable(locator);
157157
}
158158

159159
ConstraintLocator *

unittests/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_swift_unittest(SwiftASTTests
33
ASTDumperTests.cpp
44
ASTWalkerTests.cpp
55
IndexSubsetTests.cpp
6+
DiagnosticBehaviorTests.cpp
67
DiagnosticConsumerTests.cpp
78
DiagnosticGroupsTests.cpp
89
SourceLocTests.cpp
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//===--- DiagnosticBehaviorTests.cpp --------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/AST/DiagnosticEngine.h"
14+
#include "swift/AST/DiagnosticsFrontend.h"
15+
#include "swift/Basic/SourceManager.h"
16+
#include "gtest/gtest.h"
17+
18+
using namespace swift;
19+
20+
namespace {
21+
class TestDiagnosticConsumer : public DiagnosticConsumer {
22+
llvm::function_ref<void(const DiagnosticInfo &)> callback;
23+
24+
public:
25+
TestDiagnosticConsumer(decltype(callback) callback) : callback(callback) {}
26+
27+
void handleDiagnostic(SourceManager &SM,
28+
const DiagnosticInfo &Info) override {
29+
this->callback(Info);
30+
}
31+
};
32+
} // end anonymous namespace
33+
34+
static void diagnosticBehaviorTestCase(
35+
llvm::function_ref<void(DiagnosticEngine &)> diagnose,
36+
llvm::function_ref<void(DiagnosticEngine &, const DiagnosticInfo &)>
37+
callback,
38+
unsigned expectedNumCallbackCalls) {
39+
SourceManager sourceMgr;
40+
DiagnosticEngine diags(sourceMgr);
41+
42+
unsigned count = 0;
43+
44+
const auto countingCallback = [&](const DiagnosticInfo &info) {
45+
++count;
46+
callback(diags, info);
47+
};
48+
49+
TestDiagnosticConsumer consumer(countingCallback);
50+
diags.addConsumer(consumer);
51+
diagnose(diags);
52+
diags.removeConsumer(consumer);
53+
54+
EXPECT_EQ(count, expectedNumCallbackCalls);
55+
}
56+
57+
TEST(DiagnosticBehavior, WarnUntilSwiftLangMode) {
58+
diagnosticBehaviorTestCase(
59+
[](DiagnosticEngine &diags) {
60+
diags.setLanguageVersion(version::Version({5}));
61+
diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib)
62+
.warnUntilSwiftVersion(4);
63+
},
64+
[](DiagnosticEngine &diags, const DiagnosticInfo &info) {
65+
EXPECT_EQ(info.Kind, DiagnosticKind::Error);
66+
EXPECT_EQ(info.FormatString,
67+
diags.diagnosticStringFor(
68+
diag::error_immediate_mode_missing_stdlib.ID));
69+
},
70+
/*expectedNumCallbackCalls=*/1);
71+
72+
diagnosticBehaviorTestCase(
73+
[](DiagnosticEngine &diags) {
74+
diags.setLanguageVersion(version::Version({4}));
75+
diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib)
76+
.warnUntilSwiftVersion(5);
77+
},
78+
[](DiagnosticEngine &diags, const DiagnosticInfo &info) {
79+
EXPECT_EQ(info.Kind, DiagnosticKind::Warning);
80+
EXPECT_EQ(info.FormatString,
81+
diags.diagnosticStringFor(diag::error_in_swift_lang_mode.ID));
82+
83+
auto wrappedDiagInfo = info.FormatArgs.front().getAsDiagnostic();
84+
EXPECT_EQ(wrappedDiagInfo->FormatString,
85+
diags.diagnosticStringFor(
86+
diag::error_immediate_mode_missing_stdlib.ID));
87+
},
88+
/*expectedNumCallbackCalls=*/1);
89+
90+
diagnosticBehaviorTestCase(
91+
[](DiagnosticEngine &diags) {
92+
diags.setLanguageVersion(version::Version({4}));
93+
diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib)
94+
.warnUntilSwiftVersion(99);
95+
},
96+
[](DiagnosticEngine &diags, const DiagnosticInfo &info) {
97+
EXPECT_EQ(info.Kind, DiagnosticKind::Warning);
98+
EXPECT_EQ(info.FormatString,
99+
diags.diagnosticStringFor(
100+
diag::error_in_a_future_swift_lang_mode.ID));
101+
102+
auto wrappedDiagInfo = info.FormatArgs.front().getAsDiagnostic();
103+
EXPECT_EQ(wrappedDiagInfo->FormatString,
104+
diags.diagnosticStringFor(
105+
diag::error_immediate_mode_missing_stdlib.ID));
106+
},
107+
/*expectedNumCallbackCalls=*/1);
108+
}

unittests/AST/DiagnosticGroupsTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ TEST(DiagnosticGroups, DiagnosticsWrappersInheritGroups) {
105105
.limitBehaviorUntilSwiftVersion(DiagnosticBehavior::Warning, 99);
106106
},
107107
[](const DiagnosticInfo &info) {
108-
EXPECT_EQ(info.ID, diag::error_in_a_future_swift_version.ID);
108+
EXPECT_EQ(info.ID, diag::error_in_a_future_swift_lang_mode.ID);
109109
EXPECT_TRUE(info.FormatString.ends_with(" [DeprecatedDeclaration]"));
110110
},
111111
/*expectedNumCallbackCalls=*/1);

0 commit comments

Comments
 (0)