-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[C11] Generic selection expressions and qualified rvalues #96913
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
base: main
Are you sure you want to change the base?
[C11] Generic selection expressions and qualified rvalues #96913
Conversation
It is possible to get a qualified rvalue in C. Consider: struct { cont int i; } foo(); _Generic(foo().i, ...); foo() returns an rvalue for the anonymous structure, the member access expression then results in an rvalue of type const int. The lvalue to rvalue conversion we perform on the controlling expression of the generic selection expression then fails to strip any qualifiers because the expression is already an rvalue. Discussion on the WG14 reflectors suggested that the qualifiers should still be stripped from the type of the controlling expression; the standard should be corrected to make this more clear. Fixes llvm#96713
@llvm/pr-subscribers-clang Author: Aaron Ballman (AaronBallman) ChangesIt is possible to get a qualified rvalue in C. Consider: struct { cont int i; } foo(); foo() returns an rvalue for the anonymous structure, the member access Discussion on the WG14 reflectors suggested that the qualifiers should Fixes #96713 Full diff: https://github.com/llvm/llvm-project/pull/96913.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index da967fcdda808..10a718fe67e54 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -740,6 +740,10 @@ Bug Fixes in This Version
negatives where the analysis failed to detect unchecked access to guarded
data.
+- When passing a qualified rvalue as the controlling expression of a
+ ``_Generic`` selection expression, Clang now properly strips the qualifiers.
+ Fixes #GH96713
+
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index db44cfe1288b6..845fd44f3b39a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1828,18 +1828,26 @@ ExprResult Sema::CreateGenericSelectionExpr(
// Look at the canonical type of the controlling expression in case it was a
// deduced type like __auto_type. However, when issuing diagnostics, use the
// type the user wrote in source rather than the canonical one.
+ //
+ // Note: there is an edge case for this in C where you can get a
+ // qualified rvalue and the qualifiers are not stripped by the lvalue
+ // conversion applied above. e.g.,
+ //
+ // struct { const int i; } foo();
+ // _Generic(foo().i, ...);
+ //
+ // Therefore, in C, we will still ask for the non-atomic, unqualified
+ // type despite those qualifiers generally being stripped above.
+ QualType ControllingTypeToUse =
+ ControllingExpr ? ControllingExpr->getType() : ControllingType->getType();
+ ControllingTypeToUse = ControllingTypeToUse.getCanonicalType();
+ if (!getLangOpts().CPlusPlus && ControllingExpr)
+ ControllingTypeToUse = ControllingTypeToUse.getAtomicUnqualifiedType();
for (unsigned i = 0; i < NumAssocs; ++i) {
if (!Types[i])
DefaultIndex = i;
- else if (ControllingExpr &&
- Context.typesAreCompatible(
- ControllingExpr->getType().getCanonicalType(),
- Types[i]->getType()))
- CompatIndices.push_back(i);
- else if (ControllingType &&
- Context.typesAreCompatible(
- ControllingType->getType().getCanonicalType(),
- Types[i]->getType()))
+ else if (Context.typesAreCompatible(ControllingTypeToUse,
+ Types[i]->getType()))
CompatIndices.push_back(i);
}
diff --git a/clang/test/Sema/generic-selection.c b/clang/test/Sema/generic-selection.c
index 1f17896ca4cda..ff2fe6ec44eff 100644
--- a/clang/test/Sema/generic-selection.c
+++ b/clang/test/Sema/generic-selection.c
@@ -86,3 +86,13 @@ void GH55562(void) {
struct S s = { 0 };
int i = s.a;
}
+
+struct { const int i; } GH96713_1(void);
+void GH96713_2(void) {
+ _Static_assert( // ext-warning {{'_Static_assert' is a C11 extension}}
+ _Generic(GH96713_1().i, // ext-warning {{'_Generic' is a C11 extension}}
+ int : 1,
+ const int : 0 // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const int' will never be selected because it is qualified}}
+ ), ""
+ );
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation LGTM.
Does WG14 think we're right to produce a qualified rvalue in this case? Another option might be to strip qualifiers in rvalue member access, but that would have further-reaching impact. |
The sentiment on the reflector (granted, it's reflector sentiment and not asked of the whole committee in an official way) was that you do get a qualified rvalue out a member access expression and that it has utility for cases like use with |
Now Joseph Myers has brought up the opposite:
So it's now less clear which direction to resolve this. I am thinking of putting up a question on Discourse to see whether the community would support changing |
It is possible to get a qualified rvalue in C. Consider:
struct { cont int i; } foo();
_Generic(foo().i, ...);
foo() returns an rvalue for the anonymous structure, the member access
expression then results in an rvalue of type const int. The lvalue to
rvalue conversion we perform on the controlling expression of the
generic selection expression then fails to strip any qualifiers because
the expression is already an rvalue.
Discussion on the WG14 reflectors suggested that the qualifiers should
still be stripped from the type of the controlling expression; the
standard should be corrected to make this more clear.
Fixes #96713