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

Prevent positional args from being named inside function body #202

Merged
merged 2 commits into from Jun 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.rs
Expand Up @@ -8,6 +8,10 @@ fn main() {
None => return,
};

if compiler < 45 {
println!("cargo:rustc-cfg=no_span_mixed_site");
}

if compiler < 47 {
println!("cargo:rustc-cfg=self_span_hack");
}
Expand Down
8 changes: 5 additions & 3 deletions src/expand.rs
@@ -1,7 +1,7 @@
use crate::lifetime::{AddLifetimeToImplTrait, CollectLifetimes};
use crate::parse::Item;
use crate::receiver::{has_self_in_block, has_self_in_sig, mut_pat, ReplaceSelf};
use proc_macro2::TokenStream;
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use std::collections::BTreeSet as Set;
use std::mem;
Expand Down Expand Up @@ -399,8 +399,10 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
}

fn positional_arg(i: usize, pat: &Pat) -> Ident {
use syn::spanned::Spanned;
format_ident!("__arg{}", i, span = pat.span())
let span: Span = syn::spanned::Spanned::span(pat);
#[cfg(not(no_span_mixed_site))]
let span = span.resolved_at(Span::mixed_site());
format_ident!("__arg{}", i, span = span)
}

fn has_bound(supertraits: &Supertraits, marker: &Ident) -> bool {
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/arg-implementation-detail.rs
@@ -0,0 +1,22 @@
use async_trait::async_trait;

pub struct Struct;

#[async_trait]
pub trait Trait {
async fn f((_a, _b): (Struct, Struct)) {
// Expands to something like:
//
// fn f(__arg0: (Struct, Struct)) -> … {
// Box::pin(async move {
// let (_a, _b) = __arg0;
// …
// })
// }
//
// but user's code must not be allowed to name that temporary argument:
let _ = __arg0;
}
}

fn main() {}
5 changes: 5 additions & 0 deletions tests/ui/arg-implementation-detail.stderr
@@ -0,0 +1,5 @@
error[E0425]: cannot find value `__arg0` in this scope
--> tests/ui/arg-implementation-detail.rs:18:17
|
18 | let _ = __arg0;
| ^^^^^^ not found in this scope