Change the error message to make it start at the as keyword

This commit is contained in:
Guillaume Gomez 2024-07-28 21:37:22 +02:00
parent bf03e44a22
commit e74f0d20ba
3 changed files with 29 additions and 15 deletions

View File

@ -195,8 +195,8 @@ impl<'a> Expr<'a> {
fn is_as(i: &'a str, level: Level) -> ParseResult<'a, WithSpan<'a, Self>> { fn is_as(i: &'a str, level: Level) -> ParseResult<'a, WithSpan<'a, Self>> {
let start = i; let start = i;
let (i, lhs) = Self::filtered(i, level)?; let (before_keyword, lhs) = Self::filtered(i, level)?;
let (j, rhs) = opt(ws(identifier))(i)?; let (j, rhs) = opt(ws(identifier))(before_keyword)?;
let i = match rhs { let i = match rhs {
Some("is") => j, Some("is") => j,
Some("as") => { Some("as") => {
@ -207,7 +207,7 @@ impl<'a> Expr<'a> {
} else if target.is_empty() { } else if target.is_empty() {
return Err(nom::Err::Failure(ErrorContext::new( return Err(nom::Err::Failure(ErrorContext::new(
"`as` operator expects the name of a primitive type on its right-hand side", "`as` operator expects the name of a primitive type on its right-hand side",
start, before_keyword.trim_start(),
))); )));
} else { } else {
return Err(nom::Err::Failure(ErrorContext::new( 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 \ "`as` operator expects the name of a primitive type on its right-hand \
side, found `{target}`" 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)?; let (i, rhs) = opt(terminated(opt(keyword("not")), ws(keyword("defined"))))(i)?;

View File

@ -20,5 +20,9 @@ struct D;
#[template(source = r#"{{ 1234 as int32_t }}"#, ext = "html")] #[template(source = r#"{{ 1234 as int32_t }}"#, ext = "html")]
struct E; struct E;
#[derive(Template)]
#[template(source = r#"{{ (1234 + 4 * 12 / 45675445 - 13) as int32_t }}"#, ext = "html")]
struct F;
fn main() { fn main() {
} }

View File

@ -1,6 +1,6 @@
error: `as` operator expects the name of a primitive type on its right-hand side 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: failed to parse template source at row 1, column 8 near:
"1234 as 4567 }}" "as 4567 }}"
--> tests/ui/as-primitive-type.rs:3:10 --> tests/ui/as-primitive-type.rs:3:10
| |
3 | #[derive(Template)] 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) = 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 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: failed to parse template source at row 1, column 8 near:
"1234 as ? }}" "as ? }}"
--> tests/ui/as-primitive-type.rs:7:10 --> tests/ui/as-primitive-type.rs:7:10
| |
7 | #[derive(Template)] 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) = 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` 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: failed to parse template source at row 1, column 8 near:
"1234 as u1234 }}" "as u1234 }}"
--> tests/ui/as-primitive-type.rs:11:10 --> tests/ui/as-primitive-type.rs:11:10
| |
11 | #[derive(Template)] 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) = 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` 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: failed to parse template source at row 1, column 8 near:
"1234 as core::primitive::u32 }}" "as core::primitive::u32 }}"
--> tests/ui/as-primitive-type.rs:15:10 --> tests/ui/as-primitive-type.rs:15:10
| |
15 | #[derive(Template)] 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) = 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` 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: failed to parse template source at row 1, column 8 near:
"1234 as int32_t }}" "as int32_t }}"
--> tests/ui/as-primitive-type.rs:19:10 --> tests/ui/as-primitive-type.rs:19:10
| |
19 | #[derive(Template)] 19 | #[derive(Template)]
| ^^^^^^^^ | ^^^^^^^^
| |
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info) = 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)