Skip to content

Commit 31afac6

Browse files
committed
Diag: Carry over the constraint in opaque_type_in_protocol_requirement fix-it
1 parent aa74b1b commit 31afac6

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Diff for: lib/Sema/TypeCheckGeneric.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,20 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
105105
fixitLoc = originatingDecl->getStartLoc();
106106
}
107107

108-
ctx.Diags.diagnose(repr->getLoc(),
109-
diag::opaque_type_in_protocol_requirement)
110-
.fixItInsert(fixitLoc, "associatedtype <#AssocType#>\n")
111-
.fixItReplace(repr->getSourceRange(), "<#AssocType#>");
112-
108+
std::string result;
109+
const char *const placeholder = "<#AssocType#>";
110+
{
111+
llvm::raw_string_ostream out(result);
112+
out << "associatedtype " << placeholder << ": ";
113+
repr->getConstraint()->print(out);
114+
out << "\n";
115+
}
116+
117+
ctx.Diags
118+
.diagnose(repr->getLoc(), diag::opaque_type_in_protocol_requirement)
119+
.fixItInsert(fixitLoc, result)
120+
.fixItReplace(repr->getSourceRange(), placeholder);
121+
113122
return nullptr;
114123
}
115124

Diff for: test/type/opaque.swift

+11-5
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,19 @@ func rdar_51641323() {
390390

391391
// Protocol requirements cannot have opaque return types
392392
protocol OpaqueProtocolRequirement {
393-
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>\n}}{{20-26=<#AssocType#>}}
394-
func method() -> some P
393+
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>: P\n}}{{21-27=<#AssocType#>}}
394+
func method1() -> some P
395395

396-
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>\n}}{{13-19=<#AssocType#>}}
396+
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>: C & P & Q\n}}{{21-35=<#AssocType#>}}
397+
func method2() -> some C & P & Q
398+
399+
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>: Nonsense\n}}{{21-34=<#AssocType#>}}
400+
func method3() -> some Nonsense
401+
402+
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>: P\n}}{{13-19=<#AssocType#>}}
397403
var prop: some P { get }
398404

399-
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>\n}}{{18-24=<#AssocType#>}}
405+
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>: P\n}}{{18-24=<#AssocType#>}}
400406
subscript() -> some P { get }
401407
}
402408

@@ -509,4 +515,4 @@ func takesOpaqueProtocol<T : OpaqueProtocol>(generic: T) {
509515
_ = generic.asSome
510516
_ = generic.getAsSome()
511517
_ = generic[0]
512-
}
518+
}

0 commit comments

Comments
 (0)