Skip to content

Commit 9b0e0f5

Browse files
committed
Follow up fixes with feedback from #74129
Didn't need to modify any tests since this shouldn't have any functional change. Just a slight cleanup.
1 parent 60e09ff commit 9b0e0f5

File tree

5 files changed

+38
-66
lines changed

5 files changed

+38
-66
lines changed

include/swift/Sema/CSFix.h

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/Type.h"
2525
#include "swift/AST/Types.h"
2626
#include "swift/Basic/Debug.h"
27+
#include "swift/Sema/Constraint.h"
2728
#include "swift/Sema/ConstraintLocator.h"
2829
#include "swift/Sema/FixBehavior.h"
2930
#include "llvm/ADT/ArrayRef.h"
@@ -479,8 +480,8 @@ enum class FixKind : uint8_t {
479480
/// parameter.
480481
///
481482
/// 2. Where we have a function that expects a function typed parameter with a
482-
/// sending result, but is passed a function typeed parameter without a
483-
/// sending result.
483+
/// sending result, but is passed a function typed parameter without a sending
484+
/// result.
484485
AllowSendingMismatch,
485486
};
486487

@@ -2636,26 +2637,16 @@ class TreatEphemeralAsNonEphemeral final : public AllowArgumentMismatch {
26362637
/// non-Sendable is safe to transfer onto other situations. The caller though
26372638
/// that this is being sent to does not enforce that invariants within its body.
26382639
class AllowSendingMismatch final : public ContextualMismatch {
2639-
public:
2640-
enum class Kind {
2641-
Parameter,
2642-
Result,
2643-
};
2644-
2645-
private:
2646-
Kind kind;
2647-
26482640
AllowSendingMismatch(ConstraintSystem &cs, Type argType, Type paramType,
2649-
ConstraintLocator *locator, Kind kind,
2650-
FixBehavior fixBehavior)
2641+
ConstraintLocator *locator, FixBehavior fixBehavior)
26512642
: ContextualMismatch(cs, FixKind::AllowSendingMismatch, argType,
2652-
paramType, locator, fixBehavior),
2653-
kind(kind) {}
2643+
paramType, locator, fixBehavior) {}
26542644

26552645
public:
26562646
std::string getName() const override {
2657-
return "treat a function argument with sending parameter as a function "
2658-
"argument without sending parameters";
2647+
return "treat a function argument with sending parameters and results as a "
2648+
"function "
2649+
"argument without sending parameters and results";
26592650
}
26602651

26612652
bool diagnose(const Solution &solution, bool asNote = false) const override;
@@ -2664,9 +2655,8 @@ class AllowSendingMismatch final : public ContextualMismatch {
26642655
return diagnose(*commonFixes.front().first);
26652656
}
26662657

2667-
static AllowSendingMismatch *create(ConstraintSystem &cs,
2668-
ConstraintLocator *locator, Type srcType,
2669-
Type dstType, Kind kind);
2658+
static AllowSendingMismatch *create(ConstraintSystem &cs, Type srcType,
2659+
Type dstType, ConstraintLocator *locator);
26702660

