Skip to content

Commit 5fa1cc6

Browse files
committed
[clang] concepts: perform parameter mapping subsitution in correct context
Prior to this patch, during constraint normalization we could forget from which declaration an atomic constraint was normalized from. Subsequently when performing parameter mapping substitution for that atomic constraint with an incorrect context, we couldn't correctly recognize which declarations are supposed to be visible. Fixes #60336
1 parent 858bea8 commit 5fa1cc6

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ Bug Fixes to C++ Support
173173
- Clang now correctly parses potentially declarative nested-name-specifiers in pointer-to-member declarators.
174174
- Fix a crash when checking the initialzier of an object that was initialized
175175
with a string literal. (#GH82167)
176+
- Clang now correctly recognizes the correct context for parameter
177+
substitutions in concepts, so it doesn't incorrectly complain of missing
178+
module imports in those situations. (#GH60336)
176179

177180
Bug Fixes to AST Handling
178181
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Sema/SemaConcept.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ enum { ConstraintAlignment = 8 };
3030

3131
struct alignas(ConstraintAlignment) AtomicConstraint {
3232
const Expr *ConstraintExpr;
33+
NamedDecl *ConstraintDecl;
3334
std::optional<ArrayRef<TemplateArgumentLoc>> ParameterMapping;
3435

35-
AtomicConstraint(Sema &S, const Expr *ConstraintExpr) :
36-
ConstraintExpr(ConstraintExpr) { };
36+
AtomicConstraint(const Expr *ConstraintExpr, NamedDecl *ConstraintDecl)
37+
: ConstraintExpr(ConstraintExpr), ConstraintDecl(ConstraintDecl) {};
3738

3839
bool hasMatchingParameterMapping(ASTContext &C,
3940
const AtomicConstraint &Other) const {

clang/lib/Sema/SemaConcept.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,8 +1457,8 @@ substituteParameterMappings(Sema &S, NormalizedConstraint &N,
14571457
: ArgsAsWritten->arguments().front().getSourceRange().getEnd();
14581458
Sema::InstantiatingTemplate Inst(
14591459
S, InstLocBegin,
1460-
Sema::InstantiatingTemplate::ParameterMappingSubstitution{}, Concept,
1461-
{InstLocBegin, InstLocEnd});
1460+
Sema::InstantiatingTemplate::ParameterMappingSubstitution{},
1461+
Atomic.ConstraintDecl, {InstLocBegin, InstLocEnd});
14621462
if (Inst.isInvalid())
14631463
return true;
14641464
if (S.SubstTemplateArguments(*Atomic.ParameterMapping, MLTAL, SubstArgs))
@@ -1632,7 +1632,7 @@ NormalizedConstraint::fromConstraintExpr(Sema &S, NamedDecl *D, const Expr *E) {
16321632
Kind, std::move(*Sub), FE->getPattern()}};
16331633
}
16341634

1635-
return NormalizedConstraint{new (S.Context) AtomicConstraint(S, E)};
1635+
return NormalizedConstraint{new (S.Context) AtomicConstraint(E, D)};
16361636
}
16371637

16381638
bool FoldExpandedConstraint::AreCompatibleForSubsumption(

clang/test/Modules/GH60336-2.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: rm -rf %t
2+
// RUN: %clang_cc1 -x c++ -std=c++20 %s -verify -fmodules -fmodules-cache-path=%t
3+
// expected-no-diagnostics
4+
5+
#pragma clang module build std
6+
module std {
7+
module concepts {}
8+
module functional {}
9+
}
10+
#pragma clang module contents
11+
#pragma clang module begin std
12+
13+
template <class _Tp> struct common_reference {
14+
using type = _Tp;
15+
};
16+
17+
#pragma clang module end
18+
#pragma clang module begin std.concepts
19+
#pragma clang module import std
20+
21+
template <class _Tp>
22+
concept same_as = __is_same(_Tp, _Tp);
23+
24+
template <class _Tp>
25+
concept common_reference_with =
26+
same_as<typename common_reference<_Tp>::type>;
27+
28+
#pragma clang module end
29+
#pragma clang module begin std.functional
30+
#pragma clang module import std.concepts
31+
32+
template <class, class _Ip>
33+
concept sentinel_for = common_reference_with<_Ip>;
34+
35+
constexpr bool ntsf_subsumes_sf(sentinel_for<char *> auto)
36+
requires true
37+
{
38+
return true;
39+
}
40+
bool ntsf_subsumes_sf(sentinel_for<char *> auto);
41+
static_assert(ntsf_subsumes_sf(""));
42+
43+
#pragma clang module end
44+
#pragma clang module endbuild

clang/test/Modules/GH60336.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: rm -rf %t
22
// RUN: %clang_cc1 -x c++ -std=c++20 %s -verify -fmodules -fmodules-cache-path=%t
3+
// expected-no-diagnostics
4+
35
#pragma clang module build std
46
module std [system] {
57
module concepts [system] {
@@ -65,14 +67,3 @@ constexpr bool ntsf_subsumes_sf(std::nothrow_sentinel_for<char*> auto) requires
6567
}
6668
constexpr bool ntsf_subsumes_sf(std::sentinel_for<char*> auto);
6769
static_assert(ntsf_subsumes_sf("foo"));
68-
69-
// Note: Doing diagnostics verify lines in the individual modules isn't
70-
// permitted, and using 'bookmarks' in a module also doesn't work, so we're
71-
// forced to diagnose this by line-number.
72-
//
73-
// Check to ensure that this error happens, prior to a revert of a concepts
74-
// sugaring patch, this diagnostic didn't happen correctly.
75-
76-
// expected-error@* {{partial specialization of 'common_reference<_Tp, _Up>' must be imported from module 'std.type_traits' before it is required}}
77-
// expected-note@63 {{while substituting into concept arguments here}}
78-
// expected-note@*{{partial specialization declared here is not reachable}}

0 commit comments

Comments
 (0)