Skip to content

Commit a563db6

Browse files
authored
Merge pull request #64422 from Rajveer100/branch-for-issue-63993
Fix Issue #63993 by improving out-of-place binding diagnostic and reflecting 'var' or 'let' binding pattern
2 parents bc103b9 + c34f099 commit a563db6

File tree

7 files changed

+83
-44
lines changed

7 files changed

+83
-44
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace swift {
4646
class ValueDecl;
4747
class SourceFile;
4848

49-
enum class PatternKind : uint8_t;
49+
enum class DescriptivePatternKind : uint8_t;
5050
enum class SelfAccessKind : uint8_t;
5151
enum class ReferenceOwnership : uint8_t;
5252
enum class StaticSpellingKind : uint8_t;
@@ -113,7 +113,7 @@ namespace swift {
113113
Type,
114114
TypeRepr,
115115
FullyQualifiedType,
116-
PatternKind,
116+
DescriptivePatternKind,
117117
SelfAccessKind,
118118
ReferenceOwnership,
119119
StaticSpellingKind,
@@ -147,7 +147,7 @@ namespace swift {
147147
Type TypeVal;
148148
TypeRepr *TyR;
149149
FullyQualified<Type> FullyQualifiedTypeVal;
150-
PatternKind PatternKindVal;
150+
DescriptivePatternKind DescriptivePatternKindVal;
151151
SelfAccessKind SelfAccessKindVal;
152152
ReferenceOwnership ReferenceOwnershipVal;
153153
StaticSpellingKind StaticSpellingKindVal;
@@ -220,8 +220,9 @@ namespace swift {
220220
}
221221
}
222222

223-
DiagnosticArgument(PatternKind K)
224-
: Kind(DiagnosticArgumentKind::PatternKind), PatternKindVal(K) {}
223+
DiagnosticArgument(DescriptivePatternKind DPK)
224+
: Kind(DiagnosticArgumentKind::DescriptivePatternKind),
225+
DescriptivePatternKindVal(DPK) {}
225226

226227
DiagnosticArgument(ReferenceOwnership RO)
227228
: Kind(DiagnosticArgumentKind::ReferenceOwnership),
@@ -324,9 +325,9 @@ namespace swift {
324325
return FullyQualifiedTypeVal;
325326
}
326327

327-
PatternKind getAsPatternKind() const {
328-
assert(Kind == DiagnosticArgumentKind::PatternKind);
329-
return PatternKindVal;
328+
DescriptivePatternKind getAsDescriptivePatternKind() const {
329+
assert(Kind == DiagnosticArgumentKind::DescriptivePatternKind);
330+
return DescriptivePatternKindVal;
330331
}
331332

332333
ReferenceOwnership getAsReferenceOwnership() const {

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ ERROR(optional_chain_isnt_chaining,none,
12121212
"'?' must be followed by a call, member lookup, or subscript",
12131213
())
12141214
ERROR(pattern_in_expr,none,
1215-
"%0 cannot appear in an expression", (PatternKind))
1215+
"%0 cannot appear in an expression", (DescriptivePatternKind))
12161216
NOTE(note_call_to_operator,none,
12171217
"in call to operator %0", (DeclName))
12181218
NOTE(note_call_to_func,none,

include/swift/AST/Pattern.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,20 @@ enum class PatternKind : uint8_t {
4747
enum : unsigned { NumPatternKindBits =
4848
countBitsUsed(static_cast<unsigned>(PatternKind::Last_Pattern)) };
4949

50-
/// Diagnostic printing of PatternKinds.
51-
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, PatternKind kind);
50+
enum class DescriptivePatternKind : uint8_t {
51+
Paren,
52+
Tuple,
53+
Named,
54+
Any,
55+
Typed,
56+
Is,
57+
EnumElement,
58+
OptionalSome,
59+
Bool,
60+
Expr,
61+
Var,
62+
Let
63+
};
5264

5365
/// Pattern - Base class for all patterns in Swift.
5466
class alignas(8) Pattern : public ASTAllocated<Pattern> {
@@ -105,13 +117,20 @@ class alignas(8) Pattern : public ASTAllocated<Pattern> {
105117
public:
106118
PatternKind getKind() const { return PatternKind(Bits.Pattern.Kind); }
107119

120+
/// Retrieve the descriptive pattern kind for this pattern.
121+
DescriptivePatternKind getDescriptiveKind() const;
122+
108123
/// Retrieve the name of the given pattern kind.
109124
///
110125
/// This name should only be used for debugging dumps and other
111126
/// developer aids, and should never be part of a diagnostic or exposed
112127
/// to the user of the compiler in any way.
113128
static StringRef getKindName(PatternKind K);
114129

130+
/// Produce a name for the given descriptive pattern kind, which
131+
/// is suitable for use in diagnostics.
132+
static StringRef getDescriptivePatternKindName(DescriptivePatternKind K);
133+
115134
/// A pattern is implicit if it is compiler-generated and there
116135
/// exists no source code for it.
117136
bool isImplicit() const { return Bits.Pattern.isImplicit; }

lib/AST/DiagnosticEngine.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,11 @@ static void formatDiagnosticArgument(StringRef Modifier,
747747
<< FormatOpts.ClosingQuotationMark;
748748
break;
749749

750-
case DiagnosticArgumentKind::PatternKind:
751-
assert(Modifier.empty() && "Improper modifier for PatternKind argument");
752-
Out << Arg.getAsPatternKind();
750+
case DiagnosticArgumentKind::DescriptivePatternKind:
751+
assert(Modifier.empty() &&
752+
"Improper modifier for DescriptivePatternKind argument");
753+
Out << Pattern::getDescriptivePatternKindName(
754+
Arg.getAsDescriptivePatternKind());
753755
break;
754756

755757
case DiagnosticArgumentKind::SelfAccessKind:

lib/AST/Pattern.cpp

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,29 @@ using namespace swift;
3232
"Patterns are BumpPtrAllocated; the d'tor is never called");
3333
#include "swift/AST/PatternNodes.def"
3434

35-
/// Diagnostic printing of PatternKinds.
36-
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &OS, PatternKind kind) {
37-
switch (kind) {
38-
case PatternKind::Paren:
39-
return OS << "parenthesized pattern";
40-
case PatternKind::Tuple:
41-
return OS << "tuple pattern";
42-
case PatternKind::Named:
43-
return OS << "pattern variable binding";
44-
case PatternKind::Any:
45-
return OS << "'_' pattern";
46-
case PatternKind::Typed:
47-
return OS << "pattern type annotation";
48-
case PatternKind::Is:
49-
return OS << "prefix 'is' pattern";
50-
case PatternKind::Expr:
51-
return OS << "expression pattern";
35+
DescriptivePatternKind Pattern::getDescriptiveKind() const {
36+
#define TRIVIAL_PATTERN_KIND(Kind) \
37+
case PatternKind::Kind: \
38+
return DescriptivePatternKind::Kind
39+
40+
switch (getKind()) {
41+
TRIVIAL_PATTERN_KIND(Paren);
42+
TRIVIAL_PATTERN_KIND(Tuple);
43+
TRIVIAL_PATTERN_KIND(Named);
44+
TRIVIAL_PATTERN_KIND(Any);
45+
TRIVIAL_PATTERN_KIND(Typed);
46+
TRIVIAL_PATTERN_KIND(Is);
47+
TRIVIAL_PATTERN_KIND(EnumElement);
48+
TRIVIAL_PATTERN_KIND(OptionalSome);
49+
TRIVIAL_PATTERN_KIND(Bool);
50+
TRIVIAL_PATTERN_KIND(Expr);
51+
5252
case PatternKind::Binding:
53-
return OS << "'var' binding pattern";
54-
case PatternKind::EnumElement:
55-
return OS << "enum case matching pattern";
56-
case PatternKind::OptionalSome:
57-
return OS << "optional .Some matching pattern";
58-
case PatternKind::Bool:
59-
return OS << "bool matching pattern";
53+
return cast<BindingPattern>(this)->isLet() ? DescriptivePatternKind::Let
54+
: DescriptivePatternKind::Var;
6055
}
61-
llvm_unreachable("bad PatternKind");
56+
#undef TRIVIAL_PATTERN_KIND
57+
llvm_unreachable("bad DescriptivePatternKind");
6258
}
6359

6460
StringRef Pattern::getKindName(PatternKind K) {
@@ -69,6 +65,28 @@ StringRef Pattern::getKindName(PatternKind K) {
6965
llvm_unreachable("bad PatternKind");
7066
}
7167

68+
StringRef Pattern::getDescriptivePatternKindName(DescriptivePatternKind K) {
69+
#define ENTRY(Kind, String) \
70+
case DescriptivePatternKind::Kind: \
71+
return String
72+
switch (K) {
73+
ENTRY(Paren, "parenthesized pattern");
74+
ENTRY(Tuple, "tuple pattern");
75+
ENTRY(Named, "pattern variable binding");
76+
ENTRY(Any, "'_' pattern");
77+
ENTRY(Typed, "pattern type annotation");
78+
ENTRY(Is, "prefix 'is' pattern");
79+
ENTRY(EnumElement, "enum case matching pattern");
80+
ENTRY(OptionalSome, "optional pattern");
81+
ENTRY(Bool, "bool matching pattern");
82+
ENTRY(Expr, "expression pattern");
83+
ENTRY(Var, "'var' binding pattern");
84+
ENTRY(Let, "'let' binding pattern");
85+
}
86+
#undef ENTRY
87+
llvm_unreachable("bad DescriptivePatternKind");
88+
}
89+
7290
// Metaprogram to verify that every concrete class implements
7391
// a 'static bool classof(const Pattern*)'.
7492
template <bool fn(const Pattern*)> struct CheckClassOfPattern {

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4558,7 +4558,7 @@ namespace {
45584558
&& !cs.getType(simplified)->is<UnresolvedType>()) {
45594559
auto &de = cs.getASTContext().Diags;
45604560
de.diagnose(simplified->getLoc(), diag::pattern_in_expr,
4561-
expr->getSubPattern()->getKind());
4561+
expr->getSubPattern()->getDescriptiveKind());
45624562
}
45634563
return simplified;
45644564
}

test/Sema/redeclaration-checking.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,10 @@ let issue63750 = {
123123
// expected-note@-2 {{'x' previously declared here}}
124124
()
125125
}
126-
// FIXME: We should really say 'let' instead of 'var' in "'var' binding pattern cannot appear in an expression"
127-
// https://github.com/apple/swift/issues/63993
126+
128127
func bar(_ x: Int) -> Int { x }
129128
if case (bar(let x), let x) = (0,0) {}
130-
// expected-error@-1 {{'var' binding pattern cannot appear in an expression}}
129+
// expected-error@-1 {{'let' binding pattern cannot appear in an expression}}
131130
// expected-error@-2 {{invalid redeclaration of 'x'}}
132131
// expected-note@-3 {{'x' previously declared here}}
133132
}
@@ -149,7 +148,7 @@ func issue63750fn() {
149148
}
150149
func bar(_ x: Int) -> Int { x }
151150
if case (bar(let x), let x) = (0,0) {}
152-
// expected-error@-1 {{'var' binding pattern cannot appear in an expression}}
151+
// expected-error@-1 {{'let' binding pattern cannot appear in an expression}}
153152
// expected-error@-2 {{invalid redeclaration of 'x'}}
154153
// expected-note@-3 {{'x' previously declared here}}
155154
}

0 commit comments

Comments
 (0)