Skip to content

Commit 911fbb6

Browse files
authored
Merge pull request swiftlang#79864 from hamishknight/questionable-loc
[AST] Avoid setting `questionLoc` for implicit OptionalSomePatterns
2 parents 03f9632 + deac701 commit 911fbb6

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

include/swift/AST/Pattern.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -687,12 +687,15 @@ class OptionalSomePattern : public Pattern {
687687
static OptionalSomePattern *create(ASTContext &ctx, Pattern *subPattern,
688688
SourceLoc questionLoc);
689689

690-
static OptionalSomePattern *
691-
createImplicit(ASTContext &ctx, Pattern *subPattern,
692-
SourceLoc questionLoc = SourceLoc());
690+
static OptionalSomePattern *createImplicit(ASTContext &ctx,
691+
Pattern *subPattern);
693692

694693
SourceLoc getQuestionLoc() const { return QuestionLoc; }
694+
695695
SourceRange getSourceRange() const {
696+
if (QuestionLoc.isInvalid())
697+
return SubPattern->getSourceRange();
698+
696699
return SourceRange(SubPattern->getStartLoc(), QuestionLoc);
697700
}
698701

lib/AST/Pattern.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,10 @@ OptionalSomePattern *OptionalSomePattern::create(ASTContext &ctx,
367367
return new (ctx) OptionalSomePattern(ctx, subPattern, questionLoc);
368368
}
369369

370-
OptionalSomePattern *
371-
OptionalSomePattern::createImplicit(ASTContext &ctx, Pattern *subPattern,
372-
SourceLoc questionLoc) {
373-
auto *P = OptionalSomePattern::create(ctx, subPattern, questionLoc);
370+
OptionalSomePattern *OptionalSomePattern::createImplicit(ASTContext &ctx,
371+
Pattern *subPattern) {
372+
auto *P = OptionalSomePattern::create(ctx, subPattern,
373+
/*questionLoc*/ SourceLoc());
374374
P->setImplicit();
375375
return P;
376376
}

lib/Sema/TypeCheckPattern.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ Pattern *ResolvePatternRequest::evaluate(Evaluator &evaluator, Pattern *P,
645645

646646
// "if let" implicitly looks inside of an optional, so wrap it in an
647647
// OptionalSome pattern.
648-
P = OptionalSomePattern::createImplicit(Context, P, P->getEndLoc());
648+
P = OptionalSomePattern::createImplicit(Context, P);
649649
}
650650

651651
return P;
@@ -1029,7 +1029,7 @@ static NullablePtr<Pattern> simplifyToBoolPattern(ASTContext &Ctx,
10291029
if (auto wrappedType = patternTy->getOptionalObjectType()) {
10301030
if (auto P =
10311031
simplifyToBoolPattern(Ctx, EP, BLE, wrappedType).getPtrOrNull()) {
1032-
auto OP = OptionalSomePattern::createImplicit(Ctx, P, P->getEndLoc());
1032+
auto OP = OptionalSomePattern::createImplicit(Ctx, P);
10331033
OP->setType(patternTy);
10341034
return OP;
10351035
}
@@ -1475,8 +1475,7 @@ Pattern *TypeChecker::coercePatternToType(
14751475
if (lookupEnumMemberElement(dc,
14761476
baseType->lookThroughAllOptionalTypes(),
14771477
EEP->getName(), EEP->getLoc())) {
1478-
P = OptionalSomePattern::createImplicit(Context, EEP,
1479-
EEP->getEndLoc());
1478+
P = OptionalSomePattern::createImplicit(Context, EEP);
14801479
return coercePatternToType(
14811480
pattern.forSubPattern(P, /*retainTopLevel=*/true), type,
14821481
options, tryRewritePattern);

0 commit comments

Comments
 (0)