Skip to content

[AST] Avoid setting questionLoc for implicit OptionalSomePatterns #79864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions include/swift/AST/Pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,12 +687,15 @@ class OptionalSomePattern : public Pattern {
static OptionalSomePattern *create(ASTContext &ctx, Pattern *subPattern,
SourceLoc questionLoc);

static OptionalSomePattern *
createImplicit(ASTContext &ctx, Pattern *subPattern,
SourceLoc questionLoc = SourceLoc());
static OptionalSomePattern *createImplicit(ASTContext &ctx,
Pattern *subPattern);

SourceLoc getQuestionLoc() const { return QuestionLoc; }

SourceRange getSourceRange() const {
if (QuestionLoc.isInvalid())
return SubPattern->getSourceRange();

return SourceRange(SubPattern->getStartLoc(), QuestionLoc);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/AST/Pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,10 @@ OptionalSomePattern *OptionalSomePattern::create(ASTContext &ctx,
return new (ctx) OptionalSomePattern(ctx, subPattern, questionLoc);
}

OptionalSomePattern *
OptionalSomePattern::createImplicit(ASTContext &ctx, Pattern *subPattern,
SourceLoc questionLoc) {
auto *P = OptionalSomePattern::create(ctx, subPattern, questionLoc);
OptionalSomePattern *OptionalSomePattern::createImplicit(ASTContext &ctx,
Pattern *subPattern) {
auto *P = OptionalSomePattern::create(ctx, subPattern,
/*questionLoc*/ SourceLoc());
P->setImplicit();
return P;
}
Expand Down
7 changes: 3 additions & 4 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ Pattern *ResolvePatternRequest::evaluate(Evaluator &evaluator, Pattern *P,

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

return P;
Expand Down Expand Up @@ -1029,7 +1029,7 @@ static NullablePtr<Pattern> simplifyToBoolPattern(ASTContext &Ctx,
if (auto wrappedType = patternTy->getOptionalObjectType()) {
if (auto P =
simplifyToBoolPattern(Ctx, EP, BLE, wrappedType).getPtrOrNull()) {
auto OP = OptionalSomePattern::createImplicit(Ctx, P, P->getEndLoc());
auto OP = OptionalSomePattern::createImplicit(Ctx, P);
OP->setType(patternTy);
return OP;
}
Expand Down Expand Up @@ -1475,8 +1475,7 @@ Pattern *TypeChecker::coercePatternToType(
if (lookupEnumMemberElement(dc,
baseType->lookThroughAllOptionalTypes(),
EEP->getName(), EEP->getLoc())) {
P = OptionalSomePattern::createImplicit(Context, EEP,
EEP->getEndLoc());
P = OptionalSomePattern::createImplicit(Context, EEP);
return coercePatternToType(
pattern.forSubPattern(P, /*retainTopLevel=*/true), type,
options, tryRewritePattern);
Expand Down