From d2bdbd620853faa3234cc43cb99a8f0e695a4cad Mon Sep 17 00:00:00 2001 From: Holly Borla Date: Wed, 1 Sep 2021 13:48:29 -0700 Subject: [PATCH] [ConstraintSystem] Fix a silly crash in repairFailures. The code checked if the last element of locator path vector is a certain kind of element without first ensuring that the vector isn't empty. --- lib/Sema/CSSimplify.cpp | 2 +- .../compiler_crashers_2_fixed/sr14894.swift | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 validation-test/compiler_crashers_2_fixed/sr14894.swift diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index 98b3432b02157..64493780acd5f 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -4761,7 +4761,7 @@ bool ConstraintSystem::repairFailures( path.pop_back(); // Drop the tuple type path elements too, but extract each tuple type first. - if (path.back().is()) { + if (!path.empty() && path.back().is()) { rhs = path.back().getAs()->getType(); path.pop_back(); lhs = path.back().getAs()->getType(); diff --git a/validation-test/compiler_crashers_2_fixed/sr14894.swift b/validation-test/compiler_crashers_2_fixed/sr14894.swift new file mode 100644 index 0000000000000..084134a05addb --- /dev/null +++ b/validation-test/compiler_crashers_2_fixed/sr14894.swift @@ -0,0 +1,29 @@ +// RUN: %target-swift-frontend -typecheck -verify %s + +struct S { + private let data: [[String]] + private func f() {} + + func test() { + // expected-error@+1 {{static method 'buildBlock' requires that 'ForEach<[String], ()>' conform to 'View'}} + ForEach(data) { group in + ForEach(group) { month in + self.f() + } + } + } +} + +struct Wrapper {} + +protocol View {} + +@resultBuilder struct Builder { + // expected-note@+1 {{where 'Content' = 'ForEach<[String], ()>'}} + static func buildBlock(_ content: Content) -> Content { fatalError() } +} + +struct ForEach where Data : RandomAccessCollection { + init(_ data: Wrapper, @Builder content: (Wrapper) -> Content) where C : MutableCollection {} + init(_ data: Data, @Builder content: @escaping (Data.Element) -> Content) {} +}