Skip to content

Commit 8cd98bd

Browse files
Merge #9046
9046: fix: make `include!` etc. work in expression position r=jonas-schievink a=jonas-schievink This PR removes determination of fragment kinds from the eager macro implementations. The fragment kind is always determined by the syntax position in which a macro is invoked, not by the macro implementation, even for eager macros. This makes `include!` work in expression position, and should have the same effect for all macros that may be used in different positions. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 247faf2 + 31588ae commit 8cd98bd

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

crates/hir_expand/src/builtin_macro.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use base_db::{AnchoredPath, Edition, FileId};
88
use cfg::CfgExpr;
99
use either::Either;
1010
use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult};
11-
use parser::FragmentKind;
1211
use syntax::ast::{self, AstToken};
1312

1413
macro_rules! register_builtin {
@@ -47,7 +46,7 @@ macro_rules! register_builtin {
4746
let expander = match *self {
4847
$( EagerExpander::$e_kind => $e_expand, )*
4948
};
50-
expander(db,arg_id,tt)
49+
expander(db, arg_id, tt)
5150
}
5251
}
5352

@@ -64,14 +63,13 @@ macro_rules! register_builtin {
6463
#[derive(Debug)]
6564
pub struct ExpandedEager {
6665
pub(crate) subtree: tt::Subtree,
67-
pub(crate) fragment: FragmentKind,
6866
/// The included file ID of the include macro.
6967
pub(crate) included_file: Option<FileId>,
7068
}
7169

7270
impl ExpandedEager {
73-
fn new(subtree: tt::Subtree, fragment: FragmentKind) -> Self {
74-
ExpandedEager { subtree, fragment, included_file: None }
71+
fn new(subtree: tt::Subtree) -> Self {
72+
ExpandedEager { subtree, included_file: None }
7573
}
7674
}
7775

@@ -340,7 +338,7 @@ fn compile_error_expand(
340338
_ => mbe::ExpandError::BindingError("`compile_error!` argument must be a string".into()),
341339
};
342340

343-
ExpandResult { value: Some(ExpandedEager::new(quote! {}, FragmentKind::Items)), err: Some(err) }
341+
ExpandResult { value: Some(ExpandedEager::new(quote! {})), err: Some(err) }
344342
}
345343

346344
fn concat_expand(
@@ -371,7 +369,7 @@ fn concat_expand(
371369
}
372370
}
373371
}
374-
ExpandResult { value: Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr)), err }
372+
ExpandResult { value: Some(ExpandedEager::new(quote!(#text))), err }
375373
}
376374

377375
fn concat_idents_expand(
@@ -393,7 +391,7 @@ fn concat_idents_expand(
393391
}
394392
}
395393
let ident = tt::Ident { text: ident.into(), id: tt::TokenId::unspecified() };
396-
ExpandResult { value: Some(ExpandedEager::new(quote!(#ident), FragmentKind::Expr)), err }
394+
ExpandResult { value: Some(ExpandedEager::new(quote!(#ident))), err }
397395
}
398396

399397
fn relative_file(
@@ -442,14 +440,7 @@ fn include_expand(
442440

443441
match res {
444442
Ok((subtree, file_id)) => {
445-
// FIXME:
446-
// Handle include as expression
447-
448-
ExpandResult::ok(Some(ExpandedEager {
449-
subtree,
450-
fragment: FragmentKind::Items,
451-
included_file: Some(file_id),
452-
}))
443+
ExpandResult::ok(Some(ExpandedEager { subtree, included_file: Some(file_id) }))
453444
}
454445
Err(e) => ExpandResult::only_err(e),
455446
}
@@ -472,7 +463,7 @@ fn include_bytes_expand(
472463
id: tt::TokenId::unspecified(),
473464
}))],
474465
};
475-
ExpandResult::ok(Some(ExpandedEager::new(res, FragmentKind::Expr)))
466+
ExpandResult::ok(Some(ExpandedEager::new(res)))
476467
}
477468

478469
fn include_str_expand(
@@ -492,14 +483,14 @@ fn include_str_expand(
492483
let file_id = match relative_file(db, arg_id.into(), &path, true) {
493484
Ok(file_id) => file_id,
494485
Err(_) => {
495-
return ExpandResult::ok(Some(ExpandedEager::new(quote!(""), FragmentKind::Expr)));
486+
return ExpandResult::ok(Some(ExpandedEager::new(quote!(""))));
496487
}
497488
};
498489

499490
let text = db.file_text(file_id);
500491
let text = &*text;
501492

502-
ExpandResult::ok(Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr)))
493+
ExpandResult::ok(Some(ExpandedEager::new(quote!(#text))))
503494
}
504495

505496
fn get_env_inner(db: &dyn AstDatabase, arg_id: MacroCallId, key: &str) -> Option<String> {
@@ -535,7 +526,7 @@ fn env_expand(
535526
});
536527
let expanded = quote! { #s };
537528

538-
ExpandResult { value: Some(ExpandedEager::new(expanded, FragmentKind::Expr)), err }
529+
ExpandResult { value: Some(ExpandedEager::new(expanded)), err }
539530
}
540531

541532
fn option_env_expand(
@@ -553,7 +544,7 @@ fn option_env_expand(
553544
Some(s) => quote! { std::option::Some(#s) },
554545
};
555546

556-
ExpandResult::ok(Some(ExpandedEager::new(expanded, FragmentKind::Expr)))
547+
ExpandResult::ok(Some(ExpandedEager::new(expanded)))
557548
}
558549

559550
#[cfg(test)]
@@ -565,6 +556,7 @@ mod tests {
565556
};
566557
use base_db::{fixture::WithFixture, SourceDatabase};
567558
use expect_test::{expect, Expect};
559+
use parser::FragmentKind;
568560
use std::sync::Arc;
569561
use syntax::ast::NameOwner;
570562

@@ -617,6 +609,7 @@ mod tests {
617609
local_inner: false,
618610
};
619611

612+
let fragment = crate::to_fragment_kind(&macro_call);
620613
let args = macro_call.token_tree().unwrap();
621614
let parsed_args = mbe::ast_to_token_tree(&args).0;
622615
let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call));
@@ -639,7 +632,7 @@ mod tests {
639632
arg_or_expansion: Arc::new(expanded.subtree),
640633
included_file: expanded.included_file,
641634
}),
642-
kind: MacroCallKind::FnLike { ast_id: call_id, fragment: expanded.fragment },
635+
kind: MacroCallKind::FnLike { ast_id: call_id, fragment },
643636
};
644637

645638
let id: MacroCallId = db.intern_macro(loc).into();

crates/hir_expand/src/eager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub fn expand_eager_macro(
113113

114114
let ast_map = db.ast_id_map(macro_call.file_id);
115115
let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(&macro_call.value));
116+
let fragment = crate::to_fragment_kind(&macro_call.value);
116117

117118
// Note:
118119
// When `lazy_expand` is called, its *parent* file must be already exists.
@@ -152,7 +153,7 @@ pub fn expand_eager_macro(
152153
arg_or_expansion: Arc::new(expanded.subtree),
153154
included_file: expanded.included_file,
154155
}),
155-
kind: MacroCallKind::FnLike { ast_id: call_id, fragment: expanded.fragment },
156+
kind: MacroCallKind::FnLike { ast_id: call_id, fragment },
156157
};
157158

158159
Ok(db.intern_macro(loc))

crates/hir_ty/src/tests/macros.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,24 @@ fn bar() -> u32 {0}
751751
);
752752
}
753753

754+
#[test]
755+
fn infer_builtin_macros_include_expression() {
756+
check_types(
757+
r#"
758+
//- /main.rs
759+
#[rustc_builtin_macro]
760+
macro_rules! include {() => {}}
761+
fn main() {
762+
let i = include!("bla.rs");
763+
i;
764+
//^ i32
765+
}
766+
//- /bla.rs
767+
0
768+
"#,
769+
)
770+
}
771+
754772
#[test]
755773
fn infer_builtin_macros_include_child_mod() {
756774
check_types(

0 commit comments

Comments
 (0)