Skip to content

[5.3] [CS] Emit syntactic diagnostics for for loop where clauses #33041

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 2 commits into from
Jul 22, 2020
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
19 changes: 15 additions & 4 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1206,13 +1206,15 @@ struct DynamicCallableMethods {
/// Describes the target to which a constraint system's solution can be
/// applied.
class SolutionApplicationTarget {
public:
enum class Kind {
expression,
function,
stmtCondition,
caseLabelItem,
} kind;

private:
union {
struct {
/// The expression being type-checked.
Expand Down Expand Up @@ -1418,6 +1420,12 @@ class SolutionApplicationTarget {
/// For a pattern initialization target, retrieve the contextual pattern.
ContextualPattern getContextualPattern() const;

/// Whether this target is for a for-in statement.
bool isForEachStmt() const {
return kind == Kind::expression &&
getExprContextualTypePurpose() == CTP_ForEachStmt;
}

/// Whether this is an initialization for an Optional.Some pattern.
bool isOptionalSomePatternInit() const {
return kind == Kind::expression &&
Expand Down Expand Up @@ -1466,14 +1474,12 @@ class SolutionApplicationTarget {
}

const ForEachStmtInfo &getForEachStmtInfo() const {
assert(kind == Kind::expression);
assert(expression.contextualPurpose == CTP_ForEachStmt);
assert(isForEachStmt());
return expression.forEachStmt;
}

ForEachStmtInfo &getForEachStmtInfo() {
assert(kind == Kind::expression);
assert(expression.contextualPurpose == CTP_ForEachStmt);
assert(isForEachStmt());
return expression.forEachStmt;
}

Expand Down Expand Up @@ -5284,6 +5290,11 @@ bool isKnownKeyPathDecl(ASTContext &ctx, ValueDecl *decl);
/// statements that could produce non-void result.
bool hasExplicitResult(ClosureExpr *closure);

/// Emit diagnostics for syntactic restrictions within a given solution
/// application target.
void performSyntacticDiagnosticsForTarget(
const SolutionApplicationTarget &target, bool isExprStmt);

} // end namespace constraints

template<typename ...Args>
Expand Down
27 changes: 26 additions & 1 deletion lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,31 @@ bool GenericRequirementsCheckListener::diagnoseUnsatisfiedRequirement(
return false;
}

void constraints::performSyntacticDiagnosticsForTarget(
const SolutionApplicationTarget &target, bool isExprStmt) {
auto *dc = target.getDeclContext();
switch (target.kind) {
case SolutionApplicationTarget::Kind::expression: {
// First emit diagnostics for the main expression.
performSyntacticExprDiagnostics(target.getAsExpr(), dc, isExprStmt);

// If this is a for-in statement, we also need to check the where clause if
// present.
if (target.isForEachStmt()) {
if (auto *whereExpr = target.getForEachStmtInfo().whereExpr)
performSyntacticExprDiagnostics(whereExpr, dc, /*isExprStmt*/ false);
}
return;
}
case SolutionApplicationTarget::Kind::function:
case SolutionApplicationTarget::Kind::stmtCondition:
case SolutionApplicationTarget::Kind::caseLabelItem:
// Nothing to do for these.
return;
}
llvm_unreachable("Unhandled case in switch!");
}

#pragma mark High-level entry points
Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
TypeLoc convertType,
Expand Down Expand Up @@ -2122,7 +2147,7 @@ TypeChecker::typeCheckExpression(
// expression now.
if (!cs.shouldSuppressDiagnostics()) {
bool isExprStmt = options.contains(TypeCheckExprFlags::IsExprStmt);
performSyntacticExprDiagnostics(result, dc, isExprStmt);
performSyntacticDiagnosticsForTarget(*resultTarget, isExprStmt);
}

resultTarget->setExpr(result);
Expand Down
9 changes: 9 additions & 0 deletions test/Constraints/availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ func test_contextual_member_with_availability() {

_ = Test(.foo) // Ok
}

@available(*, unavailable)
func unavailableFunction(_ x: Int) -> Bool { true } // expected-note {{'unavailableFunction' has been explicitly marked unavailable here}}

// SR-13260: Availability checking not working in the where clause of a for
// loop.
func sr13260(_ arr: [Int]) {
for x in arr where unavailableFunction(x) {} // expected-error {{'unavailableFunction' is unavailable}}
}