Skip to content

Commit 21512ac

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 43f0694 commit 21512ac

File tree

5 files changed

+20
-23
lines changed

5 files changed

+20
-23
lines changed

lib/Sema/CSGen.cpp

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

26512651
CS.addConstraint(
2652-
ConstraintKind::Conversion, subPatternType, openedType,
2652+
ConstraintKind::Equal, subPatternType, openedType,
26532653
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
26542654

26552655
// FIXME [OPAQUE SUPPORT]: the distinction between where we want opaque
@@ -2740,31 +2740,30 @@ namespace {
27402740
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
27412741
if (!castType) return Type();
27422742

2743-
auto *subPattern = isPattern->getSubPattern();
2744-
Type subPatternType = getTypeForPattern(
2745-
subPattern,
2746-
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
2747-
castType, bindPatternVarsOneWay);
2748-
2749-
if (!subPatternType)
2750-
return Type();
2751-
2752-
// Make sure we can cast from the subpattern type to the type we're
2753-
// checking; if it's impossible, fail.
2754-
CS.addConstraint(
2755-
ConstraintKind::CheckedCast, subPatternType, castType,
2756-
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
2757-
27582743
// Allow `is` pattern to infer type from context which is then going
27592744
// to be propaged down to its sub-pattern via conversion. This enables
27602745
// correct handling of patterns like `_ as Foo` where `_` would
27612746
// get a type of `Foo` but `is` pattern enclosing it could still be
27622747
// inferred from enclosing context.
27632748
auto isType = CS.createTypeVariable(CS.getConstraintLocator(pattern),
27642749
TVO_CanBindToNoEscape);
2750+
2751+
// Make sure we can cast from the subpattern type to the type we're
2752+
// checking; if it's impossible, fail.
27652753
CS.addConstraint(
2766-
ConstraintKind::Conversion, subPatternType, isType,
2754+
ConstraintKind::CheckedCast, isType, castType,
27672755
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
2756+
2757+
if (auto *subPattern = isPattern->getSubPattern()) {
2758+
auto subPatternType = getTypeForPattern(
2759+
subPattern,
2760+
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
2761+
castType, bindPatternVarsOneWay);
2762+
2763+
CS.addConstraint(
2764+
ConstraintKind::Equal, subPatternType, castType,
2765+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
2766+
}
27682767
return setType(isType);
27692768
}
27702769

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14299,7 +14299,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1429914299
return SolutionKind::Error;
1430014300

1430114301
(void)matchTypes(type1, OptionalType::get(type2),
14302-
ConstraintKind::Conversion,
14302+
ConstraintKind::Equal,
1430314303
TypeMatchFlags::TMF_ApplyingFix, locator);
1430414304

1430514305
return SolutionKind::Solved;

test/Parse/matching_patterns.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +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-
// FIXME: https://github.com/apple/swift/issues/61850
311-
// expected-warning@+1 {{heterogeneous collection literal could only be inferred to '[[Base]]'; add explicit type annotation if this is intentional}}
310+
// https://github.com/apple/swift/issues/61850
312311
for case _ as [Derived] in [arr] {}
313312

314313
if case is [Derived] = arr {}

test/Parse/matching_patterns_reference_bindings.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +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-
// FIXME: https://github.com/apple/swift/issues/61850
332-
// expected-warning@+1 {{heterogeneous collection literal could only be inferred to '[[Base]]'; add explicit type annotation if this is intentional}}
331+
// https://github.com/apple/swift/issues/61850
333332
for case _ as [Derived] in [arr] {}
334333

335334
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
@@ -1323,7 +1323,7 @@ struct MissingPropertyWrapperUnwrap {
13231323

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

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

0 commit comments

Comments
 (0)