Skip to content

[Clang][Sema] Fix a bug on type constraint checking #84671

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
Mar 14, 2024
Merged

Conversation

jcsxky
Copy link
Contributor

@jcsxky jcsxky commented Mar 10, 2024

Try to fix #84368
When visiting class members in TemplateDeclInstantiator::VisitClassTemplateDecl during Sema::InstantiateClass, we miss to set attribute of friend declaration if it is(isFriend is true). This will lead to Sema::AreConstraintExpressionsEqual return false when invoked in MatchTemplateParameterKind. Because it makes Sema::getTemplateInstantiationArgs returns incorrect template argument(MultiLevelTemplateArgumentList). When we handle CXXRecordDecl In Sema::getTemplateInstantiationArgs, friend declaration(its parent context is FileContext) makes us to choose LexicalDeclContext not DeclContext and this is what we want.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 10, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 10, 2024

@llvm/pr-subscribers-clang

Author: Qizhi Hu (jcsxky)

Changes

Try to fix #84368
[temp.friend]p1:
Similarly, each specialization of the task class template has the class template
specialization task<int> as a friend, and has all specializations of the class template frd as friends.


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/SemaTemplateInstantiateDecl.cpp (+1)
  • (added) clang/test/Sema/PR84368.cpp (+16)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3b89d5a8720785..b32e739288dbc5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -259,6 +259,8 @@ Bug Fixes in This Version
   operator.
   Fixes (#GH83267).
 
+- Fix a bug on type constraint checking (#GH84368). 
+
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 20c2c93ac9c7b4..765c5bc689ae1e 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -1698,6 +1698,7 @@ Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
     assert(!Owner->isDependentContext());
     Inst->setLexicalDeclContext(Owner);
     RecordInst->setLexicalDeclContext(Owner);
+    Inst->setObjectOfFriendDecl();
 
     if (PrevClassTemplate) {
       Inst->setCommonPtr(PrevClassTemplate->getCommonPtr());
diff --git a/clang/test/Sema/PR84368.cpp b/clang/test/Sema/PR84368.cpp
new file mode 100644
index 00000000000000..073530ffd8abea
--- /dev/null
+++ b/clang/test/Sema/PR84368.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -std=c++20 -verify %s
+// RUN: %clang_cc1 -std=c++23 -verify %s
+// expected-no-diagnostics
+
+template<class T> concept IsOk = requires() { typename T::Float; };
+
+template<IsOk T> struct Thing;
+
+template<IsOk T> struct Foobar {
+	template<int> struct Inner {
+		template<IsOk T2> friend struct Thing;
+	};
+};
+
+struct MyType { using Float=float; };
+Foobar<MyType>::Inner<0> foobar;

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

The commit message and release note needs to be MUCH more detailed. Else I think this looks like an acceptable patch.

@jcsxky
Copy link
Contributor Author

jcsxky commented Mar 12, 2024

The commit message and release note needs to be MUCH more detailed. Else I think this looks like an acceptable patch.

Thanks for you remind! I have updated commit message and release note to make it more clear.


template<IsOk T> struct Thing;

template<IsOk T> struct Foobar {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Indenting here doesn't match our clang-format rules. Please fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@jcsxky jcsxky force-pushed the fix-84368 branch 10 times, most recently from 1b71ce0 to fd1711b Compare March 13, 2024 01:53
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.

"type constraint differs in template redeclaration" for template Inner Class
3 participants