Skip to content

Commit

Permalink
Remove usages of Span::DUMMY
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Dec 20, 2023
1 parent f48ecb6 commit 4ec8123
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 87 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ lsp-server = { version = "0.7.4" }

# non-local crates
anyhow = "1.0.75"
arrayvec = "0.7.4"
bitflags = "2.4.1"
cargo_metadata = "0.18.1"
command-group = "2.0.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rust-version.workspace = true
doctest = false

[dependencies]
arrayvec = "0.7.2"
arrayvec.workspace = true
bitflags.workspace = true
cov-mark = "2.0.0-pre.1"
dashmap.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use la_arena::{Arena, Idx, IdxRange, RawIdx};
use profile::Count;
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use span::SyntaxContextId;
use span::Span;
use stdx::never;
use syntax::{ast, match_ast, SyntaxKind};
use triomphe::Arc;
Expand Down Expand Up @@ -747,7 +747,7 @@ pub struct MacroCall {
pub path: Interned<ModPath>,
pub ast_id: FileAstId<ast::MacroCall>,
pub expand_to: ExpandTo,
pub call_site: SyntaxContextId,
pub call_site: Span,
}

#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ impl<'a> Ctx<'a> {
path,
ast_id,
expand_to,
call_site: span_map.span_for_range(m.syntax().text_range()).ctx,
call_site: span_map.span_for_range(m.syntax().text_range()),
};
Some(id(self.data().macro_calls.alloc(res)))
}
Expand Down
12 changes: 6 additions & 6 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use hir_expand::{
use item_tree::ExternBlock;
use la_arena::Idx;
use nameres::DefMap;
use span::SyntaxContextId;
use span::Span;
use stdx::impl_from;
use syntax::{ast, AstNode};

Expand Down Expand Up @@ -1172,7 +1172,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
return Ok(ExpandResult::only_err(ExpandError::other("malformed macro invocation")));
};

let call_site = span_map.span_for_range(self.value.syntax().text_range()).ctx;
let call_site = span_map.span_for_range(self.value.syntax().text_range());

