From b3843d90a85d84e44ccd6cb188a5aa375df84104 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Tue, 1 Aug 2023 02:21:59 +0200 Subject: parser: ensure correct drop order for `Parsed` According to [RFC 1857] the fields of a struct are dropped in the same order as they are declared. For `struct S { a: A, b: B }` field `a` is dropped before field `b`. Our struct `Parsed` is self referencial. Its field `ast` depends on `source`, so `source` must outlife `ast`. This PR changes the order of the fields to reflect this requirement. In practice it should not matter, because we know that the variant of `Node` won't access the string data during their `Drop`, but better safe than sorry - maybe `Node` changes in the future. [RFC 1857]: https://rust-lang.github.io/rfcs/1857-stabilize-drop-order.html --- askama_parser/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs index cf105cb..bc29a22 100644 --- a/askama_parser/src/lib.rs +++ b/askama_parser/src/lib.rs @@ -28,9 +28,10 @@ mod _parsed { use super::{Ast, Node, ParseError, Syntax}; pub struct Parsed { + // `source` must outlive `ast`, so `ast` must be declared before `source` + ast: Ast<'static>, #[allow(dead_code)] source: String, - ast: Ast<'static>, } impl Parsed { @@ -44,7 +45,7 @@ mod _parsed { Err(e) => return Err(e), }; - Ok(Self { source, ast }) + Ok(Self { ast, source }) } // The return value's lifetime must be limited to `self` to uphold the unsafe invariant. -- cgit