Skip to content

Commit

Permalink
Merge pull request #227 from dtolnay/syn
Browse files Browse the repository at this point in the history
Update to syn 2
  • Loading branch information
dtolnay committed Mar 18, 2023
2 parents 0e45dde + fb8b81f commit 2c65cea
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .clippy.toml
@@ -1 +1 @@
msrv = "1.31.0"
msrv = "1.56.0"
13 changes: 0 additions & 13 deletions .github/workflows/ci.yml
Expand Up @@ -40,19 +40,6 @@ jobs:
if: matrix.rust == 'nightly'
- run: cargo test --all

msrv:
name: Rust 1.31.0
needs: pre_ci
if: needs.pre_ci.outputs.continue
runs-on: ubuntu-latest
timeout-minutes: 45
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@1.31.0
with:
components: rust-src
- run: cargo check

clippy:
name: Clippy
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -9,7 +9,7 @@ edition = "2018"
keywords = ["error", "error-handling", "derive"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/dtolnay/thiserror"
rust-version = "1.31"
rust-version = "1.56"

[dependencies]
thiserror-impl = { version = "=1.0.39", path = "impl" }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -16,7 +16,7 @@ This library provides a convenient derive macro for the standard library's
thiserror = "1.0"
```

*Compiler support: requires rustc 1.31+*
*Compiler support: requires rustc 1.56+*

<br>

Expand Down
4 changes: 2 additions & 2 deletions impl/Cargo.toml
Expand Up @@ -6,15 +6,15 @@ description = "Implementation detail of the `thiserror` crate"
edition = "2018"
license = "MIT OR Apache-2.0"
repository = "https://github.com/dtolnay/thiserror"
rust-version = "1.31"
rust-version = "1.56"

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = "1.0.45"
syn = "2.0"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
38 changes: 24 additions & 14 deletions impl/src/attr.rs
Expand Up @@ -2,9 +2,9 @@ use proc_macro2::{Delimiter, Group, Span, TokenStream, TokenTree};
use quote::{format_ident, quote, ToTokens};
use std::collections::BTreeSet as Set;
use std::iter::FromIterator;
use syn::parse::{Nothing, ParseStream};
use syn::parse::ParseStream;
use syn::{
braced, bracketed, parenthesized, token, Attribute, Error, Ident, Index, LitInt, LitStr,
braced, bracketed, parenthesized, token, Attribute, Error, Ident, Index, LitInt, LitStr, Meta,
Result, Token,
};

Expand Down Expand Up @@ -54,24 +54,27 @@ pub fn get(input: &[Attribute]) -> Result<Attrs> {
};

for attr in input {
if attr.path.is_ident("error") {
if attr.path().is_ident("error") {
parse_error_attribute(&mut attrs, attr)?;
} else if attr.path.is_ident("source") {
} else if attr.path().is_ident("source") {
require_empty_attribute(attr)?;
if attrs.source.is_some() {
return Err(Error::new_spanned(attr, "duplicate #[source] attribute"));
}
attrs.source = Some(attr);
} else if attr.path.is_ident("backtrace") {
} else if attr.path().is_ident("backtrace") {
require_empty_attribute(attr)?;
if attrs.backtrace.is_some() {
return Err(Error::new_spanned(attr, "duplicate #[backtrace] attribute"));
}
attrs.backtrace = Some(attr);
} else if attr.path.is_ident("from") {
if !attr.tokens.is_empty() {
// Assume this is meant for derive_more crate or something.
continue;
} else if attr.path().is_ident("from") {
match attr.meta {
Meta::Path(_) => {}
Meta::List(_) | Meta::NameValue(_) => {
// Assume this is meant for derive_more crate or something.
continue;
}
}
if attrs.from.is_some() {
return Err(Error::new_spanned(attr, "duplicate #[from] attribute"));
Expand Down Expand Up @@ -166,21 +169,21 @@ fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStr
let delimiter = parenthesized!(content in input);
let nested = parse_token_expr(&content, true)?;
let mut group = Group::new(Delimiter::Parenthesis, nested);
group.set_span(delimiter.span);
group.set_span(delimiter.span.join());
TokenTree::Group(group)
} else if input.peek(token::Brace) {
let content;
let delimiter = braced!(content in input);
let nested = parse_token_expr(&content, true)?;
let mut group = Group::new(Delimiter::Brace, nested);
group.set_span(delimiter.span);
group.set_span(delimiter.span.join());
TokenTree::Group(group)
} else if input.peek(token::Bracket) {
let content;
let delimiter = bracketed!(content in input);
let nested = parse_token_expr(&content, true)?;
let mut group = Group::new(Delimiter::Bracket, nested);
group.set_span(delimiter.span);
group.set_span(delimiter.span.join());
TokenTree::Group(group)
} else {
input.parse()?
Expand All @@ -191,8 +194,15 @@ fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStr
}

fn require_empty_attribute(attr: &Attribute) -> Result<()> {
syn::parse2::<Nothing>(attr.tokens.clone())?;
Ok(())
let error_span = match &attr.meta {
Meta::Path(_) => return Ok(()),
Meta::List(meta) => meta.delimiter.span().open(),
Meta::NameValue(meta) => meta.eq_token.span,
};
Err(Error::new(
error_span,
"unexpected token in thiserror attribute",
))
}

impl ToTokens for Display<'_> {
Expand Down
3 changes: 1 addition & 2 deletions impl/src/expand.rs
Expand Up @@ -528,8 +528,7 @@ fn type_parameter_of_option(ty: &Type) -> Option<&Type> {

fn spanned_error_trait(input: &DeriveInput) -> TokenStream {
let vis_span = match &input.vis {
Visibility::Public(vis) => Some(vis.pub_token.span),
Visibility::Crate(vis) => Some(vis.crate_token.span),
Visibility::Public(vis) => Some(vis.span),
Visibility::Restricted(vis) => Some(vis.pub_token.span),
Visibility::Inherited => None,
};
Expand Down

0 comments on commit 2c65cea

Please sign in to comment.