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

Handle all project* attributes in that scope with one wrapper attribute #220

Merged
merged 2 commits into from
May 10, 2020
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
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ jobs:
if: matrix.rust == 'nightly'
run: |
rustup target add thumbv7m-none-eabi
- name: cargo test
- name: cargo test (stable)
if: matrix.rust != 'nightly'
run: |
cargo test --all --all-features
- name: cargo test --test compiletest
cargo test --all --all-features --exclude expandtest
- name: cargo test (nightly)
if: matrix.rust == 'nightly'
run: |
cargo test -p pin-project --all-features --test compiletest -- --ignored
cargo test --all --all-features -- -Zunstable-options --include-ignored
- name: cargo check --target thumbv7m-none-eabi
if: matrix.rust == 'nightly'
run: |
Expand Down
86 changes: 62 additions & 24 deletions pin-project-internal/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use syn::{
};

use crate::utils::{
determine_lifetime_name, insert_lifetime, parse_as_empty, Mutability, SliceExt, VecExt,
determine_lifetime_name, insert_lifetime, parse_as_empty, Immutable, Mutability, Mutable,
Owned, SliceExt, VecExt,
};

pub(crate) fn attribute(args: &TokenStream, input: Stmt, mutability: Mutability) -> TokenStream {
Expand Down Expand Up @@ -199,21 +200,30 @@ fn replace_item_impl(item: &mut ItemImpl, mutability: Mutability) -> Result<()>
}

