From 8ebabce475c30c854c2310400b7984b637f8cf26 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 12 Jun 2024 08:15:58 +0200 Subject: [PATCH] internal: Remove FileId::BOGUS --- crates/hir-def/src/lib.rs | 7 ++- crates/hir-def/src/nameres/collector.rs | 63 ++++++++----------------- crates/hir-expand/src/db.rs | 8 ++-- crates/hir-expand/src/fixup.rs | 14 ++++-- crates/hir-expand/src/quote.rs | 2 +- crates/mbe/src/syntax_bridge.rs | 4 +- crates/span/src/lib.rs | 11 +---- crates/vfs/src/lib.rs | 3 -- 8 files changed, 41 insertions(+), 71 deletions(-) diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs index b86703c3cb..b1ed0d90f0 100644 --- a/crates/hir-def/src/lib.rs +++ b/crates/hir-def/src/lib.rs @@ -90,7 +90,7 @@ use hir_expand::{ use item_tree::ExternBlock; use la_arena::Idx; use nameres::DefMap; -use span::{AstIdNode, Edition, FileAstId, FileId, SyntaxContextId}; +use span::{AstIdNode, Edition, FileAstId, SyntaxContextId}; use stdx::impl_from; use syntax::{ast, AstNode}; @@ -958,15 +958,14 @@ impl GenericDefId { match self { GenericDefId::FunctionId(it) => file_id_and_params_of_item_loc(db, it), GenericDefId::TypeAliasId(it) => file_id_and_params_of_item_loc(db, it), - GenericDefId::ConstId(_) => (FileId::BOGUS.into(), None), GenericDefId::AdtId(AdtId::StructId(it)) => file_id_and_params_of_item_loc(db, it), GenericDefId::AdtId(AdtId::UnionId(it)) => file_id_and_params_of_item_loc(db, it), GenericDefId::AdtId(AdtId::EnumId(it)) => file_id_and_params_of_item_loc(db, it), GenericDefId::TraitId(it) => file_id_and_params_of_item_loc(db, it), GenericDefId::TraitAliasId(it) => file_id_and_params_of_item_loc(db, it), GenericDefId::ImplId(it) => file_id_and_params_of_item_loc(db, it), - // We won't be using this ID anyway - GenericDefId::EnumVariantId(_) => (FileId::BOGUS.into(), None), + GenericDefId::ConstId(it) => (it.lookup(db).id.file_id(), None), + GenericDefId::EnumVariantId(it) => (it.lookup(db).id.file_id(), None), } } diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs index cf5e0c22ce..f76b0c362f 100644 --- a/crates/hir-def/src/nameres/collector.rs +++ b/crates/hir-def/src/nameres/collector.rs @@ -21,7 +21,7 @@ use itertools::{izip, Itertools}; use la_arena::Idx; use limit::Limit; use rustc_hash::{FxHashMap, FxHashSet}; -use span::{Edition, ErasedFileAstId, FileAstId, Span, SyntaxContextId}; +use span::{Edition, ErasedFileAstId, FileAstId, SyntaxContextId}; use syntax::ast; use triomphe::Arc; @@ -75,36 +75,23 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI let proc_macros = if krate.is_proc_macro { match db.proc_macros().get(&def_map.krate) { - Some(Ok(proc_macros)) => { - Ok(proc_macros - .iter() - .enumerate() - .map(|(idx, it)| { - // FIXME: a hacky way to create a Name from string. - let name = tt::Ident { - text: it.name.clone(), - span: Span { - range: syntax::TextRange::empty(syntax::TextSize::new(0)), - anchor: span::SpanAnchor { - file_id: FileId::BOGUS, - ast_id: span::ROOT_ERASED_FILE_AST_ID, - }, - ctx: SyntaxContextId::ROOT, - }, - }; - ( - name.as_name(), - if it.disabled { - CustomProcMacroExpander::disabled() - } else { - CustomProcMacroExpander::new( - hir_expand::proc_macro::ProcMacroId::new(idx as u32), - ) - }, - ) - }) - .collect()) - } + Some(Ok(proc_macros)) => Ok(proc_macros + .iter() + .enumerate() + .map(|(idx, it)| { + let name = Name::new_text_dont_use(it.name.clone()); + ( + name, + if it.disabled { + CustomProcMacroExpander::disabled() + } else { + CustomProcMacroExpander::new(hir_expand::proc_macro::ProcMacroId::new( + idx as u32, + )) + }, + ) + }) + .collect()), Some(Err(e)) => Err(e.clone().into_boxed_str()), None => Err("No proc-macros present for crate".to_owned().into_boxed_str()), } @@ -2154,19 +2141,7 @@ impl ModCollector<'_, '_> { let name; let name = match attrs.by_key("rustc_builtin_macro").string_value() { Some(it) => { - // FIXME: a hacky way to create a Name from string. - name = tt::Ident { - text: it.into(), - span: Span { - range: syntax::TextRange::empty(syntax::TextSize::new(0)), - anchor: span::SpanAnchor { - file_id: FileId::BOGUS, - ast_id: span::ROOT_ERASED_FILE_AST_ID, - }, - ctx: SyntaxContextId::ROOT, - }, - } - .as_name(); + name = Name::new_text_dont_use(it.into()); &name } None => { diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 30042ca78a..ad25a1168c 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -146,13 +146,11 @@ pub fn expand_speculative( token_to_map: SyntaxToken, ) -> Option<(SyntaxNode, SyntaxToken)> { let loc = db.lookup_intern_macro_call(actual_macro_call); - - // FIXME: This BOGUS here is dangerous once the proc-macro server can call back into the database! - let span_map = RealSpanMap::absolute(FileId::BOGUS); - let span_map = SpanMapRef::RealSpanMap(&span_map); - let (_, _, span) = db.macro_arg_considering_derives(actual_macro_call, &loc.kind); + let span_map = RealSpanMap::absolute(span.anchor.file_id); + let span_map = SpanMapRef::RealSpanMap(&span_map); + // Build the subtree and token mapping for the speculative args let (mut tt, undo_info) = match loc.kind { MacroCallKind::FnLike { .. } => ( diff --git a/crates/hir-expand/src/fixup.rs b/crates/hir-expand/src/fixup.rs index 9ec2a83162..f83e266744 100644 --- a/crates/hir-expand/src/fixup.rs +++ b/crates/hir-expand/src/fixup.rs @@ -4,7 +4,10 @@ use mbe::DocCommentDesugarMode; use rustc_hash::{FxHashMap, FxHashSet}; use smallvec::SmallVec; -use span::{ErasedFileAstId, Span, SpanAnchor, FIXUP_ERASED_FILE_AST_ID_MARKER}; +use span::{ + ErasedFileAstId, Span, SpanAnchor, SyntaxContextId, FIXUP_ERASED_FILE_AST_ID_MARKER, + ROOT_ERASED_FILE_AST_ID, +}; use stdx::never; use syntax::{ ast::{self, AstNode, HasLoopBody}, @@ -307,8 +310,13 @@ pub(crate) fn reverse_fixups(tt: &mut Subtree, undo_info: &SyntaxFixupUndoInfo) tt.delimiter.close.anchor.ast_id == FIXUP_DUMMY_AST_ID || tt.delimiter.open.anchor.ast_id == FIXUP_DUMMY_AST_ID ) { - tt.delimiter.close = Span::DUMMY; - tt.delimiter.open = Span::DUMMY; + let span = |file_id| Span { + range: TextRange::empty(TextSize::new(0)), + anchor: SpanAnchor { file_id, ast_id: ROOT_ERASED_FILE_AST_ID }, + ctx: SyntaxContextId::ROOT, + }; + tt.delimiter.open = span(tt.delimiter.open.anchor.file_id); + tt.delimiter.close = span(tt.delimiter.close.anchor.file_id); } reverse_fixups_(tt, undo_info); } diff --git a/crates/hir-expand/src/quote.rs b/crates/hir-expand/src/quote.rs index a31a111c91..8f1e32321e 100644 --- a/crates/hir-expand/src/quote.rs +++ b/crates/hir-expand/src/quote.rs @@ -231,7 +231,7 @@ mod tests { const DUMMY: tt::Span = tt::Span { range: TextRange::empty(TextSize::new(0)), - anchor: SpanAnchor { file_id: FileId::BOGUS, ast_id: ROOT_ERASED_FILE_AST_ID }, + anchor: SpanAnchor { file_id: FileId::from_raw(0xe4e4e), ast_id: ROOT_ERASED_FILE_AST_ID }, ctx: SyntaxContextId::ROOT, }; diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 412e492176..c8ff8c35e9 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -47,7 +47,7 @@ pub(crate) mod dummy_test_span_utils { pub const DUMMY: Span = Span { range: TextRange::empty(TextSize::new(0)), anchor: span::SpanAnchor { - file_id: span::FileId::BOGUS, + file_id: span::FileId::from_raw(0xe4e4e), ast_id: span::ROOT_ERASED_FILE_AST_ID, }, ctx: SyntaxContextId::ROOT, @@ -60,7 +60,7 @@ pub(crate) mod dummy_test_span_utils { Span { range, anchor: span::SpanAnchor { - file_id: span::FileId::BOGUS, + file_id: span::FileId::from_raw(0xe4e4e), ast_id: span::ROOT_ERASED_FILE_AST_ID, }, ctx: SyntaxContextId::ROOT, diff --git a/crates/span/src/lib.rs b/crates/span/src/lib.rs index 74693a270b..bbaf1b2a6d 100644 --- a/crates/span/src/lib.rs +++ b/crates/span/src/lib.rs @@ -86,15 +86,6 @@ impl SpanData { } } -impl Span { - #[deprecated = "dummy spans will panic if surfaced incorrectly, as such they should be replaced appropriately"] - pub const DUMMY: Self = Self { - range: TextRange::empty(TextSize::new(0)), - anchor: SpanAnchor { file_id: FileId::BOGUS, ast_id: ROOT_ERASED_FILE_AST_ID }, - ctx: SyntaxContextId::ROOT, - }; -} - impl fmt::Display for Span { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.anchor.file_id.index(), f)?; @@ -178,6 +169,8 @@ impl salsa::InternKey for MacroCallId { } impl MacroCallId { + pub const MAX_ID: u32 = 0x7fff_ffff; + pub fn as_file(self) -> HirFileId { MacroFileId { macro_call_id: self }.into() } diff --git a/crates/vfs/src/lib.rs b/crates/vfs/src/lib.rs index eab66f10a9..18c8699dd4 100644 --- a/crates/vfs/src/lib.rs +++ b/crates/vfs/src/lib.rs @@ -69,9 +69,6 @@ pub struct FileId(u32); // pub struct FileId(NonMaxU32); impl FileId { - /// Think twice about using this outside of tests. If this ends up in a wrong place it will cause panics! - // FIXME: To be removed once we get rid of all `SpanData::DUMMY` usages. - pub const BOGUS: FileId = FileId(0xe4e4e); pub const MAX_FILE_ID: u32 = 0x7fff_ffff; #[inline]