Skip to content

[Sema] Remove replaceInvalidRefsWithErrors param #75391

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 1 commit into from
Jul 22, 2024
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
12 changes: 2 additions & 10 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -5402,19 +5402,11 @@ class ConstraintSystem {
public:
/// Pre-check the target, validating any types that occur in it
/// and folding sequence expressions.
///
/// \param replaceInvalidRefsWithErrors Indicates whether it's allowed
/// to replace any discovered invalid member references with `ErrorExpr`.
static bool preCheckTarget(SyntacticElementTarget &target,
bool replaceInvalidRefsWithErrors);
static bool preCheckTarget(SyntacticElementTarget &target);

/// Pre-check the expression, validating any types that occur in the
/// expression and folding sequence expressions.
///
/// \param replaceInvalidRefsWithErrors Indicates whether it's allowed
/// to replace any discovered invalid member references with `ErrorExpr`.
static bool preCheckExpression(Expr *&expr, DeclContext *dc,
bool replaceInvalidRefsWithErrors);
static bool preCheckExpression(Expr *&expr, DeclContext *dc);

/// Solve the system of constraints generated from provided target.
///
Expand Down
3 changes: 1 addition & 2 deletions include/swift/Sema/IDETypeChecking.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ namespace swift {

/// Thunk around \c TypeChecker::resolveDeclRefExpr to make it available to
/// \c swift::ide
Expr *resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *Context,
bool replaceInvalidRefsWithErrors);
Expr *resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *Context);

LookupResult
lookupSemanticMember(DeclContext *DC, Type ty, DeclName name);
Expand Down
10 changes: 4 additions & 6 deletions lib/IDE/IDETypeChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1022,9 +1022,8 @@ swift::getShorthandShadows(CaptureListExpr *CaptureList, DeclContext *DC) {
}

if (auto UDRE = dyn_cast<UnresolvedDeclRefExpr>(Init)) {
if (DC) {
Init = resolveDeclRefExpr(UDRE, DC, /*replaceInvalidRefsWithErrors=*/false);
}
if (DC)
Init = resolveDeclRefExpr(UDRE, DC);
}

