Skip to content

Commit 24fabc6

Browse files
committed
[CSDiagnostics] Add a diagnostic for same-type requirement failure in opaque return type
Follow-up to swiftlang#72493 (cherry picked from commit 08e9382)
1 parent 855b89c commit 24fabc6

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,9 @@ ERROR(type_does_not_conform_anyobject_decl_owner,none,
26872687
ERROR(type_does_not_conform_in_opaque_return,none,
26882688
"return type of %kind0 requires that %1 %select{conform to %2|be a class type}3",
26892689
(const ValueDecl *, Type, Type, bool))
2690+
ERROR(type_is_not_equal_in_opaque_return,none,
2691+
"return type of %kind0 requires the types %1 and %2 be equivalent",
2692+
(const ValueDecl *, Type, Type))
26902693
ERROR(types_not_equal_decl,none,
26912694
"%kind0 requires the types %1 and %2 be equivalent",
26922695
(const ValueDecl *, Type, Type))

lib/Sema/CSDiagnostics.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,15 @@ bool RequirementFailure::diagnoseAsError() {
455455

456456
if (auto *OTD = dyn_cast<OpaqueTypeDecl>(AffectedDecl)) {
457457
auto *namingDecl = OTD->getNamingDecl();
458-
emitDiagnostic(diag::type_does_not_conform_in_opaque_return,
459-
namingDecl, lhs, rhs, rhs->isAnyObject());
458+
459+
auto &req = getRequirement();
460+
if (req.getKind() == RequirementKind::SameType) {
461+
emitDiagnostic(diag::type_is_not_equal_in_opaque_return, namingDecl, lhs,
462+
rhs);
463+
} else {
464+
emitDiagnostic(diag::type_does_not_conform_in_opaque_return, namingDecl,
465+
lhs, rhs, rhs->isAnyObject());
466+
}
460467

461468
if (auto *repr = namingDecl->getOpaqueResultTypeRepr()) {
462469
emitDiagnosticAt(repr->getLoc(), diag::opaque_return_type_declared_here)

test/type/opaque.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,11 @@ do {
603603

604604
func test1() -> some P3<Int> { // expected-note {{opaque return type declared here}}
605605
return G<S>()
606-
// expected-error@-1 {{type of local function 'test1()' requires that 'S' conform to 'Int'}}
606+
// expected-error@-1 {{return type of local function 'test1()' requires the types 'S' and 'Int' be equivalent}}
607607
}
608608

609609
func test2() -> some P3<G<S>> { // expected-note {{opaque return type declared here}}
610610
return G<S>()
611-
// expected-error@-1 {{return type of local function 'test2()' requires that 'S' conform to 'G<S>'}}
611+
// expected-error@-1 {{return type of local function 'test2()' requires the types 'S' and 'G<S>' be equivalent}}
612612
}
613613
}

test/type/parameterized_protocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ struct OpaqueTypes<E> {
158158
func returnSequenceOfIntBad() -> some Sequence<Int> {
159159
// expected-note@-1 {{opaque return type declared here}}
160160
return ConcreteSequence<E>()
161-
// expected-error@-1 {{return type of instance method 'returnSequenceOfIntBad()' requires that 'E' conform to 'Int'}}
161+
// expected-error@-1 {{return type of instance method 'returnSequenceOfIntBad()' requires the types 'E' and 'Int' be equivalent}}
162162
}
163163

164164
// Invalid

0 commit comments

Comments
 (0)