Skip to content

[clang] CTAD alias: Emit a more descriptive diagnostic message when is_deducible constraint is evaluated to false. #92389

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
May 16, 2024

Conversation

hokein
Copy link
Collaborator

@hokein hokein commented May 16, 2024

Fixes #92225

@hokein hokein requested a review from cor3ntin May 16, 2024 12:09
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 16, 2024
@llvmbot
Copy link
Member

llvmbot commented May 16, 2024

@llvm/pr-subscribers-clang

Author: Haojian Wu (hokein)

Changes

Fixes #92225


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

3 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2)
  • (modified) clang/lib/Sema/SemaConcept.cpp (+7)
  • (modified) clang/test/SemaCXX/cxx20-ctad-type-alias.cpp (+3-3)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index e648b503ac034..04bf6ed2c52f5 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3003,6 +3003,8 @@ def note_single_arg_concept_specialization_constraint_evaluated_to_false : Note<
   "%select{and|because}0 %1 does not satisfy %2">;
 def note_atomic_constraint_evaluated_to_false_elaborated : Note<
   "%select{and|because}0 '%1' (%2 %3 %4) evaluated to false">;
+def note_is_deducible_constraint_evaluated_to_false : Note<
+  "cannot deduce template arguments of %0 from %1">;
 def err_constrained_virtual_method : Error<
   "virtual function cannot have a requires clause">;
 def err_trailing_requires_clause_on_deduction_guide : Error<
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index 7bfec4e11f7aa..202dd86c67f62 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -1162,6 +1162,13 @@ static void diagnoseWellFormedUnsatisfiedConstraintExpr(Sema &S,
         break;
       }
     return;
+  } else if (auto *TTE = dyn_cast<TypeTraitExpr>(SubstExpr);
+             TTE && TTE->getTrait() == clang::TypeTrait::BTT_IsDeducible) {
+    assert(TTE->getNumArgs() == 2);
+    S.Diag(SubstExpr->getSourceRange().getBegin(),
+           diag::note_is_deducible_constraint_evaluated_to_false)
+        << TTE->getArg(0)->getType() << TTE->getArg(1)->getType();
+    return;
   }
 
   S.Diag(SubstExpr->getSourceRange().getBegin(),
diff --git a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
index 7c186dc379c7b..f7367d01bc2eb 100644
--- a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
+++ b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
@@ -111,7 +111,7 @@ struct Foo {
 template <typename X, int Y>
 using Bar = Foo<X, sizeof(X)>; // expected-note {{candidate template ignored: couldn't infer template argument 'X'}} \
                                // expected-note {{candidate template ignored: constraints not satisfied [with X = int]}} \
-                               // expected-note {{because '__is_deducible}}
+                               // expected-note {{can not deduce template arguments for 'Bar' from the type 'Foo<int, 4UL>'}}
 
 
 Bar s = {{1}}; // expected-error {{no viable constructor or deduction guide }}
@@ -138,7 +138,7 @@ template<class T> struct Foo { T c; };
 template<class X, class Y=A>
 using AFoo = Foo<Y>; // expected-note {{candidate template ignored: could not match 'Foo<type-parameter-0-0>' against 'int'}} \
                     // expected-note {{candidate template ignored: constraints not satisfied [with Y = int]}} \
-                    // expected-note {{because '__is_deducible(AFoo, Foo<int>)' evaluated to false}} \
+                    // expected-note {{cannot deduce template arguments for 'AFoo' from 'Foo<int>'}} \
                     // expected-note {{candidate function template not viable: requires 0 arguments, but 1 was provided}}
 
 AFoo s = {1}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'AFoo'}}
@@ -199,7 +199,7 @@ template<typename> concept False = false;
 // FIXME: emit a more descriptive diagnostic for "__is_deducible" constraint failure.
 template<False W>
 using BFoo = AFoo<W>; // expected-note {{candidate template ignored: constraints not satisfied [with V = int]}} \
-                      // expected-note {{because '__is_deducible(BFoo, Foo<int *>)' evaluated to false}} \
+                      // expected-note {{cannot deduce template arguments for 'BFoo' from 'Foo<int *>'}} \
                       // expected-note {{candidate template ignored: could not match 'Foo<type-parameter-0-0 *>' against 'int *'}}
 int i = 0;
 AFoo a1(&i); // OK, deduce Foo<int *>

…s_deducible

constraint is evaluated to false.

Fixes llvm#92225
@hokein hokein force-pushed the deducible-diagnostic branch from 2029448 to ada7f47 Compare May 16, 2024 12:52
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

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

LGTM

@hokein hokein merged commit a383b3c into llvm:main May 16, 2024
3 of 4 checks passed
@hokein hokein deleted the deducible-diagnostic branch May 16, 2024 18:58
@zeroomega
Copy link
Contributor

Hi @hokein , your change probably broke the test "Clang :: SemaCXX/cxx20-ctad-type-alias.cpp" on Windows. Could you take a look please?

We are seeing test failures on this with error message:

Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
c:\b\s\w\ir\x\w\llvm_build\bin\clang.exe -cc1 -internal-isystem C:\b\s\w\ir\x\w\llvm_build\lib\clang\19\include -nostdsysteminc -fsyntax-only -Wno-c++11-narrowing -Wno-literal-conversion -std=c++20 -verify C:\b\s\w\ir\x\w\llvm-llvm-project\clang\test\SemaCXX\cxx20-ctad-type-alias.cpp
# executed command: 'c:\b\s\w\ir\x\w\llvm_build\bin\clang.exe' -cc1 -internal-isystem 'C:\b\s\w\ir\x\w\llvm_build\lib\clang\19\include' -nostdsysteminc -fsyntax-only -Wno-c++11-narrowing -Wno-literal-conversion -std=c++20 -verify 'C:\b\s\w\ir\x\w\llvm-llvm-project\clang\test\SemaCXX\cxx20-ctad-type-alias.cpp'
# .---command stderr------------
# | error: 'expected-note' diagnostics expected but not seen: 
# |   File C:\b\s\w\ir\x\w\llvm-llvm-project\clang\test\SemaCXX\cxx20-ctad-type-alias.cpp Line 112: cannot deduce template arguments for 'Bar' from 'Foo<int, 4UL>'
# | error: 'expected-note' diagnostics seen but not expected: 
# |   File C:\b\s\w\ir\x\w\llvm-llvm-project\clang\test\SemaCXX\cxx20-ctad-type-alias.cpp Line 112: cannot deduce template arguments for 'Bar' from 'Foo<int, 4ULL>'
# | 2 errors generated.
# `-----------------------------
# error: command failed with exit status: 1

--

This happens on our Windows builder. Link to the task: https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-windows-x64/b8747755569988931969/overview

@mizvekov
Copy link
Contributor

I already fixed this. We just had to constrain the triple, as there are differences in size_t underlying type which affect diagnostics.

@hokein
Copy link
Collaborator Author

hokein commented May 17, 2024

Thanks @mizvekov for the quick fix.

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.

[clang] Improve is_deducible diagnostic for CTAD alias.
5 participants