Skip to content

Commit 45c73b3

Browse files
committed
[Diagnostics] Even though missing conformance could be fixed by .rawValue still diagnose it as conformance issue
Conformance requirement failure is a more descriptive error, augment it with a note about missing `.rawValue`.
1 parent e92c6eb commit 45c73b3

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6841,8 +6841,6 @@ AbstractRawRepresentableFailure::getDiagnostic() const {
68416841
return diag::cannot_convert_assign;
68426842
} else if (locator->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
68436843
return diag::cannot_convert_argument_value;
6844-
} else if (locator->isLastElement<LocatorPathElt::AnyRequirement>()) {
6845-
return diag::type_does_not_conform;
68466844
}
68476845

68486846
return None;
@@ -6938,6 +6936,26 @@ void MissingRawRepresentableInitFailure::fixIt(
69386936
}
69396937
}
69406938

6939+
bool MissingRawValueFailure::diagnoseAsError() {
6940+
auto *locator = getLocator();
6941+
6942+
if (locator->isLastElement<LocatorPathElt::AnyRequirement>()) {
6943+
MissingConformanceFailure failure(getSolution(), locator,
6944+
{RawReprType, ExpectedType});
6945+
6946+
auto diagnosed = failure.diagnoseAsError();
6947+
if (!diagnosed)
6948+
return false;
6949+
6950+
auto note = emitDiagnostic(diag::note_remapped_type, ".rawValue");
6951+
fixIt(note);
6952+
6953+
return true;
6954+
}
6955+
6956+
return AbstractRawRepresentableFailure::diagnoseAsError();
6957+
}
6958+
69416959
void MissingRawValueFailure::fixIt(InFlightDiagnostic &diagnostic) const {
69426960
auto *E = getAsExpr(getAnchor());
69436961
if (!E)

lib/Sema/CSDiagnostics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,8 @@ class MissingRawValueFailure final : public AbstractRawRepresentableFailure {
22732273
Type getFromType() const override { return RawReprType; }
22742274
Type getToType() const override { return ExpectedType; }
22752275

2276+
bool diagnoseAsError() override;
2277+
22762278
private:
22772279
void fixIt(InFlightDiagnostic &diagnostic) const override;
22782280
};

test/ClangImporter/newtype_conformance.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ func acceptHashable<T: Hashable>(_: T) {}
1616
// expected-note@-1 {{where 'T' = 'WrappedRef'}}
1717
// expected-note@-2 {{where 'T' = 'WrappedValue'}}
1818
func acceptComparable<T: Comparable>(_: T) {}
19+
// expected-note@-1 {{where 'T' = 'NSNotification.Name'}}
1920

2021
func testNewTypeWrapper(x: NSNotification.Name, y: NSNotification.Name) {
2122
acceptEquatable(x)
2223
acceptHashable(x)
23-
acceptComparable(x) // expected-error {{type 'NSNotification.Name' does not conform to protocol 'Comparable'}} {{19-19=.rawValue}}
24+
acceptComparable(x) // expected-error {{global function 'acceptComparable' requires that 'NSNotification.Name' conform to 'Comparable'}}
25+
// expected-note@-1 {{did you mean to use '.rawValue'?}} {{19-19=.rawValue}}
2426

2527
_ = x == y
2628
_ = x != y

0 commit comments

Comments
 (0)