Skip to content

Commit b561f06

Browse files
committed
[Clang] Workaround dependent source location issues
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 argumente - 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
1 parent ba52a09 commit b561f06

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ Bug Fixes to C++ Support
326326
- Fix evaluation of the index of dependent pack indexing expressions/types specifiers (#GH105900)
327327
- Correctly handle subexpressions of an immediate invocation in the presence of implicit casts. (#GH105558)
328328
- Clang now correctly handles direct-list-initialization of a structured bindings from an array. (#GH31813)
329+
- Fix an issue with dependent source location expressions (#GH106428), (#GH81155)
329330

330331
Bug Fixes to AST Handling
331332
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaExpr.cpp

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

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

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)