Skip to content

Commit e91fdf7

Browse files
committed
Auto merge of #15959 - Veykril:macro-shower3, r=lnicola
TokenMap -> SpanMap rewrite Opening early so I can have an overview over the full diff more easily, still very unfinished and lots of work to be done. The gist of what this PR does is move away from assigning IDs to tokens in arguments and expansions and instead gives the subtrees the text ranges they are sourced from (made relative to some item for incrementality). This means we now only have a single map per expension, opposed to map for expansion and arguments. A few of the things that are not done yet (in arbitrary order): - [x] generally clean up the current mess - [x] proc-macros, have been completely ignored so far - [x] syntax fixups, has been commented out for the time being needs to be rewritten on top of some marker SyntaxContextId - [x] macro invocation syntax contexts are not properly passed around yet, so $crate hygiene does not work in all cases (but most) - [x] builtin macros do not set spans properly, $crate basically does not work with them rn (which we use) ~~- [ ] remove all uses of dummy spans (or if that does not work, change the dummy entries for dummy spans so that tests will not silently pass due to havin a file id for the dummy file)~~ - [x] de-queryfy `macro_expand`, the sole caller of it is `parse_macro_expansion`, and both of these are lru-cached with the same limit so having it be a query is pointless - [x] docs and more docs - [x] fix eager macro spans and other stuff - [x] simplify include! handling - [x] Figure out how to undo the sudden `()` expression wrapping in expansions / alternatively prioritize getting invisible delimiters working again - [x] Simplify InFile stuff and HirFIleId extensions ~~- [ ] span crate containing all the file ids, span stuff, ast ids. Then remove the dependency injection generics from tt and mbe~~ Fixes #10300 Fixes #15685
2 parents 11e1cea + 18f1a3c commit e91fdf7

File tree

133 files changed

+5364
-4166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+5364
-4166
lines changed

Cargo.lock

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ authors = ["rust-analyzer team"]
1212
[profile.dev]
1313
# Disabling debug info speeds up builds a bunch,
1414
# and we don't rely on it for debugging that much.
15-
debug = 0
15+
debug = 1
1616

1717
[profile.dev.package]
1818
# These speed up local tests.

crates/base-db/src/fixture.rs

+29-16
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ use test_utils::{
88
ESCAPED_CURSOR_MARKER,
99
};
1010
use triomphe::Arc;
11-
use tt::token_id::{Leaf, Subtree, TokenTree};
11+
use tt::{Leaf, Subtree, TokenTree};
1212
use vfs::{file_set::FileSet, VfsPath};
1313