macro_call_as_call_id_with_eager(
db,
Expand Down Expand Up @@ -1202,7 +1202,7 @@ impl<T: AstIdNode> AstIdWithPath<T> {
fn macro_call_as_call_id(
db: &dyn ExpandDatabase,
call: &AstIdWithPath<ast::MacroCall>,
call_site: SyntaxContextId,
call_site: Span,
expand_to: ExpandTo,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
Expand All @@ -1214,7 +1214,7 @@ fn macro_call_as_call_id(
fn macro_call_as_call_id_with_eager(
db: &dyn ExpandDatabase,
call: &AstIdWithPath<ast::MacroCall>,
call_site: SyntaxContextId,
call_site: Span,
expand_to: ExpandTo,
krate: CrateId,
resolver: impl FnOnce(path::ModPath) -> Option<MacroDefId>,
Expand Down Expand Up @@ -1320,7 +1320,7 @@ fn derive_macro_as_call_id(
item_attr: &AstIdWithPath<ast::Adt>,
derive_attr_index: AttrId,
derive_pos: u32,
call_site: SyntaxContextId,
call_site: Span,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
Expand Down Expand Up @@ -1365,7 +1365,7 @@ fn attr_macro_as_call_id(
attr_args: arg.map(Arc::new),
invoc_attr_index: macro_attr.id,
},
macro_attr.ctxt,
macro_attr.span,
)
}

Expand Down
17 changes: 8 additions & 9 deletions crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ enum MacroDirectiveKind {
FnLike {
ast_id: AstIdWithPath<ast::MacroCall>,
expand_to: ExpandTo,
call_site: SyntaxContextId,
call_site: Span,
},
Derive {
ast_id: AstIdWithPath<ast::Adt>,
derive_attr: AttrId,
derive_pos: usize,
call_site: SyntaxContextId,
call_site: Span,
},
Attr {
ast_id: AstIdWithPath<ast::Item>,
Expand Down Expand Up @@ -1305,38 +1305,37 @@ impl DefCollector<'_> {
// Not resolved to a derive helper or the derive attribute, so try to treat as a normal attribute.
let call_id =
attr_macro_as_call_id(self.db, file_ast_id, attr, self.def_map.krate, def);
let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call_id);

// If proc attribute macro expansion is disabled, skip expanding it here
if !self.db.expand_proc_attr_macros() {
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
directive.module_id,
loc.kind,
loc.def.krate,
self.db.lookup_intern_macro_call(call_id).kind,
def.krate,
));
return recollect_without(self);
}

// Skip #[test]/#[bench] expansion, which would merely result in more memory usage
// due to duplicating functions into macro expansions
if matches!(
loc.def.kind,
def.kind,
MacroDefKind::BuiltInAttr(expander, _)
if expander.is_test() || expander.is_bench()
) {
return recollect_without(self);
}

if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
if let MacroDefKind::ProcMacro(exp, ..) = def.kind {
if exp.is_dummy() {
// If there's no expander for the proc macro (e.g.
// because proc macros are disabled, or building the
// proc macro crate failed), report this and skip
// expansion like we would if it was disabled
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
directive.module_id,
loc.kind,
loc.def.krate,
self.db.lookup_intern_macro_call(call_id).kind,
def.krate,
));

return recollect_without(self);
Expand Down
12 changes: 6 additions & 6 deletions crates/hir-expand/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use either::Either;
use intern::Interned;
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
use smallvec::{smallvec, SmallVec};
use span::SyntaxContextId;
use span::Span;
use syntax::{ast, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
use triomphe::Arc;

Expand Down Expand Up @@ -53,7 +53,7 @@ impl RawAttrs {
id,
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
path: Interned::new(ModPath::from(crate::name!(doc))),
ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx,
span: span_map.span_for_range(comment.syntax().text_range()),
}),
});
let entries: Arc<[Attr]> = Arc::from_iter(entries);
Expand Down Expand Up @@ -177,7 +177,7 @@ pub struct Attr {
pub id: AttrId,
pub path: Interned<ModPath>,
pub input: Option<Interned<AttrInput>>,
pub ctxt: SyntaxContextId,
pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -218,7 +218,7 @@ impl Attr {
} else {
None
};
Some(Attr { id, path, input, ctxt: span_map.span_for_range(ast.syntax().text_range()).ctx })
Some(Attr { id, path, input, span: span_map.span_for_range(ast.syntax().text_range()) })
}

fn from_tt(db: &dyn ExpandDatabase, tt: &tt::Subtree, id: AttrId) -> Option<Attr> {
Expand Down Expand Up @@ -266,7 +266,7 @@ impl Attr {
pub fn parse_path_comma_token_tree<'a>(
&'a self,
db: &'a dyn ExpandDatabase,
) -> Option<impl Iterator<Item = (ModPath, SyntaxContextId)> + 'a> {
) -> Option<impl Iterator<Item = (ModPath, Span)> + 'a> {
let args = self.token_tree_value()?;

if args.delimiter.kind != DelimiterKind::Parenthesis {
Expand Down Expand Up @@ -294,7 +294,7 @@ impl Attr {
return None;
}
let path = meta.path()?;
let call_site = span_map.span_at(path.syntax().text_range().start()).ctx;
let call_site = span_map.span_at(path.syntax().text_range().start());
Some((
ModPath::from_src(db, path, SpanMapRef::ExpansionSpanMap(&span_map))?,
call_site,
Expand Down
14 changes: 3 additions & 11 deletions crates/hir-expand/src/builtin_attr_macro.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Builtin attributes.
use span::{FileId, MacroCallId, Span, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
use syntax::{TextRange, TextSize};
use span::{MacroCallId, Span};

use crate::{db::ExpandDatabase, name, tt, ExpandResult, MacroCallKind};

Expand Down Expand Up @@ -110,20 +109,13 @@ fn derive_attr_expand(
pub fn pseudo_derive_attr_expansion(
tt: &tt::Subtree,
args: &tt::Subtree,
call_site: SyntaxContextId,
call_site: Span,
) -> ExpandResult<tt::Subtree> {
let mk_leaf = |char| {
tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct {
char,
spacing: tt::Spacing::Alone,
span: Span {
range: TextRange::empty(TextSize::new(0)),
anchor: span::SpanAnchor {
file_id: FileId::BOGUS,
ast_id: ROOT_ERASED_FILE_AST_ID,
},
ctx: call_site,
},
span: call_site,
}))
};

Expand Down
18 changes: 12 additions & 6 deletions crates/hir-expand/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use either::Either;
use limit::Limit;
use mbe::{syntax_node_to_token_tree, ValueResult};
use rustc_hash::FxHashSet;
use span::SyntaxContextId;
use span::{Span, SyntaxContextId};
use syntax::{
ast::{self, HasAttrs},
AstNode, Parse, SyntaxError, SyntaxNode, SyntaxToken, T,
Expand Down Expand Up @@ -57,7 +57,8 @@ impl DeclarativeMacroExpander {
tt: tt::Subtree,
call_id: MacroCallId,
) -> ExpandResult<tt::Subtree> {
let toolchain = &db.crate_graph()[db.lookup_intern_macro_call(call_id).def.krate].toolchain;
let loc = db.lookup_intern_macro_call(call_id);
let toolchain = &db.crate_graph()[loc.def.krate].toolchain;
let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
&base_db::Version {
Expand All @@ -80,6 +81,7 @@ impl DeclarativeMacroExpander {
&tt,
|s| s.ctx = apply_mark(db, s.ctx, call_id, self.transparency),
new_meta_vars,
loc.call_site,
)
.map_err(Into::into),
}
Expand All @@ -90,6 +92,7 @@ impl DeclarativeMacroExpander {
db: &dyn ExpandDatabase,
tt: tt::Subtree,
krate: CrateId,
call_site: Span,
) -> ExpandResult<tt::Subtree> {
let toolchain = &db.crate_graph()[krate].toolchain;
let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
Expand All @@ -108,7 +111,7 @@ impl DeclarativeMacroExpander {
tt::Subtree::empty(tt::DelimSpan::DUMMY),
ExpandError::other(format!("invalid macro definition: {e}")),
),
None => self.mac.expand(&tt, |_| (), new_meta_vars).map_err(Into::into),
None => self.mac.expand(&tt, |_| (), new_meta_vars, call_site).map_err(Into::into),
}
}
}
Expand Down Expand Up @@ -315,9 +318,12 @@ pub fn expand_speculative(
let adt = ast::Adt::cast(speculative_args.clone()).unwrap();
expander.expand(db, actual_macro_call, &adt, span_map)
}
MacroDefKind::Declarative(it) => {
db.decl_macro_expander(loc.krate, it).expand_unhygienic(db, tt, loc.def.krate)
}
MacroDefKind::Declarative(it) => db.decl_macro_expander(loc.krate, it).expand_unhygienic(
db,
tt,
loc.def.krate,
loc.call_site,
),
MacroDefKind::BuiltIn(it, _) => it.expand(db, actual_macro_call, &tt).map_err(Into::into),
MacroDefKind::BuiltInEager(it, _) => {
it.expand(db, actual_macro_call, &tt).map_err(Into::into)
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-expand/src/eager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//!
//! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros>
use base_db::CrateId;
use span::SyntaxContextId;
use span::Span;
use syntax::{ted, Parse, SyntaxElement, SyntaxNode, TextSize, WalkEvent};
use triomphe::Arc;

Expand All @@ -37,7 +37,7 @@ pub fn expand_eager_macro_input(
krate: CrateId,
macro_call: InFile<ast::MacroCall>,
def: MacroDefId,
call_site: SyntaxContextId,
call_site: Span,
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
) -> ExpandResult<Option<MacroCallId>> {
let ast_map = db.ast_id_map(macro_call.file_id);
Expand Down Expand Up @@ -102,7 +102,7 @@ fn lazy_expand(
def: &MacroDefId,
macro_call: InFile<ast::MacroCall>,
krate: CrateId,
call_site: SyntaxContextId,
call_site: Span,
) -> ExpandResult<(InFile<Parse<SyntaxNode>>, Arc<ExpansionSpanMap>)> {
let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);

Expand All @@ -122,7 +122,7 @@ fn eager_macro_recur(
mut offset: TextSize,
curr: InFile<SyntaxNode>,
krate: CrateId,
call_site: SyntaxContextId,
call_site: Span,
macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
) -> ExpandResult<Option<(SyntaxNode, TextSize)>> {
let original = curr.value.clone_for_update();
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-expand/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub(super) fn apply_mark(
return apply_mark_internal(db, ctxt, Some(call_id), transparency);
}

let call_site_ctxt = db.lookup_intern_macro_call(call_id).call_site;
let call_site_ctxt = db.lookup_intern_macro_call(call_id).call_site.ctx;
let mut call_site_ctxt = if transparency == Transparency::SemiTransparent {
call_site_ctxt.normalize_to_macros_2_0(db)
} else {
Expand Down
8 changes: 5 additions & 3 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,20 @@ pub struct MacroCallLoc {
pub krate: CrateId,
/// Some if this is a macro call for an eager macro. Note that this is `None`
/// for the eager input macro file.
// FIXME: This seems bad to save in an interned structure
eager: Option<Arc<EagerCallInfo>>,
pub kind: MacroCallKind,
pub call_site: SyntaxContextId,
pub call_site: Span,
}

// FIXME: Might make sense to intern this? Given it's gonna be the same for a bunch of macro calls
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId {
pub krate: CrateId,
pub kind: MacroDefKind,
pub local_inner: bool,
pub allow_internal_unsafe: bool,
// pub def_site: SyntaxContextId,
// pub def_site: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -348,7 +350,7 @@ impl MacroDefId {
db: &dyn db::ExpandDatabase,
krate: CrateId,
kind: MacroCallKind,
call_site: SyntaxContextId,
call_site: Span,
) -> MacroCallId {
db.intern_macro_call(MacroCallLoc { def: self, krate, eager: None, kind, call_site })
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ doctest = false
[dependencies]
cov-mark = "2.0.0-pre.1"
itertools.workspace = true
arrayvec = "0.7.2"
arrayvec.workspace = true
bitflags.workspace = true
smallvec.workspace = true
ena = "0.14.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ doctest = false
[dependencies]
rustc-hash.workspace = true
either.workspace = true
arrayvec = "0.7.2"
arrayvec.workspace = true
itertools.workspace = true
smallvec.workspace = true
triomphe.workspace = true
Expand Down
Loading

0 comments on commit 4ec8123

Please sign in to comment.