Skip to content

Commit 2c07057

Browse files
authored
Merge pull request #62809 from xedin/enable-result-builder-ast-transform-by-default-with-old-impl-5.8
[5.8][TypeChecker] Enable result builder AST transform by default
2 parents 175bd56 + 5875c37 commit 2c07057

30 files changed

+266
-217
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,10 @@ class PatternBindingDecl final : public Decl,
19691969
getMutablePatternList()[i].setInit(E);
19701970
}
19711971

1972+
void setOriginalInit(unsigned i, Expr *E) {
1973+
getMutablePatternList()[i].setOriginalInit(E);
1974+
}
1975+
19721976
Pattern *getPattern(unsigned i) const {
19731977
return getPatternList()[i].getPattern();
19741978
}

include/swift/Basic/Features.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ EXPERIMENTAL_FEATURE(FlowSensitiveConcurrencyCaptures, false)
104104
EXPERIMENTAL_FEATURE(MoveOnly, false)
105105
EXPERIMENTAL_FEATURE(OneWayClosureParameters, false)
106106
EXPERIMENTAL_FEATURE(TypeWitnessSystemInference, false)
107-
EXPERIMENTAL_FEATURE(ResultBuilderASTTransform, true)
108107
EXPERIMENTAL_FEATURE(LayoutPrespecialization, false)
109108
EXPERIMENTAL_FEATURE(ModuleInterfaceExportAs, true)
110109

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6460,6 +6460,10 @@ class ConjunctionElement {
64606460
Element->print(Out, SM, indent);
64616461
}
64626462

6463+
/// Returns \c false if this conjunction element is known not to contain the
6464+
/// code compleiton token.
6465+
bool mightContainCodeCompletionToken(const ConstraintSystem &cs) const;
6466+
64636467
private:
64646468
/// Find type variables referenced by this conjunction element.
64656469
/// If this is a closure body element, it would look inside \c ASTNode.

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,10 +3118,6 @@ static bool usesFeatureOneWayClosureParameters(Decl *decl) {
31183118
return false;
31193119
}
31203120

3121-
static bool usesFeatureResultBuilderASTTransform(Decl *decl) {
3122-
return false;
3123-
}
3124-
31253121
static bool usesFeatureTypeWitnessSystemInference(Decl *decl) {
31263122
return false;
31273123
}

lib/AST/Decl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,7 @@ PatternBindingDecl *PatternBindingDecl::createImplicit(
15291529
Pat, /*EqualLoc*/ SourceLoc(), nullptr, Parent);
15301530
Result->setImplicit();
15311531
Result->setInit(0, E);
1532+
Result->setOriginalInit(0, E);
15321533
return Result;
15331534
}
15341535

lib/IDE/CodeCompletionResultType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ const USRBasedType *USRBasedType::fromType(Type Ty, USRBasedTypeArena &Arena) {
184184
Ty = Ty->getCanonicalType();
185185

186186
// For opaque types like 'some View', consider them equivalent to 'View'.
187-
if (auto OpaqueType = Ty->getAs<OpaqueTypeArchetypeType>()) {
187+
if (auto OpaqueType = Ty->getAs<ArchetypeType>()) {
188188
if (auto Existential = OpaqueType->getExistentialType()) {
189189
Ty = Existential;
190190
}

lib/Sema/BuilderTransform.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -959,8 +959,8 @@ class ResultBuilderTransform
959959
VarDecl *captureExpr(Expr *expr, SmallVectorImpl<ASTNode> &container) {
960960
auto *var = builder.buildVar(expr->getStartLoc());
961961
Pattern *pattern = NamedPattern::createImplicit(ctx, var);
962-
auto *PB = PatternBindingDecl::createImplicit(ctx, StaticSpellingKind::None,
963-
pattern, expr, dc);
962+
auto *PB = PatternBindingDecl::createImplicit(
963+
ctx, StaticSpellingKind::None, pattern, expr, dc, var->getStartLoc());
964964
return recordVar(PB, container);
965965
}
966966

@@ -972,7 +972,8 @@ class ResultBuilderTransform
972972
ctx, NamedPattern::createImplicit(ctx, var),
973973
type ? type : PlaceholderType::get(ctx, var));
974974
auto *PB = PatternBindingDecl::createImplicit(
975-
ctx, StaticSpellingKind::None, placeholder, /*init=*/initExpr, dc);
975+
ctx, StaticSpellingKind::None, placeholder, /*init=*/initExpr, dc,
976+
var->getStartLoc());
976977
return recordVar(PB, container);
977978
}
978979

