From ff32d81cb0e67e10ff3b780bdf79cb9c9bc2a7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justus=20Fl=C3=BCgel?= Date: Mon, 10 Mar 2025 21:36:11 +0100 Subject: [PATCH] Simplify the trait bound store and just register where clauses directly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Justus Flügel --- miette-derive/src/diagnostic.rs | 18 +- miette-derive/src/diagnostic_source.rs | 11 +- miette-derive/src/forward.rs | 12 +- miette-derive/src/label.rs | 34 +- miette-derive/src/related.rs | 18 +- miette-derive/src/source_code.rs | 20 +- miette-derive/src/trait_bounds.rs | 412 ++++++------------------- miette-derive/src/utils.rs | 35 ++- 8 files changed, 200 insertions(+), 360 deletions(-) diff --git a/miette-derive/src/diagnostic.rs b/miette-derive/src/diagnostic.rs index 629051e..df963f0 100644 --- a/miette-derive/src/diagnostic.rs +++ b/miette-derive/src/diagnostic.rs @@ -11,7 +11,7 @@ use crate::label::Labels; use crate::related::Related; use crate::severity::Severity; use crate::source_code::SourceCode; -use crate::trait_bounds::TraitBoundStore; +use crate::trait_bounds::TypeParamBoundStore; use crate::url::Url; pub enum Diagnostic { @@ -20,13 +20,13 @@ pub enum Diagnostic { ident: syn::Ident, fields: syn::Fields, args: DiagnosticDefArgs, - bound_store: TraitBoundStore, + bound_store: TypeParamBoundStore, }, Enum { ident: syn::Ident, generics: syn::Generics, variants: Vec, - bound_store: TraitBoundStore, + bound_store: TypeParamBoundStore, }, } @@ -76,7 +76,7 @@ pub struct DiagnosticConcreteArgs { impl DiagnosticConcreteArgs { fn for_fields( fields: &syn::Fields, - bounds_store: &mut TraitBoundStore, + bounds_store: &mut TypeParamBoundStore, ) -> Result { let labels = Labels::from_fields(fields, bounds_store)?; let source_code = SourceCode::from_fields(fields, bounds_store)?; @@ -162,7 +162,7 @@ impl DiagnosticDefArgs { _ident: &syn::Ident, fields: &syn::Fields, attrs: &[&syn::Attribute], - bounds_store: &mut TraitBoundStore, + bounds_store: &mut TypeParamBoundStore, allow_transparent: bool, ) -> syn::Result { let mut errors = Vec::new(); @@ -233,7 +233,7 @@ impl Diagnostic { .collect::>(); Ok(match input.data { syn::Data::Struct(data_struct) => { - let mut bounds_store = TraitBoundStore::new(&input.generics); + let mut bounds_store = TypeParamBoundStore::new(&input.generics); let args = DiagnosticDefArgs::parse( &input.ident, @@ -253,7 +253,7 @@ impl Diagnostic { } syn::Data::Enum(syn::DataEnum { variants, .. }) => { let mut vars = Vec::new(); - let mut bound_store = TraitBoundStore::new(&input.generics); + let mut bound_store = TypeParamBoundStore::new(&input.generics); for var in variants { let mut variant_attrs = input_attrs.clone(); variant_attrs @@ -297,7 +297,7 @@ impl Diagnostic { bound_store, } => { let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); - let where_clause = bound_store.merge_with(where_clause); + let where_clause = bound_store.add_to_where_clause(where_clause); match args { DiagnosticDefArgs::Transparent(forward) => { @@ -397,7 +397,7 @@ impl Diagnostic { bound_store, } => { let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); - let where_clause = bound_store.merge_with(where_clause); + let where_clause = bound_store.add_to_where_clause(where_clause); let code_body = Code::gen_enum(variants); let help_body = Help::gen_enum(variants); diff --git a/miette-derive/src/diagnostic_source.rs b/miette-derive/src/diagnostic_source.rs index 8e66e29..4fc9c28 100644 --- a/miette-derive/src/diagnostic_source.rs +++ b/miette-derive/src/diagnostic_source.rs @@ -3,7 +3,7 @@ use quote::quote; use syn::spanned::Spanned; use crate::forward::WhichFn; -use crate::trait_bounds::TraitBoundStore; +use crate::trait_bounds::TypeParamBoundStore; use crate::{ diagnostic::{DiagnosticConcreteArgs, DiagnosticDef}, utils::{display_pat_members, gen_all_variants_with}, @@ -14,7 +14,7 @@ pub struct DiagnosticSource(syn::Member); impl DiagnosticSource { pub(crate) fn from_fields( fields: &syn::Fields, - bounds_store: &mut TraitBoundStore, + bounds_store: &mut TypeParamBoundStore, ) -> syn::Result> { match fields { syn::Fields::Named(named) => { @@ -29,7 +29,7 @@ impl DiagnosticSource { fn from_fields_vec( fields: Vec<&syn::Field>, - bounds_store: &mut TraitBoundStore, + bounds_store: &mut TypeParamBoundStore, ) -> syn::Result> { for (i, field) in fields.iter().enumerate() { for attr in &field.attrs { @@ -43,7 +43,10 @@ impl DiagnosticSource { }) }; - bounds_store.register_source_usage(&field.ty); + let ty = &field.ty; + bounds_store.add_where_predicate( + syn::parse_quote!(#ty: ::miette::Diagnostic + 'static), + ); return Ok(Some(DiagnosticSource(diagnostic_source))); } diff --git a/miette-derive/src/forward.rs b/miette-derive/src/forward.rs index 11cb3bb..33d87c2 100644 --- a/miette-derive/src/forward.rs +++ b/miette-derive/src/forward.rs @@ -6,7 +6,7 @@ use syn::{ spanned::Spanned, }; -use crate::trait_bounds::TraitBoundStore; +use crate::trait_bounds::TypeParamBoundStore; pub enum Forward { Unnamed(usize), @@ -98,7 +98,7 @@ impl WhichFn { impl Forward { pub fn for_transparent_field( fields: &syn::Fields, - bounds_store: &mut TraitBoundStore, + bounds_store: &mut TypeParamBoundStore, ) -> syn::Result { let make_err = || { syn::Error::new( @@ -118,7 +118,9 @@ impl Forward { .clone() .unwrap_or_else(|| format_ident!("unnamed")); - bounds_store.register_transparent_usage(&field.ty); + let ty = &field.ty; + bounds_store + .add_where_predicate(syn::parse_quote! {#ty: ::miette::Diagnostic + 'static}); Ok(Self::Named(field_name)) } syn::Fields::Unnamed(unnamed) => { @@ -128,7 +130,9 @@ impl Forward { return Err(make_err()); } - bounds_store.register_transparent_usage(&field.ty); + let ty = &field.ty; + bounds_store + .add_where_predicate(syn::parse_quote! {#ty: ::miette::Diagnostic + 'static}); Ok(Self::Unnamed(0)) } _ => Err(syn::Error::new( diff --git a/miette-derive/src/label.rs b/miette-derive/src/label.rs index 191b7b3..3cc071b 100644 --- a/miette-derive/src/label.rs +++ b/miette-derive/src/label.rs @@ -4,15 +4,15 @@ use syn::{ parenthesized, parse::{Parse, ParseStream}, spanned::Spanned, - Token, + Lifetime, Token, }; use crate::{ diagnostic::{DiagnosticConcreteArgs, DiagnosticDef}, fmt::{self, Display}, forward::WhichFn, - trait_bounds::TraitBoundStore, - utils::{display_pat_members, gen_all_variants_with}, + trait_bounds::TypeParamBoundStore, + utils::{display_pat_members, extract_option, gen_all_variants_with}, }; pub struct Labels(Vec