2015-12-10 23:23:14 +09:00
|
|
|
//! Syntax extensions in the Rust compiler.
|
|
|
|
|
2019-02-05 14:37:15 +01:00
|
|
|
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
|
2015-12-15 16:23:18 +09:00
|
|
|
|
2019-02-04 21:49:54 +09:00
|
|
|
#![deny(rust_2018_idioms)]
|
2019-06-11 12:21:12 +03:00
|
|
|
#![deny(unused_lifetimes)]
|
2019-02-04 21:49:54 +09:00
|
|
|
|
2018-03-20 16:41:14 +02:00
|
|
|
#![feature(in_band_lifetimes)]
|
|
|
|
#![feature(proc_macro_diagnostic)]
|
2016-10-03 09:49:39 -07:00
|
|
|
#![feature(proc_macro_internals)]
|
2018-03-20 16:41:14 +02:00
|
|
|
#![feature(proc_macro_span)]
|
2017-12-06 10:50:55 -08:00
|
|
|
#![feature(decl_macro)]
|
2019-02-10 16:13:30 +09:00
|
|
|
#![feature(nll)]
|
2018-05-10 12:02:19 -06:00
|
|
|
#![feature(rustc_diagnostic_macros)]
|
2019-07-22 12:09:34 +03:00
|
|
|
#![feature(unicode_internals)]
|
2018-02-12 23:21:20 +01:00
|
|
|
|
2018-12-13 16:57:25 +01:00
|
|
|
#![recursion_limit="256"]
|
|
|
|
|
2016-10-03 09:49:39 -07:00
|
|
|
extern crate proc_macro;
|
2019-02-04 21:49:54 +09:00
|
|
|
|
2019-04-17 07:35:54 +09:00
|
|
|
mod error_codes;
|
2018-02-12 23:21:20 +01:00
|
|
|
|
2015-12-10 23:23:14 +09:00
|
|
|
mod asm;
|
2018-05-17 21:17:53 +02:00
|
|
|
mod assert;
|
2015-12-10 23:23:14 +09:00
|
|
|
mod cfg;
|
2017-05-06 23:26:45 -04:00
|
|
|
mod compile_error;
|
2015-12-10 23:23:14 +09:00
|
|
|
mod concat;
|
|
|
|
mod concat_idents;
|
|
|
|
mod env;
|
|
|
|
mod format;
|
2016-11-11 15:23:15 +11:00
|
|
|
mod format_foreign;
|
2019-07-19 00:24:58 +03:00
|
|
|
mod global_allocator;
|
2017-03-15 21:27:40 -05:00
|
|
|
mod global_asm;
|
2015-12-10 23:23:14 +09:00
|
|
|
mod log_syntax;
|
2018-09-16 12:20:47 +03:00
|
|
|
mod proc_macro_server;
|
2018-07-20 18:04:02 -07:00
|
|
|
mod test;
|
2018-09-02 09:03:24 -07:00
|
|
|
mod test_case;
|
2018-09-16 12:20:47 +03:00
|
|
|
mod trace_macros;
|
2015-12-10 23:23:14 +09:00
|
|
|
|
2018-09-16 12:20:47 +03:00
|
|
|
pub mod deriving;
|
2018-03-16 01:09:22 +02:00
|
|
|
pub mod proc_macro_decls;
|
2017-01-09 01:31:14 -08:00
|
|
|
pub mod proc_macro_impl;
|
|
|
|
|
2018-02-27 17:11:14 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2016-09-07 23:21:59 +00:00
|
|
|
use syntax::ast;
|
2019-06-22 16:18:05 +03:00
|
|
|
use syntax::attr::Stability;
|
2019-06-16 18:58:39 +03:00
|
|
|
use syntax::ext::base::MacroExpanderFn;
|
|
|
|
use syntax::ext::base::{NamedSyntaxExtension, SyntaxExtension, SyntaxExtensionKind};
|
2019-04-06 00:15:49 +02:00
|
|
|
use syntax::edition::Edition;
|
2019-05-22 12:42:23 +10:00
|
|
|
use syntax::symbol::{sym, Symbol};
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2019-06-22 16:18:05 +03:00
|
|
|
const EXPLAIN_ASM: &str =
|
|
|
|
"inline assembly is not stable enough for use and is subject to change";
|
|
|
|
const EXPLAIN_GLOBAL_ASM: &str =
|
|
|
|
"`global_asm!` is not stable enough for use and is subject to change";
|
|
|
|
const EXPLAIN_CUSTOM_TEST_FRAMEWORKS: &str =
|
|
|
|
"custom test frameworks are an unstable feature";
|
|
|
|
const EXPLAIN_LOG_SYNTAX: &str =
|
|
|
|
"`log_syntax!` is not stable enough for use and is subject to change";
|
|
|
|
const EXPLAIN_CONCAT_IDENTS: &str =
|
|
|
|
"`concat_idents` is not stable enough for use and is subject to change";
|
|
|
|
const EXPLAIN_FORMAT_ARGS_NL: &str =
|
|
|
|
"`format_args_nl` is only for internal language use and is subject to change";
|
|
|
|
const EXPLAIN_TRACE_MACROS: &str =
|
|
|
|
"`trace_macros` is not stable enough for use and is subject to change";
|
|
|
|
const EXPLAIN_UNSTABLE_COLUMN: &str =
|
|
|
|
"internal implementation detail of the `column` macro";
|
|
|
|
|
2018-07-12 11:58:16 +02:00
|
|
|
pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
|
2019-04-06 00:15:49 +02:00
|
|
|
user_exts: Vec<NamedSyntaxExtension>,
|
|
|
|
edition: Edition) {
|
2019-06-16 18:58:39 +03:00
|
|
|
deriving::register_builtin_derives(resolver, edition);
|
2017-01-24 01:31:49 +10:30
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
let mut register = |name, ext| {
|
2018-02-27 17:11:14 +01:00
|
|
|
resolver.add_builtin(ast::Ident::with_empty_ctxt(name), Lrc::new(ext));
|
2016-09-07 23:21:59 +00:00
|
|
|
};
|
2019-06-20 02:33:39 +03:00
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
macro_rules! register {
|
|
|
|
($( $name:ident: $f:expr, )*) => { $(
|
2019-06-23 14:59:42 +03:00
|
|
|
register(sym::$name, SyntaxExtension::default(
|
2019-06-16 18:58:39 +03:00
|
|
|
SyntaxExtensionKind::LegacyBang(Box::new($f as MacroExpanderFn)), edition
|
|
|
|
));
|
|
|
|
)* }
|
|
|
|
}
|
2019-06-22 16:18:05 +03:00
|
|
|
macro_rules! register_unstable {
|
|
|
|
($( [$feature:expr, $reason:expr, $issue:expr] $name:ident: $f:expr, )*) => { $(
|
2019-06-23 14:59:42 +03:00
|
|
|
register(sym::$name, SyntaxExtension {
|
2019-06-22 16:18:05 +03:00
|
|
|
stability: Some(Stability::unstable(
|
|
|
|
$feature, Some(Symbol::intern($reason)), $issue
|
|
|
|
)),
|
|
|
|
..SyntaxExtension::default(
|
|
|
|
SyntaxExtensionKind::LegacyBang(Box::new($f as MacroExpanderFn)), edition
|
|
|
|
)
|
|
|
|
});
|
2016-09-07 23:21:59 +00:00
|
|
|
)* }
|
2015-12-10 23:23:14 +09:00
|
|
|
}
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
use syntax::ext::source_util::*;
|
|
|
|
register! {
|
|
|
|
line: expand_line,
|
|
|
|
column: expand_column,
|
|
|
|
file: expand_file,
|
|
|
|
stringify: expand_stringify,
|
|
|
|
include: expand_include,
|
|
|
|
include_str: expand_include_str,
|
|
|
|
include_bytes: expand_include_bytes,
|
|
|
|
module_path: expand_mod,
|
|
|
|
cfg: cfg::expand_cfg,
|
|
|
|
concat: concat::expand_syntax_ext,
|
|
|
|
env: env::expand_env,
|
|
|
|
option_env: env::expand_option_env,
|
2017-05-06 23:26:45 -04:00
|
|
|
compile_error: compile_error::expand_compile_error,
|
2018-03-07 16:13:15 +09:00
|
|
|
assert: assert::expand_assert,
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 16:18:05 +03:00
|
|
|
register_unstable! {
|
|
|
|
[sym::__rust_unstable_column, EXPLAIN_UNSTABLE_COLUMN, 0]
|
|
|
|
__rust_unstable_column: expand_column,
|
|
|
|
[sym::asm, EXPLAIN_ASM, 29722]
|
|
|
|
asm: asm::expand_asm,
|
|
|
|
[sym::global_asm, EXPLAIN_GLOBAL_ASM, 35119]
|
|
|
|
global_asm: global_asm::expand_global_asm,
|
|
|
|
[sym::concat_idents, EXPLAIN_CONCAT_IDENTS, 29599]
|
|
|
|
concat_idents: concat_idents::expand_syntax_ext,
|
|
|
|
[sym::log_syntax, EXPLAIN_LOG_SYNTAX, 29598]
|
|
|
|
log_syntax: log_syntax::expand_syntax_ext,
|
|
|
|
[sym::trace_macros, EXPLAIN_TRACE_MACROS, 29598]
|
|
|
|
trace_macros: trace_macros::expand_trace_macros,
|
2019-06-16 18:58:39 +03:00
|
|
|
}
|
2018-07-20 18:04:02 -07:00
|
|
|
|
2019-06-20 02:33:39 +03:00
|
|
|
let allow_internal_unstable = Some([sym::test, sym::rustc_attrs][..].into());
|
2019-06-22 16:18:05 +03:00
|
|
|
register(sym::test_case, SyntaxExtension {
|
|
|
|
stability: Some(Stability::unstable(
|
|
|
|
sym::custom_test_frameworks,
|
|
|
|
Some(Symbol::intern(EXPLAIN_CUSTOM_TEST_FRAMEWORKS)),
|
|
|
|
50297,
|
|
|
|
)),
|
2019-06-20 02:33:39 +03:00
|
|
|
allow_internal_unstable: allow_internal_unstable.clone(),
|
2019-06-22 16:18:05 +03:00
|
|
|
..SyntaxExtension::default(
|
|
|
|
SyntaxExtensionKind::LegacyAttr(Box::new(test_case::expand)), edition
|
|
|
|
)
|
|
|
|
});
|
2019-06-20 02:33:39 +03:00
|
|
|
register(sym::test, SyntaxExtension {
|
|
|
|
allow_internal_unstable: allow_internal_unstable.clone(),
|
|
|
|
..SyntaxExtension::default(
|
|
|
|
SyntaxExtensionKind::LegacyAttr(Box::new(test::expand_test)), edition
|
|
|
|
)
|
|
|
|
});
|
|
|
|
register(sym::bench, SyntaxExtension {
|
|
|
|
allow_internal_unstable,
|
|
|
|
..SyntaxExtension::default(
|
|
|
|
SyntaxExtensionKind::LegacyAttr(Box::new(test::expand_bench)), edition
|
|
|
|
)
|
|
|
|
});
|
2019-07-19 00:24:58 +03:00
|
|
|
register(sym::global_allocator, SyntaxExtension {
|
|
|
|
allow_internal_unstable: Some([sym::rustc_attrs][..].into()),
|
|
|
|
..SyntaxExtension::default(
|
|
|
|
SyntaxExtensionKind::LegacyAttr(Box::new(global_allocator::expand)), edition
|
|
|
|
)
|
|
|
|
});
|
2019-06-20 02:33:39 +03:00
|
|
|
|
2019-06-17 00:31:46 +03:00
|
|
|
let allow_internal_unstable = Some([sym::fmt_internals][..].into());
|
2019-06-23 14:59:42 +03:00
|
|
|
register(sym::format_args, SyntaxExtension {
|
2019-06-17 00:31:46 +03:00
|
|
|
allow_internal_unstable: allow_internal_unstable.clone(),
|
2019-06-16 18:58:39 +03:00
|
|
|
..SyntaxExtension::default(
|
|
|
|
SyntaxExtensionKind::LegacyBang(Box::new(format::expand_format_args)), edition
|
|
|
|
)
|
|
|
|
});
|
|
|
|
register(sym::format_args_nl, SyntaxExtension {
|
2019-06-22 16:18:05 +03:00
|
|
|
stability: Some(Stability::unstable(
|
|
|
|
sym::format_args_nl,
|
|
|
|
Some(Symbol::intern(EXPLAIN_FORMAT_ARGS_NL)),
|
|
|
|
0,
|
|
|
|
)),
|
2019-06-17 00:31:46 +03:00
|
|
|
allow_internal_unstable,
|
2019-06-16 18:58:39 +03:00
|
|
|
..SyntaxExtension::default(
|
|
|
|
SyntaxExtensionKind::LegacyBang(Box::new(format::expand_format_args_nl)), edition
|
|
|
|
)
|
|
|
|
});
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2016-09-28 22:48:55 +00:00
|
|
|
for (name, ext) in user_exts {
|
|
|
|
register(name, ext);
|
|
|
|
}
|
2015-12-10 23:23:14 +09:00
|
|
|
}
|