Skip to content

Conversation

mizvekov
Copy link
Contributor

This simplifies things a little bit. This is mostly NFCish, except the reference type deduction workaround now applies in partial ordering as well, but we don't have any test cases and any reason to suspect this is significant.

@mizvekov mizvekov self-assigned this Sep 24, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2025

@llvm/pr-subscribers-clang

Author: Matheus Izvekov (mizvekov)

Changes

This simplifies things a little bit. This is mostly NFCish, except the reference type deduction workaround now applies in partial ordering as well, but we don't have any test cases and any reason to suspect this is significant.


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

1 Files Affected:

  • (modified) clang/lib/Sema/SemaTemplateDeduction.cpp (+5-17)
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 62e867c44ad14..b0d2554d819cb 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -483,7 +483,7 @@ DeduceNonTypeTemplateArgument(Sema &S, TemplateParameterList *TemplateParams,
     return TemplateDeductionResult::Inconsistent;
   }
   Deduced[NTTP.getIndex()] = Result;
-  if (!S.getLangOpts().CPlusPlus17)
+  if (!S.getLangOpts().CPlusPlus17 && !PartialOrdering)
     return TemplateDeductionResult::Success;
 
   if (NTTP.isExpandedParameterPack())
@@ -2653,23 +2653,11 @@ DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
       switch (A.getKind()) {
       case TemplateArgument::Expression: {
         const Expr *E = A.getAsExpr();
-        // When checking NTTP, if either the parameter or the argument is
-        // dependent, as there would be otherwise nothing to deduce, we force
-        // the argument to the parameter type using this dependent implicit
-        // cast, in order to maintain invariants. Now we can deduce the
-        // resulting type from the original type, and deduce the original type
-        // against the parameter we are checking.
-        if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
-            ICE && ICE->getCastKind() == clang::CK_Dependent) {
+        // The type of the value is the type of the expression before any
+        // implicit conversions.
+        if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) {
           E = ICE->getSubExpr();
-          if (auto Result = DeduceTemplateArgumentsByTypeMatch(
-                  S, TemplateParams, ICE->getType(), E->getType(), Info,
-                  Deduced, TDF_SkipNonDependent,
-                  PartialOrdering ? PartialOrderingKind::NonCall
-                                  : PartialOrderingKind::None,
-                  /*DeducedFromArrayBound=*/false, HasDeducedAnyParam);
-              Result != TemplateDeductionResult::Success)
-            return Result;
+          assert(!isa<ImplicitCastExpr>(E));
         }
         return DeduceNonTypeTemplateArgument(
             S, TemplateParams, NTTP, DeducedTemplateArgument(A), E->getType(),

…al ordering

This simplifies things a little bit. This is mostly NFCish, except
the reference type deduction workaround now applies in partial ordering
as well, but we don't have any test cases and any reason to suspect
this is significant.
@mizvekov mizvekov force-pushed the users/mizvekov/simplify-nttp-type-deduction branch from 09539af to d45e9e3 Compare September 25, 2025 01:12
@mizvekov mizvekov requested a review from cor3ntin September 25, 2025 16:35
@mizvekov
Copy link
Contributor Author

@erichkeane sorry to ping so soon, but this patch is quite small and its starting to form a deep stack of patches.

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.

I'm not positive exactly what is the effect of this, but I'm willing to ship it for now and see.

@mizvekov mizvekov merged commit 7b28fcd into main Sep 25, 2025
11 of 12 checks passed
@mizvekov mizvekov deleted the users/mizvekov/simplify-nttp-type-deduction branch September 25, 2025 19:49
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
…al ordering (llvm#160433)

This simplifies things a little bit. This is mostly NFCish, except the
reference type deduction workaround now applies in partial ordering as
well, but we don't have any test cases and any reason to suspect this is
significant.
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.

4 participants