Make absence of perfect-derive feature on miette-derive actually disable adding trait bounds

Signed-off-by: Justus Flügel <justusfluegel@gmail.com>
This commit is contained in:
Justus Flügel 2025-04-28 12:01:57 +02:00 committed by Justus Fluegel
parent bc7b842ca3
commit 8c25ebde1e
No known key found for this signature in database
GPG Key ID: DD4B1903FEACCC4D
6 changed files with 41 additions and 3 deletions

View File

@ -50,7 +50,7 @@ strip-ansi-escapes = "0.2.0"
[features] [features]
default = ["derive"] default = ["derive"]
derive = ["dep:miette-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 = [] no-format-args-capture = []
fancy-base = [ fancy-base = [
"dep:owo-colors", "dep:owo-colors",

View File

@ -16,4 +16,4 @@ perfect-derive = ["syn/extra-traits"]
[dependencies] [dependencies]
proc-macro2 = "1.0.83" proc-macro2 = "1.0.83"
quote = "1.0.35" quote = "1.0.35"
syn = { version = "2.0.87", features = ["extra-traits"] } syn = "2.0.87"

View File

@ -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<WherePredicate, Token![,]>) {}
pub fn add_to_where_clause(&self, where_clause: Option<&WhereClause>) -> Option<WhereClause> {
where_clause.cloned()
}
}

View File

@ -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;

View File

@ -12,9 +12,13 @@ use syn::{
}; };
// Potential improvement, although idk if this actually ends up // 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<BoundLifetimes>, Type), HashSet<TypeParamBound>>); pub struct TypeParamBoundStore(HashMap<(Option<BoundLifetimes>, Type), HashSet<TypeParamBound>>);
#[cfg(feature = "perfect-derive")]
impl TypeParamBoundStore { impl TypeParamBoundStore {
/// Creates a new TraitBoundStore, filling it with some generics which are used to heuristically remove trivial bounds. /// Creates a new TraitBoundStore, filling it with some generics which are used to heuristically remove trivial bounds.
/// ///

View File

@ -146,6 +146,7 @@ fn attr_not_required() {
assert_eq!(err_span, expectation); assert_eq!(err_span, expectation);
} }
// Tests for the feature = "perfect-derive".
fn assert_impl_diagnostic<T: Diagnostic>() {} fn assert_impl_diagnostic<T: Diagnostic>() {}
#[test] #[test]