Skip to content

[IDE] Disable constraint system diagnostics during code completion #24842

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

Closed
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
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ namespace swift {
}

/// Return all \c DiagnosticConsumers.
ArrayRef<DiagnosticConsumer *> getConsumers() {
ArrayRef<DiagnosticConsumer *> getConsumers() const {
return Consumers;
}

Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/DiagnosticSuppression.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class DiagnosticSuppression {
public:
explicit DiagnosticSuppression(DiagnosticEngine &diags);
~DiagnosticSuppression();
static bool isEnabled(const DiagnosticEngine &diags);
};

}
Expand Down
7 changes: 0 additions & 7 deletions include/swift/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@ class SourceManager {
rangeContainsTokenLoc(Enclosing, Inner.End);
}

/// Returns true if range \p R contains the code-completion location, if any.
bool rangeContainsCodeCompletionLoc(SourceRange R) const {
return CodeCompletionBufferID
? rangeContainsTokenLoc(R, getCodeCompletionLoc())
: false;
}

/// Returns the buffer ID for the specified *valid* location.
///
/// Because a valid source location always corresponds to a source buffer,
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,10 @@ DiagnosticSuppression::~DiagnosticSuppression() {
diags.addConsumer(*consumer);
}

bool DiagnosticSuppression::isEnabled(const DiagnosticEngine &diags) {
return diags.getConsumers().empty();
}

BufferIndirectlyCausingDiagnosticRAII::BufferIndirectlyCausingDiagnosticRAII(
const SourceFile &SF)
: Diags(SF.getASTContext().Diags) {
Expand Down
12 changes: 0 additions & 12 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,6 @@ bool TypeChecker::typeCheckFunctionBuilderFuncBody(FuncDecl *FD,
options |= TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType;
}

// If we are performing code-completion inside the functions body, supress
// diagnostics to workaround typechecking performance problems.
if (Context.SourceMgr.rangeContainsCodeCompletionLoc(
FD->getBody()->getSourceRange()))
options |= TypeCheckExprFlags::SuppressDiagnostics;

// Type-check the single result expression.
Type returnExprType = typeCheckExpression(returnExpr, FD,
TypeLoc::withoutLoc(returnType),
Expand Down Expand Up @@ -580,12 +574,6 @@ ConstraintSystem::TypeMatchResult ConstraintSystem::applyFunctionBuilder(
assert(!builderType->hasTypeParameter());
}

// If we are performing code-completion inside the closure body, supress
// diagnostics to workaround typechecking performance problems.
if (getASTContext().SourceMgr.rangeContainsCodeCompletionLoc(
closure->getSourceRange()))
Options |= ConstraintSystemFlags::SuppressDiagnostics;

BuilderClosureVisitor visitor(getASTContext(), this,
/*wantExpr=*/true, builderType);
Expr *singleExpr = visitor.visit(closure->getBody());
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/DiagnosticsParse.h"
#include "swift/AST/DiagnosticSuppression.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/NameLookup.h"
Expand Down Expand Up @@ -2133,7 +2134,7 @@ class FallbackDiagnosticListener : public ExprTypeCheckListener {

void maybeProduceFallbackDiagnostic(Expr *expr) const {
if (Options.contains(TypeCheckExprFlags::SubExpressionDiagnostics) ||
Options.contains(TypeCheckExprFlags::SuppressDiagnostics))
DiagnosticSuppression::isEnabled(TC.Diags))
return;

// Before producing fatal error here, let's check if there are any "error"
Expand Down Expand Up @@ -2175,7 +2176,7 @@ Type TypeChecker::typeCheckExpressionImpl(Expr *&expr, DeclContext *dc,
// Construct a constraint system from this expression.
ConstraintSystemOptions csOptions = ConstraintSystemFlags::AllowFixes;

if (options.contains(TypeCheckExprFlags::SuppressDiagnostics))
if (DiagnosticSuppression::isEnabled(Diags))
csOptions |= ConstraintSystemFlags::SuppressDiagnostics;

if (options.contains(TypeCheckExprFlags::AllowUnresolvedTypeVariables))
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "swift/AST/ASTWalker.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/DiagnosticSuppression.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/NameLookup.h"
Expand Down Expand Up @@ -2031,10 +2032,10 @@ Expr* TypeChecker::constructCallToSuperInit(ConstructorDecl *ctor,
if (ctor->hasThrows())
r = new (Context) TryExpr(SourceLoc(), r, Type(), /*implicit=*/true);

DiagnosticSuppression suppression(Diags);
auto resultTy =
typeCheckExpression(r, ctor, TypeLoc(), CTP_Unused,
TypeCheckExprFlags::IsDiscarded |
TypeCheckExprFlags::SuppressDiagnostics);
TypeCheckExprFlags::IsDiscarded);
if (!resultTy)
return nullptr;

Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,7 @@ bool swift::typeCheckExpression(DeclContext *DC, Expr *&parsedExpr) {
TypeChecker &TC = createTypeChecker(ctx);

auto resultTy = TC.typeCheckExpression(parsedExpr, DC, TypeLoc(),
ContextualTypePurpose::CTP_Unused,
TypeCheckExprFlags::SuppressDiagnostics);
ContextualTypePurpose::CTP_Unused);
return !resultTy;
}

Expand Down
3 changes: 0 additions & 3 deletions lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,6 @@ enum class TypeCheckExprFlags {
/// that we force for style or other reasons.
DisableStructuralChecks = 0x02,

/// Set if the client wants diagnostics suppressed.
SuppressDiagnostics = 0x04,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay!


/// If set, the client wants a best-effort solution to the constraint system,
/// but can tolerate a solution where all of the constraints are solved, but
/// not all type variables have been determined. In this case, the constraint
Expand Down