Skip to content

Commit 8322a30

Browse files
authored
Avoid accessing unset optional, workaround for llvm#100095 (llvm#100408)
This patch avoids accessing an unset `std::optional<>`, which is a part of the manifestation of llvm#100095. The other part is an assertion failure that is not addressed here. This is not a proper fix, but enables Clang to continue working with more libc++ runtime checks enabled (specifically, `-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST`, which checks access to unset optionals among other things). A proper fix is being discussed on llvm#100095.
1 parent bfa0cae commit 8322a30

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

clang/lib/Sema/SemaTemplateDeduction.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,11 @@ class PackDeductionScope {
951951

952952
// Skip over the pack elements that were expanded into separate arguments.
953953
// If we partially expanded, this is the number of partial arguments.
954+
// FIXME: `&& FixedNumExpansions` is a workaround for UB described in
955+
// https://github.com/llvm/llvm-project/issues/100095
954956
if (IsPartiallyExpanded)
955957
PackElements += NumPartialPackArgs;
956-
else if (IsExpanded)
958+
else if (IsExpanded && FixedNumExpansions)
957959
PackElements += *FixedNumExpansions;
958960

959961
for (auto &Pack : Packs) {

clang/test/SemaCXX/pr100095.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s
2+
// XFAIL: asserts
3+
4+
template <class> struct Pair;
5+
template <class...> struct Tuple {
6+
template <class _Up> Tuple(_Up);
7+
};
8+
template <typename> struct StatusOr;
9+
template <int> using ElementType = int;
10+
template <int... fields>
11+
using Key = Tuple<ElementType<fields>...>;
12+
template <int... fields>
13+
StatusOr<Pair<Key<fields...>>> Parser();
14+
struct Helper { Helper(Tuple<>, Tuple<>, int, int); };
15+
struct D : Helper {
16+
D(Key<> f, int n, int e) : Helper(f, Parser<>, n, e) {}
17+
};

0 commit comments

Comments
 (0)