Skip to content

Commit 82a11e4

Browse files
cor3ntinAaronBallman
authored andcommitted
[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 e657e02 commit 82a11e4

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
@@ -1121,6 +1121,8 @@ Bug Fixes to C++ Support
11211121
Fixes (#GH85992).
11221122
- Fixed a crash-on-invalid bug involving extraneous template parameter with concept substitution. (#GH73885)
11231123
- Fixed assertion failure by skipping the analysis of an invalid field declaration. (#GH99868)
1124+
- Fix an issue with dependent source location expressions (#GH106428), (#GH81155), (#GH80210), (#GH85373)
1125+
11241126

11251127
Bug Fixes to AST Handling
11261128
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaExpr.cpp

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

54315431
// Rewrite to source location to refer to the context in which they are used.
54325432
ExprResult TransformSourceLocExpr(SourceLocExpr *E) {
5433-
if (E->getParentContext() == SemaRef.CurContext)
5433+
DeclContext *DC = E->getParentContext();
5434+
if (DC == SemaRef.CurContext)
54345435
return E;
5435-
return getDerived().RebuildSourceLocExpr(E->getIdentKind(), E->getType(),
5436-
E->getBeginLoc(), E->getEndLoc(),
5437-
SemaRef.CurContext);
5436+
5437+
// FIXME: During instantiation, because the rebuild of defaults arguments
5438+
// is not always done in the context of the template instantiator,
5439+
// we run the risk of producing a dependent source location
5440+
// that would never be rebuilt.
5441+
// This usually happens during overload resolution, or in contexts
5442+
// where the value of the source location does not matter.
5443+
// However, we should find a better way to deal with source location
5444+
// of function templates.
5445+
if (!SemaRef.CurrentInstantiationScope ||
5446+
!SemaRef.CurContext->isDependentContext() || DC->isDependentContext())
5447+
DC = SemaRef.CurContext;
5448+
5449+
return getDerived().RebuildSourceLocExpr(
5450+
E->getIdentKind(), E->getType(), E->getBeginLoc(), E->getEndLoc(), DC);
54385451
}
54395452
};
54405453

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)