all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Lars Ingebrigtsen <larsi@gnus.org>
To: phillip.lord@newcastle.ac.uk (Phillip Lord)
Cc: 19208@debbugs.gnu.org
Subject: bug#19208: replace-match unhelpful error message
Date: Sat, 03 Aug 2019 16:01:15 +0200	[thread overview]
Message-ID: <87imreic0k.fsf@mouse.gnus.org> (raw)
In-Reply-To: <87r3wn5kgw.fsf@newcastle.ac.uk> (Phillip Lord's message of "Fri,  28 Nov 2014 17:00:15 +0000")

phillip.lord@newcastle.ac.uk (Phillip Lord) writes:

> (progn
>   (goto-char (point-min))
>   (re-search-forward "This")
>   (replace-match "That" nil nil nil 1))
>
> This causes the following error.
>
> progn: Args out of range: -1, -1
>
> The error is caused because I have asked for a subexp that doesn't exist in
> the regexp. A programmer error for sure, but the Args out of range error seems
> rather unfriendly to me, especially as I have no idea where the -1, or
> -1 has come from.
>
> "Attempt to replace regexp subexpression that doesn't exist", for
> example, would be nicer.

Yup.

This is the code that signals the error:

      if (search_regs.start[sub] < BEGV
	  || search_regs.start[sub] > search_regs.end[sub]
	  || search_regs.end[sub] > ZV)
	args_out_of_range (make_fixnum (search_regs.start[sub]),
			   make_fixnum (search_regs.end[sub]));

Which is a weird way to check for this, surely?  It looks more like a
sanity checks that checks whether start[sub] and end[sub] is inside the
buffer, and that the start is before the end.

This happens to actually work here because when there is no match,
start[sub] is -1, which is always less than BEGV.

And this explains where the -1 in the args out of range comes from.

The sanity check seems sensible (because the buffer may have been
narrowed before calling replace-match), so I propose to add an
additional check that actually explicitly checks whether the subexp is
valid.

Hm...  or is this what the check right above it tried to do?

  if (NILP (subexp))
    sub = 0;
  else
    {
      CHECK_FIXNUM (subexp);
      if (! (0 <= XFIXNUM (subexp) && XFIXNUM (subexp) < search_regs.num_regs))
	args_out_of_range (subexp, make_fixnum (search_regs.num_regs));
      sub = XFIXNUM (subexp);
    }

Yoda conditionals and a !...  Let's see...

No, that's just another sanity check -- num_regs is the max allowed
number of sub-matches.  (I've now added some comments to clarify.)

I've now added a new check to check for this situation, and I've used
your suggested error string.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





  reply	other threads:[~2019-08-03 14:01 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-28 17:00 bug#19208: replace-match unhelpful error message Phillip Lord
2019-08-03 14:01 ` Lars Ingebrigtsen [this message]
2019-08-03 20:22 ` Paul Eggert
2019-08-03 20:31   ` Lars Ingebrigtsen
2019-08-03 20:50     ` Paul Eggert
2019-08-04  9:09       ` Andreas Schwab
2019-08-03 23:59     ` Noam Postavsky
2019-08-04  9:02       ` Andreas Schwab
2019-08-04 14:12         ` Noam Postavsky
2019-08-04 15:13           ` Andreas Schwab
2019-08-04 17:41             ` Paul Eggert
2019-08-04 18:14               ` Andreas Schwab
2019-08-05  2:25           ` Richard Stallman
2019-08-04 11:47       ` Lars Ingebrigtsen
2019-08-04  9:18   ` Andreas Schwab

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=87imreic0k.fsf@mouse.gnus.org \
    --to=larsi@gnus.org \
    --cc=19208@debbugs.gnu.org \
    --cc=phillip.lord@newcastle.ac.uk \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.