Skip to content

Commit 2c140bd

Browse files
authored
Merge pull request #58503 from xedin/rdar-92366212
[ConstraintSystem] Fail `~=` synthesis if constraint generation fails
2 parents e10ed0c + 46d14cf commit 2c140bd

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
@@ -4588,6 +4588,7 @@ class ConstraintSystem {
45884588
/// Generate constraints for the given solution target.
45894589
///
45904590
/// \returns true if an error occurred, false otherwise.
4591+
LLVM_NODISCARD
45914592
bool generateConstraints(SolutionApplicationTarget &target,
45924593
FreeTypeVariableBinding allowFreeTypeVariables);
45934594

@@ -4596,18 +4597,21 @@ class ConstraintSystem {
45964597
/// \param closure the closure expression
45974598
///
45984599
/// \returns \c true if constraint generation failed, \c false otherwise
4600+
LLVM_NODISCARD
45994601
bool generateConstraints(ClosureExpr *closure);
46004602

46014603
/// Generate constraints for the given (unchecked) expression.
46024604
///
46034605
/// \returns a possibly-sanitized expression, or null if an error occurred.
4606+
LLVM_NODISCARD
46044607
Expr *generateConstraints(Expr *E, DeclContext *dc,
46054608
bool isInputExpression = true);
46064609

46074610
/// Generate constraints for binding the given pattern to the
46084611
/// value of the given expression.
46094612
///
46104613
/// \returns a possibly-sanitized initializer, or null if an error occurred.
4614+
LLVM_NODISCARD
46114615
Type generateConstraints(Pattern *P, ConstraintLocatorBuilder locator,
46124616
bool bindPatternVarsOneWay,
46134617
PatternBindingDecl *patternBinding,
@@ -4617,6 +4621,7 @@ class ConstraintSystem {
46174621
///
46184622
/// \returns true if there was an error in constraint generation, false
46194623
/// if generation succeeded.
4624+
LLVM_NODISCARD
46204625
bool generateConstraints(StmtCondition condition, DeclContext *dc);
46214626

46224627
/// Generate constraints for a case statement.
@@ -4626,6 +4631,7 @@ class ConstraintSystem {
46264631
///
46274632
/// \returns true if there was an error in constraint generation, false
46284633
/// if generation succeeded.
4634+
LLVM_NODISCARD
46294635
bool generateConstraints(CaseStmt *caseStmt, DeclContext *dc,
46304636
Type subjectType, ConstraintLocator *locator);
46314637

@@ -4669,6 +4675,7 @@ class ConstraintSystem {
46694675
/// \param propertyType The type of the wrapped property.
46704676
///
46714677
/// \returns true if there is an error.
4678+
LLVM_NODISCARD
46724679
bool generateWrappedPropertyTypeConstraints(VarDecl *wrappedVar,
46734680
Type initializerType,
46744681
Type propertyType);

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8982,7 +8982,8 @@ static bool inferEnumMemberThroughTildeEqualsOperator(
89828982
}
89838983
}
89848984

8985-
cs.generateConstraints(target, FreeTypeVariableBinding::Disallow);
8985+
if (cs.generateConstraints(target, FreeTypeVariableBinding::Disallow))
8986+
return true;
89868987

89878988
// Sub-expression associated with expression pattern is the enum element
89888989
// access which needs to be connected to the provided element type.
@@ -9918,8 +9919,9 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
99189919
}
99199920

99209921
if (!paramDecl->getName().hasDollarPrefix()) {
9921-
generateWrappedPropertyTypeConstraints(paramDecl, backingType,
9922-
param.getParameterType());
9922+
if (generateWrappedPropertyTypeConstraints(paramDecl, backingType,
9923+
param.getParameterType()))
9924+
return false;
99239925
}
99249926

99259927
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)