Skip to content

Commit

Permalink
Merge pull request #30 from kas-gui/work2
Browse files Browse the repository at this point in the history
Fix autoimpl for traits: copy cfg attrs for trait items
  • Loading branch information
dhardy authored Dec 16, 2022
2 parents daa26b2 + f70a5ff commit 454e7ae
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.6.2], `impl-tools-lib` [0.7.1] — 2022-12-16

- Fix `#[autoimpl]` on traits: copy `#[cfg(..)]` attributes (#30)

## [0.6.1], `impl-tools-lib` [0.7.0] — 2022-12-01

- Better diagnostics for trait-redefinition: require `Deref` bound (#28)
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "impl-tools"
version = "0.6.1"
version = "0.6.2"
authors = ["Diggory Hardy <[email protected]>"]
edition = "2021"
license = "MIT/Apache-2.0"
Expand All @@ -21,7 +21,7 @@ proc-macro-error = "1.0"
version = "1.0.14"

[dependencies.impl-tools-lib]
version = "0.7.0"
version = "0.7.1"
path = "lib"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "impl-tools-lib"
version = "0.7.0"
version = "0.7.1"
authors = ["Diggory Hardy <[email protected]>"]
edition = "2021"
license = "MIT/Apache-2.0"
Expand Down
18 changes: 18 additions & 0 deletions lib/src/for_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ impl ForDeref {
for item in &trait_def.items {
match item {
TraitItem::Const(item) => {
for attr in item.attrs.iter() {
if attr.path == parse_quote! { cfg } {
attr.to_tokens(tokens);
}
}

item.const_token.to_tokens(tokens);
item.ident.to_tokens(tokens);
item.colon_token.to_tokens(tokens);
Expand All @@ -194,6 +200,12 @@ impl ForDeref {
item.semi_token.to_tokens(tokens);
}
TraitItem::Method(item) => {
for attr in item.attrs.iter() {
if attr.path == parse_quote! { cfg } {
attr.to_tokens(tokens);
}
}

if has_bound_on_self(&item.sig.generics) {
// If the method has a bound on Self, we cannot use a dereferencing
// implementation since the definitive type is not guaranteed to match
Expand Down Expand Up @@ -242,6 +254,12 @@ impl ForDeref {
} });
}
TraitItem::Type(item) => {
for attr in item.attrs.iter() {
if attr.path == parse_quote! { cfg } {
attr.to_tokens(tokens);
}
}

if has_bound_on_self(&item.generics) {
emit_call_site_error!(
"cannot autoimpl trait with Deref";
Expand Down
9 changes: 9 additions & 0 deletions tests/for_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,12 @@ fn custom_deref_target() {
z.increment();
assert_eq!(y, 12);
}

#[autoimpl(for<T: trait + ?Sized> &T)]
trait Cfgs {
#[cfg(test)]
fn included(&self);

#[cfg(feature = "never")]
fn excluded(&self);
}

0 comments on commit 454e7ae

Please sign in to comment.