fn replace_item_fn(item: &mut ItemFn, mutability: Mutability) -> Result<()> {
struct FnVisitor {
res: Result<()>,
mutability: Mutability,
}
struct FnVisitor(Result<()>);

impl FnVisitor {
fn visit_stmt(&mut self, node: &mut Stmt) -> Result<()> {
match node {
Stmt::Expr(expr) | Stmt::Semi(expr, _) => self.visit_expr(expr),
Stmt::Local(local) => {
visit_mut::visit_local_mut(self, local);
if let Some(attr) = local.attrs.find_remove(self.mutability.method_name())? {
parse_as_empty(&attr.tokens)?;
Context::new(self.mutability).replace_local(local)?;

let mut prev = None;
for &mutability in &[Immutable, Mutable, Owned] {
if let Some(attr) = local.attrs.find_remove(mutability.method_name())? {
if let Some(prev) = prev.replace(mutability) {
return Err(error!(
attr,
"attributes `{}` and `{}` are mutually exclusive",
prev.method_name(),
mutability.method_name(),
));
}
Context::new(mutability).replace_local(local)?;
}
}

Ok(())
}
// Do not recurse into nested items.
Expand All @@ -223,41 +233,69 @@ fn replace_item_fn(item: &mut ItemFn, mutability: Mutability) -> Result<()> {

fn visit_expr(&mut self, node: &mut Expr) -> Result<()> {
visit_mut::visit_expr_mut(self, node);
let attr = match node {
Expr::Match(expr) => expr.attrs.find_remove(self.mutability.method_name())?,
match node {
Expr::Match(expr) => {
let mut prev = None;
for &mutability in &[Immutable, Mutable, Owned] {
if let Some(attr) = expr.attrs.find_remove(mutability.method_name())? {
if let Some(prev) = prev.replace(mutability) {
return Err(error!(
attr,
"attributes `{}` and `{}` are mutually exclusive",
prev.method_name(),
mutability.method_name(),
));
}
}
}
if let Some(mutability) = prev {
replace_expr(node, mutability);
}
}
Expr::If(expr_if) => {
if let Expr::Let(_) = &*expr_if.cond {
expr_if.attrs.find_remove(self.mutability.method_name())?
} else {
None
let mut prev = None;
for &mutability in &[Immutable, Mutable, Owned] {
if let Some(attr) =
expr_if.attrs.find_remove(mutability.method_name())?
{
if let Some(prev) = prev.replace(mutability) {
return Err(error!(
attr,
"attributes `{}` and `{}` are mutually exclusive",
prev.method_name(),
mutability.method_name(),
));
}
}
}
if let Some(mutability) = prev {
replace_expr(node, mutability);
}
}
}
_ => return Ok(()),
};
if let Some(attr) = attr {
parse_as_empty(&attr.tokens)?;
replace_expr(node, self.mutability);
_ => {}
}
Ok(())
}
}

impl VisitMut for FnVisitor {
fn visit_stmt_mut(&mut self, node: &mut Stmt) {
if self.res.is_err() {
if self.0.is_err() {
return;
}
if let Err(e) = self.visit_stmt(node) {
self.res = Err(e)
self.0 = Err(e)
}
}

fn visit_expr_mut(&mut self, node: &mut Expr) {
if self.res.is_err() {
if self.0.is_err() {
return;
}
if let Err(e) = self.visit_expr(node) {
self.res = Err(e)
self.0 = Err(e)
}
}

Expand All @@ -270,9 +308,9 @@ fn replace_item_fn(item: &mut ItemFn, mutability: Mutability) -> Result<()> {
return Err(error!(attr, "duplicate #[{}] attribute", mutability.method_name()));
}

let mut visitor = FnVisitor { res: Ok(()), mutability };
let mut visitor = FnVisitor(Ok(()));
visitor.visit_block_mut(&mut item.block);
visitor.res
visitor.0
}

fn replace_item_use(item: &mut ItemUse, mutability: Mutability) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion tests/expand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ publish = false

[dev-dependencies]
pin-project = { path = "../.." }
macrotest = "1.0"
macrotest = "1.0.2"
4 changes: 4 additions & 0 deletions tests/expand/tests/expandtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
#[cfg_attr(any(not(cargo_expand), all(ci, not(target_os = "linux"))), ignore)]
#[test]
fn expandtest() {
#[cfg(target_os = "linux")] // FIXME
#[cfg(ci)]
macrotest::expand_without_refresh("tests/expand/*.rs");
#[cfg(not(ci))]
macrotest::expand("tests/expand/*.rs");
}
30 changes: 29 additions & 1 deletion tests/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,39 @@ fn issue_206() {
}
}

#[project]
#[test]
fn combine() {
#[pin_project(Replace)]
enum Enum<A> {
V1(#[pin] A),
V2,
}

let mut x = Enum::V1(1);
#[project]
match Pin::new(&mut x).project() {
Enum::V1(_) => {}
Enum::V2 => unreachable!(),
}
#[project_ref]
match Pin::new(&x).project_ref() {
Enum::V1(_) => {}
Enum::V2 => unreachable!(),
}
#[project_replace]
match Pin::new(&mut x).project_replace(Enum::V2) {
Enum::V1(_) => {}
Enum::V2 => unreachable!(),
}
}

// FIXME: This should be denied, but allowed for compatibility at this time.
#[project]
#[project_ref]
#[project_replace]
#[test]
fn combine() {
fn combine_compat() {
#[pin_project(Replace)]
enum Enum<A> {
V1(#[pin] A),
Expand Down
27 changes: 27 additions & 0 deletions tests/project_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,30 @@ fn project_impl() {
fn foo<'pin>(&'pin self) {}
}
}

#[project_ref]
#[test]
fn combine() {
#[pin_project(Replace)]
enum Enum<A> {
V1(#[pin] A),
V2,
}

let mut x = Enum::V1(1);
#[project]
match Pin::new(&mut x).project() {
Enum::V1(_) => {}
Enum::V2 => unreachable!(),
}
#[project_ref]
match Pin::new(&x).project_ref() {
Enum::V1(_) => {}
Enum::V2 => unreachable!(),
}
#[project_replace]
match Pin::new(&mut x).project_replace(Enum::V2) {
Enum::V1(_) => {}
Enum::V2 => unreachable!(),
}
}
27 changes: 27 additions & 0 deletions tests/project_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,30 @@ fn project_replace_stmt_expr() {
Enum::None => panic!(),
}
}

#[project_replace]
#[test]
fn combine() {
#[pin_project(Replace)]
enum Enum<A> {
V1(#[pin] A),
V2,
}

let mut x = Enum::V1(1);
#[project]
match Pin::new(&mut x).project() {
Enum::V1(_) => {}
Enum::V2 => unreachable!(),
}
#[project_ref]
match Pin::new(&x).project_ref() {
Enum::V1(_) => {}
Enum::V2 => unreachable!(),
}
#[project_replace]
match Pin::new(&mut x).project_replace(Enum::V2) {
Enum::V1(_) => {}
Enum::V2 => unreachable!(),
}
}
72 changes: 72 additions & 0 deletions tests/ui/project/invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,78 @@ mod attribute {
let A(_) = Pin::new(&mut x).project();
}

#[project]
fn combine_stmt_project1() {
let mut x = A(());
#[project]
#[project_ref] //~ ERROR are mutually exclusive
let A(_) = Pin::new(&mut x).project();
}

#[project]
fn combine_stmt_project2() {
let mut x = A(());
#[project]
#[project_replace] //~ ERROR are mutually exclusive
let A(_) = Pin::new(&mut x).project();
}

#[project]
fn combine_stmt_project3() {
let mut x = A(());
#[project_ref]
#[project_replace] //~ ERROR are mutually exclusive
let A(_) = Pin::new(&mut x).project();
}

#[project_ref]
fn combine_stmt_project_ref1() {
let mut x = A(());
#[project]
#[project_ref] //~ ERROR are mutually exclusive
let A(_) = Pin::new(&mut x).project();
}

#[project_ref]
fn combine_stmt_project_ref2() {
let mut x = A(());
#[project]
#[project_replace] //~ ERROR are mutually exclusive
let A(_) = Pin::new(&mut x).project();
}

#[project_ref]
fn combine_stmt_project_ref3() {
let mut x = A(());
#[project_ref]
#[project_replace] //~ ERROR are mutually exclusive
let A(_) = Pin::new(&mut x).project();
}

#[project_replace]
fn combine_stmt_project_replace1() {
let mut x = A(());
#[project]
#[project_ref] //~ ERROR are mutually exclusive
let A(_) = Pin::new(&mut x).project();
}

#[project_replace]
fn combine_stmt_project_replace2() {
let mut x = A(());
#[project]
#[project_replace] //~ ERROR are mutually exclusive
let A(_) = Pin::new(&mut x).project();
}

#[project_replace]
fn combine_stmt_project_replace3() {
let mut x = A(());
#[project_ref]
#[project_replace] //~ ERROR are mutually exclusive
let A(_) = Pin::new(&mut x).project();
}

#[project]
#[project] //~ ERROR duplicate #[project] attribute
fn duplicate_fn_project() {}
Expand Down
Loading