Skip to content

Stop double-diagnosing explicit convert operator in switch condition #89142

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 2 commits into from
Apr 18, 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
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ Bug Fixes in This Version

- Fixed an assertion failure on invalid InitListExpr in C89 mode (#GH88008).

- Clang will no longer diagnose an erroneous non-dependent ``switch`` condition
during instantiation, and instead will only diagnose it once, during checking
of the function template.

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17522,6 +17522,12 @@ Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result,
if (Converted.isInvalid())
return Converted;
E = Converted.get();
// The 'explicit' case causes us to get a RecoveryExpr. Give up here so we
// don't try to evaluate it later. We also don't want to return the
// RecoveryExpr here, as it results in this call succeeding, thus callers of
// this function will attempt to use 'Value'.
if (isa<RecoveryExpr>(E))
return ExprError();
if (!E->getType()->isIntegralOrUnscopedEnumerationType())
return ExprError();
} else if (!E->getType()->isIntegralOrUnscopedEnumerationType()) {
Expand Down
13 changes: 8 additions & 5 deletions clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6563,11 +6563,14 @@ diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From,
HadMultipleCandidates);
if (Result.isInvalid())
return true;
// Record usage of conversion in an implicit cast.
From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(),
CK_UserDefinedConversion, Result.get(),
nullptr, Result.get()->getValueKind(),
SemaRef.CurFPFeatureOverrides());

// Replace the conversion with a RecoveryExpr, so we don't try to
// instantiate it later, but can further diagnose here.
Result = SemaRef.CreateRecoveryExpr(From->getBeginLoc(), From->getEndLoc(),
From, Result.get()->getType());
if (Result.isInvalid())
return true;
From = Result.get();
}
return false;
}
Expand Down
15 changes: 15 additions & 0 deletions clang/test/SemaCXX/explicit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,18 @@ namespace PR18777 {
struct S { explicit operator bool() const; } s;
int *p = new int(s); // expected-error {{no viable conversion}}
}

namespace DoubleDiags {
struct ExplicitConvert{
explicit operator int();//#DOUBLE_DIAG_OP_INT
} EC;
template<typename T>
void Template(){
// expected-error@+2{{switch condition type 'struct ExplicitConvert' requires explicit conversion to 'int'}}
// expected-note@#DOUBLE_DIAG_OP_INT{{conversion to integral type 'int'}}
switch(EC){}
};
void Inst() {
Template<int>();
}
}