From c11737cd63da1893947d0d7d43ab4bfb0fdeb0ad Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 1 Dec 2023 14:58:57 +0100 Subject: [PATCH] Simplify include handling --- crates/hir-expand/src/builtin_fn_macro.rs | 55 +++++++++---------- crates/hir-expand/src/db.rs | 6 -- crates/hir-expand/src/lib.rs | 7 +-- crates/hir/src/db.rs | 4 +- crates/ide-db/src/apply_change.rs | 1 - .../src/handlers/unresolved_macro_call.rs | 16 ++++++ 6 files changed, 46 insertions(+), 43 deletions(-) diff --git a/crates/hir-expand/src/builtin_fn_macro.rs b/crates/hir-expand/src/builtin_fn_macro.rs index 726b983536..74ca2f7ec4 100644 --- a/crates/hir-expand/src/builtin_fn_macro.rs +++ b/crates/hir-expand/src/builtin_fn_macro.rs @@ -17,7 +17,7 @@ use crate::{ hygiene::span_with_def_site_ctxt, name, quote, tt::{self, DelimSpan}, - EagerCallInfo, ExpandError, ExpandResult, HirFileIdExt, MacroCallId, MacroCallLoc, + ExpandError, ExpandResult, HirFileIdExt, MacroCallId, MacroCallLoc, }; macro_rules! register_builtin { @@ -575,36 +575,32 @@ fn parse_string(tt: &tt::Subtree) -> Result { fn include_expand( db: &dyn ExpandDatabase, arg_id: MacroCallId, - _tt: &tt::Subtree, + tt: &tt::Subtree, span: SpanData, ) -> ExpandResult { - match db.include_expand(arg_id) { - Ok((res, _)) => ExpandResult::ok(res.as_ref().clone()), - Err(e) => ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e), - } -} - -// FIXME: Check if this is still needed now after the token map rewrite -pub(crate) fn include_arg_to_tt( - db: &dyn ExpandDatabase, - arg_id: MacroCallId, -) -> Result<(triomphe::Arc, FileId), ExpandError> { - let loc = db.lookup_intern_macro_call(arg_id); - let Some(EagerCallInfo { arg, arg_id, .. }) = loc.eager.as_deref() else { - panic!("include_arg_to_tt called on non include macro call: {:?}", &loc.eager); + let path = match parse_string(tt) { + Ok(it) => it, + Err(e) => { + return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e) + } }; - let path = parse_string(&arg)?; - let file_id = relative_file(db, *arg_id, &path, false)?; - - // why are we not going through a SyntaxNode here? - let subtree = parse_to_token_tree( + let file_id = match relative_file(db, arg_id, &path, false) { + Ok(file_id) => file_id, + Err(e) => { + return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e); + } + }; + match parse_to_token_tree( SpanAnchor { file_id, ast_id: ROOT_ERASED_FILE_AST_ID }, - // FIXME SyntaxContextId::ROOT, &db.file_text(file_id), - ) - .ok_or(mbe::ExpandError::ConversionError)?; - Ok((triomphe::Arc::new(subtree), file_id)) + ) { + Some(it) => ExpandResult::ok(it), + None => ExpandResult::new( + tt::Subtree::empty(DelimSpan { open: span, close: span }), + ExpandError::other("failed to parse included file"), + ), + } } fn include_bytes_expand( @@ -613,9 +609,12 @@ fn include_bytes_expand( tt: &tt::Subtree, span: SpanData, ) -> ExpandResult { - if let Err(e) = parse_string(tt) { - return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e); - } + let _path = match parse_string(tt) { + Ok(it) => it, + Err(e) => { + return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e) + } + }; // FIXME: actually read the file here if the user asked for macro expansion let res = tt::Subtree { diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 7f77a2f51b..00755b16e9 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -142,12 +142,6 @@ pub trait ExpandDatabase: SourceDatabase { def_crate: CrateId, id: AstId, ) -> Arc; - - #[salsa::invoke(crate::builtin_fn_macro::include_arg_to_tt)] - fn include_expand( - &self, - arg_id: MacroCallId, - ) -> Result<(triomphe::Arc, base_db::FileId), ExpandError>; /// Special case of the previous query for procedural macros. We can't LRU /// proc macros, since they are not deterministic in general, and /// non-determinism breaks salsa in a very, very, very bad way. diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs index 5915fe6e34..b5f5fdd22e 100644 --- a/crates/hir-expand/src/lib.rs +++ b/crates/hir-expand/src/lib.rs @@ -208,12 +208,7 @@ impl HirFileIdExt for HirFileId { match file_id.repr() { HirFileIdRepr::FileId(id) => break id, HirFileIdRepr::MacroFile(MacroFileId { macro_call_id }) => { - let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_call_id); - let is_include_expansion = loc.def.is_include() && loc.eager.is_some(); - file_id = match is_include_expansion.then(|| db.include_expand(macro_call_id)) { - Some(Ok((_, file))) => file.into(), - _ => loc.kind.file_id(), - } + file_id = db.lookup_intern_macro_call(macro_call_id).kind.file_id(); } } } diff --git a/crates/hir/src/db.rs b/crates/hir/src/db.rs index ff6f987aa1..d98e3decd2 100644 --- a/crates/hir/src/db.rs +++ b/crates/hir/src/db.rs @@ -23,7 +23,7 @@ pub use hir_def::db::{ }; pub use hir_expand::db::{ AstIdMapQuery, DeclMacroExpanderQuery, ExpandDatabase, ExpandDatabaseStorage, - ExpandProcMacroQuery, IncludeExpandQuery, InternMacroCallQuery, InternSyntaxContextQuery, - MacroArgQuery, ParseMacroExpansionErrorQuery, ParseMacroExpansionQuery, RealSpanMapQuery, + ExpandProcMacroQuery, InternMacroCallQuery, InternSyntaxContextQuery, MacroArgQuery, + ParseMacroExpansionErrorQuery, ParseMacroExpansionQuery, RealSpanMapQuery, }; pub use hir_ty::db::*; diff --git a/crates/ide-db/src/apply_change.rs b/crates/ide-db/src/apply_change.rs index 67e686dad1..343be870c9 100644 --- a/crates/ide-db/src/apply_change.rs +++ b/crates/ide-db/src/apply_change.rs @@ -99,7 +99,6 @@ impl RootDatabase { hir::db::AstIdMapQuery hir::db::DeclMacroExpanderQuery hir::db::ExpandProcMacroQuery - hir::db::IncludeExpandQuery hir::db::InternMacroCallQuery hir::db::InternSyntaxContextQuery hir::db::MacroArgQuery diff --git a/crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs b/crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs index 33e7c2e37c..edcfa073a0 100644 --- a/crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs +++ b/crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs @@ -67,6 +67,22 @@ macro_rules! m { () => {} } } self::m!(); self::m2!(); //^^ error: unresolved macro `self::m2!` +"#, + ); + } + + #[test] + #[should_panic] // FIXME: https://github.com/rust-lang/rust-analyzer/issues/14968 + fn include_does_not_break_diagnostics() { + check_diagnostics( + r#" +//- minicore: include +//- /lib.rs crate:lib +include!("include-me.rs"); +//- /include-me.rs +/// long doc that pushes the diagnostic range beyond the first file's text length +#[err] +mod prim_never {} "#, ); }