Skip to content

Commit 30f2619

Browse files
committed
chore: update syn to 2
1 parent d89b838 commit 30f2619

File tree

8 files changed

+46
-37
lines changed

8 files changed

+46
-37
lines changed

.cspell.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
version: "0.2"
22
words:
33
- clippy
4+
- codegen
45
- direnv
6+
- elems
57
- msrv
68
- nextest
79
- nixos
810
- nixpkgs
911
- nonminimal
1012
- pkgs
13+
- punct
1114
- RUSTDOCFLAGS
1215
- taplo
16+
- toks

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## Unreleased
88

9+
- Update `syn` requirement to `2`.
10+
911
## 0.6.5
1012

1113
- Improve support for async functions and mutable borrows in contracts.

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ name = "contracts"
33
version = "0.6.5"
44
description = "Design-by-contract attributes"
55
authors = ["karroffel <[email protected]>"]
6-
edition = "2018"
7-
repository = "https://github.com/x52dev/contracts"
8-
license = "MPL-2.0"
96
categories = ["development-tools", "development-tools::procedural-macro-helpers"]
107
keywords = ["design-by-contract", "precondition", "postcondition", "invariant", "verification"]
8+
repository = "https://github.com/x52dev/contracts"
9+
license = "MPL-2.0"
10+
edition = "2018"
11+
rust-version = "1.65"
1112

1213
[lib]
1314
name = "contracts"
@@ -21,6 +22,6 @@ override_log = []
2122
mirai_assertions = []
2223

