Skip to content

Commit 0e8b7ce

Browse files
[SR-13239] Adjust error message to split inferrence source into notes
1 parent 1903681 commit 0e8b7ce

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,12 @@ ERROR(cannot_convert_argument_value_generic,none,
380380
ERROR(conflicting_arguments_for_generic_parameter,none,
381381
"conflicting arguments to generic parameter %0 (%1)",
382382
(Type, StringRef))
383-
ERROR(conflicting_inferred_generic_parameter_result_and_closure,none,
384-
"conflicting inferred types from call result and closure "
385-
"argument to generic parameter %0 (%1)",
386-
(Type, StringRef))
383+
NOTE(generic_parameter_inferred_from_closure,none,
384+
"generic parameter %0 inferred as %1 from closure return expression",
385+
(Type, Type))
386+
NOTE(generic_parameter_inferred_from_result_context,none,
387+
"generic parameter %0 inferred as %1 from context",
388+
(Type, Type))
387389

388390
// @_nonEphemeral conversion diagnostics
389391
ERROR(cannot_pass_type_to_non_ephemeral,none,

lib/Sema/ConstraintSystem.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,7 +3787,7 @@ static bool diagnoseContextualFunctionCallGenericAmbiguity(
37873787
return cs.typeVarOccursInType(typeVar, paramFnType->getResult());
37883788
};
37893789

3790-
llvm::SmallVector<ClosureExpr *, 4> closureArguments;
3790+
llvm::SmallVector<ClosureExpr *, 2> closureArguments;
37913791
// A single closure argument.
37923792
if (auto *closure =
37933793
getAsExpr<ClosureExpr>(AE->getArg()->getSemanticsProvidingExpr())) {
@@ -3837,7 +3837,7 @@ static bool diagnoseContextualFunctionCallGenericAmbiguity(
38373837
return true;
38383838
})) {
38393839

3840-
if (genericParamInferredTypes.size() <= 1)
3840+
if (genericParamInferredTypes.size() != 2)
38413841
return false;
38423842

38433843
auto &DE = cs.getASTContext().Diags;
@@ -3848,9 +3848,16 @@ static bool diagnoseContextualFunctionCallGenericAmbiguity(
38483848
[&](Type argType) { OS << "'" << argType << "'"; },
38493849
[&OS] { OS << " vs. "; });
38503850

3851-
DE.diagnose(AE->getLoc(),
3852-
diag::conflicting_inferred_generic_parameter_result_and_closure,
3851+
DE.diagnose(AE->getLoc(), diag::conflicting_arguments_for_generic_parameter,
38533852
GP, OS.str());
3853+
3854+
DE.diagnose(AE->getLoc(),
3855+
diag::generic_parameter_inferred_from_result_context, GP,
3856+
genericParamInferredTypes.back());
3857+
DE.diagnose(closureArguments.front()->getStartLoc(),
3858+
diag::generic_parameter_inferred_from_closure, GP,
3859+
genericParamInferredTypes.front());
3860+
38543861
return true;
38553862
}
38563863
return false;

test/expr/closure/closures.swift

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -633,29 +633,33 @@ func callitVariadic<T>(_ fs: () -> T...) -> T {
633633
}
634634

635635
func testSR13239_Tuple() -> Int {
636-
// expected-error@+1{{conflicting inferred types from call result and closure argument to generic parameter 'T' ('()' vs. 'Int')}}
637-
callitTuple(1) {
636+
// expected-error@+2{{conflicting arguments to generic parameter 'T' ('()' vs. 'Int')}}
637+
// expected-note@+1:3{{generic parameter 'T' inferred as 'Int' from context}}
638+
callitTuple(1) { // expected-note@:18{{generic parameter 'T' inferred as '()' from closure return expression}}
638639
(print("hello"), 0)
639640
}
640641
}
641642

642643
func testSR13239() -> Int {
643-
// expected-error@+1{{conflicting inferred types from call result and closure argument to generic parameter 'T' ('()' vs. 'Int')}}
644-
callit {
644+
// expected-error@+2{{conflicting arguments to generic parameter 'T' ('()' vs. 'Int')}}
645+
// expected-note@+1:3{{generic parameter 'T' inferred as 'Int' from context}}
646+
callit { // expected-note@:10{{generic parameter 'T' inferred as '()' from closure return expression}}
645647
print("hello")
646648
}
647649
}
648650

649651
func testSR13239_Args() -> Int {
650-
// expected-error@+1{{conflicting inferred types from call result and closure argument to generic parameter 'T' ('()' vs. 'Int')}}
651-
callitArgs(1) {
652+
// expected-error@+2{{conflicting arguments to generic parameter 'T' ('()' vs. 'Int')}}
653+
// expected-note@+1:3{{generic parameter 'T' inferred as 'Int' from context}}
654+
callitArgs(1) { // expected-note@:17{{generic parameter 'T' inferred as '()' from closure return expression}}
652655
print("hello")
653656
}
654657
}
655658

656659
func testSR13239_ArgsFn() -> Int {
657-
// expected-error@+1{{conflicting inferred types from call result and closure argument to generic parameter 'T' ('()' vs. 'Int')}}
658-
callitArgsFn(1) {
660+
// expected-error@+2{{conflicting arguments to generic parameter 'T' ('()' vs. 'Int')}}
661+
// expected-note@+1:3{{generic parameter 'T' inferred as 'Int' from context}}
662+
callitArgsFn(1) { // expected-note@:19{{generic parameter 'T' inferred as '()' from closure return expression}}
659663
{ print("hello") }
660664
}
661665
}
@@ -675,8 +679,9 @@ func testSR13239_GenericArg() -> Int {
675679
}
676680

677681
func testSR13239_Variadic() -> Int {
678-
// expected-error@+1{{conflicting inferred types from call result and closure argument to generic parameter 'T' ('()' vs. 'Int')}}
679-
callitVariadic({
682+
// expected-error@+2{{conflicting arguments to generic parameter 'T' ('()' vs. 'Int')}}
683+
// expected-note@+1:3{{generic parameter 'T' inferred as 'Int' from context}}
684+
callitVariadic({ // expected-note@:18{{generic parameter 'T' inferred as '()' from closure return expression}}
680685
print("hello")
681686
})
682687
}

0 commit comments

Comments
 (0)