Skip to content

Commit 094ffd6

Browse files
[CSDiagnostics] Adjusting KeyPathRootTypeMismatchFailure implementation and message
1 parent f8cc9b5 commit 094ffd6

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ ERROR(expr_keypath_subscript_index_not_hashable, none,
580580
ERROR(expr_smart_keypath_application_type_mismatch,none,
581581
"key path of type %0 cannot be applied to a base of type %1",
582582
(Type, Type))
583+
ERROR(expr_keypath_root_type_mismatch, none,
584+
"key path with root type %0 cannot be applied to a base of type %1",
585+
(Type, Type))
583586
ERROR(expr_swift_keypath_anyobject_root,none,
584587
"the root type of a Swift key path cannot be 'AnyObject'", ())
585588
WARNING(expr_deprecated_writable_keypath,none,

lib/Sema/CSDiagnostics.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6226,21 +6226,13 @@ bool CoercionAsForceCastFailure::diagnoseAsError() {
62266226
}
62276227

62286228
bool KeyPathRootTypeMismatchFailure::diagnoseAsError() {
6229-
auto &cs = getConstraintSystem();
62306229
auto locator = getLocator();
6231-
auto anchor = locator->getAnchor();
6232-
62336230
assert(locator->isKeyPathRoot() && "Expect a key path root");
6231+
6232+
auto baseType = getFromType();
6233+
auto rootType = getToType();
62346234

6235-
if (auto *subscriptExpr = dyn_cast_or_null<SubscriptExpr>(anchor)) {
6236-
auto *tupleExpr = cast<TupleExpr>(subscriptExpr->getIndex());
6237-
auto keyPathType = cs.getType(tupleExpr->getElement(0));
6238-
auto baseType = cs.getType(subscriptExpr->getBase());
6239-
6240-
emitDiagnosticAt(anchor->getLoc(),
6241-
diag::expr_smart_keypath_application_type_mismatch,
6242-
keyPathType, baseType->getRValueType());
6243-
return true;
6244-
}
6245-
return false;
6235+
emitDiagnostic(diag::expr_keypath_root_type_mismatch,
6236+
rootType, baseType);
6237+
return true;
62466238
}

lib/Sema/CSDiagnostics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,7 @@ class CoercionAsForceCastFailure final : public ContextualFailure {
20232023
bool diagnoseAsError() override;
20242024
};
20252025

2026-
/// Diagnose n key path root type that cannot be applied to an instance
2026+
/// Diagnose a key path root type that cannot be applied to an instance
20272027
/// base that has another type.
20282028
///
20292029
/// \code

test/attr/attr_dynamic_member_lookup.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,8 @@ internal var rightStructInstance: SR12425_R = SR12425_R()
811811

812812
public extension SR12425_R {
813813
subscript<T>(dynamicMember member: WritableKeyPath<SR12425_S, T>) -> T {
814-
get { rightStructInstance[keyPath: member] } // expected-error {{key path of type 'WritableKeyPath<SR12425_S, T>' cannot be applied to a base of type 'SR12425_R'}}
815-
set { rightStructInstance[keyPath: member] = newValue } // expected-error {{key path of type 'WritableKeyPath<SR12425_S, T>' cannot be applied to a base of type 'SR12425_R'}}
814+
get { rightStructInstance[keyPath: member] } // expected-error {{key path with root type 'SR12425_S' cannot be applied to a base of type 'SR12425_R'}}
815+
set { rightStructInstance[keyPath: member] = newValue } // expected-error {{key path with root type 'SR12425_S' cannot be applied to a base of type 'SR12425_R'}}
816816
}
817817
}
818818

@@ -821,6 +821,6 @@ public struct SR12425_R1 {}
821821

822822
public extension SR12425_R1 {
823823
subscript<T>(dynamicMember member: KeyPath<SR12425_R1, T>) -> T {
824-
get { rightStructInstance[keyPath: member] } // expected-error {{key path of type 'KeyPath<SR12425_R1, T>' cannot be applied to a base of type 'SR12425_R'}}
824+
get { rightStructInstance[keyPath: member] } // expected-error {{key path with root type 'SR12425_R1' cannot be applied to a base of type 'SR12425_R'}}
825825
}
826826
}

0 commit comments

Comments
 (0)