Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 337e2ab

Browse files
committed
Auto merge of rust-lang#16167 - Veykril:dummy-spans, r=Veykril
fix: Fully remove dummy spans Fixes rust-lang/rust-analyzer#16008 Some of these spans are certainly wrong, but since we discard invisible delimiters currently it doesn't really matter.
2 parents 65ed198 + f211a40 commit 337e2ab

32 files changed

+292
-256
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ lsp-server = { version = "0.7.4" }
9393

9494
# non-local crates
9595
anyhow = "1.0.75"
96+
arrayvec = "0.7.4"
9697
bitflags = "2.4.1"
9798
cargo_metadata = "0.18.1"
9899
command-group = "2.0.1"

crates/cfg/src/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
use arbitrary::{Arbitrary, Unstructured};
22
use expect_test::{expect, Expect};
3-
use mbe::{syntax_node_to_token_tree, DummyTestSpanMap};
3+
use mbe::{syntax_node_to_token_tree, DummyTestSpanMap, DUMMY};
44
use syntax::{ast, AstNode};
55

66
use crate::{CfgAtom, CfgExpr, CfgOptions, DnfExpr};
77

88
fn assert_parse_result(input: &str, expected: CfgExpr) {
99
let source_file = ast::SourceFile::parse(input).ok().unwrap();
1010
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
11-
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap);
11+
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
1212
let cfg = CfgExpr::parse(&tt);
1313
assert_eq!(cfg, expected);
1414
}
1515

1616
fn check_dnf(input: &str, expect: Expect) {
1717
let source_file = ast::SourceFile::parse(input).ok().unwrap();
1818
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
19-
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap);
19+
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
2020
let cfg = CfgExpr::parse(&tt);
2121
let actual = format!("#![cfg({})]", DnfExpr::new(cfg));
2222
expect.assert_eq(&actual);
@@ -25,7 +25,7 @@ fn check_dnf(input: &str, expect: Expect) {
2525
fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
2626
let source_file = ast::SourceFile::parse(input).ok().unwrap();
2727
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
28-
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap);
28+
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
2929
let cfg = CfgExpr::parse(&tt);
3030
let dnf = DnfExpr::new(cfg);
3131
let why_inactive = dnf.why_inactive(opts).unwrap().to_string();
@@ -36,7 +36,7 @@ fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
3636
fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) {
3737
let source_file = ast::SourceFile::parse(input).ok().unwrap();
3838
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
39-
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap);
39+
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
4040
let cfg = CfgExpr::parse(&tt);
4141
let dnf = DnfExpr::new(cfg);
4242
let hints = dnf.compute_enable_hints(opts).map(|diff| diff.to_string()).collect::<Vec<_>>();

crates/hir-def/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rust-version.workspace = true
1212
doctest = false
1313

1414
[dependencies]
15-
arrayvec = "0.7.2"
15+
arrayvec.workspace = true
1616
bitflags.workspace = true
1717
cov-mark = "2.0.0-pre.1"
1818
dashmap.workspace = true

crates/hir-def/src/attr/tests.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
//! This module contains tests for doc-expression parsing.
22
//! Currently, it tests `#[doc(hidden)]` and `#[doc(alias)]`.
33
4+
use triomphe::Arc;
5+
46
use base_db::FileId;
5-
use hir_expand::span_map::{RealSpanMap, SpanMapRef};
7+
use hir_expand::span_map::{RealSpanMap, SpanMap};
68
use mbe::syntax_node_to_token_tree;
7-
use syntax::{ast, AstNode};
9+
use syntax::{ast, AstNode, TextRange};
810

911
use crate::attr::{DocAtom, DocExpr};
1012

1113
fn assert_parse_result(input: &str, expected: DocExpr) {
1214
let source_file = ast::SourceFile::parse(input).ok().unwrap();
1315
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
16+
let map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(FileId::from_raw(0))));
1417
let tt = syntax_node_to_token_tree(
1518
tt.syntax(),
16-
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId::from_raw(0))),
19+
map.as_ref(),
20+
map.span_for_range(TextRange::empty(0.into())),
1721
);
1822
let cfg = DocExpr::parse(&tt);
1923
assert_eq!(cfg, expected);

