Subject: bug#26837: Wrong file in "autoloading failed to define" error

From: Glenn Morris <[email-masked]>
Date: Tue, 09 May 2017 14:39:24 -0400

Glenn Morris wrote:

>> I can't claim to know what is going on within that function, but your
>> patch seems to only work for some files (e.g. your bar.el). If bar
>> starts with a comment, then bar isn't moved to the top after evaluating
>> (load "bar").
>
> The intent of the code seems to be to only adjust an existing
> load-history element for some file if the entire file is being loaded.
> I guess in the leading comment case, something else has already moved
> point looking for a significant leading comment (eg lexbind, script,
> prop-line). Hmmm.

I think it was the lexical-binding thing. Following seems to work:

--- i/src/lread.c
+++ w/src/lread.c
@@ -1885,7 +1885,7 @@ PREDICATE can also be an integer to pass to the faccessat(2) function,
       /* On the first cycle, we can easily test here
 	 whether we are reading the whole buffer.  */
       if (b && first_sexp)
-	whole_buffer = (PT == BEG && ZV == Z);
+	whole_buffer = (BUF_PT (b) == BUF_BEG (b) && BUF_ZV (b) == BUF_Z (b));
 
       instream = stream;
     read_next:
@@ -2008,6 +2008,7 @@ BUFFER is the buffer to evaluate (nil means use current buffer),
   record_unwind_protect (save_excursion_restore, save_excursion_save ());
   BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
   specbind (Qlexical_binding, lisp_file_lexically_bound_p (buf) ? Qt : Qnil);
+  BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
   readevalloop (buf, 0, filename,
 		!NILP (printflag), unibyte, Qnil, Qnil, Qnil);
   unbind_to (count, Qnil);