Skip to content

Commit e5002b2

Browse files
committed
Add more test cases
1 parent bb0a46e commit e5002b2

3 files changed

Lines changed: 86 additions & 10 deletions

File tree

include/swift/Sema/ConstraintSystem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,9 @@ enum class ConstraintSystemFlags {
19661966

19671967
/// Enable old type-checker performance hacks.
19681968
EnablePerformanceHacks = 0x80,
1969+
1970+
/// Don't record a failed constraint after adding an unsolvable constraint.
1971+
DisableRecordFailedConstraint = 0x100,
19691972
};
19701973

19711974
/// Options that affect the constraint system as a whole.
@@ -3824,6 +3827,9 @@ class ConstraintSystem {
38243827

38253828
/// Whether we should record the failure of a constraint.
38263829
bool shouldRecordFailedConstraint() const {
3830+
if (Options.contains(ConstraintSystemFlags::DisableRecordFailedConstraint))
3831+
return false;
3832+
38273833
// If we're debugging, always note a failure so we can print it out.
38283834
if (isDebugMode())
38293835
return true;

lib/Sema/TypeCheckType.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7022,11 +7022,16 @@ struct ResultBuilderUnboundTypeOpener {
70227022

70237023
auto genericSig = resultBuilderDecl->getGenericSignature();
70247024

7025-
// Try each result type and return the first one that produces a valid
7026-
// solution
7025+
// Try each result type and return the first one that
7026+
// produces a valid solution.
70277027
for (auto componentType : resultTypes) {
70287028
using namespace constraints;
7029-
ConstraintSystem cs(dc, std::nullopt);
7029+
7030+
// Avoid recording failed constraints, which are not used here.
7031+
ConstraintSystemOptions options;
7032+
options |= ConstraintSystemFlags::DisableRecordFailedConstraint;
7033+
7034+
ConstraintSystem cs(dc, options);
70307035

70317036
// Create a type variable for each of the result builder's generic params
70327037
llvm::SmallVector<Type, 8> typeVarReplacements;

test/Constraints/result_builder_diags.swift

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,14 +1080,29 @@ func testNoDuplicateStmtDiags() {
10801080
}
10811081
}
10821082

1083-
func testInferResultBuilderGenerics() {
1084-
@resultBuilder
1085-
struct SimpleArrayBuilder<Element> {
1086-
static func buildBlock(_ elements: Element...) -> [Element] {
1087-
elements
1088-
}
1083+
@resultBuilder
1084+
enum SimpleArrayBuilder<ElementKind> {
1085+
static func buildBlock(_ elements: ElementKind...) -> [ElementKind] {
1086+
elements
10891087
}
1088+
}
1089+
1090+
@resultBuilder
1091+
enum CollectionBuilder<Element> {
1092+
static func buildBlock(_ component: Element...) -> [Element] {
1093+
component
1094+
}
10901095

1096+
static func buildFinalResult(_ component: [Element]) -> [Element] {
1097+
component
1098+
}
1099+
1100+
static func buildFinalResult(_ component: [Element]) -> Set<Element> where Element: Hashable {
1101+
Set(component)
1102+
}
1103+
}
1104+
1105+
func testInferResultBuilderGenerics() {
10911106
@SimpleArrayBuilder
10921107
var stringArray: [String] {
10931108
"foo"
@@ -1157,7 +1172,7 @@ func testInferResultBuilderGenerics() {
11571172
("foo", 1) // expected-warning {{expression of type '(String, Int)' is unused}}
11581173
("bar", 2) // expected-warning {{expression of type '(String, Int)' is unused}}
11591174
}
1160-
1175+
11611176
@resultBuilder
11621177
enum ComplexListBuilder<Element> {
11631178
static func buildBlock(_ elements: Element...) -> [Element] {
@@ -1207,4 +1222,54 @@ func testInferResultBuilderGenerics() {
12071222
"foo"
12081223
"bar"
12091224
}
1225+
1226+
@CollectionBuilder
1227+
var array: [String] {
1228+
"a"
1229+
"b"
1230+
}
1231+
1232+
@CollectionBuilder
1233+
var set: Set<String> {
1234+
"c"
1235+
"d"
1236+
}
1237+
1238+
@CollectionBuilder // expected-error {{unable to infer generic parameters for result builder 'CollectionBuilder'}}
1239+
var contiguousArray: ContiguousArray<String> {
1240+
"c" // expected-warning {{string literal is unused}}
1241+
"d" // expected-warning {{string literal is unused}}
1242+
}
1243+
}
1244+
1245+
extension Array {
1246+
init(@SimpleArrayBuilder build1: () -> Self) {
1247+
self = build1()
1248+
}
1249+
1250+
init(@SimpleArrayBuilder build2: () -> [Element]) {
1251+
self = build2()
1252+
}
1253+
1254+
init(@SimpleArrayBuilder build3: () -> Array<Self.Element>) {
1255+
self = build3()
1256+
}
1257+
1258+
init(@SimpleArrayBuilder build3: () -> ContiguousArray<Element>) { // expected-error {{unable to infer generic parameters for result builder 'SimpleArrayBuilder'}}
1259+
self = Array(build3())
1260+
}
1261+
}
1262+
1263+
extension Set {
1264+
init(@CollectionBuilder build: () -> Self) {
1265+
self = build()
1266+
}
1267+
1268+
init(@CollectionBuilder build2: () -> [Element]) {
1269+
self = Set(build2())
1270+
}
1271+
1272+
init(@SimpleArrayBuilder build3: () -> Self) { // expected-error {{unable to infer generic parameters for result builder 'SimpleArrayBuilder'}}
1273+
self = build3()
1274+
}
12101275
}

0 commit comments

Comments
 (0)