Skip to content

Commit c3267c5

Browse files
committed
[Clang] Fix a crash in the diagnostic emission of invalid immediate calls.
`CXXCtorInitializer` may not refer to a FieldDecl because it might also denote another constructor call. Fixes #66324
1 parent e2a9d3f commit c3267c5

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

clang/docs/ReleaseNotes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ Bug Fixes to C++ Support
297297
definition the specialization was instantiated from.
298298
(`#26057 <https://github.com/llvm/llvm-project/issues/26057>`_`)
299299

300+
- Fix a crash when a default member initializer of a base aggregate
301+
makes an invalid call to an immediate function.
302+
(`#66324 <https://github.com/llvm/llvm-project/issues/66324>`_)
303+
300304
Bug Fixes to AST Handling
301305
^^^^^^^^^^^^^^^^^^^^^^^^^
302306
- Fixed an import failure of recursive friend class template.

clang/lib/Sema/SemaDeclCXX.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -2514,12 +2514,15 @@ void Sema::DiagnoseImmediateEscalatingReason(FunctionDecl *FD) {
25142514
Range = CurrentInit->isWritten() ? CurrentInit->getSourceRange()
25152515
: SourceRange();
25162516
}
2517+
2518+
FieldDecl* InitializedField = CurrentInit ? CurrentInit->getAnyMember() : nullptr;
2519+
25172520
SemaRef.Diag(Loc, diag::note_immediate_function_reason)
25182521
<< ImmediateFn << Fn << Fn->isConsteval() << IsCall
25192522
<< isa<CXXConstructorDecl>(Fn) << ImmediateFnIsConstructor
2520-
<< (CurrentInit != nullptr)
2523+
<< (InitializedField != nullptr)
25212524
<< (CurrentInit && !CurrentInit->isWritten())
2522-
<< (CurrentInit ? CurrentInit->getAnyMember() : nullptr) << Range;
2525+
<< InitializedField << Range;
25232526
}
25242527
bool TraverseCallExpr(CallExpr *E) {
25252528
if (const auto *DR =

clang/test/SemaCXX/cxx2b-consteval-propagate.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,26 @@ struct S {
330330
S s(0); // expected-note {{in the default initializer of 'j'}}
331331

332332
}
333+
334+
namespace GH66324 {
335+
336+
consteval int allocate(); // expected-note 2{{declared here}}
337+
338+
struct _Vector_base {
339+
int b = allocate(); // expected-note 2{{undefined function 'allocate' cannot be used in a constant expression}} \
340+
// expected-error {{call to consteval function 'GH66324::allocate' is not a constant expression}} \
341+
// expected-note {{declared here}}
342+
};
343+
344+
template <typename>
345+
struct vector : _Vector_base {
346+
constexpr vector()
347+
// expected-note@-1 {{'vector' is an immediate constructor because its body contains a call to a consteval function 'allocate' and that call is not a constant expression}}
348+
: _Vector_base{} {} // expected-note {{in the default initializer of 'b'}}
349+
};
350+
351+
vector<void> v{};
352+
// expected-error@-1 {{call to immediate function 'GH66324::vector<void>::vector' is not a constant expression}}
353+
// expected-note@-2 {{in call to 'vector()'}}
354+
355+
}

0 commit comments

Comments
 (0)