Skip to content

Commit 2946597

Browse files
committed
[Parse] Drop Swift3 support for conditional compilation condition
In Swift3: * `#if A is B` were warning * `#if A(foo:)` were warning * `#if A || B && C` has different meaning from Swift4+ and was diagnosed with migration support fix-it.
1 parent 622a140 commit 2946597

File tree

2 files changed

+5
-109
lines changed

2 files changed

+5
-109
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,15 +1504,6 @@ ERROR(unsupported_platform_condition_argument,none,
15041504
(StringRef))
15051505
ERROR(unsupported_conditional_compilation_expression_type,none,
15061506
"invalid conditional compilation expression", ())
1507-
WARNING(swift3_unsupported_conditional_compilation_expression_type,none,
1508-
"ignoring invalid conditional compilation expression, "
1509-
"which will be rejected in future version of Swift", ())
1510-
WARNING(swift3_conditional_compilation_expression_compound,none,
1511-
"ignoring parentheses in compound name, "
1512-
"which will be rejected in future version of Swift", ())
1513-
WARNING(swift3_conditional_compilation_expression_precedence,none,
1514-
"future version of Swift have different rule for evaluating condition; "
1515-
"add parentheses to make the condition compatible with Swift4", ())
15161507
ERROR(unsupported_conditional_compilation_integer,none,
15171508
"'%0' is not a valid conditional compilation expression, use '%1'",
15181509
(StringRef, StringRef))

lib/Parse/ParseIfConfig.cpp

Lines changed: 5 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,9 @@ class ValidateIfConfigCondition :
6767
auto UDRE = dyn_cast<UnresolvedDeclRefExpr>(E);
6868
if (!UDRE ||
6969
!UDRE->hasName() ||
70-
UDRE->getRefKind() != Kind)
70+
UDRE->getRefKind() != Kind ||
71+
UDRE->getName().isCompoundName())
7172
return None;
72-
if (UDRE->getName().isCompoundName()) {
73-
if (!Ctx.isSwiftVersion3())
74-
return None;
75-
// Swift3 used to accept compound names; warn and return the basename.
76-
D.diagnose(UDRE->getNameLoc().getLParenLoc(),
77-
diag::swift3_conditional_compilation_expression_compound)
78-
.fixItRemove({ UDRE->getNameLoc().getLParenLoc(),
79-
UDRE->getNameLoc().getRParenLoc() });
80-
}
8173

8274
return UDRE->getName().getBaseIdentifier().str();
8375
}
@@ -155,59 +147,6 @@ class ValidateIfConfigCondition :
155147
return LHS;
156148
}
157149