2324
[dependencies]
24-
proc-macro2 = "1.0"
25-
quote = "1.0"
26-
syn = { version = "1.0", features = ["extra-traits", "full", "visit", "visit-mut"] }
25+
proc-macro2 = "1"
26+
quote = "1"
27+
syn = { version = "2", features = ["extra-traits", "full", "visit", "visit-mut"] }

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
[![dependency status](https://deps.rs/crate/contracts/0.6.5/status.svg)](https://deps.rs/crate/contracts/0.6.5)
1010
![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/contracts.svg)
1111
<br />
12-
[![CI](https://github.com/x52dev/contracts-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/x52dev/contracts-rs/actions/workflows/ci.yml)
13-
[![codecov](https://codecov.io/gh/x52dev/contracts-rs/graph/badge.svg?token=OpYe6I7dj5)](https://codecov.io/gh/x52dev/contracts-rs)
12+
[![CI](https://github.com/x52dev/contracts/actions/workflows/ci.yml/badge.svg)](https://github.com/x52dev/contracts-rs/actions/workflows/ci.yml)
13+
[![codecov](https://codecov.io/gh/x52dev/contracts/graph/badge.svg?token=OpYe6I7dj5)](https://codecov.io/gh/x52dev/contracts-rs)
1414
![Version](https://img.shields.io/crates/msrv/contracts.svg)
1515
[![Download](https://img.shields.io/crates/d/contracts.svg)](https://crates.io/crates/contracts)
1616

src/implementation/invariant.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use proc_macro2::TokenStream;
66
use quote::ToTokens;
7-
use syn::{FnArg, ImplItem, ImplItemMethod, Item, ItemFn, ItemImpl};
7+
use syn::{FnArg, ImplItem, ImplItemFn, Item, ItemFn, ItemImpl};
88

99
use crate::implementation::{ContractMode, ContractType, FuncWithContracts};
1010

@@ -53,7 +53,7 @@ fn invariant_impl(
5353

5454
let invariant_ident = syn::Ident::new(&name, proc_macro2::Span::call_site());
5555

56-
fn method_uses_self(method: &ImplItemMethod) -> bool {
56+
fn method_uses_self(method: &ImplItemFn) -> bool {
5757
let inputs = &method.sig.inputs;
5858

5959
if !inputs.is_empty() {
@@ -64,7 +64,7 @@ fn invariant_impl(
6464
}
6565

6666
for item in &mut impl_def.items {
67-
if let ImplItem::Method(method) = item {
67+
if let ImplItem::Fn(method) = item {
6868
// only implement invariants for methods that take `self`
6969
if !method_uses_self(method) {
7070
continue;
@@ -75,7 +75,7 @@ fn invariant_impl(
7575
#method
7676
};
7777

78-
let met: ImplItemMethod = syn::parse_quote!(#method_toks);
78+
let met: ImplItemFn = syn::parse_quote!(#method_toks);
7979

8080
*method = met;
8181
}

src/implementation/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) mod traits;
1212

1313
pub(crate) use ensures::ensures;
1414
pub(crate) use invariant::invariant;
15-
use proc_macro2::{Span, TokenStream, TokenTree};
15+
use proc_macro2::{Span, TokenStream};
1616
use quote::ToTokens;
1717
pub(crate) use requires::requires;
1818
use syn::{Expr, ItemFn};
@@ -166,23 +166,25 @@ impl FuncWithContracts {
166166
.attrs
167167
.iter()
168168
.filter_map(|a| {
169-
let name = a.path.segments.last().unwrap().ident.to_string();
169+
let name = a.path().segments.last().unwrap().ident.to_string();
170170
let (ty, mode) = ContractType::contract_type_and_mode(&name)?;
171171
Some((ty, mode, a))
172172
})
173-
.map(|(ty, mode, a)| {
173+
.map(|(ty, mode, attr)| {
174174
// the tts on attributes contains the out parenthesis, so some
175175
// code might be mistakenly parsed as tuples, that's not good!
176176
//
177177
// this is a hack to get to the inner token stream.
178178

179-
let tok_tree = a.tokens.clone().into_iter().next().unwrap();
180-
let toks = match tok_tree {
181-
TokenTree::Group(group) => group.stream(),
182-
TokenTree::Ident(i) => i.into_token_stream(),
183-
TokenTree::Punct(p) => p.into_token_stream(),
184-
TokenTree::Literal(l) => l.into_token_stream(),
185-
};
179+
fn extract_first_arg_stream(attr: &syn::Attribute) -> TokenStream {
180+
match &attr.meta {
181+
syn::Meta::List(list) => list.tokens.clone(),
182+
syn::Meta::Path(path) => path.to_token_stream(),
183+
syn::Meta::NameValue(nv) => dbg!(nv.value.to_token_stream()),
184+
}
185+
}
186+
187+
let toks = extract_first_arg_stream(attr);
186188

187189
Contract::from_toks(ty, mode, toks)
188190
});
@@ -197,7 +199,7 @@ impl FuncWithContracts {
197199
.into_iter()
198200
.filter(|attr| {
199201
ContractType::contract_type_and_mode(
200-
&attr.path.segments.last().unwrap().ident.to_string(),
202+
&attr.path().segments.last().unwrap().ident.to_string(),
201203
)
202204
.is_none()
203205
})

src/implementation/traits.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use proc_macro2::TokenStream;
66
use quote::ToTokens;
7-
use syn::{FnArg, ImplItem, ItemImpl, ItemTrait, Pat, TraitItem, TraitItemMethod};
7+
use syn::{FnArg, ImplItem, ItemImpl, ItemTrait, Pat, TraitItem, TraitItemFn};
88

99
use crate::implementation::ContractType;
1010

@@ -16,8 +16,8 @@ fn contract_method_impl_name(name: &str) -> String {
1616
/// Modifies a trait item in a way that it includes contracts.
1717
pub(crate) fn contract_trait_item_trait(_attrs: TokenStream, mut trait_: ItemTrait) -> TokenStream {
1818
/// Just rename the method to have an internal, generated name.
19-
fn create_method_rename(method: &TraitItemMethod) -> TraitItemMethod {
20-
let mut m: TraitItemMethod = (*method).clone();
19+
fn create_method_rename(method: &TraitItemFn) -> TraitItemFn {
20+
let mut m = method.clone();
2121

2222
// rename method and modify attributes
2323
{
@@ -35,8 +35,8 @@ pub(crate) fn contract_trait_item_trait(_attrs: TokenStream, mut trait_: ItemTra
3535
new_attrs.extend(
3636
m.attrs
3737
.iter()
38-
.filter(|a| {
39-
let name = a.path.segments.last().unwrap().ident.to_string();
38+
.filter(|attr| {
39+
let name = attr.path().segments.last().unwrap().ident.to_string();
4040

4141
ContractType::contract_type_and_mode(&name).is_none()
4242
})
@@ -55,7 +55,7 @@ pub(crate) fn contract_trait_item_trait(_attrs: TokenStream, mut trait_: ItemTra
5555
/// includes contracts.
5656
///
5757
/// This new function forwards the call to the actual implementation.
58-
fn create_method_wrapper(method: &TraitItemMethod) -> TraitItemMethod {
58+
fn create_method_wrapper(method: &TraitItemFn) -> TraitItemFn {
5959
struct ArgInfo {
6060
call_toks: proc_macro2::TokenStream,
6161
}
@@ -92,7 +92,7 @@ pub(crate) fn contract_trait_item_trait(_attrs: TokenStream, mut trait_: ItemTra
9292
}
9393
}
9494

95-
let mut m: TraitItemMethod = (*method).clone();
95+
let mut m = method.clone();
9696

9797
let argument_data = m
9898
.sig
@@ -138,7 +138,7 @@ pub(crate) fn contract_trait_item_trait(_attrs: TokenStream, mut trait_: ItemTra
138138
m.attrs
139139
.iter()
140140
.filter(|a| {
141-
let name = a.path.segments.last().unwrap().ident.to_string();
141+
let name = a.path().segments.last().unwrap().ident.to_string();
142142
// is doc?
143143
if name == "doc" {
144144
return true;
@@ -168,11 +168,11 @@ pub(crate) fn contract_trait_item_trait(_attrs: TokenStream, mut trait_: ItemTra
168168
.items
169169
.iter()
170170
.filter_map(|item| {
171-
if let TraitItem::Method(m) = item {
171+
if let TraitItem::Fn(m) = item {
172172
let rename = create_method_rename(m);
173173
let wrapper = create_method_wrapper(m);
174174

175-
Some(vec![TraitItem::Method(rename), TraitItem::Method(wrapper)])
175+
Some(vec![TraitItem::Fn(rename), TraitItem::Fn(wrapper)])
176176
} else {
177177
None
178178
}
@@ -183,7 +183,7 @@ pub(crate) fn contract_trait_item_trait(_attrs: TokenStream, mut trait_: ItemTra
183183
// remove all previous methods
184184
trait_
185185
.items
186-
.retain(|item| !matches!(item, TraitItem::Method(_)));
186+
.retain(|item| !matches!(item, TraitItem::Fn(_)));
187187

188188
// add back new methods
189189
trait_.items.extend(funcs);
@@ -198,7 +198,7 @@ pub(crate) fn contract_trait_item_impl(_attrs: TokenStream, impl_: ItemImpl) ->
198198
let mut impl_: ItemImpl = impl_;
199199

200200
impl_.items.iter_mut().for_each(|it| {
201-
if let ImplItem::Method(method) = it {
201+
if let ImplItem::Fn(method) = it {
202202
let new_name = contract_method_impl_name(&method.sig.ident.to_string());
203203
let new_ident = syn::Ident::new(&new_name, method.sig.ident.span());
204204

0 commit comments

Comments
 (0)