Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update syn to v2 #113

Merged
merged 3 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/msrv-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.54.0
toolchain: 1.56.0
target: thumbv6m-none-eabi
- name: Use MSRV Cargo.lock
run: cp Cargo.lock.msrv Cargo.lock
Expand All @@ -24,10 +24,10 @@ jobs:
with:
command: build
args: --all --locked
toolchain: 1.54.0
toolchain: 1.56.0
- name: Build no-std with MSRV
uses: actions-rs/cargo@v1
with:
command: build
args: --manifest-path=num_enum/Cargo.toml --target thumbv6m-none-eabi --no-default-features --locked
toolchain: 1.54.0
toolchain: 1.56.0
33 changes: 25 additions & 8 deletions Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion num_enum_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ features = ["external_doc"]
proc-macro2 = "1"
proc-macro-crate = { version = "1", optional = true }
quote = "1"
syn = { version = "1.0.15", features = ["parsing"] }
syn = { version = "2.0.10", features = ["parsing"] }
62 changes: 22 additions & 40 deletions num_enum_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{format_ident, quote};
use quote::{format_ident, quote, ToTokens};
use std::collections::BTreeSet;
use syn::{
parse::{Parse, ParseStream},
parse_macro_input, parse_quote,
spanned::Spanned,
Attribute, Data, DeriveInput, Error, Expr, ExprLit, ExprUnary, Fields, Ident, Lit, LitInt,
Meta, Result, UnOp,
parse_macro_input, parse_quote, Attribute, Data, DeriveInput, Error, Expr, ExprLit, ExprUnary,
Fields, Ident, Lit, LitInt, Meta, Result, UnOp,
};

macro_rules! die {
Expand Down Expand Up @@ -86,11 +84,11 @@ fn parse_alternative_values(val_expr: &Expr) -> Result<Vec<DiscriminantValue>> {
}

if let Expr::Range(syn::ExprRange {
from, to, limits, ..
start, end, limits, ..
}) = val_expr
{
let lower = range_expr_value_to_number(val_expr, from)?;
let upper = range_expr_value_to_number(val_expr, to)?;
let lower = range_expr_value_to_number(val_expr, start)?;
let upper = range_expr_value_to_number(val_expr, end)?;
// While this is technically allowed in Rust, and results in an empty range, it's almost certainly a mistake in this context.
if lower > upper {
die!(val_expr => "When using ranges for alternate values, upper bound must not be less than lower bound");
Expand Down Expand Up @@ -136,7 +134,7 @@ struct NumEnumVariantAttributes {
impl Parse for NumEnumVariantAttributes {
fn parse(input: ParseStream<'_>) -> Result<Self> {
Ok(Self {
items: input.parse_terminated(NumEnumVariantAttributeItem::parse)?,
items: input.parse_terminated(NumEnumVariantAttributeItem::parse, syn::Token![,])?,
})
}
}
Expand Down Expand Up @@ -174,12 +172,6 @@ impl Parse for VariantDefaultAttribute {
}
}

impl Spanned for VariantDefaultAttribute {
fn span(&self) -> Span {
self.keyword.span()
}
}

struct VariantCatchAllAttribute {
keyword: kw::catch_all,
}
Expand All @@ -192,14 +184,8 @@ impl Parse for VariantCatchAllAttribute {
}
}

impl Spanned for VariantCatchAllAttribute {
fn span(&self) -> Span {
self.keyword.span()
}
}

struct VariantAlternativesAttribute {
keyword: kw::alternatives,
_keyword: kw::alternatives,
_eq_token: syn::Token![=],
_bracket_token: syn::token::Bracket,
expressions: syn::punctuated::Punctuated<Expr, syn::Token![,]>,
Expand All @@ -211,22 +197,16 @@ impl Parse for VariantAlternativesAttribute {
let keyword = input.parse()?;
let _eq_token = input.parse()?;
let _bracket_token = syn::bracketed!(content in input);
let expressions = content.parse_terminated(Expr::parse)?;
let expressions = content.parse_terminated(Expr::parse, syn::Token![,])?;
Ok(Self {
keyword,
_keyword: keyword,
_eq_token,
_bracket_token,
expressions,
})
}
}

impl Spanned for VariantAlternativesAttribute {
fn span(&self) -> Span {
self.keyword.span()
}
}

struct VariantInfo {
ident: Ident,
is_default: bool,
Expand Down Expand Up @@ -329,16 +309,16 @@ impl Parse for EnumInfo {
let mut attrs = input.attrs.into_iter();
loop {
if let Some(attr) = attrs.next() {
if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
if let Meta::List(meta_list) = &attr.meta {
if let Some(ident) = meta_list.path.get_ident() {
if ident == "repr" {
let mut nested = meta_list.nested.iter();
if nested.len() != 1 {
die!(attr =>
let mut nested = meta_list.tokens.clone().into_iter();
let repr = match (nested.next(), nested.next()) {
(Some(repr), None) => repr,
_ => die!(attr =>
"Expected exactly one `repr` argument"
);
}
let repr = nested.next().unwrap();
),
};
let repr: Ident = parse_quote! {
#repr
};
Expand Down Expand Up @@ -385,7 +365,7 @@ impl Parse for EnumInfo {
let mut is_catch_all: bool = false;

for attribute in &variant.attrs {
if attribute.path.is_ident("default") {
if attribute.path().is_ident("default") {
if has_default_variant {
die!(attribute =>
"Multiple variants marked `#[default]` or `#[num_enum(default)]` found"
Expand All @@ -399,7 +379,7 @@ impl Parse for EnumInfo {
has_default_variant = true;
}

if attribute.path.is_ident("num_enum") {
if attribute.path().is_ident("num_enum") {
match attribute.parse_args_with(NumEnumVariantAttributes::parse) {
Ok(variant_attributes) => {
for variant_attribute in variant_attributes.items {
Expand Down Expand Up @@ -457,7 +437,9 @@ impl Parse for EnumInfo {
}
Err(err) => {
if cfg!(not(feature = "complex-expressions")) {
let attribute_str = format!("{}", attribute.tokens);
let tokens = attribute.meta.to_token_stream();

let attribute_str = format!("{}", tokens);
if attribute_str.contains("alternatives")
&& attribute_str.contains("..")
{
Expand Down