Skip to content

Commit c11737c

Browse files
committed
Simplify include handling
1 parent 0003e56 commit c11737c

File tree

6 files changed

+46
-43
lines changed

6 files changed

+46
-43
lines changed

crates/hir-expand/src/builtin_fn_macro.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
hygiene::span_with_def_site_ctxt,
1818
name, quote,
1919
tt::{self, DelimSpan},
20-
EagerCallInfo, ExpandError, ExpandResult, HirFileIdExt, MacroCallId, MacroCallLoc,
20+
ExpandError, ExpandResult, HirFileIdExt, MacroCallId, MacroCallLoc,
2121
};
2222

2323
macro_rules! register_builtin {
@@ -575,36 +575,32 @@ fn parse_string(tt: &tt::Subtree) -> Result<String, ExpandError> {
575575
fn include_expand(
576576
db: &dyn ExpandDatabase,
577577
arg_id: MacroCallId,
578-
_tt: &tt::Subtree,
578+
tt: &tt::Subtree,
579579
span: SpanData,
580580
) -> ExpandResult<tt::Subtree> {
581-
match db.include_expand(arg_id) {
582-
Ok((res, _)) => ExpandResult::ok(res.as_ref().clone()),
583-
Err(e) => ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e),
584-
}
585-
}
586-
587-
// FIXME: Check if this is still needed now after the token map rewrite
588-
pub(crate) fn include_arg_to_tt(
589-
db: &dyn ExpandDatabase,
590-
arg_id: MacroCallId,
591-
) -> Result<(triomphe::Arc<tt::Subtree>, FileId), ExpandError> {
592-
let loc = db.lookup_intern_macro_call(arg_id);
593-
let Some(EagerCallInfo { arg, arg_id, .. }) = loc.eager.as_deref() else {
594-
panic!("include_arg_to_tt called on non include macro call: {:?}", &loc.eager);
581+
let path = match parse_string(tt) {
582+
Ok(it) => it,
583+
Err(e) => {
584+
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e)
585+
}
595586
};
596-
let path = parse_string(&arg)?;
597-
let file_id = relative_file(db, *arg_id, &path, false)?;
598-
599-
// why are we not going through a SyntaxNode here?
600-
let subtree = parse_to_token_tree(
587+
let file_id = match relative_file(db, arg_id, &path, false) {
588+
Ok(file_id) => file_id,
589+
Err(e) => {
590+
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e);
591+
}
592+
};
593+
match parse_to_token_tree(
601594
SpanAnchor { file_id, ast_id: ROOT_ERASED_FILE_AST_ID },
602-
// FIXME
603595
SyntaxContextId::ROOT,
604596
&db.file_text(file_id),
605-
)
606-
.ok_or(mbe::ExpandError::ConversionError)?;
607-
Ok((triomphe::Arc::new(subtree), file_id))
597+
) {
598+
Some(it) => ExpandResult::ok(it),
599+
None => ExpandResult::new(
600+
tt::Subtree::empty(DelimSpan { open: span, close: span }),
601+
ExpandError::other("failed to parse included file"),
602+
),
603+
}
608604
}
609605

610606
fn include_bytes_expand(
@@ -613,9 +609,12 @@ fn include_bytes_expand(
613609
tt: &tt::Subtree,
614610
span: SpanData,
615611
) -> ExpandResult<tt::Subtree> {
616-
if let Err(e) = parse_string(tt) {
617-
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e);
618-
}
612+
let _path = match parse_string(tt) {
613+
Ok(it) => it,
614+
Err(e) => {
615+
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e)
616+
}
617+
};
619618

620619
// FIXME: actually read the file here if the user asked for macro expansion
621620
let res = tt::Subtree {

crates/hir-expand/src/db.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,6 @@ pub trait ExpandDatabase: SourceDatabase {
142142
def_crate: CrateId,
143143
id: AstId<ast::Macro>,
144144
) -> Arc<DeclarativeMacroExpander>;
145-
146-
#[salsa::invoke(crate::builtin_fn_macro::include_arg_to_tt)]
147-
fn include_expand(
148-
&self,
149-
arg_id: MacroCallId,
150-
) -> Result<(triomphe::Arc<tt::Subtree>, base_db::FileId), ExpandError>;
151145
/// Special case of the previous query for procedural macros. We can't LRU
152146
/// proc macros, since they are not deterministic in general, and
153147
/// non-determinism breaks salsa in a very, very, very bad way.

crates/hir-expand/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,7 @@ impl HirFileIdExt for HirFileId {
208208
match file_id.repr() {
209209
HirFileIdRepr::FileId(id) => break id,
210210
HirFileIdRepr::MacroFile(MacroFileId { macro_call_id }) => {
211-
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_call_id);
212-
let is_include_expansion = loc.def.is_include() && loc.eager.is_some();
213-
file_id = match is_include_expansion.then(|| db.include_expand(macro_call_id)) {
214-
Some(Ok((_, file))) => file.into(),
215-
_ => loc.kind.file_id(),
216-
}
211+
file_id = db.lookup_intern_macro_call(macro_call_id).kind.file_id();
217212
}
218213
}
219214
}

crates/hir/src/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use hir_def::db::{
2323
};
2424
pub use hir_expand::db::{
2525
AstIdMapQuery, DeclMacroExpanderQuery, ExpandDatabase, ExpandDatabaseStorage,
26-
ExpandProcMacroQuery, IncludeExpandQuery, InternMacroCallQuery, InternSyntaxContextQuery,
27-
MacroArgQuery, ParseMacroExpansionErrorQuery, ParseMacroExpansionQuery, RealSpanMapQuery,
26+
ExpandProcMacroQuery, InternMacroCallQuery, InternSyntaxContextQuery, MacroArgQuery,
27+
ParseMacroExpansionErrorQuery, ParseMacroExpansionQuery, RealSpanMapQuery,
2828
};
2929
pub use hir_ty::db::*;

crates/ide-db/src/apply_change.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ impl RootDatabase {
9999
hir::db::AstIdMapQuery
100100
hir::db::DeclMacroExpanderQuery
101101
hir::db::ExpandProcMacroQuery
102-
hir::db::IncludeExpandQuery
103102
hir::db::InternMacroCallQuery
104103
hir::db::InternSyntaxContextQuery
105104
hir::db::MacroArgQuery

crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ macro_rules! m { () => {} } }
6767
6868
self::m!(); self::m2!();
6969
//^^ error: unresolved macro `self::m2!`
70+
"#,
71+
);
72+
}
73+
74+
#[test]
75+
#[should_panic] // FIXME: https://github.com/rust-lang/rust-analyzer/issues/14968
76+
fn include_does_not_break_diagnostics() {
77+
check_diagnostics(
78+
r#"
79+
//- minicore: include
80+
//- /lib.rs crate:lib
81+
include!("include-me.rs");
82+
//- /include-me.rs
83+
/// long doc that pushes the diagnostic range beyond the first file's text length
84+
#[err]
85+
mod prim_never {}
7086
"#,
7187
);
7288
}

0 commit comments

Comments
 (0)