Skip to content

Commit 25ce660

Browse files
committed
[CSDiagnostics] Add a diagnostic for superclass requirement failure in opaque return type
1 parent 08e9382 commit 25ce660

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,6 +2690,9 @@ ERROR(type_does_not_conform_in_opaque_return,none,
26902690
ERROR(type_is_not_equal_in_opaque_return,none,
26912691
"return type of %kind0 requires the types %1 and %2 be equivalent",
26922692
(const ValueDecl *, Type, Type))
2693+
ERROR(types_not_inherited_in_opaque_return,none,
2694+
"return type of %kind0 requires that %1 inherit from %2",
2695+
(const ValueDecl *, Type, Type))
26932696
ERROR(types_not_equal_decl,none,
26942697
"%kind0 requires the types %1 and %2 be equivalent",
26952698
(const ValueDecl *, Type, Type))

lib/Sema/CSDiagnostics.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,25 @@ bool RequirementFailure::diagnoseAsError() {
457457
auto *namingDecl = OTD->getNamingDecl();
458458

459459
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 {
460+
switch (req.getKind()) {
461+
case RequirementKind::Conformance:
462+
case RequirementKind::Layout:
464463
emitDiagnostic(diag::type_does_not_conform_in_opaque_return, namingDecl,
465464
lhs, rhs, rhs->isAnyObject());
465+
break;
466+
467+
case RequirementKind::Superclass:
468+
emitDiagnostic(diag::types_not_inherited_in_opaque_return, namingDecl,
469+
lhs, rhs);
470+
break;
471+
472+
case RequirementKind::SameType:
473+
emitDiagnostic(diag::type_is_not_equal_in_opaque_return, namingDecl, lhs,
474+
rhs);
475+
break;
476+
477+
case RequirementKind::SameShape:
478+
return false;
466479
}
467480

468481
if (auto *repr = namingDecl->getOpaqueResultTypeRepr()) {

test/type/opaque.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,8 @@ do {
601601

602602
struct S: P1 {}
603603

604+
class A {}
605+
604606
func test1() -> some P3<Int> { // expected-note {{opaque return type declared here}}
605607
return G<S>()
606608
// expected-error@-1 {{return type of local function 'test1()' requires the types 'S' and 'Int' be equivalent}}
@@ -610,4 +612,9 @@ do {
610612
return G<S>()
611613
// expected-error@-1 {{return type of local function 'test2()' requires the types 'S' and 'G<S>' be equivalent}}
612614
}
615+
616+
func test3() -> some P1 & A { // expected-note {{opaque return type declared here}}
617+
S()
618+
// expected-error@-1 {{return type of local function 'test3()' requires that 'S' inherit from 'A'}}
619+
}
613620
}

0 commit comments

Comments
 (0)