Use Defer instead of relying on a "Don't return before this" comment

This commit is contained in:
Rangi42
2025-07-13 12:52:45 -04:00
parent 611b0041c4
commit 041b86b8dd

View File

@@ -1328,17 +1328,16 @@ static std::shared_ptr<std::string> readInterpolation(size_t depth) {
std::string fmtBuf; std::string fmtBuf;
FormatSpec fmt{}; FormatSpec fmt{};
bool disableInterpolation = lexerState->disableInterpolation;
// In a context where `lexerState->disableInterpolation` is true, `peek` will expand // In a context where `lexerState->disableInterpolation` is true, `peek` will expand
// nested interpolations itself, which can lead to stack overflow. This lets // nested interpolations itself, which can lead to stack overflow. This lets
// `readInterpolation` handle its own nested expansions, increasing `depth` each time. // `readInterpolation` handle its own nested expansions, increasing `depth` each time.
bool disableInterpolation = lexerState->disableInterpolation;
lexerState->disableInterpolation = true; lexerState->disableInterpolation = true;
for (;;) { // Reset `lexerState->disableInterpolation` when exiting this loop
int c = peek(); for (Defer reset{[&] { lexerState->disableInterpolation = disableInterpolation; }};;) {
if (int c = peek(); c == '{') { // Nested interpolation
if (c == '{') { // Nested interpolation
shiftChar(); shiftChar();
if (auto str = readInterpolation(depth + 1); str) { if (auto str = readInterpolation(depth + 1); str) {
beginExpansion(str, *str); beginExpansion(str, *str);
@@ -1366,9 +1365,6 @@ static std::shared_ptr<std::string> readInterpolation(size_t depth) {
} }
} }
// Don't return before `lexerState->disableInterpolation` is reset!
lexerState->disableInterpolation = disableInterpolation;
if (fmtBuf.starts_with('#')) { if (fmtBuf.starts_with('#')) {
// Skip a '#' raw symbol prefix, but after expanding any nested interpolations. // Skip a '#' raw symbol prefix, but after expanding any nested interpolations.
fmtBuf.erase(0, 1); fmtBuf.erase(0, 1);