Skip to content

Commit

Permalink
Fix raw identifiers expanding as-is in ParseAttrs (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: Kai Ren <tyranron@gmail.com>
  • Loading branch information
ilslv and tyranron committed Aug 13, 2021
1 parent cc0bfc6 commit 4179cab
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 15 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ All user visible changes to this project will be documented in this file. This p



## [0.1.1] · 2021-08-13
[0.1.1]: /../../tree/v0.1.1

[Diff](/../../compare/v0.1.0...v0.1.1)

### Fixed

- Raw identifiers (with `r#`) expanding as-is. ([#1])

[#1]: /../../pull/1




## [0.1.0] · 2021-06-25
[0.1.0]: /../../tree/v0.1.0

Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "synthez"
version = "0.1.0"
version = "0.1.1"
edition = "2018"
resolver = "2"
description = "Steroids for syn, quote and proc_macro2 crates"
Expand All @@ -24,8 +24,8 @@ include = ["/src/", "/Cargo.toml", "/CHANGELOG.md", "/LICENSE.md", "/README.md"]
full = ["synthez-core/full"]

[dependencies]
synthez-codegen = { version = "0.1.0", path = "./codegen" }
synthez-core = { version = "0.1.0", path = "./core" }
synthez-codegen = { version = "0.1.1", path = "./codegen" }
synthez-core = { version = "0.1.1", path = "./core" }

[workspace]
members = ["codegen", "core"]
Expand Down
4 changes: 2 additions & 2 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "synthez-codegen"
version = "0.1.0"
version = "0.1.1"
edition = "2018"
resolver = "2"
description = "Internal codegen shim of synthez crate"
Expand All @@ -17,7 +17,7 @@ include = ["/src/", "/Cargo.toml", "/README.md"]
proc-macro = true

[dependencies]
synthez-core = { version = "0.1.0", path = "../core" }
synthez-core = { version = "0.1.1", path = "../core" }
[dependencies.proc-macro2]
version = "1.0.4"
default-features = false
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "synthez-core"
version = "0.1.0"
version = "0.1.1"
edition = "2018"
resolver = "2"
description = "Internal implementations of synthez crate"
Expand Down
22 changes: 13 additions & 9 deletions core/src/codegen/parse_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
use std::{collections::HashSet, convert::TryFrom, iter};

use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens, TokenStreamExt as _};
use quote::{quote, ToTokens};
use syn::{
ext::IdentExt as _,
parse::{Parse, ParseStream},
token,
};
Expand Down Expand Up @@ -256,7 +257,8 @@ impl Definition {
/// Representation of a [`ParseAttrs`]'s field, used for code generation.
#[derive(Debug)]
struct Field {
/// [`syn::Ident`] of this [`Field`] in the original code.
/// [`syn::Ident`] of this [`Field`] in the original code (without possible
/// `r#` part).
ident: syn::Ident,

/// [`syn::Type`] of this [`Field`] (with [`field::Container`]).
Expand Down Expand Up @@ -292,7 +294,7 @@ impl TryFrom<syn::Field> for Field {
let ident = field.ident.unwrap();

let mut names = if attrs.args.is_empty() {
iter::once(ident.clone()).collect()
iter::once(ident.unraw()).collect()
} else {
attrs.args
};
Expand Down Expand Up @@ -591,16 +593,17 @@ impl Parse for Spanning<Kind> {
}

impl ToTokens for Kind {
fn to_tokens(&self, tokens: &mut TokenStream) {
fn to_tokens(&self, out: &mut TokenStream) {
let variant = syn::Ident::new_on_call_site(match self {
Self::Ident => "Ident",
Self::Nested => "Nested",
Self::Value(_) => "Value",
Self::Map => "Map",
});
tokens.append_all(&[quote! {
(quote! {
::synthez::parse::attrs::kind::#variant
}]);
})
.to_tokens(out);
}
}

Expand Down Expand Up @@ -645,14 +648,15 @@ impl Parse for Spanning<Dedup> {
}

impl ToTokens for Dedup {
fn to_tokens(&self, tokens: &mut TokenStream) {
fn to_tokens(&self, out: &mut TokenStream) {
let variant = syn::Ident::new_on_call_site(match self {
Self::Unique => "Unique",
Self::First => "First",
Self::Last => "Last",
});
tokens.append_all(&[quote! {
(quote! {
::synthez::parse::attrs::dedup::#variant
}]);
})
.to_tokens(out);
}
}
96 changes: 96 additions & 0 deletions tests/parse_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,35 @@ mod ident {
assert_eq!(err, "wrong!");
}
}

mod raw {
use synthez::proc_macro2::Span;

use super::*;

#[derive(Debug, Default, ParseAttrs)]
struct Attr {
#[parse(ident)]
r#type: Option<syn::token::Type>,
}

#[test]
fn is_unrawed() {
let input: syn::DeriveInput = syn::parse_quote! {
#[attr(type)]
struct Dummy;
};

let _ident = syn::Ident::new_on_call_site("type");
let res = Attr::parse_attrs("attr", &input);
assert!(res.is_ok(), "failed: {}", res.unwrap_err());

assert_eq!(
res.unwrap().r#type,
Some(syn::Token![type](Span::call_site())),
);
}
}
}

mod value {
Expand Down Expand Up @@ -1043,6 +1072,32 @@ mod value {
);
}
}

mod raw {
use super::*;

#[derive(Debug, Default, ParseAttrs)]
struct Attr {
#[parse(value)]
r#type: Option<syn::Ident>,
}

#[test]
fn is_unrawed() {
let input: syn::DeriveInput = syn::parse_quote! {
#[attr(type = minas)]
struct Dummy;
};

let res = Attr::parse_attrs("attr", &input);
assert!(res.is_ok(), "failed: {}", res.unwrap_err());

assert_eq!(
res.unwrap().r#type,
Some(syn::Ident::new_on_call_site("minas")),
);
}
}
}

mod map {
Expand Down Expand Up @@ -1574,6 +1629,47 @@ mod map {
assert_eq!(err, "expected `=`");
}
}

mod raw {
use super::*;

#[derive(Debug, Default, ParseAttrs)]
struct Attr {
#[parse(map)]
r#type: HashMap<syn::Ident, syn::LitStr>,
}

#[test]
fn is_unrawed() {
let input: syn::DeriveInput = syn::parse_quote! {
#[attr(type minas = "tirith")]
#[attr(type loth = "lorien")]
struct Dummy;
};

let res = Attr::parse_attrs("attr", &input);
assert!(res.is_ok(), "failed: {}", res.unwrap_err());

let out = res.unwrap().r#type;
assert_eq!(out.len(), 2, "wrong length of {:?}", out);
assert_eq!(
out.get(&syn::Ident::new_on_call_site("minas"))
.map(syn::LitStr::value)
.as_deref(),
Some("tirith"),
"wrong item of {:?}",
out,
);
assert_eq!(
out.get(&syn::Ident::new_on_call_site("loth"))
.map(syn::LitStr::value)
.as_deref(),
Some("lorien"),
"wrong item of {:?}",
out,
);
}
}
}

mod nested {
Expand Down

0 comments on commit 4179cab

Please sign in to comment.