Skip to content

[CSDiagnostics] Add a diagnostic for same-type/supertype requirement failures in… #72558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2687,6 +2687,12 @@ ERROR(type_does_not_conform_anyobject_decl_owner,none,
ERROR(type_does_not_conform_in_opaque_return,none,
"return type of %kind0 requires that %1 %select{conform to %2|be a class type}3",
(const ValueDecl *, Type, Type, bool))
ERROR(type_is_not_equal_in_opaque_return,none,
"return type of %kind0 requires the types %1 and %2 be equivalent",
(const ValueDecl *, Type, Type))
ERROR(types_not_inherited_in_opaque_return,none,
"return type of %kind0 requires that %1 inherit from %2",
(const ValueDecl *, Type, Type))
ERROR(types_not_equal_decl,none,
"%kind0 requires the types %1 and %2 be equivalent",
(const ValueDecl *, Type, Type))
Expand Down
24 changes: 22 additions & 2 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,28 @@ bool RequirementFailure::diagnoseAsError() {

if (auto *OTD = dyn_cast<OpaqueTypeDecl>(AffectedDecl)) {
auto *namingDecl = OTD->getNamingDecl();
emitDiagnostic(diag::type_does_not_conform_in_opaque_return,
namingDecl, lhs, rhs, rhs->isAnyObject());

auto &req = getRequirement();
switch (req.getKind()) {
case RequirementKind::Conformance:
case RequirementKind::Layout:
emitDiagnostic(diag::type_does_not_conform_in_opaque_return, namingDecl,
lhs, rhs, rhs->isAnyObject());
break;

case RequirementKind::Superclass:
emitDiagnostic(diag::types_not_inherited_in_opaque_return, namingDecl,
lhs, rhs);
break;

case RequirementKind::SameType:
emitDiagnostic(diag::type_is_not_equal_in_opaque_return, namingDecl, lhs,
rhs);
break;

case RequirementKind::SameShape:
return false;
}

if (auto *repr = namingDecl->getOpaqueResultTypeRepr()) {
emitDiagnosticAt(repr->getLoc(), diag::opaque_return_type_declared_here)
Expand Down
11 changes: 9 additions & 2 deletions test/type/opaque.swift
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,20 @@ do {

struct S: P1 {}

class A {}

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

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

func test3() -> some P1 & A { // expected-note {{opaque return type declared here}}
S()
// expected-error@-1 {{return type of local function 'test3()' requires that 'S' inherit from 'A'}}
}
}
2 changes: 1 addition & 1 deletion test/type/parameterized_protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ struct OpaqueTypes<E> {
func returnSequenceOfIntBad() -> some Sequence<Int> {
// expected-note@-1 {{opaque return type declared here}}
return ConcreteSequence<E>()
// expected-error@-1 {{return type of instance method 'returnSequenceOfIntBad()' requires that 'E' conform to 'Int'}}
// expected-error@-1 {{return type of instance method 'returnSequenceOfIntBad()' requires the types 'E' and 'Int' be equivalent}}
}

// Invalid
Expand Down