Skip to content

[clang][Sema] Fix assertion in tryDiagnoseOverloadedCast #108021

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
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: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ Bug Fixes to C++ Support
- Fixed a crash when clang tries to subtitute parameter pack while retaining the parameter
pack. #GH63819, #GH107560
- Fix a crash when a static assert declaration has an invalid close location. (#GH108687)
- Fixed an assertion failure in debug mode, and potential crashes in release mode, when
diagnosing a failed cast caused indirectly by a failed implicit conversion to the type of the constructor parameter.

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/Sema/SemaCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,12 @@ static bool tryDiagnoseOverloadedCast(Sema &S, CastType CT,
: InitializationKind::CreateCast(/*type range?*/ range);
InitializationSequence sequence(S, entity, initKind, src);

assert(sequence.Failed() && "initialization succeeded on second try?");
// It could happen that a constructor failed to be used because
// it requires a temporary of a broken type. Still, it will be found when
// looking for a match.
if (!sequence.Failed())
return false;

switch (sequence.getFailureKind()) {
default: return false;

Expand Down
26 changes: 26 additions & 0 deletions clang/test/Parser/cxx-bad-cast-diagnose-broken-template.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s

template<typename>
struct StringTrait {};

template< int N >
struct StringTrait< const char[ N ] > {
typedef char CharType;
static const MissingIntT length = N - 1; // expected-error {{unknown type name 'MissingIntT'}}
};

class String {
public:
template <typename T>
String(T& str, typename StringTrait<T>::CharType = 0);
};


class Exception {
public:
Exception(String const&);
};

void foo() {
throw Exception("some error"); // expected-error {{functional-style cast from 'const char[11]' to 'Exception' is not allowed}}
}
Loading