mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Sema: Let .foo patterns fall back to being ExprPatterns if they don't match an enum case.
This lets you match `case .foo` when `foo` resolves to any static member, instead of only a `case`, albeit without the exhaustiveness checking and subpattern capabilities of proper cases. While we're here, adjust the type system we set up for unresolved patterns embedded in expressions so that we give better signal in the error messages too.
This commit is contained in:
@@ -164,6 +164,9 @@ ERROR(value_type_comparison_with_nil_illegal,none,
|
||||
ERROR(cannot_match_expr_pattern_with_value,none,
|
||||
"expression pattern of type %0 cannot match values of type %1",
|
||||
(Type, Type))
|
||||
ERROR(cannot_match_unresolved_expr_pattern_with_value,none,
|
||||
"pattern cannot match values of type %0",
|
||||
(Type))
|
||||
|
||||
ERROR(cannot_reference_compare_types,none,
|
||||
"cannot check reference equality of functions; operands here have types "
|
||||
|
||||
@@ -492,7 +492,7 @@ class EnumElementPattern : public Pattern {
|
||||
SourceLoc DotLoc;
|
||||
SourceLoc NameLoc;
|
||||
Identifier Name;
|
||||
EnumElementDecl *ElementDecl;
|
||||
PointerUnion<EnumElementDecl *, Expr*> ElementDeclOrUnresolvedOriginalExpr;
|
||||
Pattern /*nullable*/ *SubPattern;
|
||||
|
||||
public:
|
||||
@@ -501,10 +501,25 @@ public:
|
||||
Pattern *SubPattern, Optional<bool> Implicit = None)
|
||||
: Pattern(PatternKind::EnumElement),
|
||||
ParentType(ParentType), DotLoc(DotLoc), NameLoc(NameLoc), Name(Name),
|
||||
ElementDecl(Element), SubPattern(SubPattern) {
|
||||
ElementDeclOrUnresolvedOriginalExpr(Element),
|
||||
SubPattern(SubPattern) {
|
||||
if (Implicit.hasValue() && *Implicit)
|
||||
setImplicit();
|
||||
}
|
||||
|
||||
/// Create an unresolved EnumElementPattern for a `.foo` pattern relying on
|
||||
/// contextual type.
|
||||
EnumElementPattern(SourceLoc DotLoc,
|
||||
SourceLoc NameLoc,
|
||||
Identifier Name,
|
||||
Pattern *SubPattern,
|
||||
Expr *UnresolvedOriginalExpr)
|
||||
: Pattern(PatternKind::EnumElement),
|
||||
ParentType(), DotLoc(DotLoc), NameLoc(NameLoc), Name(Name),
|
||||
ElementDeclOrUnresolvedOriginalExpr(UnresolvedOriginalExpr),
|
||||
SubPattern(SubPattern) {
|
||||
|
||||
}
|
||||
|
||||
bool hasSubPattern() const { return SubPattern; }
|
||||
|
||||
@@ -524,8 +539,19 @@ public:
|
||||
|
||||
Identifier getName() const { return Name; }
|
||||
|
||||
EnumElementDecl *getElementDecl() const { return ElementDecl; }
|
||||
void setElementDecl(EnumElementDecl *d) { ElementDecl = d; }
|
||||
EnumElementDecl *getElementDecl() const {
|
||||
return ElementDeclOrUnresolvedOriginalExpr.dyn_cast<EnumElementDecl*>();
|
||||
}
|
||||
void setElementDecl(EnumElementDecl *d) {
|
||||
ElementDeclOrUnresolvedOriginalExpr = d;
|
||||
}
|
||||
|
||||
Expr *getUnresolvedOriginalExpr() const {
|
||||
return ElementDeclOrUnresolvedOriginalExpr.get<Expr*>();
|
||||
}
|
||||
bool hasUnresolvedOriginalExpr() const {
|
||||
return ElementDeclOrUnresolvedOriginalExpr.is<Expr*>();
|
||||
}
|
||||
|
||||
SourceLoc getNameLoc() const { return NameLoc; }
|
||||
SourceLoc getLoc() const { return NameLoc; }
|
||||
|
||||
Reference in New Issue
Block a user