Skip to content

Commit a9f1144

Browse files
committed
[Diagnostics] Diagnose an attempt to assign a value to an overloaded name
Example: ```swift struct X { } func X(_: Int) -> Int { return 42 } X = 42 ```
1 parent adf83fe commit a9f1144

File tree

6 files changed

+32
-1
lines changed

6 files changed

+32
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ ERROR(no_overloads_match_exactly_in_call_no_labels,none,
6969
ERROR(no_overloads_match_exactly_in_call_special,none,
7070
"no exact matches in call to %0",
7171
(DescriptiveDeclKind))
72+
ERROR(no_overloads_match_exactly_in_assignment,none,
73+
"no exact matches in assignment to %0",
74+
(DeclBaseName))
7275

7376
ERROR(ambiguous_subscript,none,
7477
"ambiguous subscript with base type %0 and index type %1",
@@ -3401,6 +3404,10 @@ ERROR(assignment_dynamic_property_has_immutable_base,none,
34013404
ERROR(assignment_bang_has_immutable_subcomponent,none,
34023405
"cannot assign through '!': %0", (StringRef))
34033406

3407+
NOTE(candidate_is_not_assignable,none,
3408+
"candidate is not assignable: %0 %1",
3409+
(DescriptiveDeclKind, DeclName))
3410+
34043411
NOTE(change_to_mutating,none,
34053412
"mark %select{method|accessor}0 'mutating' to make 'self' mutable",
34063413
(bool))

lib/Sema/CSDiagnostics.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,17 @@ bool RValueTreatedAsLValueFailure::diagnoseAsError() {
14401440
return failure.diagnose();
14411441
}
14421442

1443+
bool RValueTreatedAsLValueFailure::diagnoseAsNote() {
1444+
auto overload = getChoiceFor(getLocator());
1445+
if (!(overload && overload->choice.isDecl()))
1446+
return false;
1447+
1448+
auto *decl = overload->choice.getDecl();
1449+
emitDiagnostic(decl, diag::candidate_is_not_assignable,
1450+
decl->getDescriptiveKind(), decl->getFullName());
1451+
return true;
1452+
}
1453+
14431454
static Decl *findSimpleReferencedDecl(const Expr *E) {
14441455
if (auto *LE = dyn_cast<LoadExpr>(E))
14451456
E = LE->getSubExpr();

lib/Sema/CSDiagnostics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ class RValueTreatedAsLValueFailure final : public FailureDiagnostic {
638638
: FailureDiagnostic(cs, locator) {}
639639

640640
bool diagnoseAsError() override;
641+
bool diagnoseAsNote() override;
641642
};
642643

643644
class TrailingClosureAmbiguityFailure final : public FailureDiagnostic {

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2815,6 +2815,14 @@ bool ConstraintSystem::repairFailures(
28152815
return true;
28162816
}
28172817

2818+
if (auto *ODRE = dyn_cast<OverloadedDeclRefExpr>(anchor)) {
2819+
if (lhs->is<LValueType>()) {
2820+
conversionsOrFixes.push_back(
2821+
TreatRValueAsLValue::create(*this, getConstraintLocator(locator)));
2822+
return true;
2823+
}
2824+
}
2825+
28182826
if (auto *AE = dyn_cast<AssignExpr>(anchor)) {
28192827
if (repairByInsertingExplicitCall(lhs, rhs))
28202828
return true;

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,6 +2701,10 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
27012701
DE.diagnose(commonAnchor->getLoc(),
27022702
diag::could_not_find_subscript_member_did_you_mean,
27032703
getType(UDE->getBase()));
2704+
} else if (fix->getKind() == FixKind::TreatRValueAsLValue) {
2705+
DE.diagnose(commonAnchor->getLoc(),
2706+
diag::no_overloads_match_exactly_in_assignment,
2707+
decl->getBaseName());
27042708
} else {
27052709
auto name = decl->getFullName();
27062710
// Three choices here:

test/NameBinding/import-resolution-overload.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ scopedFunction = 42
4646
// FIXME: Should be an error -- a type name and a function cannot overload.
4747
var _ : Int = TypeNameWins(42)
4848

49-
TypeNameWins = 42 // expected-error {{ambiguous reference to member 'TypeNameWins'}}
49+
TypeNameWins = 42 // expected-error {{no exact matches in assignment to 'TypeNameWins'}}
5050
var _ : TypeNameWins // no-warning
5151

5252
// rdar://problem/21739333

0 commit comments

Comments
 (0)