Skip to content

Commit 319559f

Browse files
committed
Deprecate string path
1 parent 014e044 commit 319559f

File tree

19 files changed

+161
-137
lines changed

19 files changed

+161
-137
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct Example1<T>(PhantomData<T>);
3535
If using a different package name, you must specify this:
3636

3737
```rust
38-
#[derive_where(crate = "derive_where_")]
38+
#[derive_where(crate = derive_where_)]
3939
#[derive_where(Clone, Debug)]
4040
struct Example<T>(PhantomData<T>);
4141
```
@@ -209,7 +209,7 @@ that would break their invariants.
209209
is to avoid ambiguity between another method also called `zeroize`.
210210

211211
```rust
212-
#[derive_where(Zeroize(crate = "zeroize_"))]
212+
#[derive_where(Zeroize(crate = zeroize_))]
213213
struct Example(#[derive_where(Zeroize(fqs))] i32);
214214

215215
impl Example {
@@ -242,7 +242,7 @@ and can be implemented without [`Zeroize`], otherwise it only implements
242242
crate in case of a re-export or rename.
243243

244244
```rust
245-
#[derive_where(ZeroizeOnDrop(crate = "zeroize_"))]
245+
#[derive_where(ZeroizeOnDrop(crate = zeroize_))]
246246
struct Example(i32);
247247

248248
assert!(core::mem::needs_drop::<Example>());