crates/hir-def/src/item_tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use la_arena::{Arena, Idx, IdxRange, RawIdx};
5555
use profile::Count;
5656
use rustc_hash::FxHashMap;
5757
use smallvec::SmallVec;
58-
use span::SyntaxContextId;
58+
use span::Span;
5959
use stdx::never;
6060
use syntax::{ast, match_ast, SyntaxKind};
6161
use triomphe::Arc;
@@ -747,7 +747,7 @@ pub struct MacroCall {
747747
pub path: Interned<ModPath>,
748748
pub ast_id: FileAstId<ast::MacroCall>,
749749
pub expand_to: ExpandTo,
750-
pub call_site: SyntaxContextId,
750+
pub call_site: Span,
751751
}
752752

753753
#[derive(Debug, Clone, Eq, PartialEq)]

crates/hir-def/src/item_tree/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ impl<'a> Ctx<'a> {
549549
path,
550550
ast_id,
551551
expand_to,
552-
call_site: span_map.span_for_range(m.syntax().text_range()).ctx,
552+
call_site: span_map.span_for_range(m.syntax().text_range()),
553553
};
554554
Some(id(self.data().macro_calls.alloc(res)))
555555
}

crates/hir-def/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ use hir_expand::{
8080
use item_tree::ExternBlock;
8181
use la_arena::Idx;
8282
use nameres::DefMap;
83-
use span::SyntaxContextId;
83+
use span::Span;
8484
use stdx::impl_from;
8585
use syntax::{ast, AstNode};
8686

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

1175-
let call_site = span_map.span_for_range(self.value.syntax().text_range()).ctx;
1175+
let call_site = span_map.span_for_range(self.value.syntax().text_range());
11761176

11771177
macro_call_as_call_id_with_eager(
11781178
db,
@@ -1202,7 +1202,7 @@ impl<T: AstIdNode> AstIdWithPath<T> {
12021202
fn macro_call_as_call_id(
12031203
db: &dyn ExpandDatabase,
12041204
call: &AstIdWithPath<ast::MacroCall>,
1205-
call_site: SyntaxContextId,
1205+
call_site: Span,
12061206
expand_to: ExpandTo,
12071207
krate: CrateId,
12081208
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
@@ -1214,7 +1214,7 @@ fn macro_call_as_call_id(
12141214
fn macro_call_as_call_id_with_eager(
12151215
db: &dyn ExpandDatabase,
12161216
call: &AstIdWithPath<ast::MacroCall>,
1217-
call_site: SyntaxContextId,
1217+
call_site: Span,
12181218
expand_to: ExpandTo,
12191219
krate: CrateId,
12201220
resolver: impl FnOnce(path::ModPath) -> Option<MacroDefId>,
@@ -1320,7 +1320,7 @@ fn derive_macro_as_call_id(
13201320
item_attr: &AstIdWithPath<ast::Adt>,
13211321
derive_attr_index: AttrId,
13221322
derive_pos: u32,
1323-
call_site: SyntaxContextId,
1323+
call_site: Span,
13241324
krate: CrateId,
13251325
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
13261326
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
@@ -1350,7 +1350,7 @@ fn attr_macro_as_call_id(
13501350
let arg = match macro_attr.input.as_deref() {
13511351
Some(AttrInput::TokenTree(tt)) => {
13521352
let mut tt = tt.as_ref().clone();
1353-
tt.delimiter = tt::Delimiter::DUMMY_INVISIBLE;
1353+
tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
13541354
Some(tt)
13551355
}
13561356

@@ -1365,7 +1365,7 @@ fn attr_macro_as_call_id(
13651365
attr_args: arg.map(Arc::new),
13661366
invoc_attr_index: macro_attr.id,
13671367
},
1368-
macro_attr.ctxt,
1368+
macro_attr.span,
13691369
)
13701370
}
13711371

crates/hir-def/src/nameres/collector.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ enum MacroDirectiveKind {
228228
FnLike {
229229
ast_id: AstIdWithPath<ast::MacroCall>,
230230
expand_to: ExpandTo,
231-
call_site: SyntaxContextId,
231+
call_site: Span,
232232
},
233233
Derive {
234234
ast_id: AstIdWithPath<ast::Adt>,
235235
derive_attr: AttrId,
236236
derive_pos: usize,
237-
call_site: SyntaxContextId,
237+
call_site: Span,
238238
},
239239
Attr {
240240
ast_id: AstIdWithPath<ast::Item>,
@@ -1307,38 +1307,37 @@ impl DefCollector<'_> {
13071307
// Not resolved to a derive helper or the derive attribute, so try to treat as a normal attribute.
13081308
let call_id =
13091309
attr_macro_as_call_id(self.db, file_ast_id, attr, self.def_map.krate, def);
1310-
let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call_id);
13111310

13121311
// If proc attribute macro expansion is disabled, skip expanding it here
13131312
if !self.db.expand_proc_attr_macros() {
13141313
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
13151314
directive.module_id,
1316-
loc.kind,
1317-
loc.def.krate,
1315+
self.db.lookup_intern_macro_call(call_id).kind,
1316+
def.krate,
13181317
));
13191318
return recollect_without(self);
13201319
}
13211320

13221321
// Skip #[test]/#[bench] expansion, which would merely result in more memory usage
13231322
// due to duplicating functions into macro expansions
13241323
if matches!(
1325-
loc.def.kind,
1324+
def.kind,
13261325
MacroDefKind::BuiltInAttr(expander, _)
13271326
if expander.is_test() || expander.is_bench()
13281327
) {
13291328
return recollect_without(self);
13301329
}
13311330

1332-
if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
1331+
if let MacroDefKind::ProcMacro(exp, ..) = def.kind {
13331332
if exp.is_dummy() {
13341333
// If there's no expander for the proc macro (e.g.
13351334
// because proc macros are disabled, or building the
13361335
// proc macro crate failed), report this and skip
13371336
// expansion like we would if it was disabled
13381337
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
13391338
directive.module_id,
1340-
loc.kind,
1341-
loc.def.krate,
1339+
self.db.lookup_intern_macro_call(call_id).kind,
1340+
def.krate,
13421341
));
13431342

13441343
return recollect_without(self);

crates/hir-expand/src/attrs.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use either::Either;
77
use intern::Interned;
88
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
99
use smallvec::{smallvec, SmallVec};
10-
use span::SyntaxContextId;
10+
use span::Span;
1111
use syntax::{ast, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
1212
use triomphe::Arc;
1313

@@ -53,7 +53,7 @@ impl RawAttrs {
5353
id,
5454
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
5555
path: Interned::new(ModPath::from(crate::name!(doc))),
56-
ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx,
56+
span: span_map.span_for_range(comment.syntax().text_range()),
5757
}),
5858
});
5959
let entries: Arc<[Attr]> = Arc::from_iter(entries);
@@ -120,7 +120,7 @@ impl RawAttrs {
120120
let attrs =
121121
parts.enumerate().take(1 << AttrId::CFG_ATTR_BITS).filter_map(|(idx, attr)| {
122122
let tree = Subtree {
123-
delimiter: tt::Delimiter::dummy_invisible(),
123+
delimiter: tt::Delimiter::invisible_spanned(attr.first()?.first_span()),
124124
token_trees: attr.to_vec(),
125125
};
126126
Attr::from_tt(db, &tree, index.with_cfg_attr(idx))
@@ -177,7 +177,7 @@ pub struct Attr {
177177
pub id: AttrId,
178178
pub path: Interned<ModPath>,
179179
pub input: Option<Interned<AttrInput>>,
180-
pub ctxt: SyntaxContextId,
180+
pub span: Span,
181181
}
182182

183183
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -206,19 +206,20 @@ impl Attr {
206206
id: AttrId,
207207
) -> Option<Attr> {
208208
let path = Interned::new(ModPath::from_src(db, ast.path()?, span_map)?);
209+
let span = span_map.span_for_range(ast.syntax().text_range());
209210
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
210211
let value = match lit.kind() {
211212
ast::LiteralKind::String(string) => string.value()?.into(),
212213
_ => lit.syntax().first_token()?.text().trim_matches('"').into(),
213214
};
214215
Some(Interned::new(AttrInput::Literal(value)))
215216
} else if let Some(tt) = ast.token_tree() {
216-
let tree = syntax_node_to_token_tree(tt.syntax(), span_map);
217+
let tree = syntax_node_to_token_tree(tt.syntax(), span_map, span);
217218
Some(Interned::new(AttrInput::TokenTree(Box::new(tree))))
218219
} else {
219220
None
220221
};
221-
Some(Attr { id, path, input, ctxt: span_map.span_for_range(ast.syntax().text_range()).ctx })
222+
Some(Attr { id, path, input, span })
222223
}
223224

224225
fn from_tt(db: &dyn ExpandDatabase, tt: &tt::Subtree, id: AttrId) -> Option<Attr> {
@@ -266,7 +267,7 @@ impl Attr {
266267
pub fn parse_path_comma_token_tree<'a>(
267268
&'a self,
268269
db: &'a dyn ExpandDatabase,
269-
) -> Option<impl Iterator<Item = (ModPath, SyntaxContextId)> + 'a> {
270+
) -> Option<impl Iterator<Item = (ModPath, Span)> + 'a> {
270271
let args = self.token_tree_value()?;
271272

272273
if args.delimiter.kind != DelimiterKind::Parenthesis {
@@ -282,7 +283,7 @@ impl Attr {
282283
// FIXME: This is necessarily a hack. It'd be nice if we could avoid allocation
283284
// here or maybe just parse a mod path from a token tree directly
284285
let subtree = tt::Subtree {
285-
delimiter: tt::Delimiter::dummy_invisible(),
286+
delimiter: tt::Delimiter::invisible_spanned(tts.first()?.first_span()),
286287
token_trees: tts.to_vec(),
287288
};
288289
let (parse, span_map) =
@@ -294,7 +295,7 @@ impl Attr {
294295
return None;
295296
}
296297
let path = meta.path()?;
297-
let call_site = span_map.span_at(path.syntax().text_range().start()).ctx;
298+
let call_site = span_map.span_at(path.syntax().text_range().start());
298299
Some((
299300
ModPath::from_src(db, path, SpanMapRef::ExpansionSpanMap(&span_map))?,
300301
call_site,

crates/hir-expand/src/builtin_attr_macro.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Builtin attributes.
2-
use span::{FileId, MacroCallId, Span, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
3-
use syntax::{TextRange, TextSize};
2+
use span::{MacroCallId, Span};
43

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

@@ -102,28 +101,26 @@ fn derive_attr_expand(
102101
MacroCallKind::Attr { attr_args: Some(attr_args), .. } if loc.def.is_attribute_derive() => {
103102
attr_args
104103
}
105-
_ => return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan::DUMMY)),
104+
_ => {
105+
return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan {
106+
open: loc.call_site,
107+
close: loc.call_site,
108+
}))
109+
}
106110
};
107111
pseudo_derive_attr_expansion(tt, derives, loc.call_site)
108112
}
109113

110114
pub fn pseudo_derive_attr_expansion(
111115
tt: &tt::Subtree,
112116
args: &tt::Subtree,
113-
call_site: SyntaxContextId,
117+
call_site: Span,
114118
) -> ExpandResult<tt::Subtree> {
115119
let mk_leaf = |char| {
116120
tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct {
117121
char,
118122
spacing: tt::Spacing::Alone,
119-
span: Span {
120-
range: TextRange::empty(TextSize::new(0)),
121-
anchor: span::SpanAnchor {
122-
file_id: FileId::BOGUS,
123-
ast_id: ROOT_ERASED_FILE_AST_ID,
124-
},
125-
ctx: call_site,
126-
},
123+
span: call_site,
127124
}))
128125
};
129126

0 commit comments

Comments
 (0)