Subject: Re: bug#78777: 30.1; insert-file-contents should not set buffer-file-name to nil

From: Stefan Monnier <[email-masked]>
Date: Mon, 16 Jun 2025 11:14:20 -0400

> Can userlock--ask-user-about-supersession-threat be modified to check for
> create-lockfiles and a newly introduced inhibit-file-lock internal variable
> before running userlock--check-content-unchanged?
>
> This change also seems to have been introduced to before-change-functions
> are called at the right time
> <https://lists.gnu.org/archive/html/bug-gnu-emacs/2016-08/msg01029.html>,
> perhaps we should ask Stefan what the best thing to do here is.

No, if you look at the diff, you'll see that this just moved the
`specbind`.  It was introduced earlier by:

```diff
commit d07af40d882673ccb30c267a617e673c22a85ee4
Author: Kenichi Handa <[email-masked]>
Date:   Mon Oct 23 12:40:32 2006 +0000

    (Finsert_file_contents): On replacing, temporarily bind
    buffer-file-name to Qnil before calling insert_from_buffer.

diff --git a/src/fileio.c b/src/fileio.c
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -4359,8 +4359,12 @@
       inserted_chars
 	= (buf_bytepos_to_charpos (XBUFFER (conversion_buffer),
 				   same_at_start + inserted)
 	   - same_at_start_charpos);
+      /* This binding is to avoid ask-user-about-supersession-threat
+	 being called in insert_from_buffer (via in
+	 prepare_to_modify_buffer).  */
+      specbind (intern ("buffer-file-name"), Qnil);
       insert_from_buffer (XBUFFER (conversion_buffer),
 			  same_at_start_charpos, inserted_chars, 0);
       /* Set `inserted' to the number of inserted characters.  */
       inserted = PT - temp;
```

I'm not sure exactly what was the motivation for the change.
I mean, the comment explains that the purpose is to silence a potential
`ask-user-about-supersession-threat`, but I don't know why we should
care about such a possibility and why it would be right to silence
it here.

Nor do I understand why it was added to the code path where coding
conversion was needed but not to the other one.

I see that back in 2016 I apparently understood the situation a bit
better (or at least I thought) because while moving the `specbind`
I managed to improve the comment with:

             AFAICT we could avoid ask-user-about-supersession-threat by
             setting current_buffer->modtime earlier, but we could still
             end up calling ask-user-about-supersession-threat if the
             file is modified while we read it, so we bind
             buffer-file-name instead.  */

but it's still insufficient for me to understand now what's going on.
Handa?

        Stefan