Skip to content

Commit

Permalink
rustc: Stabilize much of the proc_macro feature
Browse files Browse the repository at this point in the history
This commit stabilizes some of the `proc_macro` language feature as well as a
number of APIs in the `proc_macro` crate as [previously discussed][1]. This
means that on stable Rust you can now define custom procedural macros which
operate as attributes attached to items or `macro_rules!`-like bang-style
invocations. This extends the suite of currently stable procedural macros,
custom derives, with custom attributes and custom bang macros.

Note though that despite the stabilization in this commit procedural macros are
still not usable on stable Rust. To stabilize that we'll need to stabilize at
least part of the `use_extern_macros` feature. Currently you can define a
procedural macro attribute but you can't import it to call it!

A summary of the changes made in this PR (as well as the various consequences)
is:

* The `proc_macro` language and library features are now stable.
* Other APIs not stabilized in the `proc_macro` crate are now named under a
  different feature, such as `proc_macro_diagnostic` or `proc_macro_span`.
* A few checks in resolution for `proc_macro` being enabled have switched over
  to `use_extern_macros` being enabled. This means that code using
  `#![feature(proc_macro)]` today will likely need to move to
  `#![feature(use_extern_macros)]`.

It's intended that this PR, once landed, will be followed up with an attempt to
stabilize a small slice of `use_extern_macros` just for procedural macros to
make this feature 100% usable on stable.

[1]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
  • Loading branch information
alexcrichton committed Jul 16, 2018
1 parent 3d5753f commit 65f3007
Show file tree
Hide file tree
Showing 80 changed files with 241 additions and 515 deletions.
241 changes: 0 additions & 241 deletions src/doc/unstable-book/src/language-features/proc-macro.md

This file was deleted.

16 changes: 8 additions & 8 deletions src/libproc_macro/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use Span;
use rustc_errors as rustc;

/// An enum representing a diagnostic level.
#[unstable(feature = "proc_macro", issue = "38356")]
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
#[derive(Copy, Clone, Debug)]
pub enum Level {
/// An error.
Expand All @@ -30,7 +30,7 @@ pub enum Level {

/// A structure representing a diagnostic message and associated children
/// messages.
#[unstable(feature = "proc_macro", issue = "38356")]
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
#[derive(Clone, Debug)]
pub struct Diagnostic {
level: Level,
Expand All @@ -43,15 +43,15 @@ macro_rules! diagnostic_child_methods {
($spanned:ident, $regular:ident, $level:expr) => (
/// Add a new child diagnostic message to `self` with the level
/// identified by this methods name with the given `span` and `message`.
#[unstable(feature = "proc_macro", issue = "38356")]
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
pub fn $spanned<T: Into<String>>(mut self, span: Span, message: T) -> Diagnostic {
self.children.push(Diagnostic::spanned(span, $level, message));
self
}

/// Add a new child diagnostic message to `self` with the level
/// identified by this method's name with the given `message`.
#[unstable(feature = "proc_macro", issue = "38356")]
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic {
self.children.push(Diagnostic::new($level, message));
self
Expand All @@ -61,7 +61,7 @@ macro_rules! diagnostic_child_methods {

impl Diagnostic {
/// Create a new diagnostic with the given `level` and `message`.
#[unstable(feature = "proc_macro", issue = "38356")]
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
Diagnostic {
level: level,
Expand All @@ -73,7 +73,7 @@ impl Diagnostic {

/// Create a new diagnostic with the given `level` and `message` pointing to
/// the given `span`.
#[unstable(feature = "proc_macro", issue = "38356")]
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
pub fn spanned<T: Into<String>>(span: Span, level: Level, message: T) -> Diagnostic {
Diagnostic {
level: level,
Expand All @@ -89,13 +89,13 @@ impl Diagnostic {
diagnostic_child_methods!(span_help, help, Level::Help);

/// Returns the diagnostic `level` for `self`.
#[unstable(feature = "proc_macro", issue = "38356")]
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
pub fn level(&self) -> Level {
self.level
}

/// Emit the diagnostic.
#[unstable(feature = "proc_macro", issue = "38356")]
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
pub fn emit(self) {
::__internal::with_sess(move |sess, _| {
let handler = &sess.span_diagnostic;
Expand Down
Loading

0 comments on commit 65f3007

Please sign in to comment.