Skip to content
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

[Sema] Requestify pattern resolution #74387

Merged
merged 1 commit into from
Jun 20, 2024
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
27 changes: 27 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -2527,6 +2527,33 @@ class EnumElementExprPatternRequest
bool isCached() const { return true; }
};

/// Perform top-down syntactic disambiguation of a pattern. Where ambiguous
/// expr/pattern productions occur (tuples, function calls, etc.), favor the
/// pattern interpretation if it forms a valid pattern; otherwise, leave it as
/// an expression. This does no type-checking except for the bare minimum to
/// disambiguate semantics-dependent pattern forms.
///
/// Currently cached to ensure the constraint system does not resolve the same
/// pattern multiple times along different solver paths. Once we move pattern
/// resolution into pre-checking, we could make this uncached.
class ResolvePatternRequest
: public SimpleRequest<ResolvePatternRequest,
Pattern *(Pattern *, DeclContext *, bool),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
Pattern *evaluate(Evaluator &evaluator, Pattern *P, DeclContext *DC,
bool isStmtCondition) const;
public:
// Cached.
bool isCached() const { return true; }
};

class InterfaceTypeRequest :
public SimpleRequest<InterfaceTypeRequest,
Type (ValueDecl *),
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ SWIFT_REQUEST(TypeChecker, ExprPatternMatchRequest,
SWIFT_REQUEST(TypeChecker, EnumElementExprPatternRequest,
ExprPattern *(const EnumElementPattern *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, ResolvePatternRequest,
Pattern *(Pattern *, DeclContext *, bool),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, OpaqueReadOwnershipRequest,
OpaqueReadOwnership(AbstractStorageDecl *), SeparatelyCached,
NoLocationInfo)
Expand Down
17 changes: 10 additions & 7 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,13 +624,9 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,

} // end anonymous namespace

/// Perform top-down syntactic disambiguation of a pattern. Where ambiguous
/// expr/pattern productions occur (tuples, function calls, etc.), favor the
/// pattern interpretation if it forms a valid pattern; otherwise, leave it as
/// an expression. This does no type-checking except for the bare minimum to
/// disambiguate semantics-dependent pattern forms.
Pattern *TypeChecker::resolvePattern(Pattern *P, DeclContext *DC,
bool isStmtCondition) {
Pattern *ResolvePatternRequest::evaluate(Evaluator &evaluator, Pattern *P,
DeclContext *DC,
bool isStmtCondition) const {
P = ResolvePattern(DC).visit(P);

TypeChecker::diagnoseDuplicateBoundVars(P);
Expand Down Expand Up @@ -691,6 +687,13 @@ Pattern *TypeChecker::resolvePattern(Pattern *P, DeclContext *DC,
return P;
}

Pattern *TypeChecker::resolvePattern(Pattern *P, DeclContext *DC,
bool isStmtCondition) {
auto &eval = DC->getASTContext().evaluator;
return evaluateOrDefault(eval, ResolvePatternRequest{P, DC, isStmtCondition},
nullptr);
}

static Type
validateTypedPattern(TypedPattern *TP, DeclContext *dc,
TypeResolutionOptions options,
Expand Down
10 changes: 10 additions & 0 deletions validation-test/IDE/crashers_2_fixed/rdar128661960.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %batch-code-completion

func takeClosure(closure: () -> Bool) {}

func test(someLocal: Int) {
takeClosure {
if case .begin(#^COMPLETE^#)
}
}
// COMPLETE: someLocal
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ func test(result: MyEnum, optResult: MyEnum?) {
}

let _ = {
if let .co = result { // expected-error 2 {{pattern matching in a condition requires the 'case' keyword}}
if let .co = result { // expected-error {{pattern matching in a condition requires the 'case' keyword}}
// expected-error@-1 {{type 'MyEnum' has no member 'co'}}
}
}

let _ = {
if let .co = optResult { // expected-error 2 {{pattern matching in a condition requires the 'case' keyword}}
if let .co = optResult { // expected-error {{pattern matching in a condition requires the 'case' keyword}}
// expected-error@-1 {{type 'MyEnum?' has no member 'co'}}
}
}
Expand Down