From ef2f02189290864629ed698ec2131b271c13e3f0 Mon Sep 17 00:00:00 2001 From: Rangi Date: Fri, 22 May 2026 18:17:25 -0400 Subject: [PATCH] Simplify `sectError` handling, without the need for an empty-string sentinel The `errorNoTrace` case for the empty-string sentinel was unreachable, so we can just have a `fatalNoTrace` error right away. --- src/asm/section.cpp | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 7d5062f5..50635128 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -271,18 +271,6 @@ static void mergeSections( ) { sectErrors.clear(); - auto sectAlreadyDefinedCallback = [§]() { - fprintf(stderr, "Section \"%s\" already defined\n", sect.name.c_str()); - fstk_TraceCurrent(); - fputs(" and also:\n", stderr); - sect.src->printBacktrace(sect.fileLine); - }; - auto sectAlreadyDefinedError = []() { - // The empty string is a sentinel for the `sectAlreadyDefinedCallback` error, - // since it cannot be preformatted as a string - sectErrors.push_back(""); - }; - if (type != sect.type) { sectError( "Section \"%s\" already exists but with type `%s`", @@ -321,26 +309,28 @@ static void mergeSections( } case SECTION_NORMAL: - sectAlreadyDefinedError(); + // Only union/fragment sections can end up with multiple errors queued in `sectErrors`, + // and they cannot encounter this error, so it's okay for this one to skip the queue. + // Queueing it in `sectErrors` would require a sentinel value anyway (e.g. an empty + // string) to handle the "no trace" callback. + assume(sectErrors.empty()); + fatalNoTrace([§]() { + fprintf(stderr, "Section \"%s\" already defined\n", sect.name.c_str()); + fstk_TraceCurrent(); + fputs(" and also:\n", stderr); + sect.src->printBacktrace(sect.fileLine); + }); break; } } if (size_t nbSectErrors = sectErrors.size(); nbSectErrors == 1) { // If there was only one error, print it as a fatal error - if (std::string const &message = sectErrors[0]; message.empty()) { - fatalNoTrace(sectAlreadyDefinedCallback); - } else { - fatal("%s", message.c_str()); - } + fatal("%s", sectErrors.front().c_str()); } else if (nbSectErrors > 1) { // If there were multiple errors, print each of them, followed by a fatal summary error for (std::string const &message : sectErrors) { - if (message.empty()) { - errorNoTrace(sectAlreadyDefinedCallback); - } else { - error("%s", message.c_str()); - } + error("%s", message.c_str()); } fatal( "Cannot create section \"%s\" (%zu error%s)",