From 6ccd01393bacd83cf5d2ffaff6b4df177dd45372 Mon Sep 17 00:00:00 2001 From: Pavel Yaskevich Date: Tue, 4 Aug 2020 14:00:59 -0700 Subject: [PATCH] [Diagnostics] Detect and diagnose type mismatches related to function builder result Generic requirement failures are already covered but general type mismatches have to be handled separately. Resolves: rdar://problem/65413640 (cherry picked from commit 98007904b83a6cc5ef3b8320635c9258972dfcff) --- lib/Sema/CSDiagnostics.cpp | 5 +++++ lib/Sema/CSSimplify.cpp | 6 ++++++ lib/Sema/ConstraintSystem.cpp | 5 +++++ test/Constraints/function_builder_diags.swift | 4 ++++ 4 files changed, 20 insertions(+) diff --git a/lib/Sema/CSDiagnostics.cpp b/lib/Sema/CSDiagnostics.cpp index 257c7fb22c3d1..e1a7f1089b99d 100644 --- a/lib/Sema/CSDiagnostics.cpp +++ b/lib/Sema/CSDiagnostics.cpp @@ -2107,6 +2107,11 @@ bool ContextualFailure::diagnoseAsError() { return true; } + case ConstraintLocator::FunctionBuilderBodyResult: { + diagnostic = *getDiagnosticFor(CTP_Initialization, toType); + break; + } + default: return false; } diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index da385f2b0d5c0..74acf2bffa059 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -4264,6 +4264,12 @@ bool ConstraintSystem::repairFailures( break; } + case ConstraintLocator::FunctionBuilderBodyResult: { + conversionsOrFixes.push_back(ContextualMismatch::create( + *this, lhs, rhs, getConstraintLocator(locator))); + break; + } + default: break; } diff --git a/lib/Sema/ConstraintSystem.cpp b/lib/Sema/ConstraintSystem.cpp index 740382e1d352f..a1e7e6e48d984 100644 --- a/lib/Sema/ConstraintSystem.cpp +++ b/lib/Sema/ConstraintSystem.cpp @@ -3531,6 +3531,11 @@ void constraints::simplifyLocator(Expr *&anchor, continue; } + case ConstraintLocator::FunctionBuilderBodyResult: { + path = path.slice(1); + break; + } + default: // FIXME: Lots of other cases to handle. break; diff --git a/test/Constraints/function_builder_diags.swift b/test/Constraints/function_builder_diags.swift index 66a9ecaca9df0..0e32913c39619 100644 --- a/test/Constraints/function_builder_diags.swift +++ b/test/Constraints/function_builder_diags.swift @@ -602,4 +602,8 @@ struct MyView { case . // expected-error {{expected ':' after 'case'}} } // expected-error {{expected identifier after '.' expression}} } + + @TupleBuilder var invalidConversion: Int { + "" // expected-error {{cannot convert value of type 'String' to specified type 'Int'}} + } }