2019-11-09 21:03:24 -06:00
|
|
|
//! Builtin macro
|
2019-11-11 00:15:09 -06:00
|
|
|
use crate::db::AstDatabase;
|
|
|
|
use crate::{
|
2020-03-02 00:05:15 -06:00
|
|
|
ast::{self, AstToken, HasStringValue},
|
|
|
|
name, AstId, CrateId, MacroDefId, MacroDefKind, TextUnit,
|
2019-11-11 00:15:09 -06:00
|
|
|
};
|
|
|
|
|
2020-03-06 08:58:45 -06:00
|
|
|
use crate::{quote, EagerMacroId, LazyMacroId, MacroCallId};
|
2020-03-02 00:05:15 -06:00
|
|
|
use either::Either;
|
2020-03-06 08:58:45 -06:00
|
|
|
use ra_db::{FileId, RelativePath};
|
2020-03-02 00:05:15 -06:00
|
|
|
use ra_parser::FragmentKind;
|
2019-11-09 21:03:24 -06:00
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
macro_rules! register_builtin {
|
2020-03-02 00:05:15 -06:00
|
|
|
( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => {
|
2019-11-23 08:48:34 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum BuiltinFnLikeExpander {
|
|
|
|
$($kind),*
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum EagerExpander {
|
|
|
|
$($e_kind),*
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:48:34 -06:00
|
|
|
impl BuiltinFnLikeExpander {
|
|
|
|
pub fn expand(
|
|
|
|
&self,
|
|
|
|
db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
id: LazyMacroId,
|
2019-11-23 08:48:34 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
let expander = match *self {
|
|
|
|
$( BuiltinFnLikeExpander::$kind => $expand, )*
|
|
|
|
};
|
|
|
|
expander(db, id, tt)
|
|
|
|
}
|
2020-03-02 00:05:15 -06:00
|
|
|
}
|
2019-12-21 06:33:44 -06:00
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
impl EagerExpander {
|
|
|
|
pub fn expand(
|
|
|
|
&self,
|
2020-03-06 08:58:45 -06:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
arg_id: EagerMacroId,
|
2020-03-02 00:05:15 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
|
|
|
|
let expander = match *self {
|
|
|
|
$( EagerExpander::$e_kind => $e_expand, )*
|
|
|
|
};
|
2020-03-06 08:58:45 -06:00
|
|
|
expander(db,arg_id,tt)
|
2019-12-21 06:33:44 -06:00
|
|
|
}
|
2019-11-23 08:48:34 -06:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
fn find_by_name(ident: &name::Name) -> Option<Either<BuiltinFnLikeExpander, EagerExpander>> {
|
|
|
|
match ident {
|
|
|
|
$( id if id == &name::name![$name] => Some(Either::Left(BuiltinFnLikeExpander::$kind)), )*
|
|
|
|
$( id if id == &name::name![$e_name] => Some(Either::Right(EagerExpander::$e_kind)), )*
|
|
|
|
_ => return None,
|
|
|
|
}
|
2019-11-23 08:48:34 -06:00
|
|
|
}
|
2019-11-22 11:47:35 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
pub fn find_builtin_macro(
|
|
|
|
ident: &name::Name,
|
|
|
|
krate: CrateId,
|
|
|
|
ast_id: AstId<ast::MacroCall>,
|
|
|
|
) -> Option<MacroDefId> {
|
|
|
|
let kind = find_by_name(ident)?;
|
|
|
|
|
|
|
|
match kind {
|
|
|
|
Either::Left(kind) => Some(MacroDefId {
|
|
|
|
krate: Some(krate),
|
|
|
|
ast_id: Some(ast_id),
|
|
|
|
kind: MacroDefKind::BuiltIn(kind),
|
|
|
|
}),
|
|
|
|
Either::Right(kind) => Some(MacroDefId {
|
|
|
|
krate: Some(krate),
|
|
|
|
ast_id: Some(ast_id),
|
|
|
|
kind: MacroDefKind::BuiltInEager(kind),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
register_builtin! {
|
2020-03-02 00:05:15 -06:00
|
|
|
LAZY:
|
2019-12-13 14:43:53 -06:00
|
|
|
(column, Column) => column_expand,
|
|
|
|
(compile_error, CompileError) => compile_error_expand,
|
|
|
|
(file, File) => file_expand,
|
|
|
|
(line, Line) => line_expand,
|
|
|
|
(stringify, Stringify) => stringify_expand,
|
|
|
|
(format_args, FormatArgs) => format_args_expand,
|
2019-12-21 06:33:44 -06:00
|
|
|
(env, Env) => env_expand,
|
|
|
|
(option_env, OptionEnv) => option_env_expand,
|
2019-12-06 03:57:20 -06:00
|
|
|
// format_args_nl only differs in that it adds a newline in the end,
|
|
|
|
// so we use the same stub expansion for now
|
2020-03-02 00:05:15 -06:00
|
|
|
(format_args_nl, FormatArgsNl) => format_args_expand,
|
|
|
|
|
|
|
|
EAGER:
|
2020-03-06 08:58:45 -06:00
|
|
|
(concat, Concat) => concat_expand,
|
|
|
|
(include, Include) => include_expand
|
2019-11-22 11:47:35 -06:00
|
|
|
}
|
|
|
|
|
2019-11-11 00:15:09 -06:00
|
|
|
fn line_expand(
|
2020-01-14 11:47:01 -06:00
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-11-11 00:15:09 -06:00
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
2020-01-14 11:47:01 -06:00
|
|
|
// dummy implementation for type-checking purposes
|
|
|
|
let line_num = 0;
|
2019-11-11 00:15:09 -06:00
|
|
|
let expanded = quote! {
|
|
|
|
#line_num
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
2019-11-22 00:56:50 -06:00
|
|
|
|
|
|
|
fn stringify_expand(
|
|
|
|
db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
id: LazyMacroId,
|
2019-11-22 00:56:50 -06:00
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
let loc = db.lookup_intern_macro(id);
|
|
|
|
|
|
|
|
let macro_content = {
|
2019-12-05 08:10:33 -06:00
|
|
|
let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
|
2019-12-20 08:43:01 -06:00
|
|
|
let macro_args = arg;
|
2019-11-22 00:56:50 -06:00
|
|
|
let text = macro_args.text();
|
|
|
|
let without_parens = TextUnit::of_char('(')..text.len() - TextUnit::of_char(')');
|
|
|
|
text.slice(without_parens).to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#macro_content
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
2019-11-22 09:05:04 -06:00
|
|
|
|
2019-12-21 06:33:44 -06:00
|
|
|
fn env_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-21 06:33:44 -06:00
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
// dummy implementation for type-checking purposes
|
2020-03-06 22:53:40 -06:00
|
|
|
let expanded = quote! { "__RA_UNIMPLEMENTATED__" };
|
2019-12-21 06:33:44 -06:00
|
|
|
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn option_env_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-21 06:33:44 -06:00
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
// dummy implementation for type-checking purposes
|
|
|
|
let expanded = quote! { std::option::Option::None::<&str> };
|
|
|
|
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
|
|
|
|
2019-11-22 07:48:33 -06:00
|
|
|
fn column_expand(
|
2020-01-14 11:47:01 -06:00
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-11-22 07:48:33 -06:00
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
2020-01-14 11:47:01 -06:00
|
|
|
// dummy implementation for type-checking purposes
|
|
|
|
let col_num = 0;
|
2019-11-22 07:48:33 -06:00
|
|
|
let expanded = quote! {
|
|
|
|
#col_num
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:05:04 -06:00
|
|
|
fn file_expand(
|
2019-12-05 08:10:33 -06:00
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-11-22 09:05:04 -06:00
|
|
|
_tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
// FIXME: RA purposefully lacks knowledge of absolute file names
|
|
|
|
// so just return "".
|
|
|
|
let file_name = "";
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#file_name
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
2019-11-22 11:47:35 -06:00
|
|
|
|
2019-11-24 18:01:51 -06:00
|
|
|
fn compile_error_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-11-24 18:01:51 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
if tt.count() == 1 {
|
2020-02-18 07:32:19 -06:00
|
|
|
if let tt::TokenTree::Leaf(tt::Leaf::Literal(it)) = &tt.token_trees[0] {
|
|
|
|
let s = it.text.as_str();
|
|
|
|
if s.contains('"') {
|
|
|
|
return Ok(quote! { loop { #it }});
|
2019-11-24 18:01:51 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(mbe::ExpandError::BindingError("Must be a string".into()))
|
|
|
|
}
|
|
|
|
|
2019-12-06 03:57:20 -06:00
|
|
|
fn format_args_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-06 12:30:01 -06:00
|
|
|
tt: &tt::Subtree,
|
2019-12-06 03:57:20 -06:00
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
2019-12-08 02:26:17 -06:00
|
|
|
// We expand `format_args!("", a1, a2)` to
|
|
|
|
// ```
|
|
|
|
// std::fmt::Arguments::new_v1(&[], &[
|
|
|
|
// std::fmt::ArgumentV1::new(&arg1,std::fmt::Display::fmt),
|
|
|
|
// std::fmt::ArgumentV1::new(&arg2,std::fmt::Display::fmt),
|
|
|
|
// ])
|
|
|
|
// ```,
|
2019-12-06 12:30:01 -06:00
|
|
|
// which is still not really correct, but close enough for now
|
|
|
|
let mut args = Vec::new();
|
|
|
|
let mut current = Vec::new();
|
|
|
|
for tt in tt.token_trees.iter().cloned() {
|
|
|
|
match tt {
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ',' => {
|
2019-12-08 02:26:17 -06:00
|
|
|
args.push(current);
|
2019-12-06 12:30:01 -06:00
|
|
|
current = Vec::new();
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
current.push(tt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !current.is_empty() {
|
2019-12-08 02:26:17 -06:00
|
|
|
args.push(current);
|
2019-12-06 12:30:01 -06:00
|
|
|
}
|
|
|
|
if args.is_empty() {
|
|
|
|
return Err(mbe::ExpandError::NoMatchingRule);
|
|
|
|
}
|
|
|
|
let _format_string = args.remove(0);
|
2019-12-08 02:26:17 -06:00
|
|
|
let arg_tts = args.into_iter().flat_map(|arg| {
|
|
|
|
quote! { std::fmt::ArgumentV1::new(&(##arg), std::fmt::Display::fmt), }
|
|
|
|
}.token_trees).collect::<Vec<_>>();
|
2019-12-06 03:57:20 -06:00
|
|
|
let expanded = quote! {
|
2019-12-06 12:30:01 -06:00
|
|
|
std::fmt::Arguments::new_v1(&[], &[##arg_tts])
|
2019-12-06 03:57:20 -06:00
|
|
|
};
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
fn unquote_str(lit: &tt::Literal) -> Option<String> {
|
|
|
|
let lit = ast::make::tokens::literal(&lit.to_string());
|
|
|
|
let token = ast::String::cast(lit)?;
|
|
|
|
token.value()
|
|
|
|
}
|
|
|
|
|
2020-03-06 08:58:45 -06:00
|
|
|
fn concat_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
|
|
|
_arg_id: EagerMacroId,
|
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
|
2020-03-02 00:05:15 -06:00
|
|
|
let mut text = String::new();
|
|
|
|
for (i, t) in tt.token_trees.iter().enumerate() {
|
|
|
|
match t {
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Literal(it)) if i % 2 == 0 => {
|
|
|
|
text += &unquote_str(&it).ok_or_else(|| mbe::ExpandError::ConversionError)?;
|
|
|
|
}
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if i % 2 == 1 && punct.char == ',' => (),
|
|
|
|
_ => return Err(mbe::ExpandError::UnexpectedToken),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((quote!(#text), FragmentKind::Expr))
|
|
|
|
}
|
|
|
|
|
2020-03-06 08:58:45 -06:00
|
|
|
fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> {
|
|
|
|
let call_site = call_id.as_file().original_file(db);
|
|
|
|
let path = RelativePath::new(&path);
|
|
|
|
|
|
|
|
db.resolve_relative_path(call_site, &path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn include_expand(
|
|
|
|
db: &dyn AstDatabase,
|
|
|
|
arg_id: EagerMacroId,
|
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
|
|
|
|
let path = tt
|
|
|
|
.token_trees
|
|
|
|
.get(0)
|
|
|
|
.and_then(|tt| match tt {
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => unquote_str(&it),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.ok_or_else(|| mbe::ExpandError::ConversionError)?;
|
|
|
|
|
|
|
|
let file_id =
|
|
|
|
relative_file(db, arg_id.into(), &path).ok_or_else(|| mbe::ExpandError::ConversionError)?;
|
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
// Handle include as expression
|
|
|
|
let node =
|
|
|
|
db.parse_or_expand(file_id.into()).ok_or_else(|| mbe::ExpandError::ConversionError)?;
|
|
|
|
let res =
|
|
|
|
mbe::syntax_node_to_token_tree(&node).ok_or_else(|| mbe::ExpandError::ConversionError)?.0;
|
|
|
|
|
|
|
|
Ok((res, FragmentKind::Items))
|
|
|
|
}
|
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-03-02 00:05:15 -06:00
|
|
|
use crate::{name::AsName, test_db::TestDB, AstNode, MacroCallId, MacroCallKind, MacroCallLoc};
|
2019-11-22 11:47:35 -06:00
|
|
|
use ra_db::{fixture::WithFixture, SourceDatabase};
|
2019-12-21 06:33:44 -06:00
|
|
|
use ra_syntax::ast::NameOwner;
|
2019-11-22 11:47:35 -06:00
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
fn expand_builtin_macro(ra_fixture: &str) -> String {
|
|
|
|
let (db, file_id) = TestDB::with_single_file(&ra_fixture);
|
2019-11-22 11:47:35 -06:00
|
|
|
let parsed = db.parse(file_id);
|
|
|
|
let macro_calls: Vec<_> =
|
2020-02-18 06:53:02 -06:00
|
|
|
parsed.syntax_node().descendants().filter_map(ast::MacroCall::cast).collect();
|
2019-11-22 11:47:35 -06:00
|
|
|
|
|
|
|
let ast_id_map = db.ast_id_map(file_id.into());
|
|
|
|
|
2020-03-02 00:05:15 -06:00
|
|
|
let expander = find_by_name(¯o_calls[0].name().unwrap().as_name()).unwrap();
|
|
|
|
let expander = expander.left().unwrap();
|
2019-12-21 06:33:44 -06:00
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
// the first one should be a macro_rules
|
|
|
|
let def = MacroDefId {
|
2019-12-05 08:10:33 -06:00
|
|
|
krate: Some(CrateId(0)),
|
|
|
|
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_calls[0]))),
|
2019-11-22 11:47:35 -06:00
|
|
|
kind: MacroDefKind::BuiltIn(expander),
|
|
|
|
};
|
|
|
|
|
|
|
|
let loc = MacroCallLoc {
|
|
|
|
def,
|
2019-12-05 08:10:33 -06:00
|
|
|
kind: MacroCallKind::FnLike(AstId::new(
|
|
|
|
file_id.into(),
|
|
|
|
ast_id_map.ast_id(¯o_calls[1]),
|
|
|
|
)),
|
2019-11-22 11:47:35 -06:00
|
|
|
};
|
|
|
|
|
2020-02-17 05:32:13 -06:00
|
|
|
let id: MacroCallId = db.intern_macro(loc).into();
|
2019-12-08 02:16:52 -06:00
|
|
|
let parsed = db.parse_or_expand(id.as_file()).unwrap();
|
2019-11-22 11:47:35 -06:00
|
|
|
|
|
|
|
parsed.text().to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_column_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! column {() => {}}
|
|
|
|
column!()
|
|
|
|
"#,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
2020-01-14 11:47:01 -06:00
|
|
|
assert_eq!(expanded, "0");
|
2019-11-22 11:47:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_line_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! line {() => {}}
|
|
|
|
line!()
|
|
|
|
"#,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
2020-01-14 11:47:01 -06:00
|
|
|
assert_eq!(expanded, "0");
|
2019-11-22 11:47:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stringify_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! stringify {() => {}}
|
|
|
|
stringify!(a b c)
|
|
|
|
"#,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, "\"a b c\"");
|
|
|
|
}
|
|
|
|
|
2019-12-21 06:33:44 -06:00
|
|
|
#[test]
|
|
|
|
fn test_env_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! env {() => {}}
|
|
|
|
env!("TEST_ENV_VAR")
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, "\"\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_option_env_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! option_env {() => {}}
|
|
|
|
option_env!("TEST_ENV_VAR")
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
2020-03-04 10:12:39 -06:00
|
|
|
assert_eq!(expanded, "std::option::Option::None:: < &str>");
|
2019-12-21 06:33:44 -06:00
|
|
|
}
|
|
|
|
|
2019-11-22 11:47:35 -06:00
|
|
|
#[test]
|
|
|
|
fn test_file_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! file {() => {}}
|
|
|
|
file!()
|
|
|
|
"#,
|
2019-11-22 11:47:35 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, "\"\"");
|
|
|
|
}
|
2019-11-24 18:01:51 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_compile_error_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! compile_error {
|
|
|
|
($msg:expr) => ({ /* compiler built-in */ });
|
|
|
|
($msg:expr,) => ({ /* compiler built-in */ })
|
|
|
|
}
|
|
|
|
compile_error!("error!");
|
|
|
|
"#,
|
2019-11-24 18:01:51 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(expanded, r#"loop{"error!"}"#);
|
|
|
|
}
|
2019-12-06 12:30:01 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_args_expand() {
|
|
|
|
let expanded = expand_builtin_macro(
|
|
|
|
r#"
|
2019-12-21 06:33:44 -06:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! format_args {
|
|
|
|
($fmt:expr) => ({ /* compiler built-in */ });
|
|
|
|
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
|
|
|
|
}
|
|
|
|
format_args!("{} {:?}", arg1(a, b, c), arg2);
|
|
|
|
"#,
|
2019-12-06 12:30:01 -06:00
|
|
|
);
|
|
|
|
|
2019-12-12 07:34:03 -06:00
|
|
|
assert_eq!(
|
|
|
|
expanded,
|
2020-03-04 10:12:39 -06:00
|
|
|
r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#
|
2019-12-12 07:34:03 -06:00
|
|
|
);
|
2019-12-06 12:30:01 -06:00
|
|
|
}
|
2019-11-22 11:47:35 -06:00
|
|
|
}
|