From e06c4b0b5d68e4dab8e721863d6b2407594f4a2d Mon Sep 17 00:00:00 2001 From: joshua-maros <60271685+joshua-maros@users.noreply.github.com> Date: Tue, 26 Dec 2023 14:32:19 -0800 Subject: [PATCH] Don't use deprecated diagnostic library. --- ouroboros_macro/Cargo.toml | 2 +- ouroboros_macro/src/generate/with_each.rs | 15 +++++++++++---- ouroboros_macro/src/info_structures.rs | 8 +++++--- ouroboros_macro/src/lib.rs | 10 +++++++--- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/ouroboros_macro/Cargo.toml b/ouroboros_macro/Cargo.toml index c945ebb..104c269 100644 --- a/ouroboros_macro/Cargo.toml +++ b/ouroboros_macro/Cargo.toml @@ -15,7 +15,7 @@ proc-macro = true heck = "0.4.1" itertools = "0.12.0" proc-macro2 = "1.0" -proc-macro-error = "1.0.4" +proc-macro2-diagnostics = "0.10" quote = "1.0" syn = { version = "2.0", features = ["full"] } diff --git a/ouroboros_macro/src/generate/with_each.rs b/ouroboros_macro/src/generate/with_each.rs index c6a5431..38a663a 100644 --- a/ouroboros_macro/src/generate/with_each.rs +++ b/ouroboros_macro/src/generate/with_each.rs @@ -1,10 +1,17 @@ use crate::info_structures::{FieldType, Options, StructInfo}; use proc_macro2::TokenStream; +use proc_macro2_diagnostics::Diagnostic; use quote::{format_ident, quote}; use syn::Error; -pub fn make_with_functions(info: &StructInfo, options: Options) -> Result, Error> { +pub enum ProcessingError { + Syntax(Error), + Covariance(Vec), +} + +pub fn make_with_functions(info: &StructInfo, options: Options) -> (Vec, Vec) { let mut users = Vec::new(); + let mut errors = Vec::new(); for field in &info.fields { let visibility = &field.vis; let field_name = &field.name; @@ -50,7 +57,7 @@ pub fn make_with_functions(info: &StructInfo, options: Options) -> Result Result Result Diagnostic { let error = concat!( "Ouroboros cannot automatically determine if this type is covariant.\n\n", "As an example, a Box<&'this ()> is covariant because it can be used as a\n", @@ -224,7 +226,7 @@ impl StructFieldInfo { "guarantee means the value is not covariant.\n\n", "To resolve this error, add #[covariant] or #[not_covariant] to the field.\n", ); - proc_macro_error::emit_error!(self.typ, error); + self.typ.span().error(error) } pub fn make_constructor_arg_type_impl( diff --git a/ouroboros_macro/src/lib.rs b/ouroboros_macro/src/lib.rs index 2589043..0aa26ca 100644 --- a/ouroboros_macro/src/lib.rs +++ b/ouroboros_macro/src/lib.rs @@ -22,10 +22,10 @@ use generate::{ }; use heck::ToSnakeCase; use info_structures::BuilderType; +use itertools::Itertools; use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use proc_macro2::TokenTree; -use proc_macro_error::proc_macro_error; use quote::{format_ident, quote}; use syn::{Error, ItemStruct}; @@ -61,7 +61,11 @@ fn self_referencing_impl( async_send_try_constructor_def, ) = create_try_builder_and_constructor(&info, options, BuilderType::AsyncSend)?; - let with_defs = make_with_functions(&info, options)?; + let (with_defs, with_errors) = make_with_functions(&info, options); + let with_errors = with_errors + .into_iter() + .map(|err| err.emit_as_expr_tokens()) + .collect_vec(); let (with_all_struct_def, with_all_fn_def) = make_with_all_function(&info, options)?; let (with_all_mut_struct_def, with_all_mut_fn_def) = make_with_all_mut_function(&info, options)?; @@ -99,6 +103,7 @@ fn self_referencing_impl( #async_send_try_builder_def #with_all_struct_def #with_all_mut_struct_def + #(#with_errors)* #heads_struct_def #impls impl <#generic_params> #struct_name <#(#generic_args),*> #generic_where { @@ -125,7 +130,6 @@ fn self_referencing_impl( })) } -#[proc_macro_error] #[proc_macro_attribute] pub fn self_referencing(attr: TokenStream, item: TokenStream) -> TokenStream { let mut options = Options {