Skip to content

Commit a35a43f

Browse files
committed
[CS] Fix a couple of constraints in getTypeForPattern
The TypedPattern and IsPattern constraints were incorrectly written, with conversions propagating out of the patterns, when really conversions ought to propagate into patterns. In any case, it seems like we really want equality here. Fix the constraints to use equality, and have the cast constraint operate on the external pattern type instead of the subpattern type.
1 parent 3bc14bb commit a35a43f

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

lib/Sema/CSGen.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,7 +2714,7 @@ namespace {
27142714
return Type();
27152715

27162716
CS.addConstraint(
2717-
ConstraintKind::Conversion, subPatternType, openedType,
2717+
ConstraintKind::Equal, subPatternType, openedType,
27182718
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
27192719

27202720
// FIXME [OPAQUE SUPPORT]: the distinction between where we want opaque
@@ -2805,21 +2805,6 @@ namespace {
28052805
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
28062806
if (!castType) return Type();
28072807

2808-
auto *subPattern = isPattern->getSubPattern();
2809-
Type subPatternType = getTypeForPattern(
2810-
subPattern,
2811-
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
2812-
castType, bindPatternVarsOneWay);
2813-
2814-
if (!subPatternType)
2815-
return Type();
2816-
2817-
// Make sure we can cast from the subpattern type to the type we're
2818-
// checking; if it's impossible, fail.
2819-
CS.addConstraint(
2820-
ConstraintKind::CheckedCast, subPatternType, castType,
2821-
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
2822-
28232808
// Allow `is` pattern to infer type from context which is then going
28242809
// to be propaged down to its sub-pattern via conversion. This enables
28252810
// correct handling of patterns like `_ as Foo` where `_` would
@@ -2828,9 +2813,23 @@ namespace {
28282813
auto isType =
28292814
CS.createTypeVariable(CS.getConstraintLocator(pattern),
28302815
TVO_CanBindToNoEscape | TVO_CanBindToHole);
2816+
2817+
// Make sure we can cast from the subpattern type to the type we're
2818+
// checking; if it's impossible, fail.
28312819
CS.addConstraint(
2832-
ConstraintKind::Conversion, subPatternType, isType,
2820+
ConstraintKind::CheckedCast, isType, castType,
28332821
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
2822+
2823+
if (auto *subPattern = isPattern->getSubPattern()) {
2824+
auto subPatternType = getTypeForPattern(
2825+
subPattern,
2826+
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
2827+
castType, bindPatternVarsOneWay);
2828+
2829+
CS.addConstraint(
2830+
ConstraintKind::Equal, subPatternType, castType,
2831+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
2832+
}
28342833
return setType(isType);
28352834
}
28362835

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14821,8 +14821,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1482114821
if (recordFix(fix))
1482214822
return SolutionKind::Error;
1482314823

14824-
(void)matchTypes(type1, OptionalType::get(type2),
14825-
ConstraintKind::Conversion,
14824+
(void)matchTypes(type1, OptionalType::get(type2), ConstraintKind::Equal,
1482614825
TypeMatchFlags::TMF_ApplyingFix, locator);
1482714826

1482814827
return SolutionKind::Solved;

test/Parse/matching_patterns.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ do {
307307
while case let _ as [Derived] = arr {}
308308
// expected-warning@-1 {{'let' pattern has no effect; sub-pattern didn't bind any variables}}
309309

310+
// https://github.com/apple/swift/issues/61850
310311
for case _ as [Derived] in [arr] {}
311312

312313
if case is [Derived] = arr {}

test/Parse/matching_patterns_reference_bindings.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ do {
328328
while case let _ as [Derived] = arr {}
329329
// expected-warning@-1 {{'let' pattern has no effect; sub-pattern didn't bind any variables}}
330330

331+
// https://github.com/apple/swift/issues/61850
331332
for case _ as [Derived] in [arr] {}
332333

333334
if case is [Derived] = arr {}

test/decl/var/property_wrappers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,7 @@ struct MissingPropertyWrapperUnwrap {
13381338

13391339
struct InvalidPropertyDelegateUse {
13401340
// TODO(diagnostics): We need to a tailored diagnostic for extraneous arguments in property delegate initialization
1341-
@Foo var x: Int = 42 // expected-error@:21 {{argument passed to call that takes no arguments}}
1341+
@Foo var x: Int = 42 // expected-error@:21 {{extra argument 'wrappedValue' in call}}
13421342

13431343
func test() {
13441344
self.x.foo() // expected-error {{value of type 'Int' has no member 'foo'}}

0 commit comments

Comments
 (0)