Auto merge of #51762 - petrochenkov:oh-hi-mark, r=oli-obk

hygiene: Implement transparent marks and use them for call-site hygiene in proc-macros

Fixes https://github.com/rust-lang/rust/issues/50050
This commit is contained in:
bors 2018-06-30 09:19:21 +00:00
commit bfc1ee4968
40 changed files with 549 additions and 169 deletions

View file

@ -23,5 +23,5 @@ fn main() {
bang_proc_macro2!();
//~^ ERROR cannot find value `foobar2` in this scope
//~^^ did you mean `foobar`?
println!("{}", x); //~ ERROR cannot find value `x` in this scope
println!("{}", x);
}

View file

@ -0,0 +1,37 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// no-prefer-dynamic
#![crate_type = "proc-macro"]
#![feature(proc_macro)]
extern crate proc_macro;
use proc_macro::*;
#[proc_macro]
pub fn check(input: TokenStream) -> TokenStream {
// Parsed `x2` can refer to `x2` from `input`
let parsed1: TokenStream = "let x3 = x2;".parse().unwrap();
// `x3` parsed from one string can refer to `x3` parsed from another string.
let parsed2: TokenStream = "let x4 = x3;".parse().unwrap();
// Manually assembled `x4` can refer to parsed `x4`.
let manual: Vec<TokenTree> = vec![
Ident::new("let", Span::call_site()).into(),
Ident::new("x5", Span::call_site()).into(),
Punct::new('=', Spacing::Alone).into(),
Ident::new("x4", Span::call_site()).into(),
Punct::new(';', Spacing::Alone).into(),
];
input.into_iter().chain(parsed1.into_iter())
.chain(parsed2.into_iter())
.chain(manual.into_iter())
.collect()
}

View file

@ -0,0 +1,23 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// aux-build:call-site.rs
// ignore-stage1
#![feature(proc_macro, proc_macro_non_items)]
extern crate call_site;
use call_site::*;
fn main() {
let x1 = 10;
call_site::check!(let x2 = x1;);
let x6 = x5;
}

View file

@ -19,3 +19,38 @@ pub mod foo {
}
}
}
pub struct SomeType;
// `$crate`
pub macro uses_dollar_crate_modern() {
type Alias = $crate::SomeType;
}
pub macro define_uses_dollar_crate_modern_nested($uses_dollar_crate_modern_nested: ident) {
macro $uses_dollar_crate_modern_nested() {
type AliasCrateModernNested = $crate::SomeType;
}
}
#[macro_export]
macro_rules! define_uses_dollar_crate_legacy_nested {
() => {
macro_rules! uses_dollar_crate_legacy_nested {
() => {
type AliasLegacyNested = $crate::SomeType;
}
}
}
}
// `crate`
pub macro uses_crate_modern() {
type AliasCrate = crate::SomeType;
}
pub macro define_uses_crate_modern_nested($uses_crate_modern_nested: ident) {
macro $uses_crate_modern_nested() {
type AliasCrateModernNested = crate::SomeType;
}
}

View file

@ -0,0 +1,16 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(decl_macro, rustc_attrs)]
#[rustc_transparent_macro]
pub macro dollar_crate() {
let s = $crate::S;
}

View file

@ -0,0 +1,35 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Make sure `$crate` and `crate` work in for basic cases of nested macros.
// compile-pass
// aux-build:intercrate.rs
#![feature(decl_macro, crate_in_paths)]
extern crate intercrate;
// `$crate`
intercrate::uses_dollar_crate_modern!();
intercrate::define_uses_dollar_crate_modern_nested!(uses_dollar_crate_modern_nested);
uses_dollar_crate_modern_nested!();
intercrate::define_uses_dollar_crate_legacy_nested!();
uses_dollar_crate_legacy_nested!();
// `crate`
intercrate::uses_crate_modern!();
intercrate::define_uses_crate_modern_nested!(uses_crate_modern_nested);
uses_crate_modern_nested!();
fn main() {}

View file

@ -0,0 +1,24 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// This is an equivalent of issue #50504, but for declarative macros.
#![feature(decl_macro, rustc_attrs)]
#[rustc_transparent_macro]
macro genmod() {
mod m {
type A = S; //~ ERROR cannot find type `S` in this scope
}
}
struct S;
genmod!();

View file

@ -0,0 +1,17 @@
error[E0412]: cannot find type `S` in this scope
--> $DIR/generate-mod.rs:18:18
|
LL | type A = S; //~ ERROR cannot find type `S` in this scope
| ^ did you mean `A`?
...
LL | genmod!();
| ---------- in this macro invocation
error[E0601]: `main` function not found in crate `generate_mod`
|
= note: consider adding a `main` function to `$DIR/generate-mod.rs`
error: aborting due to 2 previous errors
Some errors occurred: E0412, E0601.
For more information about an error, try `rustc --explain E0412`.

View file

@ -0,0 +1,53 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-pass
// aux-build:transparent-basic.rs
#![feature(decl_macro, rustc_attrs)]
extern crate transparent_basic;
#[rustc_transparent_macro]
macro binding() {
let x = 10;
}
#[rustc_transparent_macro]
macro label() {
break 'label
}
macro_rules! legacy {
() => {
binding!();
let y = x;
}
}
fn legacy_interaction1() {
legacy!();
}
struct S;
fn check_dollar_crate() {
// `$crate::S` inside the macro resolves to `S` from this crate.
transparent_basic::dollar_crate!();
}
fn main() {
binding!();
let y = x;
'label: loop {
label!();
}
}