From e74f0d20bae705fa446a0fc1d0637a7e5d20ca06 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 28 Jul 2024 21:37:22 +0200 Subject: [PATCH] Change the error message to make it start at the `as` keyword --- rinja_parser/src/expr.rs | 10 ++++---- testing/tests/ui/as-primitive-type.rs | 4 +++ testing/tests/ui/as-primitive-type.stderr | 30 +++++++++++++++-------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/rinja_parser/src/expr.rs b/rinja_parser/src/expr.rs index 8c23a6ecc..2dd1b2570 100644 --- a/rinja_parser/src/expr.rs +++ b/rinja_parser/src/expr.rs @@ -195,8 +195,8 @@ impl<'a> Expr<'a> { fn is_as(i: &'a str, level: Level) -> ParseResult<'a, WithSpan<'a, Self>> { let start = i; - let (i, lhs) = Self::filtered(i, level)?; - let (j, rhs) = opt(ws(identifier))(i)?; + let (before_keyword, lhs) = Self::filtered(i, level)?; + let (j, rhs) = opt(ws(identifier))(before_keyword)?; let i = match rhs { Some("is") => j, Some("as") => { @@ -207,7 +207,7 @@ impl<'a> Expr<'a> { } else if target.is_empty() { return Err(nom::Err::Failure(ErrorContext::new( "`as` operator expects the name of a primitive type on its right-hand side", - start, + before_keyword.trim_start(), ))); } else { return Err(nom::Err::Failure(ErrorContext::new( @@ -215,11 +215,11 @@ impl<'a> Expr<'a> { "`as` operator expects the name of a primitive type on its right-hand \ side, found `{target}`" ), - start, + before_keyword.trim_start(), ))); } } - _ => return Ok((i, lhs)), + _ => return Ok((before_keyword, lhs)), }; let (i, rhs) = opt(terminated(opt(keyword("not")), ws(keyword("defined"))))(i)?; diff --git a/testing/tests/ui/as-primitive-type.rs b/testing/tests/ui/as-primitive-type.rs index 35298fdb7..3e46d2588 100644 --- a/testing/tests/ui/as-primitive-type.rs +++ b/testing/tests/ui/as-primitive-type.rs @@ -20,5 +20,9 @@ struct D; #[template(source = r#"{{ 1234 as int32_t }}"#, ext = "html")] struct E; +#[derive(Template)] +#[template(source = r#"{{ (1234 + 4 * 12 / 45675445 - 13) as int32_t }}"#, ext = "html")] +struct F; + fn main() { } diff --git a/testing/tests/ui/as-primitive-type.stderr b/testing/tests/ui/as-primitive-type.stderr index 98f9f35e8..2ec4b5b40 100644 --- a/testing/tests/ui/as-primitive-type.stderr +++ b/testing/tests/ui/as-primitive-type.stderr @@ -1,6 +1,6 @@ error: `as` operator expects the name of a primitive type on its right-hand side - failed to parse template source at row 1, column 3 near: - "1234 as 4567 }}" + failed to parse template source at row 1, column 8 near: + "as 4567 }}" --> tests/ui/as-primitive-type.rs:3:10 | 3 | #[derive(Template)] @@ -9,8 +9,8 @@ error: `as` operator expects the name of a primitive type on its right-hand side = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info) error: `as` operator expects the name of a primitive type on its right-hand side - failed to parse template source at row 1, column 3 near: - "1234 as ? }}" + failed to parse template source at row 1, column 8 near: + "as ? }}" --> tests/ui/as-primitive-type.rs:7:10 | 7 | #[derive(Template)] @@ -19,8 +19,8 @@ error: `as` operator expects the name of a primitive type on its right-hand side = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info) error: `as` operator expects the name of a primitive type on its right-hand side, found `u1234` - failed to parse template source at row 1, column 3 near: - "1234 as u1234 }}" + failed to parse template source at row 1, column 8 near: + "as u1234 }}" --> tests/ui/as-primitive-type.rs:11:10 | 11 | #[derive(Template)] @@ -29,8 +29,8 @@ error: `as` operator expects the name of a primitive type on its right-hand side = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info) error: `as` operator expects the name of a primitive type on its right-hand side, found `core` - failed to parse template source at row 1, column 3 near: - "1234 as core::primitive::u32 }}" + failed to parse template source at row 1, column 8 near: + "as core::primitive::u32 }}" --> tests/ui/as-primitive-type.rs:15:10 | 15 | #[derive(Template)] @@ -39,11 +39,21 @@ error: `as` operator expects the name of a primitive type on its right-hand side = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info) error: `as` operator expects the name of a primitive type on its right-hand side, found `int32_t` - failed to parse template source at row 1, column 3 near: - "1234 as int32_t }}" + failed to parse template source at row 1, column 8 near: + "as int32_t }}" --> tests/ui/as-primitive-type.rs:19:10 | 19 | #[derive(Template)] | ^^^^^^^^ | = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: `as` operator expects the name of a primitive type on its right-hand side, found `int32_t` + failed to parse template source at row 1, column 35 near: + "as int32_t }}" + --> tests/ui/as-primitive-type.rs:23:10 + | +23 | #[derive(Template)] + | ^^^^^^^^ + | + = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)