Skip to content

text: a bad escape is reported at the start of the string literal, and UnescapeError carries no offset #648

Open

Description

@iainmcgin
buffa::text::unescape_str(r#""abc\q""#)
// Err: unrecognised escape sequence

buffa::text::decode_from_str::<StringValue>("value: \"abc\"\n  \"def\\q\"")
// Err: text format parse error (line 1:8): invalid string: unrecognised escape sequence

The bad escape in the second input is on line 2. The error points at the opening quote of the first literal, because adjacent literals are one token and the decoder reports the token's position (TextDecoder::read_string and read_bytes, buffa/src/text/decoder.rs:454-481). UnescapeError::BadEscape(&'static str) holds only a fixed reason (buffa/src/text/string.rs:35-45), so neither the decoder nor a direct caller of unescape / unescape_str can say where in the literal the failure is, or which sequence caused it.

Proposed: record the byte offset of the offending backslash within raw, print it in Display, and have the decoder add it to the token position before it computes line and column. UnescapeError is a public enum without #[non_exhaustive], so a new field on BadEscape is a breaking change and belongs in a minor release.

Activity

  1. Divyansh151005 commented on Oct 17, 2026

    @Divyansh151005
    Contributor

    I'll take this.

    Plan: extend UnescapeError::BadEscape with a byte offset into the token raw (breaking API, as noted in the issue). Thread offsets through unescape / unescape_str, include the offset in Display, and in TextDecoder::read_string / read_bytes add that offset to tok.pos before line_col. Tests: direct unescape on a concatenated literal, plus decode_from_str pointing at the second literal's bad escape (issue example).

  2. added a commit that references this issue on Nov 4, 2026
    9cb523a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions