Skip to content

Commit

Permalink
Don't use deprecated diagnostic library.
Browse files Browse the repository at this point in the history
  • Loading branch information
someguynamedjosh committed Dec 26, 2023
1 parent 4890f78 commit e06c4b0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ouroboros_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
15 changes: 11 additions & 4 deletions ouroboros_macro/src/generate/with_each.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<TokenStream>, Error> {
pub enum ProcessingError {
Syntax(Error),
Covariance(Vec<Diagnostic>),
}

pub fn make_with_functions(info: &StructInfo, options: Options) -> (Vec<TokenStream>, Vec<Diagnostic>) {
let mut users = Vec::new();
let mut errors = Vec::new();
for field in &info.fields {
let visibility = &field.vis;
let field_name = &field.name;
Expand Down Expand Up @@ -50,7 +57,7 @@ pub fn make_with_functions(info: &StructInfo, options: Options) -> Result<Vec<To
}
});
} else if field.covariant.is_none() {
field.covariance_error();
errors.push(field.covariance_error());
}
// If it is not borrowed at all it's safe to allow mutably borrowing it.
let user_name = format_ident!("with_{}_mut", &field.name);
Expand Down Expand Up @@ -113,7 +120,7 @@ pub fn make_with_functions(info: &StructInfo, options: Options) -> Result<Vec<To
// Skip the other functions, they will cause compiler errors.
continue;
} else if field.covariant.is_none() {
field.covariance_error();
errors.push(field.covariance_error());
}
}
let borrower_name = format_ident!("borrow_{}", &field.name);
Expand All @@ -131,5 +138,5 @@ pub fn make_with_functions(info: &StructInfo, options: Options) -> Result<Vec<To
// to get any other kinds of references to it.
}
}
Ok(users)
(users, errors)
}
8 changes: 5 additions & 3 deletions ouroboros_macro/src/info_structures.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::utils::{make_generic_arguments, make_generic_consumers, replace_this_with_lifetime};
use proc_macro2::{Ident, TokenStream};
use proc_macro2_diagnostics::{Diagnostic, SpanDiagnosticExt};
use quote::{format_ident, quote, ToTokens};
use syn::{
punctuated::Punctuated, token::Comma, Attribute, ConstParam, Error, GenericParam, Generics,
LifetimeParam, Type, TypeParam, Visibility,
LifetimeParam, Type, TypeParam, Visibility, spanned::Spanned,
};

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -213,7 +214,8 @@ impl StructFieldInfo {

/// Generates an error requesting that the user explicitly specify whether or not the
/// field's type is covariant.
pub fn covariance_error(&self) {
#[must_use]
pub fn covariance_error(&self) -> 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",
Expand All @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions ouroboros_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit e06c4b0

Please sign in to comment.