src/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'a> Input<'a> {
174174

175175
#[cfg(feature = "zeroize")]
176176
{
177-
// `Zeroize(crate = "..")` or `ZeroizeOnDrop(crate = "..")` is used.
177+
// `Zeroize(crate = ..)` or `ZeroizeOnDrop(crate = ..)` is used.
178178
if let DeriveTrait::Zeroize { crate_: Some(_) }
179179
| DeriveTrait::ZeroizeOnDrop { crate_: Some(_) } = *trait_
180180
{

src/lib.rs

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
//! # extern crate derive_where as derive_where_;
4242
//! # use std::marker::PhantomData;
4343
//! # use derive_where::derive_where;
44-
//! #[derive_where(crate = "derive_where_")]
44+
//! #[derive_where(crate = derive_where_)]
4545
//! #[derive_where(Clone, Debug)]
4646
//! struct Example<T>(PhantomData<T>);
4747
//! ```
@@ -237,7 +237,7 @@
237237
//! # use std::marker::PhantomData;
238238
//! # use derive_where::derive_where;
239239
//! # use zeroize_::Zeroize;
240-
//! #[derive_where(Zeroize(crate = "zeroize_"))]
240+
//! #[derive_where(Zeroize(crate = zeroize_))]
241241
//! struct Example(#[derive_where(Zeroize(fqs))] i32);
242242
//!
243243
//! impl Example {
@@ -275,7 +275,7 @@
275275
//! # {
276276
//! # use std::marker::PhantomData;
277277
//! # use derive_where::derive_where;
278-
//! #[derive_where(ZeroizeOnDrop(crate = "zeroize_"))]
278+
//! #[derive_where(ZeroizeOnDrop(crate = zeroize_))]
279279
//! struct Example(i32);
280280
//!
281281
//! assert!(core::mem::needs_drop::<Example>());
@@ -394,8 +394,8 @@ use proc_macro2::TokenStream;
394394
use quote::{quote, quote_spanned, ToTokens};
395395
use syn::{
396396
punctuated::Punctuated, spanned::Spanned, Attribute, DataEnum, DataStruct, DataUnion,
397-
DeriveInput, Expr, ExprLit, Fields, FieldsNamed, FieldsUnnamed, Generics, Lit, Meta, Path,
398-
Token, Variant,
397+
DeriveInput, Expr, ExprLit, ExprPath, Fields, FieldsNamed, FieldsUnnamed, Generics, Lit, Meta,
398+
Path, Token, Variant,
399399
};
400400

401401
#[cfg(feature = "zeroize")]
@@ -421,13 +421,12 @@ const DERIVE_WHERE_FORWARD: &str = "DeriveWhere";
421421
const DERIVE_WHERE_VISITED: &str = "derive_where_visited";
422422

423423
/// Item-level options:
424-
/// - `#[derive_where(crate = "path")]`: Specify path to the `derive_where`
425-
/// crate.
424+
/// - `#[derive_where(crate = path)]`: Specify path to the `derive_where` crate.
426425
/// - `#[derive_where(Clone, ..; T, ..)]`: Specify traits to implement and
427426
/// optionally bounds.
428-
/// - `#[derive_where(Zeroize(crate = "path"))]`: Specify path to [`Zeroize`]
427+
/// - `#[derive_where(Zeroize(crate = path))]`: Specify path to [`Zeroize`]
429428
/// trait.
430-
/// - `#[derive_where(ZeroizeOnDrop(crate = "path"))]`: Specify path to
429+
/// - `#[derive_where(ZeroizeOnDrop(crate = path))]`: Specify path to
431430
/// [`ZeroizeOnDrop`] trait.
432431
/// - `#[derive_where(skip_inner(EqHashOrd, ..))]`: Skip all fields in the item.
433432
/// Optionally specify trait groups to constrain skipping fields. Only works
@@ -492,36 +491,35 @@ fn derive_where_internal(mut item: DeriveInput) -> Result<TokenStream, syn::Erro
492491

493492
if meta.path().is_ident("crate") {
494493
if let Meta::NameValue(name_value) = meta {
495-
if let Expr::Lit(ExprLit {
496-
lit: Lit::Str(lit_str),
497-
..
498-
}) = &name_value.value
499-
{
500-
match lit_str.parse::<Path>() {
501-
Ok(path) => {
502-
if path == util::path_from_strs(&[DERIVE_WHERE]) {
503-
return Err(Error::path_unnecessary(
504-
path.span(),
505-
&format!("::{}", DERIVE_WHERE),
506-
));
507-
}
508-
509-
match crate_ {
510-
Some(_) => {
511-
return Err(Error::option_duplicate(
512-
name_value.span(),
513-
"crate",
514-
))
515-
}
516-
None => crate_ = Some(path),
517-
}
518-
}
494+
let path = match &name_value.value {
495+
Expr::Lit(ExprLit {
496+
lit: Lit::Str(lit_str),
497+
..
498+
}) => match lit_str.parse::<Path>() {
499+
Ok(path) => path,
519500
Err(error) => {
520501
return Err(Error::path(lit_str.span(), error))
521502
}
503+
},
504+
Expr::Path(ExprPath { path, .. }) => path.clone(),
505+
_ => return Err(Error::option_syntax(name_value.value.span())),
506+
};
507+
508+
if path == util::path_from_strs(&[DERIVE_WHERE]) {
509+
return Err(Error::path_unnecessary(
510+
path.span(),
511+
&format!("::{}", DERIVE_WHERE),
512+
));
513+
}
514+
515+
match crate_ {
516+
Some(_) => {
517+
return Err(Error::option_duplicate(
518+
name_value.span(),
519+
"crate",
520+
))
522521
}
523-
} else {
524-
return Err(Error::option_syntax(name_value.value.span()));
522+
None => crate_ = Some(path),
525523
}
526524
} else {
527525
return Err(Error::option_syntax(meta.span()));

src/test/use_case.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn default() -> Result<()> {
131131
#[cfg(feature = "zeroize")]
132132
fn zeroize_crate() -> Result<()> {
133133
compiles(quote! {
134-
#[derive_where(Zeroize(crate = "zeroize_"))]
134+
#[derive_where(Zeroize(crate = zeroize_))]
135135
struct Test(u8);
136136
})
137137
}

src/test/zeroize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn both() -> Result<()> {
133133
fn crate_() -> Result<()> {
134134
test_derive(
135135
quote! {
136-
#[derive_where(Zeroize(crate = "zeroize_"); T)]
136+
#[derive_where(Zeroize(crate = zeroize_); T)]
137137
struct Test<T>(T);
138138
},
139139
quote! {
@@ -158,7 +158,7 @@ fn crate_() -> Result<()> {
158158
fn crate_drop() -> Result<()> {
159159
test_derive(
160160
quote! {
161-
#[derive_where(ZeroizeOnDrop(crate = "zeroize_"); T)]
161+
#[derive_where(ZeroizeOnDrop(crate = zeroize_); T)]
162162
struct Test<T>(T);
163163
},
164164
#[cfg(not(feature = "zeroize-on-drop"))]

src/trait_/zeroize.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use proc_macro2::{Span, TokenStream};
44
use quote::quote;
55
use syn::{
6-
punctuated::Punctuated, spanned::Spanned, Expr, ExprLit, Lit, Meta, Path, Result, Token,
6+
punctuated::Punctuated, spanned::Spanned, Expr, ExprLit, ExprPath, Lit, Meta, Path, Result,
7+
Token,
78
};
89

910
use crate::{util, Data, DeriveTrait, Error, Item, SimpleType, TraitImpl};
@@ -43,27 +44,23 @@ impl TraitImpl for Zeroize {
4344
if name_value.path.is_ident("crate") {
4445
// Check for duplicate `crate` option.
4546
if crate_.is_none() {
46-
if let Expr::Lit(ExprLit {
47-
lit: Lit::Str(lit_str),
48-
..
49-
}) = &name_value.value
50-
{
51-
match lit_str.parse::<Path>() {
52-
Ok(path) => {
53-
if path == util::path_from_strs(&["zeroize"]) {
54-
return Err(Error::path_unnecessary(
55-
path.span(),
56-
"::zeroize",
57-
));
58-
}
59-
60-
crate_ = Some(path);
61-
}
47+
let path = match &name_value.value {
48+
Expr::Lit(ExprLit {
49+
lit: Lit::Str(lit_str),
50+
..
51+
}) => match lit_str.parse::<Path>() {
52+
Ok(path) => path,
6253
Err(error) => return Err(Error::path(lit_str.span(), error)),
63-
}
64-
} else {
65-
return Err(Error::option_syntax(name_value.value.span()));
54+
},
55+
Expr::Path(ExprPath { path, .. }) => path.clone(),
56+
_ => return Err(Error::option_syntax(name_value.value.span())),
57+
};
58+
59+
if path == util::path_from_strs(&["zeroize"]) {
60+
return Err(Error::path_unnecessary(path.span(), "::zeroize"));
6661
}
62+
63+
crate_ = Some(path);
6764
} else {
6865
return Err(Error::option_duplicate(name_value.span(), "crate"));
6966
}

src/trait_/zeroize_on_drop.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use proc_macro2::{Span, TokenStream};
44
use quote::quote;
55
use syn::{
6-
punctuated::Punctuated, spanned::Spanned, Expr, ExprLit, Lit, Meta, Path, Result, Token,
6+
punctuated::Punctuated, spanned::Spanned, Expr, ExprLit, ExprPath, Lit, Meta, Path, Result,
7+
Token,
78
};
89

910
use crate::{util, Data, DeriveTrait, Error, Item, SimpleType, TraitImpl};
@@ -37,27 +38,23 @@ impl TraitImpl for ZeroizeOnDrop {
3738
if name_value.path.is_ident("crate") {
3839
// Check for duplicate `crate` option.
3940
if crate_.is_none() {
40-
if let Expr::Lit(ExprLit {
41-
lit: Lit::Str(lit_str),
42-
..
43-
}) = &name_value.value
44-
{
45-
match lit_str.parse::<Path>() {
46-
Ok(path) => {
47-
if path == util::path_from_strs(&["zeroize"]) {
48-
return Err(Error::path_unnecessary(
49-
path.span(),
50-
"::zeroize",
51-
));
52-
}
53-
54-
crate_ = Some(path);
55-
}
41+
let path = match &name_value.value {
42+
Expr::Lit(ExprLit {
43+
lit: Lit::Str(lit_str),
44+
..
45+
}) => match lit_str.parse::<Path>() {
46+
Ok(path) => path,
5647
Err(error) => return Err(Error::path(lit_str.span(), error)),
57-
}
58-
} else {
59-
return Err(Error::option_syntax(name_value.value.span()));
48+
},
49+
Expr::Path(ExprPath { path, .. }) => path.clone(),
50+
_ => return Err(Error::option_syntax(name_value.value.span())),
51+
};
52+
53+
if path == util::path_from_strs(&["zeroize"]) {
54+
return Err(Error::path_unnecessary(path.span(), "::zeroize"));
6055
}
56+
57+
crate_ = Some(path);
6158
} else {
6259
return Err(Error::option_duplicate(name_value.span(), "crate"));
6360
}

test-crates/crate_/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ use core::marker::PhantomData;
44

55
use derive_where_::derive_where;
66

7+
#[derive_where(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
8+
#[cfg_attr(feature = "zeroize", derive_where(Zeroize(crate = zeroize_)))]
9+
#[derive_where(crate = derive_where_)]
10+
pub struct Test<T>(PhantomData<T>);
11+
712
#[derive_where(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
813
#[cfg_attr(feature = "zeroize", derive_where(Zeroize(crate = "zeroize_")))]
914
#[derive_where(crate = "derive_where_")]
10-
pub struct Test<T>(PhantomData<T>);
15+
pub struct TestDeprecated<T>(PhantomData<T>);

tests/ui/item.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ struct EmptyAttribute<T>(PhantomData<T>);
1414
struct WrongCrateSyntax<T>(PhantomData<T>);
1515

1616
#[derive_where(crate = "struct Test")]
17-
struct InvalidPath<T>(PhantomData<T>);
17+
struct InvalidPathDeprecated<T>(PhantomData<T>);
1818

19-
// The error message here shows that `crate = ".."` should be in it's own
19+
// The error message here shows that `crate = ..` should be in it's own
2020
// attribute instead of an error pointing out this is duplicate. This is not
2121
// ideal but much less complicated to implement.
22-
#[derive_where(crate = "derive_where_", crate = "derive_where_")]
22+
#[derive_where(crate = derive_where_, crate = derive_where_)]
2323
struct DuplicateCrate1<T>(PhantomData<T>);
2424

25-
#[derive_where(crate = "derive_where_")]
26-
#[derive_where(crate = "derive_where_")]
25+
#[derive_where(crate = derive_where_)]
26+
#[derive_where(crate = derive_where_)]
2727
struct DuplicateCrate2<T>(PhantomData<T>);
2828

29-
#[derive_where(crate = "derive_where_")]
29+
#[derive_where(crate = derive_where_)]
3030
struct OnlyCrate<T>(PhantomData<T>);
3131

32-
#[derive_where(crate = "::derive_where")]
32+
#[derive_where(crate = ::derive_where)]
3333
struct DefaultCrate<T>(PhantomData<T>);
3434

3535
#[derive_where(Debug = invalid; T)]

tests/ui/item.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ error: expected path, expected identifier
2929
error: the `crate` option has to be defined in it's own `#[derive_where(..)` attribute
3030
--> tests/ui/item.rs:22:16
3131
|
32-
22 | #[derive_where(crate = "derive_where_", crate = "derive_where_")]
32+
22 | #[derive_where(crate = derive_where_, crate = derive_where_)]
3333
| ^^^^^
3434

3535
error: duplicate `crate` option
3636
--> tests/ui/item.rs:26:16
3737
|
38-
26 | #[derive_where(crate = "derive_where_")]
39-
| ^^^^^^^^^^^^^^^^^^^^^^^
38+
26 | #[derive_where(crate = derive_where_)]
39+
| ^^^^^^^^^^^^^^^^^^^^^
4040

4141
error: no traits found to implement, use `#[derive_where(..)` to specify some
4242
--> tests/ui/item.rs:30:1
@@ -47,8 +47,8 @@ error: no traits found to implement, use `#[derive_where(..)` to specify some
4747
error: unnecessary path qualification, `::derive_where` is used by default
4848
--> tests/ui/item.rs:32:24
4949
|
50-
32 | #[derive_where(crate = "::derive_where")]
51-
| ^^^^^^^^^^^^^^^^
50+
32 | #[derive_where(crate = ::derive_where)]
51+
| ^^^^^^^^^^^^^^
5252

5353
error: unexpected option syntax
5454
--> tests/ui/item.rs:35:16

0 commit comments

Comments
 (0)