Skip to content
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

[AST, Sema] Replace HoleType with PlaceholderType #35589

Merged
merged 14 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[Sema] Add support for folding placeholders into TypeExprs
Move markAcceptableDiscardExprs into PreCheckExpr so that we can perform this analysis before we convert expressions like `_? = <value>` into `_ = <value>` since `_?` is now an expression with meaning, and we only want to perform this transformation when `_?` is on the LHS of an assignment
  • Loading branch information
Jumhyn committed Feb 17, 2021
commit 40122dfecd9488ac1dd4db3def583837f0dd1aaf
32 changes: 0 additions & 32 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,6 @@ namespace {
= { nullptr, nullptr };
unsigned currentEditorPlaceholderVariable = 0;

/// Keep track of acceptable DiscardAssignmentExpr's.
llvm::SmallPtrSet<DiscardAssignmentExpr*, 2> CorrectDiscardAssignmentExprs;

/// A map from each UnresolvedMemberExpr to the respective (implicit) base
/// found during our walk.
llvm::MapVector<UnresolvedMemberExpr *, Type> UnresolvedBaseTypes;
Expand Down Expand Up @@ -2850,13 +2847,6 @@ namespace {
}

Type visitDiscardAssignmentExpr(DiscardAssignmentExpr *expr) {
/// Diagnose a '_' that isn't on the immediate LHS of an assignment.
if (!CorrectDiscardAssignmentExprs.count(expr)) {
auto &DE = CS.getASTContext().Diags;
DE.diagnose(expr->getLoc(), diag::discard_expr_outside_of_assignment);
return Type();
}

auto locator = CS.getConstraintLocator(expr);
auto typeVar = CS.createTypeVariable(locator, TVO_CanBindToNoEscape);
return LValueType::get(typeVar);
Expand Down Expand Up @@ -2894,25 +2884,6 @@ namespace {
}
}

/// Scout out the specified destination of an AssignExpr to recursively
/// identify DiscardAssignmentExpr in legal places. We can only allow them
/// in simple pattern-like expressions, so we reject anything complex here.
void markAcceptableDiscardExprs(Expr *E) {
if (!E) return;

if (auto *PE = dyn_cast<ParenExpr>(E))
return markAcceptableDiscardExprs(PE->getSubExpr());
if (auto *TE = dyn_cast<TupleExpr>(E)) {
for (auto &elt : TE->getElements())
markAcceptableDiscardExprs(elt);
return;
}
if (auto *DAE = dyn_cast<DiscardAssignmentExpr>(E))
CorrectDiscardAssignmentExprs.insert(DAE);

// Otherwise, we can't support this.
}

Type visitAssignExpr(AssignExpr *expr) {
// Handle invalid code.
if (!expr->getDest() || !expr->getSrc())
Expand Down Expand Up @@ -3498,9 +3469,6 @@ namespace {
return { false, expr };
}

if (auto *assignment = dyn_cast<AssignExpr>(expr))
CG.markAcceptableDiscardExprs(assignment->getDest());

return { true, expr };
}

Expand Down
39 changes: 38 additions & 1 deletion lib/Sema/PreCheckExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,9 @@ namespace {
/// The expressions that are direct arguments of call expressions.
llvm::SmallPtrSet<Expr *, 4> CallArgs;

/// Keep track of acceptable DiscardAssignmentExpr's.
llvm::SmallPtrSet<DiscardAssignmentExpr*, 2> CorrectDiscardAssignmentExprs;

/// Simplify expressions which are type sugar productions that got parsed
/// as expressions due to the parser not knowing which identifiers are
/// type names.
Expand Down Expand Up @@ -935,6 +938,27 @@ namespace {
StrangeInterpolationRewriter(getASTContext()));
}

/// Scout out the specified destination of an AssignExpr to recursively
/// identify DiscardAssignmentExpr in legal places. We can only allow them
/// in simple pattern-like expressions, so we reject anything complex here.
void markAcceptableDiscardExprs(Expr *E) {
if (!E) return;

if (auto *PE = dyn_cast<ParenExpr>(E))
return markAcceptableDiscardExprs(PE->getSubExpr());
if (auto *TE = dyn_cast<TupleExpr>(E)) {
for (auto &elt : TE->getElements())
markAcceptableDiscardExprs(elt);
return;
}
if (auto *BOE = dyn_cast<BindOptionalExpr>(E))
return markAcceptableDiscardExprs(BOE->getSubExpr());
if (auto *DAE = dyn_cast<DiscardAssignmentExpr>(E))
CorrectDiscardAssignmentExprs.insert(DAE);

// Otherwise, we can't support this.
}

public:
PreCheckExpression(DeclContext *dc, Expr *parent,
bool replaceInvalidRefsWithErrors)
Expand Down Expand Up @@ -1118,6 +1142,9 @@ namespace {
if (auto *ISLE = dyn_cast<InterpolatedStringLiteralExpr>(expr))
correctInterpolationIfStrange(ISLE);

if (auto *assignment = dyn_cast<AssignExpr>(expr))
markAcceptableDiscardExprs(assignment->getDest());

return finish(true, expr);
}

Expand Down Expand Up @@ -1244,7 +1271,8 @@ namespace {
// generating any of the unnecessary constraints.
if (auto BOE = dyn_cast<BindOptionalExpr>(expr)) {
if (auto DAE = dyn_cast<DiscardAssignmentExpr>(BOE->getSubExpr()))
return DAE;
if (CorrectDiscardAssignmentExprs.count(DAE))
return DAE;
}

// If this is a sugared type that needs to be folded into a single
Expand Down Expand Up @@ -1425,6 +1453,13 @@ TypeExpr *PreCheckExpression::simplifyTypeExpr(Expr *E) {
return simplifyNestedTypeExpr(UDE);
}

// Fold '_' into a placeholder type.
if (auto *DAE = dyn_cast<DiscardAssignmentExpr>(E)) {
auto *placeholderRepr =
new (getASTContext()) PlaceholderTypeRepr(DAE->getLoc());
return new (getASTContext()) TypeExpr(placeholderRepr);
}

// Fold T? into an optional type when T is a TypeExpr.
if (isa<OptionalEvaluationExpr>(E) || isa<BindOptionalExpr>(E)) {
TypeExpr *TyExpr;
Expand Down Expand Up @@ -1630,6 +1665,8 @@ TypeExpr *PreCheckExpression::simplifyTypeExpr(Expr *E) {
return nullptr;
if (auto *TyE = dyn_cast<TypeExpr>(E))
return TyE->getTypeRepr();
if (auto *DAE = dyn_cast<DiscardAssignmentExpr>(E))
return new (getASTContext()) PlaceholderTypeRepr(DAE->getLoc());
if (auto *TE = dyn_cast<TupleExpr>(E))
if (TE->getNumElements() == 0)
return TupleTypeRepr::createEmpty(ctx, TE->getSourceRange());
Expand Down