From 39b9f59fe710ba8541d8231cba63db689ba5d20a Mon Sep 17 00:00:00 2001 From: Martin Dahl Date: Wed, 13 Dec 2023 14:52:32 +0100 Subject: [PATCH] Fix elided_lifetimes_in_associated_constant compiler warning. Will become an error in a future rust release. https://github.com/rust-lang/rust/issues/115010 --- r2r_msg_gen/src/lib.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/r2r_msg_gen/src/lib.rs b/r2r_msg_gen/src/lib.rs index e019d1f..26c3d19 100644 --- a/r2r_msg_gen/src/lib.rs +++ b/r2r_msg_gen/src/lib.rs @@ -700,10 +700,21 @@ pub fn generate_rust_msg(module_: &str, prefix_: &str, name_: &str) -> proc_macr .into_iter() .flatten() .map(|(const_name, typ)| { - let typ: Box = syn::parse_str(typ).unwrap(); let const_name = format_ident!("{const_name}"); let value = format_ident!("{key}__{const_name}"); - quote! { pub const #const_name: #typ = #value; } + if let Ok(mut typ) = syn::parse_str::>(typ) { + // If the constant is a reference, rustc needs it to be static. + // (see https://github.com/rust-lang/rust/issues/115010) + typ.lifetime = Some(syn::Lifetime::new("'static", proc_macro2::Span::call_site())); + quote! { pub const #const_name: #typ = #value; } + } + else if let Ok(typ) = syn::parse_str::>(typ) { + // Value + quote! { pub const #const_name: #typ = #value; } + } else { + // Something else, hope for the best but will most likely fail to compile. + quote! { pub const #const_name: #typ = #value; } + } }) .collect();