Issue 238212
Summary [clang] Add diagnostic notes explaining why a constrained class template partial specialization was not selected
Labels clang
Assignees
Reporter yulin26c
    **Summary**

When a constrained class template partial specialization is not selected because its associated constraints are not satisfied, Clang currently falls back silently to the primary template (or another specialization) without emitting any note explaining the rejection. For class template partial specializations that are rejected because they are not more specialized than the primary template, Clang already produces `-Winvalid-partial-specialization`. However, when a `requires`-clause evaluates to `false`, no diagnostic is produced at all. This makes debugging constrained partial specializations unnecessarily opaque, especially when the failure manifests much later as a `static_assert` or a missing member.

**Motivation**

Consider:

```cpp
template<typename T>
struct trait { static constexpr bool value = false; };

template<std::integral T>
struct trait<T> { static constexpr bool value = true; };

static_assert(trait<double>::value); // fails
```

The user sees only:

```text
error: static assertion failed due to requirement 'trait<double>::value'
```

There is no indication that the partial specialization `trait<T>` was rejected because `std::integral<double>` is `false`. The user must manually deduce the reason. The same opacity applies to any trait or dispatch built with constrained partial specializations.

**Proposed Behavior**

When a constrained class template partial specialization is considered during template argument deduction but is rejected solely because its associated constraints are not satisfied, Clang should emit a note for each such rejected specialization, analogous to the existing notes for function templates and variable template partial specializations. For example:

```text
note: partial specialization #1 was not selected
note:   constraint 'std::integral<T>' was not satisfied
note:     because 'std::is_integral_v<double>' evaluated to false
note:   requires-clause was not satisfied: 'sizeof(T) == 4'
note:     evaluated to false for T = double
```

If the rejection is due to partial ordering rather than constraint satisfaction (i.e., the constraints are satisfied but the specialization is less constrained than another candidate), a distinct note should be emitted:

```text
note: partial specialization #2 was not selected
note:   candidate is less constrained than the selected specialization
```

Diagnostic depth should be bounded. The existing `-fconcepts-diagnostics-depth=` flag, which already controls the depth of concept-related diagnostic notes, can be reused here rather than introducing a new flag.

**Precedent and Feasibility**

Clang already computes constraint satisfaction for partial specializations as part of template argument deduction and partial ordering. The `ConstraintSatisfaction` result type already stores the unsatisfied atomic constraints along with substituted expressions or substitution diagnostics. The elaborated concept diagnostics for function templates and variable template partial specializations already demonstrate the machinery needed to produce the proposed notes. The feature is therefore primarily a matter of plumbing existing constraint-satisfaction results into a diagnostic path for class template partial specializations, rather than implementing new semantic analysis.

**Scope Limitation (Intentional)**

This request is explicitly limited to diagnoses based on **C++20 constraints and `requires`-clauses**. It does not request explaining failures that arise from template argument deduction, SFINAE, or non-constraint-based matching, where the "reason" may not be locally observable without speculating about a user’s intent. The constrained case is well-defined and matches user expectations: if a `requires`-clause was written to control selection, its failure is the information the user needs.

**Reproducer**

```cpp
// Compile with: clang++ -std=c++20 -fsyntax-only
#include <concepts>

template<typename T>
struct trait { static constexpr bool value = false; };

template<std::integral T>
struct trait<T> { static constexpr bool value = true; };

static_assert(trait<double>::value, "expected integral");
```

**Expected Diagnostic (new)**

```text
error: static assertion failed due to requirement 'trait<double>::value': expected integral
note: partial specialization #1 was not selected
note:   constraint 'std::integral<T>' was not satisfied
note:     because 'std::is_integral_v<double>' evaluated to false
```

**Actual Diagnostic (current)**

```text
error: static assertion failed due to requirement 'trait<double>::value': expected integral
```

No note explaining the rejection of `trait<T>`.

**Additional Context**

- This is not a request for a default‑on, always‑verbose diagnostic. The notes should be emitted only when a diagnostic is already being produced (e.g., the instantiation results in an error, or a `static_assert` fails), or behind an opt‑in flag if the community prefers a separate control.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to