From: Junio C Hamano <gitster@pobox.com>
To: Elijah Newren <newren@gmail.com>
Cc: Taylor Blau <me@ttaylorr.com>,
Elijah Newren via GitGitGadget <gitgitgadget@gmail.com>,
git@vger.kernel.org, Dmitry Goncharov <dgoncharov@users.sf.net>
Subject: Re: [PATCH 2/2] merge-ort: fix slightly overzealous assertion for rename-to-self
Date: Thu, 13 Mar 2025 11:34:07 -0700 [thread overview]
Message-ID: <xmqqcyekok4g.fsf@gitster.g> (raw)
In-Reply-To: <CABPp-BF01a8bEZ2mfp7H_=HKsv6mSE4+ee8bi9D9xKWbH=0b5g@mail.gmail.com> (Elijah Newren's message of "Thu, 13 Mar 2025 10:15:49 -0700")
Elijah Newren <newren@gmail.com> writes:
> "if (condition) BUG()" is invalid; it needs more arguments. "if
> (condition) BUG(something)" requires a separate "something", which
> requires awkward additional wording and/or is needlessly duplicative.
Ah, obviously we differ on that point.
I consider it an advantage that <something> can be more descriptive
in a developer friendly way than <condition> expression alone. If
you wrote
if (istate->cache_nr < i)
BUG("ran past the end of the istate->cache[] array?");
you can spot a bug of the assertion, because the human-readable part
tells us the intention that the condition is supposed to be the
usual array bounds check, and should be checking with "<=". In
contrast
BUG_ON(istate->cache_nr < i);
does not give us the extra information we can use to double check
what the developer who wrote it, who may not be with us anymore,
wanted to really check. Is it a misspelt "<=", or does this code
path use 'i' not just as a variable to iterate over the array but
also allows it to go one past its end, so "<" is the condition it
really wants to validate?
> If you don't want to see assert in the codebase because of NDEBUG,
> then obviously we'd leave NDEBUG out of BUG_ON().
Absolutely, because the largest problem with assert() is that the
condition can be compiled away while the compiler does not help
ensure that the condition part is free of side effects. If we drop
NDEBUG, that problem goes away.
> Such a BUG_ON() would be an improvement because it maintains the
> ergonomics of assert() while avoiding the potential mistake of
> accidentally including something with side-effects.
Yes, we are half on the same page (as I disagree that assert() that
does not give a room to explain why we are checking the condition is
ergonomic at all) and agree that discarding NDEBUG gives us safer
variant than assert().
> "If (condition)
> BUG(something)" doesn't preserve the ergonomics and is thus worse,
Again, this is philosophical difference between us. It gives us the
same safety as NDEBUG-less BUG_ON() but allows us to be more
descriptive to help developers. While I understand sometimes the
condition may be self evident (especially while developing new code,
where the developer thought the problem long and deep) and people
may find it tedious to have to explain the reasoning behind the
check, I find it highly problematic not to give room to explain to
those who want to. And /* comments */ is not the same thing, as I
think it is important to explain your BUG() well, just like it is
good practice to explain your commit well in the log message.
One thing I view as a downside of "if (condition) BUG(message)" is
that it does not make it clear syntactically that (condition) is
about assertion, and you can write
if (condition) {
do something;
BUG(message);
}
which probably is not what you want. I am OK with
BUG_ON(<condition>, <message>);
which would make such a construct impossible.
Thanks.
next prev parent reply other threads:[~2025-03-13 18:34 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-06 15:30 [PATCH 0/2] merge-ort: fix a crash in process_renames for a file transitively renamed to itself Elijah Newren via GitGitGadget
2025-03-06 15:30 ` [PATCH 1/2] t6423: add a testcase causing a failed assertion in process_renames Dmitry Goncharov via GitGitGadget
2025-03-06 15:30 ` [PATCH 2/2] merge-ort: fix slightly overzealous assertion for rename-to-self Elijah Newren via GitGitGadget
2025-03-12 22:00 ` Taylor Blau
2025-03-12 22:36 ` Elijah Newren
2025-03-12 23:18 ` Junio C Hamano
2025-03-13 6:22 ` Elijah Newren
2025-03-13 16:55 ` Junio C Hamano
2025-03-13 17:15 ` Elijah Newren
2025-03-13 18:34 ` Junio C Hamano [this message]
2025-03-14 0:24 ` Elijah Newren
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqcyekok4g.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=dgoncharov@users.sf.net \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=me@ttaylorr.com \
--cc=newren@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).