Skip to content

Commit 4bccb01

Browse files
[Clang] Workaround dependent source location issues (#106925)
In #78436 we made some SourceLocExpr dependent to deal with the fact that their value should reflect the name of specialized function - rather than the rtemplate in which they are first used. However SourceLocExpr are unusual in two ways - They don't depend on template arguments - They morally depend on the context in which they are used (rather than called from). It's fair to say that this is quite novels and confuses clang. In particular, in some cases, we used to create dependent SourceLocExpr and never subsequently transform them, leaving dependent objects in instantiated functions types. To work around that we avoid replacing SourceLocExpr when we think they could remain dependent. It's certainly not perfect but it fixes a number of reported bugs, and seem to only affect scenarios in which the value of the SourceLocExpr does not matter (overload resolution). Fixes #106428 Fixes #81155 Fixes #80210 Fixes #85373 --------- Co-authored-by: Aaron Ballman <[email protected]>
1 parent 427e202 commit 4bccb01

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ Bug Fixes to C++ Support
342342
specialization right before its declaration context. (#GH64082)
343343
- Fixed a constraint comparison bug for friend declarations. (#GH78101)
344344
- Fix handling of ``_`` as the name of a lambda's init capture variable. (#GH107024)
345+
- Fix an issue with dependent source location expressions (#GH106428), (#GH81155), (#GH80210), (#GH85373)
346+
345347

346348
Bug Fixes to AST Handling
347349
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaExpr.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5443,11 +5443,24 @@ struct EnsureImmediateInvocationInDefaultArgs
54435443

54445444
// Rewrite to source location to refer to the context in which they are used.
54455445
ExprResult TransformSourceLocExpr(SourceLocExpr *E) {
5446-
if (E->getParentContext() == SemaRef.CurContext)
5446+
DeclContext *DC = E->getParentContext();
5447+
if (DC == SemaRef.CurContext)
54475448
return E;
5448-
return getDerived().RebuildSourceLocExpr(E->getIdentKind(), E->getType(),
5449-
E->getBeginLoc(), E->getEndLoc(),
5450-
SemaRef.CurContext);
5449+
5450+
// FIXME: During instantiation, because the rebuild of defaults arguments
5451+
// is not always done in the context of the template instantiator,
5452+
// we run the risk of producing a dependent source location
5453+
// that would never be rebuilt.
5454+
// This usually happens during overload resolution, or in contexts
5455+
// where the value of the source location does not matter.
5456+
// However, we should find a better way to deal with source location
5457+
// of function templates.
5458+
if (!SemaRef.CurrentInstantiationScope ||
5459+
!SemaRef.CurContext->isDependentContext() || DC->isDependentContext())
5460+
DC = SemaRef.CurContext;
5461+
5462+
return getDerived().RebuildSourceLocExpr(
5463+
E->getIdentKind(), E->getType(), E->getBeginLoc(), E->getEndLoc(), DC);
54515464
}
54525465
};
54535466

clang/test/SemaCXX/source_location.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,3 +929,63 @@ void test() {
929929
}
930930

931931
}
932+
933+
namespace GH106428 {
934+
935+
struct add_fn {
936+
template <typename T>
937+
constexpr auto operator()(T lhs, T rhs,
938+
const std::source_location loc = std::source_location::current())
939+
const -> T
940+
{
941+
return lhs + rhs;
942+
}
943+
};
944+
945+
946+
template <class _Fp, class... _Args>
947+
decltype(_Fp{}(0, 0))
948+
__invoke(_Fp&& __f);
949+
950+
template<typename T>
951+
struct type_identity { using type = T; };
952+
953+
template<class Fn>
954+
struct invoke_result : type_identity<decltype(__invoke(Fn{}))> {};
955+
956+
using i = invoke_result<add_fn>::type;
957+
static_assert(__is_same(i, int));
958+
959+
}
960+
961+
#if __cplusplus >= 202002L
962+
963+
namespace GH81155 {
964+
struct buff {
965+
buff(buff &, const char * = __builtin_FUNCTION());
966+
};
967+
968+
template <class Ty>
969+
Ty declval();
970+
971+
template <class Fx>
972+
auto Call(buff arg) -> decltype(Fx{}(arg));
973+
974+
template <typename>
975+
struct F {};
976+
977+
template <class Fx>
978+
struct InvocableR : F<decltype(Call<Fx>(declval<buff>()))> {
979+
static constexpr bool value = false;
980+
};
981+
982+
template <class Fx, bool = InvocableR<Fx>::value>
983+
void Help(Fx) {}
984+
985+
void Test() {
986+
Help([](buff) {});
987+
}
988+
989+
}
990+
991+
#endif

0 commit comments

Comments
 (0)