Skip to content

Conversation

@AmrDeveloper
Copy link
Member

Fix assume_aligned incorrectly diagnoses a dependent return type

Fixes: #111563

@AmrDeveloper AmrDeveloper added the clang Clang issues not falling into any other category label Oct 8, 2024
@llvmbot llvmbot added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Oct 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 8, 2024

@llvm/pr-subscribers-clang

Author: Amr Hesham (AmrDeveloper)

Changes

Fix assume_aligned incorrectly diagnoses a dependent return type

Fixes: #111563


Full diff: https://github.com/llvm/llvm-project/pull/111573.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDeclAttr.cpp (+2)
  • (modified) clang/test/SemaCXX/builtin-assume-aligned-tmpl.cpp (+3)
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index af983349a89b58..c1d1ef31db6e92 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -1217,6 +1217,8 @@ static void handlePreferredName(Sema &S, Decl *D, const ParsedAttr &AL) {
 
 bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
   if (RefOkay) {
+    if (T->isDependentType())
+      return true;
     if (T->isReferenceType())
       return true;
   } else {
diff --git a/clang/test/SemaCXX/builtin-assume-aligned-tmpl.cpp b/clang/test/SemaCXX/builtin-assume-aligned-tmpl.cpp
index 61b85557d6b294..4459580cdccc5d 100644
--- a/clang/test/SemaCXX/builtin-assume-aligned-tmpl.cpp
+++ b/clang/test/SemaCXX/builtin-assume-aligned-tmpl.cpp
@@ -52,6 +52,9 @@ T *atest3() __attribute__((assume_aligned(31, o))); // expected-error {{requeste
 template <typename T, int o>
 T *atest4() __attribute__((assume_aligned(32, o)));
 
+template<typename T>
+T atest5(int) __attribute__((assume_aligned(2)));
+
 void test22() {
   atest3<int, 5>();
   atest4<int, 5>();

T *atest4() __attribute__((assume_aligned(32, o)));

template<typename T>
T atest5(int) __attribute__((assume_aligned(2)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change causes a number of attributes to change their behavior (see all uses of isValidPointerAttrType), so we should test each of those.

Additionally, we should separately check (via a different declaration) that the diagnostic fires when instantiated.


bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
if (RefOkay) {
if (T->isDependentType())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem like the right place for this, we should be skipping ANY time the type is dependent, not only when a ref argument is acceptable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think you, updated

@AmrDeveloper AmrDeveloper force-pushed the clang_assume_aligned_dependent_ret_type branch from fd3247e to 076c867 Compare October 8, 2024 18:34
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably valuable to audit the other uses of this function well, and remove checks for dependence there as redundant. And probably ensure the documentation of this function mentions that it skips on dependence.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, please check the updated documentation of the function

@AmrDeveloper AmrDeveloper force-pushed the clang_assume_aligned_dependent_ret_type branch from 076c867 to 4202153 Compare October 8, 2024 18:52
@AmrDeveloper AmrDeveloper requested a review from Endilll as a code owner October 8, 2024 18:52
/// attributes. By default, we look through references (the behavior used by
/// nonnull), but if the second parameter is true, then we treat a reference
/// type as valid.
/// attributes. By default, we skip dependence and look through references
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by default implies that the dependence check can bie disabled, but it very much cannot.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, all comments addressed

@AmrDeveloper
Copy link
Member Author

This part in ObjectiveC tests conflict with the current solution, any suggestion how we can go around it right now until to be handled later

// FIXME: clang shouldn't reject this.
noescapeFunc5(^{
(void)b9;
});

@erichkeane @AaronBallman

/// attributes. By default, we look through references (the behavior used by
/// nonnull), but if the second parameter is true, then we treat a reference
/// type as valid.
/// attributes. We skip dependence By default, we look through references
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, we're not skipping dependence by default, we're ALWAYS skipping dependence. We skip references (presumably by default). I think the dependence behavior just needs its own sentence here.

Copy link
Member Author

@AmrDeveloper AmrDeveloper Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i think i was planning to make it like We skip dependence and By default ..., or We skip dependence then By default ...

@erichkeane
Copy link
Collaborator

This part in ObjectiveC tests conflict with the current solution, any suggestion how we can go around it right now until to be handled later

// FIXME: clang shouldn't reject this.
noescapeFunc5(^{
(void)b9;
});

@erichkeane @AaronBallman

Doesn't the fixme above that say it was diagnosing unintentionally? If so, that fixme likely needs to be removed, and this is 'correct' now.

@AmrDeveloper
Copy link
Member Author

This part in ObjectiveC tests conflict with the current solution, any suggestion how we can go around it right now until to be handled later

// FIXME: clang shouldn't reject this.
noescapeFunc5(^{
(void)b9;
});

@erichkeane @AaronBallman

Doesn't the fixme above that say it was diagnosing unintentionally? If so, that fixme likely needs to be removed, and this is 'correct' now.

Yes but still got call to deleted constructor of 'S6' and 'S6' has been explicitly marked deleted here errors on CI and local

@AmrDeveloper AmrDeveloper force-pushed the clang_assume_aligned_dependent_ret_type branch 2 times, most recently from 3e83fd9 to d3f8538 Compare October 9, 2024 16:47
@AmrDeveloper
Copy link
Member Author

This part in ObjectiveC tests conflict with the current solution, any suggestion how we can go around it right now until to be handled later

// FIXME: clang shouldn't reject this.
noescapeFunc5(^{
(void)b9;
});

@erichkeane @AaronBallman

Doesn't the fixme above that say it was diagnosing unintentionally? If so, that fixme likely needs to be removed, and this is 'correct' now.

I handled it, i didn't know that // expected-note n is the number of expected notes,

Thank you

/// attributes. By default, we look through references (the behavior used by
/// nonnull), but if the second parameter is true, then we treat a reference
/// type as valid.
/// attributes. We skip dependence then By default, we look through references
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// attributes. We skip dependence then By default, we look through references
/// attributes. Dependent types are considered valid, so they can be checked during instantiation time. By default, we look through references

^^ make sure to clang-format.

@AmrDeveloper AmrDeveloper force-pushed the clang_assume_aligned_dependent_ret_type branch from d3f8538 to a0066f7 Compare October 9, 2024 20:36
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// treat a reference type as valid..
/// treat a reference type as valid.

Missed this!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// attributes. Dependent types are considered valid, so they can be checked
/// attributes. Dependent types are considered valid so they can be checked

@AmrDeveloper AmrDeveloper force-pushed the clang_assume_aligned_dependent_ret_type branch from a0066f7 to 0f1405e Compare October 9, 2024 20:41
@AmrDeveloper AmrDeveloper merged commit 562999a into llvm:main Oct 10, 2024
8 checks passed
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
…lvm#111573)

Fix `assume_aligned` incorrectly diagnoses a dependent return type

Fixes: llvm#111563
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assume_aligned incorrectly diagnoses a dependent return type

3 participants