Skip to content

[clang] Improve bit-field in ref NTTP diagnostic #71077

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 1 commit into from
Jan 9, 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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ Improvements to Clang's diagnostics
- Clang now diagnoses narrowing conversions involving const references.
(`#63151: <https://github.com/llvm/llvm-project/issues/63151>`_).
- Clang now diagnoses unexpanded packs within the template argument lists of function template specializations.
- Clang now diagnoses attempts to bind a bitfield to an NTTP of a reference type as erroneous
converted constant expression and not as a reference to subobject.


Improvements to Clang's time-trace
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,8 @@ def warn_cxx17_compat_aggregate_init_paren_list : Warning<
def err_reference_bind_to_bitfield : Error<
"%select{non-const|volatile}0 reference cannot bind to "
"bit-field%select{| %1}2">;
def err_reference_bind_to_bitfield_in_cce : Error<
"reference cannot bind to bit-field in converted constant expression">;
def err_reference_bind_to_vector_element : Error<
"%select{non-const|volatile}0 reference cannot bind to vector element">;
def err_reference_bind_to_matrix_element : Error<
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6056,6 +6056,16 @@ static ExprResult BuildConvertedConstantExpression(Sema &S, Expr *From,
diag::err_typecheck_converted_constant_expression_indirect)
<< From->getType() << From->getSourceRange() << T;
}
// 'TryCopyInitialization' returns incorrect info for attempts to bind
// a reference to a bit-field due to C++ [over.ics.ref]p4. Namely,
// 'SCS->DirectBinding' occurs to be set to 'true' despite it is not
// the direct binding according to C++ [dcl.init.ref]p5. Hence, check this
// case explicitly.
if (From->refersToBitField() && T.getTypePtr()->isReferenceType()) {
return S.Diag(From->getBeginLoc(),
diag::err_reference_bind_to_bitfield_in_cce)
<< From->getSourceRange();
}

// Usually we can simply apply the ImplicitConversionSequence we formed
// earlier, but that's not guaranteed to work when initializing an object of
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CXX/drs/dr12xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ namespace dr1295 { // dr1295: 4
Y<x.bitfield> y; // #dr1295-y
// cxx98-14-error@-1 {{non-type template argument does not refer to any declaration}}
// cxx98-14-note@#dr1295-Y {{template parameter is declared here}}
// since-cxx17-error@#dr1295-y {{non-type template argument refers to subobject 'x.bitfield'}}
// since-cxx17-error@#dr1295-y {{reference cannot bind to bit-field in converted constant expression}}

#if __cplusplus >= 201103L
const unsigned other = 0;
Expand Down
6 changes: 6 additions & 0 deletions clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ namespace ConvertedConstant {
template <A> struct X {};
void f(X<1.0f>) {}
void g(X<2>) {}

struct {
int i : 2;
} b;
template <const int&> struct Y {};
void f(Y<b.i>) {} // expected-error {{reference cannot bind to bit-field in converted constant expression}}
}

namespace CopyCounting {
Expand Down