@@ -1058,11 +1059,20 @@ class ResultBuilderTransform
10581059
{Identifier()});
10591060
}
10601061

1061-
auto *capture = captureExpr(expr, newBody);
1062-
// A reference to the synthesized variable is passed as an argument
1063-
// to buildBlock.
1064-
buildBlockArguments.push_back(
1065-
builder.buildVarRef(capture, element.getStartLoc()));
1062+
if (isa<CodeCompletionExpr>(expr)) {
1063+
// Insert the CodeCompletionExpr directly into the buildBlock call. That
1064+
// way, we can extract the contextual type of the code completion token
1065+
// to rank code completion items that match the type expected by
1066+
// buildBlock higher.
1067+
buildBlockArguments.push_back(expr);
1068+
} else {
1069+
auto *capture = captureExpr(expr, newBody);
1070+
// A reference to the synthesized variable is passed as an argument
1071+
// to buildBlock.
1072+
buildBlockArguments.push_back(
1073+
builder.buildVarRef(capture, element.getStartLoc()));
1074+
}
1075+
10661076
return None;
10671077
}
10681078

@@ -2459,7 +2469,9 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
24592469
return None;
24602470
}
24612471

2462-
if (Context.LangOpts.hasFeature(Feature::ResultBuilderASTTransform)) {
2472+
auto disableASTTransform = [&](NominalTypeDecl *builder) { return false; };
2473+
2474+
if (!disableASTTransform(builder)) {
24632475
auto transformedBody = getBuilderTransformedBody(fn, builder);
24642476
// If this builder transform has not yet been applied to this function,
24652477
// let's do it and cache the result.
@@ -2517,7 +2529,7 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
25172529
auto &log = llvm::errs();
25182530
auto indent = solverState ? solverState->getCurrentIndent() : 0;
25192531
log.indent(indent) << "------- Transfomed Body -------\n";
2520-
transformedBody->second->dump(log);
2532+
transformedBody->second->dump(log, &getASTContext(), indent);
25212533
log << '\n';
25222534
}
25232535

lib/Sema/CSStep.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,17 @@ bool ConjunctionStep::attempt(const ConjunctionElement &element) {
880880
CS.Timer.emplace(element.getLocator(), CS);
881881
}
882882

883+
assert(!ModifiedOptions.hasValue() &&
884+
"Previously modified options should have been restored in resume");
885+
if (CS.isForCodeCompletion() &&
886+
!element.mightContainCodeCompletionToken(CS)) {
887+
ModifiedOptions.emplace(CS.Options);
888+
// If we know that this conjunction element doesn't contain the code
889+
// completion token, type check it in normal mode without any special
890+
// behavior that is intended for the code completion token.
891+
CS.Options -= ConstraintSystemFlags::ForCodeCompletion;
892+
}
893+
883894
auto success = element.attempt(CS);
884895

885896
// If element attempt has failed, mark whole conjunction
@@ -891,6 +902,9 @@ bool ConjunctionStep::attempt(const ConjunctionElement &element) {
891902
}
892903

893904
StepResult ConjunctionStep::resume(bool prevFailed) {
905+
// Restore the old ConstraintSystemOptions if 'attempt' modified them.
906+
ModifiedOptions.reset();
907+
894908
// Return from the follow-up splitter step that
895909
// attempted to apply information gained from the
896910
// isolated constraint to the outer context.

lib/Sema/CSStep.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,11 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
966966
/// in isolated mode.
967967
SmallVector<Solution, 4> IsolatedSolutions;
968968

969+
/// If \c ConjunctionStep::attempt modified the constraint system options,
970+
/// it will store the original options in this \c llvm::SaveAndRestore.
971+
/// Upon \c resume, these values will be restored.
972+
Optional<llvm::SaveAndRestore<ConstraintSystemOptions>> ModifiedOptions;
973+
969974
public:
970975
ConjunctionStep(ConstraintSystem &cs, Constraint *conjunction,
971976
SmallVectorImpl<Solution> &solutions)

lib/Sema/CSSyntacticElement.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,13 +1290,17 @@ class SyntacticElementSolutionApplication
12901290
virtual ~SyntacticElementSolutionApplication() {}
12911291

12921292
private:
1293-
ASTNode visit(Stmt *S) {
1293+
1294+
ASTNode visit(Stmt *S, bool performSyntacticDiagnostics = true) {
12941295
auto rewritten = ASTVisitor::visit(S);
12951296
if (!rewritten)
12961297
return {};
12971298

1298-
if (auto *stmt = getAsStmt(rewritten))
1299-
performStmtDiagnostics(stmt, context.getAsDeclContext());
1299+
if (performSyntacticDiagnostics) {
1300+
if (auto *stmt = getAsStmt(rewritten)) {
1301+
performStmtDiagnostics(stmt, context.getAsDeclContext());
1302+
}
1303+
}
13001304

13011305
return rewritten;
13021306
}
@@ -1819,8 +1823,9 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
18191823

18201824
private:
18211825
ASTNode visitDoStmt(DoStmt *doStmt) override {
1822-
if (auto transformed = transformDo(doStmt))
1823-
return visit(transformed.get());
1826+
if (auto transformed = transformDo(doStmt)) {
1827+
return visit(transformed.get(), /*performSyntacticDiagnostics=*/false);
1828+
}
18241829

18251830
auto newBody = visit(doStmt->getBody());
18261831
if (!newBody)
@@ -2111,11 +2116,9 @@ SolutionApplicationToFunctionResult ConstraintSystem::applySolution(
21112116
if (auto transform = solution.getAppliedBuilderTransform(fn)) {
21122117
NullablePtr<BraceStmt> newBody;
21132118

2114-
if (Context.LangOpts.hasFeature(Feature::ResultBuilderASTTransform)) {
2115-
BraceStmt *transformedBody =
2116-
const_cast<BraceStmt *>(transform->transformedBody.get());
2117-
2118-
fn.setParsedBody(transformedBody, /*singleExpression=*/false);
2119+
if (auto transformedBody = transform->transformedBody) {
2120+
fn.setParsedBody(const_cast<BraceStmt *>(transformedBody.get()),
2121+
/*singleExpression=*/false);
21192122

21202123
ResultBuilderRewriter rewriter(solution, fn, *transform, rewriteTarget);
21212124

@@ -2203,6 +2206,21 @@ bool ConstraintSystem::applySolutionToBody(Solution &solution,
22032206
return false;
22042207
}
22052208

2209+
bool ConjunctionElement::mightContainCodeCompletionToken(
2210+
const ConstraintSystem &cs) const {
2211+
if (Element->getKind() == ConstraintKind::SyntacticElement) {
2212+
if (Element->getSyntacticElement().getSourceRange().isInvalid()) {
2213+
return true;
2214+
} else {
2215+
return cs.containsIDEInspectionTarget(Element->getSyntacticElement());
2216+
}
2217+
} else {
2218+
// All other constraint kinds are not handled yet. Assume that they might
2219+
// contain the code completion token.
2220+
return true;
2221+
}
2222+
}
2223+
22062224
void ConjunctionElement::findReferencedVariables(
22072225
ConstraintSystem &cs, SmallPtrSetImpl<TypeVariableType *> &typeVars) const {
22082226
auto referencedVars = Element->getTypeVariables();

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -998,11 +998,6 @@ static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs,
998998
return found->second;
999999

10001000
if (auto archetypeType = dyn_cast<ArchetypeType>(origType)) {
1001-
// We leave opaque types and their nested associated types alone here.
1002-
// They're globally available.
1003-
if (isa<OpaqueTypeArchetypeType>(archetypeType))
1004-
return origType;
1005-
10061001
auto root = archetypeType->getRoot();
10071002
// For other nested types, fail here so the default logic in subst()
10081003
// for nested types applies.
@@ -1033,7 +1028,8 @@ static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs,
10331028
types[origType] = replacement;
10341029
return replacement;
10351030
},
1036-
MakeAbstractConformanceForGenericType());
1031+
MakeAbstractConformanceForGenericType(),
1032+
SubstFlags::SubstituteOpaqueArchetypes);
10371033
}
10381034

10391035
bool TypeChecker::typesSatisfyConstraint(Type type1, Type type2,

0 commit comments

Comments
 (0)