Skip to content

Commit 63fcd67

Browse files
committed
[Diagnostics] Diagnose a mismatch between result builder result and return type
Add a tailored diagnostic for cases where result builder result type disagrees with expected contextual return type. Resolves: #59390
1 parent 0909728 commit 63fcd67

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6173,6 +6173,10 @@ ERROR(result_builder_requires_explicit_var_initialization,none,
61736173
ERROR(cannot_declare_computed_var_in_result_builder,none,
61746174
"cannot declare local %select{lazy|wrapped|computed|observed}0 variable "
61756175
"in result builder", (unsigned))
6176+
ERROR(cannot_convert_result_builder_result_to_return_type,none,
6177+
"cannot convert result builder result type %0 to return type %1",
6178+
(Type,Type))
6179+
61766180

61776181
//------------------------------------------------------------------------------
61786182
// MARK: Tuple Shuffle Diagnostics

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,10 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
796796
break;
797797
}
798798

799+
case ConstraintLocator::ResultBuilderBodyResult:
800+
diagnostic = diag::cannot_convert_result_builder_result_to_return_type;
801+
break;
802+
799803
case ConstraintLocator::AutoclosureResult:
800804
case ConstraintLocator::ApplyArgToParam:
801805
case ConstraintLocator::ApplyArgument: {

test/Constraints/result_builder_diags.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,3 +838,20 @@ func test_rdar89742267() {
838838
}
839839
}
840840
}
841+
842+
// https://github.com/apple/swift/issues/59390
843+
func test_invalid_result_is_diagnosed() {
844+
@resultBuilder
845+
struct MyBuilder {
846+
static func buildBlock<T1>(_ t1: T1) -> T1 {
847+
return t1
848+
}
849+
}
850+
851+
struct S<T> {} // expected-note {{arguments to generic parameter 'T' ('Int' and 'String') are expected to be equal}}
852+
853+
@MyBuilder
854+
func test() -> S<String> { // expected-error {{cannot convert result builder result type 'S<Int>' to return type 'S<String>}}
855+
S<Int>()
856+
}
857+
}

0 commit comments

Comments
 (0)