158-
// In Swift3 mode, leave sequence as a sequence because it has strange
159-
// evaluation rule. See 'EvaluateIfConfigCondition::visitSequenceExpr'.
160-
Expr *validateSequence(ArrayRef<Expr *> &S) {
161-
assert(Ctx.isSwiftVersion3());
162-
163-
SmallVector<Expr *, 3> Filtered;
164-
SmallVector<unsigned, 2> AndIdxs;
165-
Filtered.push_back(validate(S[0]));
166-
S = S.slice(1);
167-
168-
while (!S.empty()) {
169-
auto OpName = getDeclRefStr(S[0], DeclRefKind::BinaryOperator);
170-
if (!OpName.hasValue() || (*OpName != "||" && *OpName != "&&")) {
171-
// Warning and ignore in Swift3 mode.
172-
D.diagnose(
173-
S[0]->getLoc(),
174-
diag::swift3_unsupported_conditional_compilation_expression_type)
175-
.highlight({ S[0]->getLoc(), S[1]->getEndLoc() });
176-
} else {
177-
// Remember the start and end of '&&' sequence.
178-
bool InAnd = (AndIdxs.size() & 1) == 1;
179-
if ((*OpName == "&&" && !InAnd) || (*OpName == "||" && InAnd))
180-
AndIdxs.push_back(Filtered.size() - 1);
181-
182-
Filtered.push_back(S[0]);
183-
Filtered.push_back(validate(S[1]));
184-
}
185-
S = S.slice(2);
186-
}
187-
assert((Filtered.size() & 1) == 1);
188-
189-
// If the last OpName is '&&', close it with a parenthesis, except if the
190-
// operators are '&&' only.
191-
if ((1 == (AndIdxs.size() & 1)) && AndIdxs.back() > 0)
192-
AndIdxs.push_back(Filtered.size() - 1);
193-
// Emit fix-its to make this sequence compatilble with Swift >=4 even in
194-
// Swift3 mode.
195-
if (AndIdxs.size() >= 2) {
196-
assert((AndIdxs.size() & 1) == 0);
197-
auto diag = D.diagnose(
198-
Filtered[AndIdxs[0]]->getStartLoc(),
199-
diag::swift3_conditional_compilation_expression_precedence);
200-
for (unsigned i = 0, e = AndIdxs.size(); i < e; i += 2) {
201-
diag.fixItInsert(Filtered[AndIdxs[i]]->getStartLoc(), "(");
202-
diag.fixItInsertAfter(Filtered[AndIdxs[i + 1]]->getEndLoc(), ")");
203-
}
204-
}
205-
206-
if (Filtered.size() == 1)
207-
return Filtered[0];
208-
return SequenceExpr::create(Ctx, Filtered);
209-
}
210-
211150
public:
212151
ValidateIfConfigCondition(ASTContext &Ctx, DiagnosticEngine &D)
213152
: Ctx(Ctx), D(D), HasError(false) {}
@@ -369,14 +308,9 @@ class ValidateIfConfigCondition :
369308
// Fold sequence expression for non-Swift3 mode.
370309
Expr *visitSequenceExpr(SequenceExpr *E) {
371310
ArrayRef<Expr*> Elts = E->getElements();
372-
Expr *foldedExpr;
373-
if (Ctx.isSwiftVersion3()) {
374-
foldedExpr = validateSequence(Elts);
375-
} else {
376-
auto LHS = validate(Elts[0]);
377-
Elts = Elts.slice(1);
378-
foldedExpr = foldSequence(LHS, Elts);
379-
}
311+
Expr *foldedExpr = validate(Elts[0]);
312+
Elts = Elts.slice(1);
313+
foldedExpr = foldSequence(foldedExpr, Elts);
380314
assert(Elts.empty());
381315
return foldedExpr;
382316
}
@@ -477,42 +411,13 @@ class EvaluateIfConfigCondition :
477411
}
478412

479413
bool visitBinaryExpr(BinaryExpr *E) {
480-
assert(!Ctx.isSwiftVersion3() && "BinaryExpr in Swift3 mode");
481414
auto OpName = getDeclRefStr(E->getFn());
482415
auto Args = E->getArg()->getElements();
483416
if (OpName == "||") return visit(Args[0]) || visit(Args[1]);
484417
if (OpName == "&&") return visit(Args[0]) && visit(Args[1]);
485418
llvm_unreachable("unsupported binary operator");
486419
}
487420

488-
bool visitSequenceExpr(SequenceExpr *E) {
489-
assert(Ctx.isSwiftVersion3() && "SequenceExpr in non-Swift3 mode");
490-
ArrayRef<Expr *> Elems = E->getElements();
491-
auto Result = visit(Elems[0]);
492-
Elems = Elems.slice(1);
493-
while (!Elems.empty()) {
494-
auto OpName = getDeclRefStr(Elems[0]);
495-
496-
if (OpName == "||") {
497-
Result = Result || visit(Elems[1]);
498-
if (Result)
499-
// Note that this is the Swift3 behavior.
500-
// e.g. 'false || true && false' evaluates to 'true'.
501-
return true;
502-
} else if (OpName == "&&") {
503-
Result = Result && visit(Elems[1]);
504-
if (!Result)
505-
// Ditto.
506-
// e.g. 'false && true || true' evaluates to 'false'.
507-
return false;
508-
} else {
509-
llvm_unreachable("must be removed in validation phase");
510-
}
511-
Elems = Elems.slice(2);
512-
}
513-
return Result;
514-
}
515-
516421
bool visitExpr(Expr *E) { llvm_unreachable("Unvalidated condition?"); }
517422
};
518423

0 commit comments

Comments
 (0)