Skip to content

Commit 70300ad

Browse files
committed
[Function builders] Perform syntactic use checks within function builders.
1 parent 0895235 commit 70300ad

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "ConstraintSystem.h"
19+
#include "MiscDiagnostics.h"
1920
#include "SolutionResult.h"
2021
#include "TypeChecker.h"
2122
#include "swift/AST/ASTVisitor.h"
@@ -645,7 +646,7 @@ class BuilderClosureRewriter
645646
const Solution &solution;
646647
DeclContext *dc;
647648
AppliedBuilderTransform builderTransform;
648-
std::function<Expr *(Expr *)> rewriteExpr;
649+
std::function<Expr *(Expr *)> rewriteExprFn;
649650
std::function<Expr *(Expr *, Type, ConstraintLocator *)> coerceToType;
650651

651652
/// Retrieve the temporary variable that will be used to capture the
@@ -747,6 +748,13 @@ class BuilderClosureRewriter
747748
elements.push_back(pbd);
748749
}
749750

751+
Expr *rewriteExpr(Expr *expr) {
752+
Expr *result = rewriteExprFn(expr);
753+
if (result)
754+
performSyntacticExprDiagnostics(expr, dc, /*isExprStmt=*/false);
755+
return result;
756+
}
757+
750758
public:
751759
BuilderClosureRewriter(
752760
const Solution &solution,
@@ -756,7 +764,7 @@ class BuilderClosureRewriter
756764
std::function<Expr *(Expr *, Type, ConstraintLocator *)> coerceToType
757765
) : ctx(solution.getConstraintSystem().getASTContext()),
758766
solution(solution), dc(dc), builderTransform(builderTransform),
759-
rewriteExpr(rewriteExpr),
767+
rewriteExprFn(rewriteExpr),
760768
coerceToType(coerceToType){ }
761769

762770
Stmt *visitBraceStmt(BraceStmt *braceStmt, FunctionBuilderTarget target,

test/Constraints/function_builder_diags.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,16 @@ struct MyTuplifiedStruct {
256256
}
257257
}
258258
}
259+
260+
// Check that we're performing syntactic use diagnostics/
261+
func acceptMetatype<T>(_: T.Type) -> Bool { true }
262+
263+
func syntacticUses<T>(_: T) {
264+
tuplify(true) { x in
265+
if x && acceptMetatype(T) { // expected-error{{expected member name or constructor call after type name}}
266+
// expected-note@-1{{use '.self' to reference the type object}}
267+
acceptMetatype(T) // expected-error{{expected member name or constructor call after type name}}
268+
// expected-note@-1{{use '.self' to reference the type object}}
269+
}
270+
}
271+
}

0 commit comments

Comments
 (0)