auto *ReferencedVar = Init->getReferencedDecl().getDecl();
Expand All @@ -1045,9 +1044,8 @@ swift::getShorthandShadows(LabeledConditionalStmt *CondStmt, DeclContext *DC) {

Expr *Init = Cond.getInitializer();
if (auto UDRE = dyn_cast<UnresolvedDeclRefExpr>(Init)) {
if (DC) {
Init = resolveDeclRefExpr(UDRE, DC, /*replaceInvalidRefsWithErrors=*/false);
}
if (DC)
Init = resolveDeclRefExpr(UDRE, DC);
}

auto ReferencedVar = Init->getReferencedDecl().getDecl();
Expand Down
5 changes: 2 additions & 3 deletions lib/IDE/PostfixCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,7 @@ getOperatorCompletionTypes(DeclContext *DC, Type LHSType, OperatorDecl *Op) {
UnresolvedDeclRefExpr UDRE(DeclNameRef(Op->getName()),
getDeclRefKindOfOperator(Op), DeclNameLoc(Loc));
DiagnosticTransaction IgnoreDiags(DC->getASTContext().Diags);
Expr *OpExpr =
resolveDeclRefExpr(&UDRE, DC, /*replaceInvalidRefsWithErrors=*/true);
Expr *OpExpr = resolveDeclRefExpr(&UDRE, DC);
IgnoreDiags.abort();
if (isa<ErrorExpr>(OpExpr)) {
// If we couldn't resolve the operator (e.g. because there is only an
Expand All @@ -318,7 +317,7 @@ getOperatorCompletionTypes(DeclContext *DC, Type LHSType, OperatorDecl *Op) {
llvm_unreachable("unexpected operator kind");
}

CS.preCheckExpression(OpCallExpr, DC, /*replaceInvalidRefsWithErrors=*/true);
CS.preCheckExpression(OpCallExpr, DC);
OpCallExpr = CS.generateConstraints(OpCallExpr, DC);

CS.assignFixedType(CS.getType(&LHS)->getAs<TypeVariableType>(), LHSType);
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,7 @@ class PreCheckResultBuilderApplication : public ASTWalker {
// Suppress any diagnostics which could be produced by this expression.
DiagnosticTransaction transaction(diagEngine);

HasError |= ConstraintSystem::preCheckExpression(
E, DC, /*replaceInvalidRefsWithErrors=*/true);
HasError |= ConstraintSystem::preCheckExpression(E, DC);

HasError |= transaction.hasErrors();

Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10761,7 +10761,7 @@ static bool inferEnumMemberThroughTildeEqualsOperator(

DiagnosticTransaction diagnostics(ctx.Diags);
{
if (cs.preCheckTarget(target, /*replaceInvalidRefWithErrors=*/true)) {
if (cs.preCheckTarget(target)) {
// Skip diagnostics if they are disabled, otherwise it would result in
// duplicate diagnostics, since this operation is going to be repeated
// in diagnostic mode.
Expand Down
5 changes: 2 additions & 3 deletions lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ class SyntacticElementConstraintGenerator
void visitExprPattern(ExprPattern *EP) {
auto target = SyntacticElementTarget::forExprPattern(EP);

if (cs.preCheckTarget(target, /*replaceInvalidRefWithErrors=*/true)) {
if (cs.preCheckTarget(target)) {
hadError = true;
return;
}
Expand Down Expand Up @@ -771,8 +771,7 @@ class SyntacticElementConstraintGenerator
init, patternType, patternBinding, index,
/*bindPatternVarsOneWay=*/false);

if (ConstraintSystem::preCheckTarget(
target, /*replaceInvalidRefsWithErrors=*/true))
if (ConstraintSystem::preCheckTarget(target))
return std::nullopt;

return target;
Expand Down
47 changes: 16 additions & 31 deletions lib/Sema/PreCheckExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,7 @@ static BinaryExpr *getCompositionExpr(Expr *expr) {
/// returning the resultant expression. Context is the DeclContext used
/// for the lookup.
Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
DeclContext *DC,
bool replaceInvalidRefsWithErrors) {
DeclContext *DC) {
// Process UnresolvedDeclRefExpr by doing an unqualified lookup.
DeclNameRef Name = UDRE->getName();
SourceLoc Loc = UDRE->getLoc();
Expand All @@ -431,10 +430,10 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
LookupName = DeclNameRef(lookupName);
}

auto &Context = DC->getASTContext();

auto errorResult = [&]() -> Expr * {
if (replaceInvalidRefsWithErrors)
return new (DC->getASTContext()) ErrorExpr(UDRE->getSourceRange());
return UDRE;
return new (Context) ErrorExpr(UDRE->getSourceRange());
};

// Perform standard value name lookup.
Expand All @@ -449,8 +448,6 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
bool AllDeclRefs = true;
SmallVector<ValueDecl*, 4> ResultValues;

auto &Context = DC->getASTContext();

// First, look for a local binding in scope.
if (Loc.isValid() && !Name.isOperator()) {
ASTScope::lookupLocalDecls(DC->getParentSourceFile(),
Expand Down Expand Up @@ -487,8 +484,7 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
Context.Diags.diagnose(Loc, diag::use_local_before_declaration, Name);
Context.Diags.diagnose(innerDecl, diag::decl_declared_here,
localDeclAfterUse);
Expr *error = new (Context) ErrorExpr(UDRE->getSourceRange());
return error;
return errorResult();
}

Lookup.shiftDownResults();
Expand Down Expand Up @@ -705,7 +701,7 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
UDRE->getRefKind() == DeclRefKind::BinaryOperator
? 0
: UDRE->getRefKind() == DeclRefKind::PrefixOperator ? 1 : 2);
return new (Context) ErrorExpr(UDRE->getSourceRange());
return errorResult();
}

// For operators, sort the results so that non-generic operations come
Expand Down Expand Up @@ -844,7 +840,7 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
auto *Decl = Result.getValueDecl();
Context.Diags.diagnose(Decl, diag::decl_declared_here, Decl);
}
return new (Context) ErrorExpr(UDRE->getSourceRange());
return errorResult();
}

/// If an expression references 'self.init' or 'super.init' in an
Expand Down Expand Up @@ -1036,10 +1032,6 @@ namespace {

Expr *ParentExpr;

/// Indicates whether pre-check is allowed to insert
/// implicit `ErrorExpr` in place of invalid references.
bool UseErrorExprs;

/// A stack of expressions being walked, used to determine where to
/// insert RebindSelfInConstructorExpr nodes.
llvm::SmallVector<Expr *, 8> ExprStack;
Expand Down Expand Up @@ -1107,10 +1099,8 @@ namespace {
void markAcceptableDiscardExprs(Expr *E);

public:
PreCheckExpression(DeclContext *dc, Expr *parent,
bool replaceInvalidRefsWithErrors)
: Ctx(dc->getASTContext()), DC(dc), ParentExpr(parent),
UseErrorExprs(replaceInvalidRefsWithErrors) {}
PreCheckExpression(DeclContext *dc, Expr *parent)
: Ctx(dc->getASTContext()), DC(dc), ParentExpr(parent) {}

ASTContext &getASTContext() const { return Ctx; }

Expand Down Expand Up @@ -1232,8 +1222,7 @@ namespace {
new (Ctx) ErrorExpr(unresolved->getSourceRange()));
}

auto *refExpr =
TypeChecker::resolveDeclRefExpr(unresolved, DC, UseErrorExprs);
auto *refExpr = TypeChecker::resolveDeclRefExpr(unresolved, DC);

// Check whether this is standalone `self` in init accessor, which
// is invalid.
Expand Down Expand Up @@ -2461,14 +2450,13 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
: nullptr;
}

bool ConstraintSystem::preCheckTarget(SyntacticElementTarget &target,
bool replaceInvalidRefsWithErrors) {
bool ConstraintSystem::preCheckTarget(SyntacticElementTarget &target) {
auto *DC = target.getDeclContext();

bool hadErrors = false;

if (auto *expr = target.getAsExpr()) {
hadErrors |= preCheckExpression(expr, DC, replaceInvalidRefsWithErrors);
hadErrors |= preCheckExpression(expr, DC);
// Even if the pre-check fails, expression still has to be re-set.
target.setExpr(expr);
}
Expand All @@ -2479,12 +2467,10 @@ bool ConstraintSystem::preCheckTarget(SyntacticElementTarget &target,
auto *sequenceExpr = stmt->getParsedSequence();
auto *whereExpr = stmt->getWhere();

hadErrors |= preCheckExpression(sequenceExpr, DC,
/*replaceInvalidRefsWithErrors=*/true);
hadErrors |= preCheckExpression(sequenceExpr, DC);

if (whereExpr) {
hadErrors |= preCheckExpression(whereExpr, DC,
/*replaceInvalidRefsWithErrors=*/true);
hadErrors |= preCheckExpression(whereExpr, DC);
}

// Update sequence and where expressions to pre-checked versions.
Expand All @@ -2501,12 +2487,11 @@ bool ConstraintSystem::preCheckTarget(SyntacticElementTarget &target,

/// Pre-check the expression, validating any types that occur in the
/// expression and folding sequence expressions.
bool ConstraintSystem::preCheckExpression(Expr *&expr, DeclContext *dc,
bool replaceInvalidRefsWithErrors) {
bool ConstraintSystem::preCheckExpression(Expr *&expr, DeclContext *dc) {
auto &ctx = dc->getASTContext();
FrontendStatsTracer StatsTracer(ctx.Stats, "precheck-expr", expr);

PreCheckExpression preCheck(dc, expr, replaceInvalidRefsWithErrors);
PreCheckExpression preCheck(dc, expr);

// Perform the pre-check.
if (auto result = expr->walk(preCheck)) {
Expand Down
9 changes: 2 additions & 7 deletions lib/Sema/TypeCheckCodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,7 @@ bool TypeChecker::typeCheckForCodeCompletion(
if (needsPrecheck) {
// First, pre-check the expression, validating any types that occur in the
// expression and folding sequence expressions.
auto failedPreCheck =
ConstraintSystem::preCheckTarget(target,
/*replaceInvalidRefsWithErrors=*/true);

auto failedPreCheck = ConstraintSystem::preCheckTarget(target);
if (failedPreCheck)
return false;
}
Expand Down Expand Up @@ -303,9 +300,7 @@ static std::optional<Type>
getTypeOfCompletionContextExpr(DeclContext *DC, CompletionTypeCheckKind kind,
Expr *&parsedExpr,
ConcreteDeclRef &referencedDecl) {
if (constraints::ConstraintSystem::preCheckExpression(
parsedExpr, DC,
/*replaceInvalidRefsWithErrors=*/true))
if (constraints::ConstraintSystem::preCheckExpression(parsedExpr, DC))
return std::nullopt;

switch (kind) {
Expand Down
4 changes: 1 addition & 3 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,8 @@ TypeChecker::typeCheckTarget(SyntacticElementTarget &target,

// First, pre-check the target, validating any types that occur in the
// expression and folding sequence expressions.
if (ConstraintSystem::preCheckTarget(
target, /*replaceInvalidRefsWithErrors=*/true)) {
if (ConstraintSystem::preCheckTarget(target))
return errorResult();
}

// Check whether given target has a code completion token which requires
// special handling. Returns true if handled, in which case we've already
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,7 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,


// Perform unqualified name lookup to find out what the UDRE is.
return getSubExprPattern(TypeChecker::resolveDeclRefExpr(
ude, DC, /*replaceInvalidRefsWithErrors=*/true));
return getSubExprPattern(TypeChecker::resolveDeclRefExpr(ude, DC));
}

// Call syntax forms a pattern if:
Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,9 @@ bool swift::typeCheckForCodeCompletion(
callback);
}

Expr *swift::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *Context,
bool replaceInvalidRefsWithErrors) {
return TypeChecker::resolveDeclRefExpr(UDRE, Context, replaceInvalidRefsWithErrors);
Expr *swift::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
DeclContext *Context) {
return TypeChecker::resolveDeclRefExpr(UDRE, Context);
}

void TypeChecker::checkForForbiddenPrefix(ASTContext &C, DeclBaseName Name) {
Expand Down
6 changes: 1 addition & 5 deletions lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,7 @@ Type getOptionalType(SourceLoc loc, Type elementType);
/// Bind an UnresolvedDeclRefExpr by performing name lookup and
/// returning the resultant expression. Context is the DeclContext used
/// for the lookup.
///
/// \param replaceInvalidRefsWithErrors Indicates whether it's allowed
/// to replace any discovered invalid member references with `ErrorExpr`.
Expr *resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *Context,
bool replaceInvalidRefsWithErrors);
Expr *resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *Context);

/// Check for invalid existential types in the given declaration.
void checkExistentialTypes(Decl *decl);
Expand Down
2 changes: 1 addition & 1 deletion unittests/Sema/KeypathFunctionConversionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ TEST_F(SemaTest, TestKeypathFunctionConversionPrefersNarrowConversion) {

Expr *target = callExpr;
ConstraintSystem cs(DC, ConstraintSystemOptions());
ASSERT_FALSE(cs.preCheckExpression(target, DC, false));
ASSERT_FALSE(cs.preCheckExpression(target, DC));
auto *expr = cs.generateConstraints(callExpr, DC);
ASSERT_TRUE(expr);

Expand Down