Skip to content

Commit

Permalink
Auto merge of rust-lang#68717 - petrochenkov:stabexpat, r=varkor
Browse files Browse the repository at this point in the history
Stabilize fn-like proc macros in expression, pattern and statement positions

I.e. all the positions in which stable `macro_rules` macros are supported.

Depends on rust-lang#68716 ("Stabilize `Span::mixed_site`").

cc rust-lang#54727
cc rust-lang#54727 (comment)

Stabilization report: rust-lang#68717 (comment).
  • Loading branch information
bors committed May 19, 2020
2 parents 89988fe + 5c6f7b3 commit 5943351
Show file tree
Hide file tree
Showing 41 changed files with 73 additions and 202 deletions.
31 changes: 0 additions & 31 deletions src/librustc_expand/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
ExpandResult::Ready(match invoc.kind {
InvocationKind::Bang { mac, .. } => match ext {
SyntaxExtensionKind::Bang(expander) => {
self.gate_proc_macro_expansion_kind(span, fragment_kind);
let tok_result = match expander.expand(self.cx, span, mac.args.inner_tokens()) {
Err(_) => return ExpandResult::Ready(fragment_kind.dummy(span)),
Ok(ts) => ts,
Expand Down Expand Up @@ -846,36 +845,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
}

fn gate_proc_macro_expansion_kind(&self, span: Span, kind: AstFragmentKind) {
let kind = match kind {
AstFragmentKind::Expr | AstFragmentKind::OptExpr => "expressions",
AstFragmentKind::Pat => "patterns",
AstFragmentKind::Stmts => "statements",
AstFragmentKind::Ty
| AstFragmentKind::Items
| AstFragmentKind::TraitItems
| AstFragmentKind::ImplItems
| AstFragmentKind::ForeignItems => return,
AstFragmentKind::Arms
| AstFragmentKind::Fields
| AstFragmentKind::FieldPats
| AstFragmentKind::GenericParams
| AstFragmentKind::Params
| AstFragmentKind::StructFields
| AstFragmentKind::Variants => panic!("unexpected AST fragment kind"),
};
if self.cx.ecfg.proc_macro_hygiene() {
return;
}
feature_err(
self.cx.parse_sess,
sym::proc_macro_hygiene,
span,
&format!("procedural macros cannot be expanded to {}", kind),
)
.emit();
}

fn parse_ast_fragment(
&mut self,
toks: TokenStream,
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_feature/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,7 @@ declare_features! (
/// Allows `#[marker]` on certain traits allowing overlapping implementations.
(active, marker_trait_attr, "1.30.0", Some(29864), None),

/// Allows macro invocations on modules expressions and statements and
/// procedural macros to expand to non-items.
/// Allows macro attributes on expressions, statements and non-inline modules.
(active, proc_macro_hygiene, "1.30.0", Some(54727), None),

/// Allows unsized rvalues at arguments and parameters.
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/auxiliary/cond_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// no-prefer-dynamic

#![crate_type = "proc-macro"]
#![feature(proc_macro_hygiene)]
#![feature(proc_macro_quote)]

extern crate proc_macro;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/auxiliary/hello_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// no-prefer-dynamic

#![crate_type = "proc-macro"]
#![feature(proc_macro_hygiene, proc_macro_quote)]
#![feature(proc_macro_quote)]

extern crate proc_macro;

Expand Down
1 change: 0 additions & 1 deletion src/test/ui/auxiliary/proc_macro_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// no-prefer-dynamic

#![crate_type = "proc-macro"]
#![feature(proc_macro_hygiene)]
#![feature(proc_macro_quote)]

extern crate proc_macro;
Expand Down
4 changes: 1 addition & 3 deletions src/test/ui/macro-quote-cond.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// run-pass

#![allow(unused_parens)]
// aux-build:cond_plugin.rs

#![feature(proc_macro_hygiene)]
#![allow(unused_parens)]

extern crate cond_plugin;

Expand Down
4 changes: 1 addition & 3 deletions src/test/ui/macro-quote-test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// run-pass
// Test that a macro can emit delimiters with nothing inside - `()`, `{}`

// run-pass
// aux-build:hello_macro.rs

#![feature(proc_macro_hygiene)]

extern crate hello_macro;

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/macros/auxiliary/proc_macro_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// no-prefer-dynamic

#![crate_type = "proc-macro"]
#![feature(proc_macro_span, proc_macro_hygiene, proc_macro_quote)]
#![feature(proc_macro_span, proc_macro_quote)]

extern crate proc_macro;

Expand Down
7 changes: 4 additions & 3 deletions src/test/ui/proc-macro/attr-invalid-exprs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// aux-build:attr-stmt-expr.rs

//! Attributes producing expressions in invalid locations

#![feature(stmt_expr_attributes, proc_macro_hygiene)]
// aux-build:attr-stmt-expr.rs

#![feature(proc_macro_hygiene)]
#![feature(stmt_expr_attributes)]

extern crate attr_stmt_expr;
use attr_stmt_expr::{duplicate, no_output};
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/proc-macro/attr-invalid-exprs.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: expected expression, found end of macro arguments
--> $DIR/attr-invalid-exprs.rs:11:13
--> $DIR/attr-invalid-exprs.rs:12:13
|
LL | let _ = #[no_output] "Hello, world!";
| ^^^^^^^^^^^^

error: macro expansion ignores token `,` and any following
--> $DIR/attr-invalid-exprs.rs:14:13
--> $DIR/attr-invalid-exprs.rs:15:13
|
LL | let _ = #[duplicate] "Hello, world!";
| ^^^^^^^^^^^^- help: you might be missing a semicolon here: `;`
Expand All @@ -15,7 +15,7 @@ LL | let _ = #[duplicate] "Hello, world!";
= note: the usage of `duplicate!` is likely invalid in expression context

error: macro expansion ignores token `,` and any following
--> $DIR/attr-invalid-exprs.rs:23:9
--> $DIR/attr-invalid-exprs.rs:24:9
|
LL | #[duplicate]
| ^^^^^^^^^^^^- help: you might be missing a semicolon here: `;`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/auxiliary/count_compound_ops.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// force-host
// no-prefer-dynamic

#![feature(proc_macro_hygiene, proc_macro_quote)]
#![feature(proc_macro_quote)]
#![crate_type = "proc-macro"]

extern crate proc_macro;
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/proc-macro/auxiliary/generate-dollar-ident.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// force-host
// no-prefer-dynamic

#![feature(proc_macro_hygiene)]
#![feature(proc_macro_quote)]
#![crate_type = "proc-macro"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// force-host
// no-prefer-dynamic

#![feature(proc_macro_quote, proc_macro_hygiene)]
#![feature(proc_macro_quote)]
#![crate_type = "proc-macro"]

extern crate proc_macro as proc_macro_renamed; // This does not break `quote!`
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/proc-macro/auxiliary/mixed-site-span.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// force-host
// no-prefer-dynamic

#![feature(proc_macro_hygiene)]
#![feature(proc_macro_quote)]

#![crate_type = "proc-macro"]
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/proc-macro/auxiliary/resolved-located-at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#![feature(proc_macro_def_site)]
#![feature(proc_macro_diagnostic)]
#![feature(proc_macro_hygiene)]
#![feature(proc_macro_quote)]
#![crate_type = "proc-macro"]

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/proc-macro/bang-macro.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// run-pass
// aux-build:bang-macro.rs

#![feature(proc_macro_hygiene)]

extern crate bang_macro;
use bang_macro::rewrite;

Expand Down
8 changes: 1 addition & 7 deletions src/test/ui/proc-macro/call-site.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// run-pass

#![allow(unused_variables)]
#![allow(unused_imports)]
// check-pass
// aux-build:call-site.rs

#![feature(proc_macro_hygiene)]

extern crate call_site;
use call_site::*;

fn main() {
let x1 = 10;
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/proc-macro/count_compound_ops.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// run-pass
// aux-build:count_compound_ops.rs

#![feature(proc_macro_hygiene)]

extern crate count_compound_ops;
use count_compound_ops::count_compound_ops;

Expand Down
6 changes: 1 addition & 5 deletions src/test/ui/proc-macro/hygiene_example.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// run-pass

#![allow(unused_macros)]
// check-pass
// aux-build:hygiene_example_codegen.rs
// aux-build:hygiene_example.rs

#![feature(proc_macro_hygiene)]

extern crate hygiene_example;
use hygiene_example::hello;

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/is-available.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// run-pass

#![feature(proc_macro_hygiene, proc_macro_is_available)]
#![feature(proc_macro_is_available)]

extern crate proc_macro;

Expand Down
3 changes: 0 additions & 3 deletions src/test/ui/proc-macro/lints_in_proc_macros.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// aux-build:bang_proc_macro2.rs

#![feature(proc_macro_hygiene)]
#![allow(unused_macros)]

extern crate bang_proc_macro2;

use bang_proc_macro2::bang_proc_macro2;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/lints_in_proc_macros.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0425]: cannot find value `foobar2` in this scope
--> $DIR/lints_in_proc_macros.rs:12:5
--> $DIR/lints_in_proc_macros.rs:9:5
|
LL | bang_proc_macro2!();
| ^^^^^^^^^^^^^^^^^^^^ help: a local variable with a similar name exists: `foobar`
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/proc-macro/macro-use-bang.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// build-pass (FIXME(62277): could be check-pass?)
// aux-build:test-macros.rs

#![feature(proc_macro_hygiene)]

#[macro_use]
extern crate test_macros;

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/proc-macro/mixed-site-span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

// aux-build:mixed-site-span.rs

#![feature(proc_macro_hygiene)]

#[macro_use]
extern crate mixed_site_span;

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/proc-macro/mixed-site-span.stderr
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
error[E0426]: use of undeclared label `'label_use`
--> $DIR/mixed-site-span.rs:15:9
--> $DIR/mixed-site-span.rs:13:9
|
LL | proc_macro_rules!();
| ^^^^^^^^^^^^^^^^^^^^ undeclared label `'label_use`
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find value `local_use` in this scope
--> $DIR/mixed-site-span.rs:15:9
--> $DIR/mixed-site-span.rs:13:9
|
LL | proc_macro_rules!();
| ^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find value `local_def` in this scope
--> $DIR/mixed-site-span.rs:19:9
--> $DIR/mixed-site-span.rs:17:9
|
LL | local_def;
| ^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `ItemUse` in crate `$crate`
--> $DIR/mixed-site-span.rs:26:1
--> $DIR/mixed-site-span.rs:24:1
|
LL | pass_dollar_crate!();
| ^^^^^^^^^^^^^^^^^^^^^ not found in `$crate`
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/proc-macro/multispan.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// aux-build:multispan.rs

#![feature(proc_macro_hygiene)]

extern crate multispan;

use multispan::hello;
Expand Down
Loading

0 comments on commit 5943351

Please sign in to comment.