From c3749bcb0486e409bf234782b05f63ca69fe38d5 Mon Sep 17 00:00:00 2001 From: Andreas Longva Date: Mon, 1 Apr 2024 15:26:10 +0200 Subject: [PATCH] stack! impl: use fixed prefix everywhere This ensures that the expected generated code in tests is the actual generated code when used in the wild. --- nalgebra-macros/src/lib.rs | 6 +- nalgebra-macros/src/stack_impl.rs | 137 +++++++++++++++--------------- 2 files changed, 72 insertions(+), 71 deletions(-) diff --git a/nalgebra-macros/src/lib.rs b/nalgebra-macros/src/lib.rs index 46ee476d2..722323f8b 100644 --- a/nalgebra-macros/src/lib.rs +++ b/nalgebra-macros/src/lib.rs @@ -24,6 +24,7 @@ use matrix_vector_impl::{Matrix, Vector}; use crate::matrix_vector_impl::{dmatrix_impl, dvector_impl, matrix_impl, vector_impl}; use proc_macro::TokenStream; use quote::quote; +use stack_impl::stack_impl; use syn::parse_macro_input; /// Construct a fixed-size matrix directly from data. @@ -255,8 +256,5 @@ pub fn point(stream: TokenStream) -> TokenStream { #[proc_macro] pub fn stack(stream: TokenStream) -> TokenStream { let matrix = parse_macro_input!(stream as Matrix); - proc_macro::TokenStream::from(match stack_impl::stack_impl("__11f075cdd4a86538", matrix) { - Ok(res) => res, - Err(err) => err.into_compile_error(), - }) + proc_macro::TokenStream::from(stack_impl(matrix).unwrap_or_else(|err| err.into_compile_error())) } diff --git a/nalgebra-macros/src/stack_impl.rs b/nalgebra-macros/src/stack_impl.rs index c0c92fb3f..caaa50819 100644 --- a/nalgebra-macros/src/stack_impl.rs +++ b/nalgebra-macros/src/stack_impl.rs @@ -4,11 +4,14 @@ use quote::{format_ident, quote, quote_spanned}; use syn::spanned::Spanned; use syn::{Error, Expr, Lit}; -// The implementation of the stack macro. The prefix is used to construct variable names -// that are extremely unlikely to collide with variable names used in e.g. expressions -// by the user. #[allow(clippy::too_many_lines)] -pub fn stack_impl(prefix: &str, matrix: Matrix) -> syn::Result { +pub fn stack_impl(matrix: Matrix) -> syn::Result { + // The prefix is used to construct variable names + // that are extremely unlikely to collide with variable names used in e.g. expressions + // by the user. Although we could use a long, pseudo-random string, this makes the generated + // code very painful to parse, so we settle for something more semantic that is still + // very unlikely to collide + let prefix = "___na"; let n_block_rows = matrix.nrows(); let n_block_cols = matrix.ncols(); @@ -188,33 +191,33 @@ mod tests { 0, b; ]; - let result = stack_impl("", input).unwrap(); + let result = stack_impl(input).unwrap(); let expected = quote! {{ - let ref _stack_0_0_block = a; - let _stack_0_0_shape = _stack_0_0_block.shape_generic(); - let ref _stack_1_1_block = b; - let _stack_1_1_shape = _stack_1_1_block.shape_generic(); - let _stack_row_0_dim = _stack_0_0_shape.0; - let _stack_row_0_offset = 0; - let _stack_row_1_dim = _stack_1_1_shape.0; - let _stack_row_1_offset = _stack_row_0_offset + <_ as nalgebra::Dim>::value(&_stack_row_0_dim); - let _stack_col_0_dim = _stack_0_0_shape.1; - let _stack_col_0_offset = 0; - let _stack_col_1_dim = _stack_1_1_shape.1; - let _stack_col_1_offset = _stack_col_0_offset + <_ as nalgebra::Dim>::value(&_stack_col_0_dim); + let ref ___na_stack_0_0_block = a; + let ___na_stack_0_0_shape = ___na_stack_0_0_block.shape_generic(); + let ref ___na_stack_1_1_block = b; + let ___na_stack_1_1_shape = ___na_stack_1_1_block.shape_generic(); + let ___na_stack_row_0_dim = ___na_stack_0_0_shape.0; + let ___na_stack_row_0_offset = 0; + let ___na_stack_row_1_dim = ___na_stack_1_1_shape.0; + let ___na_stack_row_1_offset = ___na_stack_row_0_offset + <_ as nalgebra::Dim>::value(&___na_stack_row_0_dim); + let ___na_stack_col_0_dim = ___na_stack_0_0_shape.1; + let ___na_stack_col_0_offset = 0; + let ___na_stack_col_1_dim = ___na_stack_1_1_shape.1; + let ___na_stack_col_1_offset = ___na_stack_col_0_offset + <_ as nalgebra::Dim>::value(&___na_stack_col_0_dim); let mut matrix = nalgebra::Matrix::zeros_generic( - <_ as nalgebra::DimAdd<_>>::add(_stack_row_0_dim, _stack_row_1_dim), - <_ as nalgebra::DimAdd<_>>::add(_stack_col_0_dim, _stack_col_1_dim) + <_ as nalgebra::DimAdd<_>>::add(___na_stack_row_0_dim, ___na_stack_row_1_dim), + <_ as nalgebra::DimAdd<_>>::add(___na_stack_col_0_dim, ___na_stack_col_1_dim) ); - let start = (_stack_row_0_offset, _stack_col_0_offset); - let shape = (_stack_row_0_dim, _stack_col_0_dim); - let input_view = _stack_0_0_block.generic_view((0,0), shape); + let start = (___na_stack_row_0_offset, ___na_stack_col_0_offset); + let shape = (___na_stack_row_0_dim, ___na_stack_col_0_dim); + let input_view = ___na_stack_0_0_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); - let start = (_stack_row_1_offset, _stack_col_1_offset); - let shape = (_stack_row_1_dim, _stack_col_1_dim); - let input_view = _stack_1_1_block.generic_view((0,0), shape); + let start = (___na_stack_row_1_offset, ___na_stack_col_1_offset); + let shape = (___na_stack_row_1_dim, ___na_stack_col_1_dim); + let input_view = ___na_stack_1_1_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); matrix @@ -231,64 +234,64 @@ mod tests { e, 0, 0; ]; - let result = stack_impl("", input).unwrap(); + let result = stack_impl(input).unwrap(); let expected = quote! {{ - let ref _stack_0_0_block = a; - let _stack_0_0_shape = _stack_0_0_block.shape_generic(); - let ref _stack_0_2_block = b; - let _stack_0_2_shape = _stack_0_2_block.shape_generic(); - let ref _stack_1_1_block = c; - let _stack_1_1_shape = _stack_1_1_block.shape_generic(); - let ref _stack_1_2_block = d; - let _stack_1_2_shape = _stack_1_2_block.shape_generic(); - let ref _stack_2_0_block = e; - let _stack_2_0_shape = _stack_2_0_block.shape_generic(); - let _stack_row_0_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfRows < _ , _ >> :: representative (_stack_0_0_shape . 0 , _stack_0_2_shape . 0) . expect ("All blocks in block row 0 must have the same number of rows") ; - let _stack_row_0_offset = 0; - let _stack_row_1_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfRows < _ , _ >> :: representative (_stack_1_1_shape . 0 , _stack_1_2_shape . 0) . expect ("All blocks in block row 1 must have the same number of rows") ; - let _stack_row_1_offset = _stack_row_0_offset + <_ as nalgebra::Dim>::value(&_stack_row_0_dim); - let _stack_row_2_dim = _stack_2_0_shape.0; - let _stack_row_2_offset = _stack_row_1_offset + <_ as nalgebra::Dim>::value(&_stack_row_1_dim); - let _stack_col_0_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfColumns < _ , _ >> :: representative (_stack_0_0_shape . 1 , _stack_2_0_shape . 1) . expect ("All blocks in block column 0 must have the same number of columns") ; - let _stack_col_0_offset = 0; - let _stack_col_1_dim = _stack_1_1_shape.1; - let _stack_col_1_offset = _stack_col_0_offset + <_ as nalgebra::Dim>::value(&_stack_col_0_dim); - let _stack_col_2_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfColumns < _ , _ >> :: representative (_stack_0_2_shape . 1 , _stack_1_2_shape . 1) . expect ("All blocks in block column 2 must have the same number of columns") ; - let _stack_col_2_offset = _stack_col_1_offset + <_ as nalgebra::Dim>::value(&_stack_col_1_dim); + let ref ___na_stack_0_0_block = a; + let ___na_stack_0_0_shape = ___na_stack_0_0_block.shape_generic(); + let ref ___na_stack_0_2_block = b; + let ___na_stack_0_2_shape = ___na_stack_0_2_block.shape_generic(); + let ref ___na_stack_1_1_block = c; + let ___na_stack_1_1_shape = ___na_stack_1_1_block.shape_generic(); + let ref ___na_stack_1_2_block = d; + let ___na_stack_1_2_shape = ___na_stack_1_2_block.shape_generic(); + let ref ___na_stack_2_0_block = e; + let ___na_stack_2_0_shape = ___na_stack_2_0_block.shape_generic(); + let ___na_stack_row_0_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfRows < _ , _ >> :: representative (___na_stack_0_0_shape . 0 , ___na_stack_0_2_shape . 0) . expect ("All blocks in block row 0 must have the same number of rows") ; + let ___na_stack_row_0_offset = 0; + let ___na_stack_row_1_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfRows < _ , _ >> :: representative (___na_stack_1_1_shape . 0 , ___na_stack_1_2_shape . 0) . expect ("All blocks in block row 1 must have the same number of rows") ; + let ___na_stack_row_1_offset = ___na_stack_row_0_offset + <_ as nalgebra::Dim>::value(&___na_stack_row_0_dim); + let ___na_stack_row_2_dim = ___na_stack_2_0_shape.0; + let ___na_stack_row_2_offset = ___na_stack_row_1_offset + <_ as nalgebra::Dim>::value(&___na_stack_row_1_dim); + let ___na_stack_col_0_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfColumns < _ , _ >> :: representative (___na_stack_0_0_shape . 1 , ___na_stack_2_0_shape . 1) . expect ("All blocks in block column 0 must have the same number of columns") ; + let ___na_stack_col_0_offset = 0; + let ___na_stack_col_1_dim = ___na_stack_1_1_shape.1; + let ___na_stack_col_1_offset = ___na_stack_col_0_offset + <_ as nalgebra::Dim>::value(&___na_stack_col_0_dim); + let ___na_stack_col_2_dim = < nalgebra :: constraint :: ShapeConstraint as nalgebra :: constraint :: SameNumberOfColumns < _ , _ >> :: representative (___na_stack_0_2_shape . 1 , ___na_stack_1_2_shape . 1) . expect ("All blocks in block column 2 must have the same number of columns") ; + let ___na_stack_col_2_offset = ___na_stack_col_1_offset + <_ as nalgebra::Dim>::value(&___na_stack_col_1_dim); let mut matrix = nalgebra::Matrix::zeros_generic( <_ as nalgebra::DimAdd<_>>::add( - <_ as nalgebra::DimAdd<_>>::add(_stack_row_0_dim, _stack_row_1_dim), - _stack_row_2_dim + <_ as nalgebra::DimAdd<_>>::add(___na_stack_row_0_dim, ___na_stack_row_1_dim), + ___na_stack_row_2_dim ), <_ as nalgebra::DimAdd<_>>::add( - <_ as nalgebra::DimAdd<_>>::add(_stack_col_0_dim, _stack_col_1_dim), - _stack_col_2_dim + <_ as nalgebra::DimAdd<_>>::add(___na_stack_col_0_dim, ___na_stack_col_1_dim), + ___na_stack_col_2_dim ) ); - let start = (_stack_row_0_offset, _stack_col_0_offset); - let shape = (_stack_row_0_dim, _stack_col_0_dim); - let input_view = _stack_0_0_block.generic_view((0,0), shape); + let start = (___na_stack_row_0_offset, ___na_stack_col_0_offset); + let shape = (___na_stack_row_0_dim, ___na_stack_col_0_dim); + let input_view = ___na_stack_0_0_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); - let start = (_stack_row_0_offset, _stack_col_2_offset); - let shape = (_stack_row_0_dim, _stack_col_2_dim); - let input_view = _stack_0_2_block.generic_view((0,0), shape); + let start = (___na_stack_row_0_offset, ___na_stack_col_2_offset); + let shape = (___na_stack_row_0_dim, ___na_stack_col_2_dim); + let input_view = ___na_stack_0_2_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); - let start = (_stack_row_1_offset, _stack_col_1_offset); - let shape = (_stack_row_1_dim, _stack_col_1_dim); - let input_view = _stack_1_1_block.generic_view((0,0), shape); + let start = (___na_stack_row_1_offset, ___na_stack_col_1_offset); + let shape = (___na_stack_row_1_dim, ___na_stack_col_1_dim); + let input_view = ___na_stack_1_1_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); - let start = (_stack_row_1_offset, _stack_col_2_offset); - let shape = (_stack_row_1_dim, _stack_col_2_dim); - let input_view = _stack_1_2_block.generic_view((0,0), shape); + let start = (___na_stack_row_1_offset, ___na_stack_col_2_offset); + let shape = (___na_stack_row_1_dim, ___na_stack_col_2_dim); + let input_view = ___na_stack_1_2_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); - let start = (_stack_row_2_offset, _stack_col_0_offset); - let shape = (_stack_row_2_dim, _stack_col_0_dim); - let input_view = _stack_2_0_block.generic_view((0,0), shape); + let start = (___na_stack_row_2_offset, ___na_stack_col_0_offset); + let shape = (___na_stack_row_2_dim, ___na_stack_col_0_dim); + let input_view = ___na_stack_2_0_block.generic_view((0,0), shape); let mut output_view = matrix.generic_view_mut(start, shape); output_view.copy_from(&input_view); matrix