Skip to content

Commit 136f2ba

Browse files
authored
[Clang][AST] Fix HandleLValueBase to deal with references (#140105)
Since P2280R4 Unknown references and pointers was implemented, HandleLValueBase now has to deal with referneces: D.MostDerivedType->getAsCXXRecordDecl() will return a nullptr if D.MostDerivedType is a ReferenceType. The fix is to use getNonReferenceType() to obtain the Pointee Type if we have a reference. Fixes: #139452
1 parent f113cab commit 136f2ba

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,8 @@ Bug Fixes in This Version
615615
argument which contains a pragma. (#GH113722)
616616
- Fixed assertion failures when generating name lookup table in modules. (#GH61065, #GH134739)
617617
- Fixed an assertion failure in constant compound literal statements. (#GH139160)
618+
- Fix crash due to unknown references and pointer implementation and handling of
619+
base classes. (GH139452)
618620

619621
Bug Fixes to Compiler Builtins
620622
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/AST/ExprConstant.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3314,7 +3314,11 @@ static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
33143314
return false;
33153315

33163316
// Extract most-derived object and corresponding type.
3317-
DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl();
3317+
// FIXME: After implementing P2280R4 it became possible to get references
3318+
// here. We do MostDerivedType->getAsCXXRecordDecl() in several other
3319+
// locations and if we see crashes in those locations in the future
3320+
// it may make more sense to move this fix into Lvalue::set.
3321+
DerivedDecl = D.MostDerivedType.getNonReferenceType()->getAsCXXRecordDecl();
33183322
if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength))
33193323
return false;
33203324

clang/test/SemaCXX/constant-expression-p2280r4.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,24 @@ namespace extern_reference_used_as_unknown {
178178
int y;
179179
constinit int& g = (x,y); // expected-warning {{left operand of comma operator has no effect}}
180180
}
181+
182+
namespace GH139452 {
183+
struct Dummy {
184+
explicit operator bool() const noexcept { return true; }
185+
};
186+
187+
struct Base { int error; };
188+
struct Derived : virtual Base { };
189+
190+
template <class R>
191+
constexpr R get_value() {
192+
const auto& derived_val = Derived{};
193+
if (derived_val.error != 0)
194+
/* nothing */;
195+
return R{};
196+
}
197+
198+
int f() {
199+
return !get_value<Dummy>(); // contextually convert the function call result to bool
200+
}
201+
}

0 commit comments

Comments
 (0)