Skip to content

Implement include macro #3494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions crates/ra_hir_expand/src/builtin_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use crate::{
name, AstId, CrateId, MacroDefId, MacroDefKind, TextUnit,
};

use crate::{quote, LazyMacroId};
use crate::{quote, EagerMacroId, LazyMacroId, MacroCallId};
use either::Either;
use ra_db::{FileId, RelativePath};
use ra_parser::FragmentKind;

macro_rules! register_builtin {
Expand Down Expand Up @@ -38,12 +39,14 @@ macro_rules! register_builtin {
impl EagerExpander {
pub fn expand(
&self,
db: &dyn AstDatabase,
arg_id: EagerMacroId,
tt: &tt::Subtree,
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
let expander = match *self {
$( EagerExpander::$e_kind => $e_expand, )*
};
expander(tt)
expander(db,arg_id,tt)
}
}

Expand Down Expand Up @@ -80,7 +83,6 @@ pub fn find_builtin_macro(

register_builtin! {
LAZY:

(column, Column) => column_expand,
(compile_error, CompileError) => compile_error_expand,
(file, File) => file_expand,
Expand All @@ -94,8 +96,8 @@ register_builtin! {
(format_args_nl, FormatArgsNl) => format_args_expand,

EAGER:
// eagers
(concat, Concat) => concat_expand
(concat, Concat) => concat_expand,
(include, Include) => include_expand
}

fn line_expand(
Expand Down Expand Up @@ -251,7 +253,11 @@ fn unquote_str(lit: &tt::Literal) -> Option<String> {
token.value()
}

fn concat_expand(tt: &tt::Subtree) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
fn concat_expand(
_db: &dyn AstDatabase,
_arg_id: EagerMacroId,
tt: &tt::Subtree,
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
let mut text = String::new();
for (i, t) in tt.token_trees.iter().enumerate() {
match t {
Expand All @@ -266,6 +272,40 @@ fn concat_expand(tt: &tt::Subtree) -> Result<(tt::Subtree, FragmentKind), mbe::E
Ok((quote!(#text), FragmentKind::Expr))
}

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)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, ideally, I think we should do something like parse_to_token_tree(db.file_text(file_id)). That is, it seems like we don't need to construct a syntax tree at this point at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. It will be my next PR :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Submitted #3518

let res =
mbe::syntax_node_to_token_tree(&node).ok_or_else(|| mbe::ExpandError::ConversionError)?.0;

Ok((res, FragmentKind::Items))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
28 changes: 15 additions & 13 deletions crates/ra_hir_expand/src/eager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,26 @@ pub fn expand_eager_macro(
// When `lazy_expand` is called, its *parent* file must be already exists.
// Here we store an eager macro id for the argument expanded subtree here
// for that purpose.
let arg_id: MacroCallId = db
.intern_eager_expansion({
EagerCallLoc {
def,
fragment: FragmentKind::Expr,
subtree: Arc::new(parsed_args.clone()),
file_id: macro_call.file_id,
}
})
.into();
let arg_id = db.intern_eager_expansion({
EagerCallLoc {
def,
fragment: FragmentKind::Expr,
subtree: Arc::new(parsed_args.clone()),
file_id: macro_call.file_id,
}
});
let arg_file_id: MacroCallId = arg_id.into();

let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, FragmentKind::Expr).ok()?.0;
let result =
eager_macro_recur(db, InFile::new(arg_id.as_file(), parsed_args.syntax_node()), resolver)?;
let result = eager_macro_recur(
db,
InFile::new(arg_file_id.as_file(), parsed_args.syntax_node()),
resolver,
)?;
let subtree = to_subtree(&result)?;

if let MacroDefKind::BuiltInEager(eager) = def.kind {
let (subtree, fragment) = eager.expand(&subtree).ok()?;
let (subtree, fragment) = eager.expand(db, arg_id, &subtree).ok()?;
let eager =
EagerCallLoc { def, fragment, subtree: Arc::new(subtree), file_id: macro_call.file_id };

Expand Down
1 change: 1 addition & 0 deletions crates/ra_hir_expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub mod known {
line,
stringify,
concat,
include,
format_args,
format_args_nl,
env,
Expand Down
45 changes: 45 additions & 0 deletions crates/ra_hir_ty/src/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,51 @@ fn main() {
);
}

#[test]
fn infer_builtin_macros_include() {
let (db, pos) = TestDB::with_position(
r#"
//- /main.rs
#[rustc_builtin_macro]
macro_rules! include {() => {}}

include!("foo.rs");

fn main() {
bar()<|>;
}

//- /foo.rs
fn bar() -> u32 {0}
"#,
);
assert_eq!("u32", type_at_pos(&db, pos));
}

#[test]
fn infer_builtin_macros_include_concat() {
let (db, pos) = TestDB::with_position(
r#"
//- /main.rs
#[rustc_builtin_macro]
macro_rules! include {() => {}}

#[rustc_builtin_macro]
macro_rules! concat {() => {}}

include!(concat!("f", "oo.rs"));

fn main() {
bar()<|>;
}

//- /foo.rs
fn bar() -> u32 {0}
"#,
);
assert_eq!("u32", type_at_pos(&db, pos));
}

#[test]
fn infer_builtin_macros_concat_with_lazy() {
assert_snapshot!(
Expand Down