diff --git a/Cargo.toml b/Cargo.toml index 24e52ef..1a963c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,7 +50,7 @@ strip-ansi-escapes = "0.2.0" [features] default = ["derive"] derive = ["dep:miette-derive"] -perfect-derive = ["dep:miette-derive","miette-derive?/perfect-derive"] +perfect-derive = ["derive","miette-derive?/perfect-derive"] no-format-args-capture = [] fancy-base = [ "dep:owo-colors", diff --git a/miette-derive/Cargo.toml b/miette-derive/Cargo.toml index ae8c3b6..cddb729 100644 --- a/miette-derive/Cargo.toml +++ b/miette-derive/Cargo.toml @@ -16,4 +16,4 @@ perfect-derive = ["syn/extra-traits"] [dependencies] proc-macro2 = "1.0.83" quote = "1.0.35" -syn = { version = "2.0.87", features = ["extra-traits"] } +syn = "2.0.87" diff --git a/miette-derive/src/trait_bounds/mock_store.rs b/miette-derive/src/trait_bounds/mock_store.rs new file mode 100644 index 0000000..3074a31 --- /dev/null +++ b/miette-derive/src/trait_bounds/mock_store.rs @@ -0,0 +1,24 @@ +#![allow(dead_code)] +use syn::{punctuated::Punctuated, Generics, PredicateType, Token, WhereClause, WherePredicate}; + +// Mock for when perfect-derive is not enabled, +// this should be completely optimized away and enables +// easily switching on/off the perfect-derive feature without +// needing to modify any other code. +pub struct TypeParamBoundStore; + +impl TypeParamBoundStore { + pub fn new(_: &Generics) -> Self { + Self + } + + pub fn add_predicate(&mut self, _: PredicateType) {} + + pub fn add_where_predicate(&mut self, _: WherePredicate) {} + + pub fn extend_where_predicates(&mut self, _: Punctuated) {} + + pub fn add_to_where_clause(&self, where_clause: Option<&WhereClause>) -> Option { + where_clause.cloned() + } +} diff --git a/miette-derive/src/trait_bounds/mod.rs b/miette-derive/src/trait_bounds/mod.rs new file mode 100644 index 0000000..cb33efc --- /dev/null +++ b/miette-derive/src/trait_bounds/mod.rs @@ -0,0 +1,9 @@ +#[cfg(not(feature = "perfect-derive"))] +mod mock_store; +#[cfg(not(feature = "perfect-derive"))] +pub use mock_store::TypeParamBoundStore; + +#[cfg(feature = "perfect-derive")] +mod store; +#[cfg(feature = "perfect-derive")] +pub use store::TypeParamBoundStore; diff --git a/miette-derive/src/trait_bounds.rs b/miette-derive/src/trait_bounds/store.rs similarity index 97% rename from miette-derive/src/trait_bounds.rs rename to miette-derive/src/trait_bounds/store.rs index 8894f3b..c7c5d17 100644 --- a/miette-derive/src/trait_bounds.rs +++ b/miette-derive/src/trait_bounds/store.rs @@ -12,9 +12,13 @@ use syn::{ }; // Potential improvement, although idk if this actually ends up -// mattering is to switch this to something like FxHashMap like the rustc compiler uses internally +// mattering (if it is a messurable improvement) is to switch this to something like FxHashMap +// like the rustc compiler uses internally, although we should benchmark this and can always do it later +// since it is easy enough to change. +#[cfg(feature = "perfect-derive")] pub struct TypeParamBoundStore(HashMap<(Option, Type), HashSet>); +#[cfg(feature = "perfect-derive")] impl TypeParamBoundStore { /// Creates a new TraitBoundStore, filling it with some generics which are used to heuristically remove trivial bounds. /// diff --git a/tests/test_derive_attr.rs b/tests/test_derive_attr.rs index 44c520a..f80d01b 100644 --- a/tests/test_derive_attr.rs +++ b/tests/test_derive_attr.rs @@ -146,6 +146,7 @@ fn attr_not_required() { assert_eq!(err_span, expectation); } +// Tests for the feature = "perfect-derive". fn assert_impl_diagnostic() {} #[test]