1414
use crate::{
1515
input::{CrateName, CrateOrigin, LangCrateOrigin},
16+
span::SpanData,
1617
Change, CrateDisplayName, CrateGraph, CrateId, Dependency, DependencyKind, Edition, Env,
1718
FileId, FilePosition, FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError,
1819
ProcMacros, ReleaseChannel, SourceDatabaseExt, SourceRoot, SourceRootId,
@@ -539,10 +540,13 @@ struct IdentityProcMacroExpander;
539540
impl ProcMacroExpander for IdentityProcMacroExpander {
540541
fn expand(
541542
&self,
542-
subtree: &Subtree,
543-
_: Option<&Subtree>,
543+
subtree: &Subtree<SpanData>,
544+
_: Option<&Subtree<SpanData>>,
544545
_: &Env,
545-
) -> Result<Subtree, ProcMacroExpansionError> {
546+
_: SpanData,
547+
_: SpanData,
548+
_: SpanData,
549+
) -> Result<Subtree<SpanData>, ProcMacroExpansionError> {
546550
Ok(subtree.clone())
547551
}
548552
}
@@ -553,10 +557,13 @@ struct AttributeInputReplaceProcMacroExpander;
553557
impl ProcMacroExpander for AttributeInputReplaceProcMacroExpander {
554558
fn expand(
555559
&self,
556-
_: &Subtree,
557-
attrs: Option<&Subtree>,
560+
_: &Subtree<SpanData>,
561+
attrs: Option<&Subtree<SpanData>>,
558562
_: &Env,
559-
) -> Result<Subtree, ProcMacroExpansionError> {
563+
_: SpanData,
564+
_: SpanData,
565+
_: SpanData,
566+
) -> Result<Subtree<SpanData>, ProcMacroExpansionError> {
560567
attrs
561568
.cloned()
562569
.ok_or_else(|| ProcMacroExpansionError::Panic("Expected attribute input".into()))
@@ -568,11 +575,14 @@ struct MirrorProcMacroExpander;
568575
impl ProcMacroExpander for MirrorProcMacroExpander {
569576
fn expand(
570577
&self,
571-
input: &Subtree,
572-
_: Option<&Subtree>,
578+
input: &Subtree<SpanData>,
579+
_: Option<&Subtree<SpanData>>,
573580
_: &Env,
574-
) -> Result<Subtree, ProcMacroExpansionError> {
575-
fn traverse(input: &Subtree) -> Subtree {
581+
_: SpanData,
582+
_: SpanData,
583+
_: SpanData,
584+
) -> Result<Subtree<SpanData>, ProcMacroExpansionError> {
585+
fn traverse(input: &Subtree<SpanData>) -> Subtree<SpanData> {
576586
let mut token_trees = vec![];
577587
for tt in input.token_trees.iter().rev() {
578588
let tt = match tt {
@@ -595,13 +605,16 @@ struct ShortenProcMacroExpander;
595605
impl ProcMacroExpander for ShortenProcMacroExpander {
596606
fn expand(
597607
&self,
598-
input: &Subtree,
599-
_: Option<&Subtree>,
608+
input: &Subtree<SpanData>,
609+
_: Option<&Subtree<SpanData>>,
600610
_: &Env,
601-
) -> Result<Subtree, ProcMacroExpansionError> {
611+
_: SpanData,
612+
_: SpanData,
613+
_: SpanData,
614+
) -> Result<Subtree<SpanData>, ProcMacroExpansionError> {
602615
return Ok(traverse(input));
603616

604-
fn traverse(input: &Subtree) -> Subtree {
617+
fn traverse(input: &Subtree<SpanData>) -> Subtree<SpanData> {
605618
let token_trees = input
606619
.token_trees
607620
.iter()
@@ -613,7 +626,7 @@ impl ProcMacroExpander for ShortenProcMacroExpander {
613626
Subtree { delimiter: input.delimiter, token_trees }
614627
}
615628

616-
fn modify_leaf(leaf: &Leaf) -> Leaf {
629+
fn modify_leaf(leaf: &Leaf<SpanData>) -> Leaf<SpanData> {
617630
let mut leaf = leaf.clone();
618631
match &mut leaf {
619632
Leaf::Literal(it) => {

crates/base-db/src/input.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use la_arena::{Arena, Idx};
1313
use rustc_hash::{FxHashMap, FxHashSet};
1414
use syntax::SmolStr;
1515
use triomphe::Arc;
16-
use tt::token_id::Subtree;
1716
use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
1817

18+
use crate::span::SpanData;
19+
1920
// Map from crate id to the name of the crate and path of the proc-macro. If the value is `None`,
2021
// then the crate for the proc-macro hasn't been build yet as the build data is missing.
2122
pub type ProcMacroPaths = FxHashMap<CrateId, Result<(Option<String>, AbsPathBuf), String>>;
@@ -242,6 +243,9 @@ impl CrateDisplayName {
242243
}
243244
}
244245

246+
// FIXME: These should not be defined in here? Why does base db know about proc-macros
247+
// ProcMacroKind is used in [`fixture`], but that module probably shouldn't be in this crate either.
248+
245249
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
246250
pub struct ProcMacroId(pub u32);
247251

@@ -255,10 +259,13 @@ pub enum ProcMacroKind {
255259
pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe {
256260
fn expand(
257261
&self,
258-
subtree: &Subtree,
259-
attrs: Option<&Subtree>,
262+
subtree: &tt::Subtree<SpanData>,
263+
attrs: Option<&tt::Subtree<SpanData>>,
260264
env: &Env,
261-
) -> Result<Subtree, ProcMacroExpansionError>;
265+
def_site: SpanData,
266+
call_site: SpanData,
267+
mixed_site: SpanData,
268+
) -> Result<tt::Subtree<SpanData>, ProcMacroExpansionError>;
262269
}
263270

264271
#[derive(Debug)]
@@ -323,7 +330,9 @@ pub struct CrateData {
323330
pub dependencies: Vec<Dependency>,
324331
pub origin: CrateOrigin,
325332
pub is_proc_macro: bool,
326-
// FIXME: These things should not be per crate! These are more per workspace crate graph level things
333+
// FIXME: These things should not be per crate! These are more per workspace crate graph level
334+
// things. This info does need to be somewhat present though as to prevent deduplication from
335+
// happening across different workspaces with different layouts.
327336
pub target_layout: TargetLayoutLoadResult,
328337
pub channel: Option<ReleaseChannel>,
329338
}

crates/base-db/src/lib.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
mod input;
66
mod change;
77
pub mod fixture;
8+
pub mod span;
89

910
use std::panic;
1011

1112
use rustc_hash::FxHashSet;
1213
use syntax::{ast, Parse, SourceFile, TextRange, TextSize};
1314
use triomphe::Arc;
1415

15-
pub use crate::input::DependencyKind;
1616
pub use crate::{
1717
change::Change,
1818
input::{
1919
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
20-
Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander, ProcMacroExpansionError,
21-
ProcMacroId, ProcMacroKind, ProcMacroLoadResult, ProcMacroPaths, ProcMacros,
22-
ReleaseChannel, SourceRoot, SourceRootId, TargetLayoutLoadResult,
20+
DependencyKind, Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander,
21+
ProcMacroExpansionError, ProcMacroId, ProcMacroKind, ProcMacroLoadResult, ProcMacroPaths,
22+
ProcMacros, ReleaseChannel, SourceRoot, SourceRootId, TargetLayoutLoadResult,
2323
},
2424
};
2525
pub use salsa::{self, Cancelled};
@@ -68,8 +68,7 @@ pub trait FileLoader {
6868
/// model. Everything else in rust-analyzer is derived from these queries.
6969
#[salsa::query_group(SourceDatabaseStorage)]
7070
pub trait SourceDatabase: FileLoader + std::fmt::Debug {
71-
// Parses the file into the syntax tree.
72-
#[salsa::invoke(parse_query)]
71+
/// Parses the file into the syntax tree.
7372
fn parse(&self, file_id: FileId) -> Parse<ast::SourceFile>;
7473

7574
/// The crate graph.
@@ -81,7 +80,7 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
8180
fn proc_macros(&self) -> Arc<ProcMacros>;
8281
}
8382

84-
fn parse_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
83+
fn parse(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
8584
let _p = profile::span("parse_query").detail(|| format!("{file_id:?}"));
8685
let text = db.file_text(file_id);
8786
SourceFile::parse(&text)

0 commit comments

Comments
 (0)