Skip to content

Commit d1bc45f

Browse files
authored
Merge pull request #58534 from xedin/rdar-92366212-5.7
[5.7][ConstraintSystem] Fail `~=` synthesis if constraint generation fails
2 parents 35acb3e + 64ef9ad commit d1bc45f

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4544,6 +4544,7 @@ class ConstraintSystem {
45444544
/// Generate constraints for the given solution target.
45454545
///
45464546
/// \returns true if an error occurred, false otherwise.
4547+
LLVM_NODISCARD
45474548
bool generateConstraints(SolutionApplicationTarget &target,
45484549
FreeTypeVariableBinding allowFreeTypeVariables);
45494550

@@ -4552,18 +4553,21 @@ class ConstraintSystem {
45524553
/// \param closure the closure expression
45534554
///
45544555
/// \returns \c true if constraint generation failed, \c false otherwise
4556+
LLVM_NODISCARD
45554557
bool generateConstraints(ClosureExpr *closure);
45564558

45574559
/// Generate constraints for the given (unchecked) expression.
45584560
///
45594561
/// \returns a possibly-sanitized expression, or null if an error occurred.
4562+
LLVM_NODISCARD
45604563
Expr *generateConstraints(Expr *E, DeclContext *dc,
45614564
bool isInputExpression = true);
45624565

45634566
/// Generate constraints for binding the given pattern to the
45644567
/// value of the given expression.
45654568
///
45664569
/// \returns a possibly-sanitized initializer, or null if an error occurred.
4570+
LLVM_NODISCARD
45674571
Type generateConstraints(Pattern *P, ConstraintLocatorBuilder locator,
45684572
bool bindPatternVarsOneWay,
45694573
PatternBindingDecl *patternBinding,
@@ -4573,6 +4577,7 @@ class ConstraintSystem {
45734577
///
45744578
/// \returns true if there was an error in constraint generation, false
45754579
/// if generation succeeded.
4580+
LLVM_NODISCARD
45764581
bool generateConstraints(StmtCondition condition, DeclContext *dc);
45774582

45784583
/// Generate constraints for a case statement.
@@ -4582,6 +4587,7 @@ class ConstraintSystem {
45824587
///
45834588
/// \returns true if there was an error in constraint generation, false
45844589
/// if generation succeeded.
4590+
LLVM_NODISCARD
45854591
bool generateConstraints(CaseStmt *caseStmt, DeclContext *dc,
45864592
Type subjectType, ConstraintLocator *locator);
45874593

@@ -4625,6 +4631,7 @@ class ConstraintSystem {
46254631
/// \param propertyType The type of the wrapped property.
46264632
///
46274633
/// \returns true if there is an error.
4634+
LLVM_NODISCARD
46284635
bool generateWrappedPropertyTypeConstraints(VarDecl *wrappedVar,
46294636
Type initializerType,
46304637
Type propertyType);

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8971,7 +8971,8 @@ static bool inferEnumMemberThroughTildeEqualsOperator(
89718971
}
89728972
}
89738973

8974-
cs.generateConstraints(target, FreeTypeVariableBinding::Disallow);
8974+
if (cs.generateConstraints(target, FreeTypeVariableBinding::Disallow))
8975+
return true;
89758976

89768977
// Sub-expression associated with expression pattern is the enum element
89778978
// access which needs to be connected to the provided element type.
@@ -9907,8 +9908,9 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
99079908
}
99089909

99099910
if (!paramDecl->getName().hasDollarPrefix()) {
9910-
generateWrappedPropertyTypeConstraints(paramDecl, backingType,
9911-
param.getParameterType());
9911+
if (generateWrappedPropertyTypeConstraints(paramDecl, backingType,
9912+
param.getParameterType()))
9913+
return false;
99129914
}
99139915

99149916
auto result = applyPropertyWrapperToParameter(backingType, param.getParameterType(),

test/expr/closure/multi_statement.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,16 @@ func test_wrapped_var_without_initializer() {
316316
var v;
317317
}
318318
}
319+
320+
// rdar://92366212 - crash in ConstraintSystem::getType
321+
func test_unknown_refs_in_tilde_operator() {
322+
enum E {
323+
}
324+
325+
let _: (E) -> Void = {
326+
if case .test(unknown) = $0 {
327+
// expected-error@-1 {{type 'E' has no member 'test'}}
328+
// expected-error@-2 2 {{cannot find 'unknown' in scope}}
329+
}
330+
}
331+
}

unittests/Sema/PlaceholderTypeInferenceTests.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ TEST_F(SemaTest, TestPlaceholderInferenceForArrayLiteral) {
3636

3737
ConstraintSystem cs(DC, ConstraintSystemOptions());
3838
cs.setContextualType(arrayExpr, {arrayRepr, arrayTy}, CTP_Initialization);
39-
cs.generateConstraints(target, FreeTypeVariableBinding::Disallow);
39+
auto failed = cs.generateConstraints(target, FreeTypeVariableBinding::Disallow);
40+
ASSERT_FALSE(failed);
41+
4042
SmallVector<Solution, 2> solutions;
4143
cs.solve(solutions);
4244

@@ -75,7 +77,9 @@ TEST_F(SemaTest, TestPlaceholderInferenceForDictionaryLiteral) {
7577

7678
ConstraintSystem cs(DC, ConstraintSystemOptions());
7779
cs.setContextualType(dictExpr, {dictRepr, dictTy}, CTP_Initialization);
78-
cs.generateConstraints(target, FreeTypeVariableBinding::Disallow);
80+
auto failed = cs.generateConstraints(target, FreeTypeVariableBinding::Disallow);
81+
ASSERT_FALSE(failed);
82+
7983
SmallVector<Solution, 2> solutions;
8084
cs.solve(solutions);
8185

unittests/Sema/UnresolvedMemberLookupTests.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ TEST_F(SemaTest, TestLookupAlwaysLooksThroughOptionalBase) {
3333
auto *UMCRE = new (Context) UnresolvedMemberChainResultExpr(UME, UME);
3434

3535
ConstraintSystem cs(DC, ConstraintSystemOptions());
36-
cs.generateConstraints(UMCRE, DC);
36+
auto *expr = cs.generateConstraints(UMCRE, DC);
37+
ASSERT_TRUE(expr);
38+
3739
cs.addConstraint(
38-
ConstraintKind::Conversion, cs.getType(UMCRE), intOptType,
40+
ConstraintKind::Conversion, cs.getType(expr), intOptType,
3941
cs.getConstraintLocator(UMCRE, ConstraintLocator::ContextualType));
4042
SmallVector<Solution, 2> solutions;
4143
cs.solve(solutions);
@@ -67,10 +69,12 @@ TEST_F(SemaTest, TestLookupPrefersResultsOnOptionalRatherThanBase) {
6769
auto *UMCRE = new (Context) UnresolvedMemberChainResultExpr(UME, UME);
6870

6971
ConstraintSystem cs(DC, ConstraintSystemOptions());
70-
cs.generateConstraints(UMCRE, DC);
72+
auto *expr = cs.generateConstraints(UMCRE, DC);
73+
ASSERT_TRUE(expr);
74+
7175
cs.addConstraint(
72-
ConstraintKind::Conversion, cs.getType(UMCRE), intOptType,
73-
cs.getConstraintLocator(UMCRE, ConstraintLocator::ContextualType));
76+
ConstraintKind::Conversion, cs.getType(expr), intOptType,
77+
cs.getConstraintLocator(expr, ConstraintLocator::ContextualType));
7478
SmallVector<Solution, 2> solutions;
7579
cs.solve(solutions);
7680

0 commit comments

Comments
 (0)