26712661
static bool classof(const ConstraintFix *fix) {
26722662
return fix->getKind() == FixKind::AllowSendingMismatch;

lib/Sema/CSDiagnostics.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7918,21 +7918,25 @@ bool NonEphemeralConversionFailure::diagnoseAsError() {
79187918
return true;
79197919
}
79207920

7921-
bool SendingOnFunctionParameterMismatchFail::diagnoseAsError() {
7922-
emitDiagnosticAt(getLoc(), diag::sending_function_wrong_sending,
7923-
getFromType(), getToType())
7921+
bool SendingMismatchFailure::diagnoseAsError() {
7922+
if (getLocator()->getLastElementAs<LocatorPathElt::FunctionArgument>())
7923+
return diagnoseArgFailure();
7924+
return diagnoseResultFailure();
7925+
}
7926+
7927+
bool SendingMismatchFailure::diagnoseArgFailure() {
7928+
emitDiagnostic(diag::sending_function_wrong_sending, getFromType(),
7929+
getToType())
79247930
.warnUntilSwiftVersion(6);
7925-
emitDiagnosticAt(getLoc(),
7926-
diag::sending_function_param_with_sending_param_note);
7931+
emitDiagnostic(diag::sending_function_param_with_sending_param_note);
79277932
return true;
79287933
}
79297934

7930-
bool SendingOnFunctionResultMismatchFailure::diagnoseAsError() {
7931-
emitDiagnosticAt(getLoc(), diag::sending_function_wrong_sending,
7932-
getFromType(), getToType())
7935+
bool SendingMismatchFailure::diagnoseResultFailure() {
7936+
emitDiagnostic(diag::sending_function_wrong_sending, getFromType(),
7937+
getToType())
79337938
.warnUntilSwiftVersion(6);
7934-
emitDiagnosticAt(getLoc(),
7935-
diag::sending_function_result_with_sending_param_note);
7939+
emitDiagnostic(diag::sending_function_result_with_sending_param_note);
79367940
return true;
79377941
}
79387942

lib/Sema/CSDiagnostics.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,26 +2292,17 @@ class NonEphemeralConversionFailure final : public ArgumentMismatchFailure {
22922292
void emitSuggestionNotes() const;
22932293
};
22942294

2295-
class SendingOnFunctionParameterMismatchFail final : public ContextualFailure {
2295+
class SendingMismatchFailure final : public ContextualFailure {
22962296
public:
2297-
SendingOnFunctionParameterMismatchFail(const Solution &solution, Type srcType,
2298-
Type dstType,
2299-
ConstraintLocator *locator,
2300-
FixBehavior fixBehavior)
2297+
SendingMismatchFailure(const Solution &solution, Type srcType, Type dstType,
2298+
ConstraintLocator *locator, FixBehavior fixBehavior)
23012299
: ContextualFailure(solution, srcType, dstType, locator, fixBehavior) {}
23022300

23032301
bool diagnoseAsError() override;
2304-
};
2305-
2306-
class SendingOnFunctionResultMismatchFailure final : public ContextualFailure {
2307-
public:
2308-
SendingOnFunctionResultMismatchFailure(const Solution &solution, Type srcType,
2309-
Type dstType,
2310-
ConstraintLocator *locator,
2311-
FixBehavior fixBehavior)
2312-
: ContextualFailure(solution, srcType, dstType, locator, fixBehavior) {}
23132302

2314-
bool diagnoseAsError() override;
2303+
private:
2304+
bool diagnoseArgFailure();
2305+
bool diagnoseResultFailure();
23152306
};
23162307

23172308
class AssignmentTypeMismatchFailure final : public ContextualFailure {

lib/Sema/CSFix.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,30 +1847,19 @@ std::string TreatEphemeralAsNonEphemeral::getName() const {
18471847

18481848
bool AllowSendingMismatch::diagnose(const Solution &solution,
18491849
bool asNote) const {
1850-
switch (kind) {
1851-
case Kind::Parameter: {
1852-
SendingOnFunctionParameterMismatchFail failure(
1853-
solution, getFromType(), getToType(), getLocator(), fixBehavior);
1854-
return failure.diagnose(asNote);
1855-
}
1856-
case Kind::Result: {
1857-
SendingOnFunctionResultMismatchFailure failure(
1858-
solution, getFromType(), getToType(), getLocator(), fixBehavior);
1859-
return failure.diagnose(asNote);
1860-
}
1861-
}
1862-
llvm_unreachable("Covered switch isn't covered?!");
1850+
SendingMismatchFailure failure(solution, getFromType(), getToType(),
1851+
getLocator(), fixBehavior);
1852+
return failure.diagnose(asNote);
18631853
}
18641854

18651855
AllowSendingMismatch *AllowSendingMismatch::create(ConstraintSystem &cs,
1866-
ConstraintLocator *locator,
18671856
Type srcType, Type dstType,
1868-
Kind kind) {
1857+
ConstraintLocator *locator) {
18691858
auto fixBehavior = cs.getASTContext().LangOpts.isSwiftVersionAtLeast(6)
18701859
? FixBehavior::Error
18711860
: FixBehavior::DowngradeToWarning;
18721861
return new (cs.getAllocator())
1873-
AllowSendingMismatch(cs, srcType, dstType, locator, kind, fixBehavior);
1862+
AllowSendingMismatch(cs, srcType, dstType, locator, fixBehavior);
18741863
}
18751864

18761865
bool SpecifyBaseTypeForContextualMember::diagnose(const Solution &solution,

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,9 +3238,8 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
32383238
// () -> sending T can be a subtype of () -> T... but not vis-a-versa.
32393239
if (func1->hasSendingResult() != func2->hasSendingResult() &&
32403240
(!func1->hasSendingResult() || kind < ConstraintKind::Subtype)) {
3241-
auto *fix = AllowSendingMismatch::create(
3242-
*this, getConstraintLocator(locator), func1, func2,
3243-
AllowSendingMismatch::Kind::Result);
3241+
auto *fix = AllowSendingMismatch::create(*this, func1, func2,
3242+
getConstraintLocator(locator));
32443243
if (recordFix(fix))
32453244
return getTypeMatchFailure(locator);
32463245
}
@@ -3680,8 +3679,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
36803679
if (func1Param.getParameterFlags().isSending() &&
36813680
!func2Param.getParameterFlags().isSending()) {
36823681
auto *fix = AllowSendingMismatch::create(
3683-
*this, getConstraintLocator(argumentLocator), func1, func2,
3684-
AllowSendingMismatch::Kind::Parameter);
3682+
*this, func1, func2, getConstraintLocator(argumentLocator));
36853683
if (recordFix(fix))
36863684
return getTypeMatchFailure(argumentLocator);
36873685
}

0 commit comments

Comments
 (0)