Skip to content

Commit fd240c0

Browse files
committed
Auto merge of rust-lang#17620 - Veykril:edition-aware-parser, r=Veykril
Edition aware parser Fixes rust-lang/rust-analyzer#16324 by allowing us to properly thread through the edition to the parser
2 parents e6e8bd2 + 754f53c commit fd240c0

File tree

675 files changed

+2642
-1425
lines changed

Some content is hidden

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

675 files changed

+2642
-1425
lines changed

src/tools/rust-analyzer/crates/base-db/src/input.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use cfg::CfgOptions;
1212
use intern::Symbol;
1313
use la_arena::{Arena, Idx, RawIdx};
1414
use rustc_hash::{FxHashMap, FxHashSet};
15-
use span::Edition;
15+
use span::{Edition, EditionedFileId};
1616
use triomphe::Arc;
1717
use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
1818

@@ -662,6 +662,10 @@ impl CrateData {
662662
fn add_dep(&mut self, dep: Dependency) {
663663
self.dependencies.push(dep)
664664
}
665+
666+
pub fn root_file_id(&self) -> EditionedFileId {
667+
EditionedFileId::new(self.root_file_id, self.edition)
668+
}
665669
}
666670

667671
impl Extend<(String, String)> for Env {

src/tools/rust-analyzer/crates/base-db/src/lib.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ mod input;
66
use std::panic;
77

88
use salsa::Durability;
9+
use span::EditionedFileId;
910
use syntax::{ast, Parse, SourceFile, SyntaxError};
1011
use triomphe::Arc;
12+
use vfs::FileId;
1113

1214
pub use crate::{
1315
change::FileChange,
@@ -18,8 +20,7 @@ pub use crate::{
1820
},
1921
};
2022
pub use salsa::{self, Cancelled};
21-
pub use span::{FilePosition, FileRange};
22-
pub use vfs::{file_set::FileSet, AnchoredPath, AnchoredPathBuf, FileId, VfsPath};
23+
pub use vfs::{file_set::FileSet, AnchoredPath, AnchoredPathBuf, VfsPath};
2324

2425
pub use semver::{BuildMetadata, Prerelease, Version, VersionReq};
2526

@@ -58,10 +59,10 @@ pub trait FileLoader {
5859
#[salsa::query_group(SourceDatabaseStorage)]
5960
pub trait SourceDatabase: FileLoader + std::fmt::Debug {
6061
/// Parses the file into the syntax tree.
61-
fn parse(&self, file_id: FileId) -> Parse<ast::SourceFile>;
62+
fn parse(&self, file_id: EditionedFileId) -> Parse<ast::SourceFile>;
6263

6364
/// Returns the set of errors obtained from parsing the file including validation errors.
64-
fn parse_errors(&self, file_id: FileId) -> Option<Arc<[SyntaxError]>>;
65+
fn parse_errors(&self, file_id: EditionedFileId) -> Option<Arc<[SyntaxError]>>;
6566

6667
/// The crate graph.
6768
#[salsa::input]
@@ -82,14 +83,14 @@ fn toolchain_channel(db: &dyn SourceDatabase, krate: CrateId) -> Option<ReleaseC
8283
db.toolchain(krate).as_ref().and_then(|v| ReleaseChannel::from_str(&v.pre))
8384
}
8485

85-
fn parse(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
86+
fn parse(db: &dyn SourceDatabase, file_id: EditionedFileId) -> Parse<ast::SourceFile> {
8687
let _p = tracing::info_span!("parse", ?file_id).entered();
88+
let (file_id, edition) = file_id.unpack();
8789
let text = db.file_text(file_id);
88-
// FIXME: Edition based parsing
89-
SourceFile::parse(&text, span::Edition::CURRENT)
90+
SourceFile::parse(&text, edition)
9091
}
9192

92-
fn parse_errors(db: &dyn SourceDatabase, file_id: FileId) -> Option<Arc<[SyntaxError]>> {
93+
fn parse_errors(db: &dyn SourceDatabase, file_id: EditionedFileId) -> Option<Arc<[SyntaxError]>> {
9394
let errors = db.parse(file_id).errors();
9495
match &*errors {
9596
[] => None,

src/tools/rust-analyzer/crates/hir-def/src/attr.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -657,19 +657,22 @@ mod tests {
657657
//! Currently, it tests `#[doc(hidden)]` and `#[doc(alias)]`.
658658
659659
use intern::Symbol;
660+
use span::EditionedFileId;
660661
use triomphe::Arc;
661662

662-
use base_db::FileId;
663663
use hir_expand::span_map::{RealSpanMap, SpanMap};
664664
use mbe::{syntax_node_to_token_tree, DocCommentDesugarMode};
665+
use span::FileId;
665666
use syntax::{ast, AstNode, TextRange};
666667

667668
use crate::attr::{DocAtom, DocExpr};
668669

669670
fn assert_parse_result(input: &str, expected: DocExpr) {
670671
let source_file = ast::SourceFile::parse(input, span::Edition::CURRENT).ok().unwrap();
671672
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
672-
let map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(FileId::from_raw(0))));
673+
let map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(
674+
EditionedFileId::current_edition(FileId::from_raw(0)),
675+
)));
673676
let tt = syntax_node_to_token_tree(
674677
tt.syntax(),
675678
map.as_ref(),

src/tools/rust-analyzer/crates/hir-def/src/body/scope.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ fn compute_expr_scopes(
288288

289289
#[cfg(test)]
290290
mod tests {
291-
use base_db::{FileId, SourceDatabase};
291+
use base_db::SourceDatabase;
292292
use hir_expand::{name::AsName, InFile};
293+
use span::FileId;
293294
use syntax::{algo::find_node_at_offset, ast, AstNode};
294295
use test_fixture::WithFixture;
295296
use test_utils::{assert_eq_text, extract_offset};
@@ -325,7 +326,7 @@ mod tests {
325326

326327
let file_syntax = db.parse(file_id).syntax_node();
327328
let marker: ast::PathExpr = find_node_at_offset(&file_syntax, offset).unwrap();
328-
let function = find_function(&db, file_id);
329+
let function = find_function(&db, file_id.file_id());
329330

330331
let scopes = db.expr_scopes(function.into());
331332
let (_body, source_map) = db.body_with_source_map(function.into());
@@ -480,7 +481,7 @@ fn foo() {
480481
.expect("failed to find a name at the target offset");
481482
let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), offset).unwrap();
482483

483-
let function = find_function(&db, file_id);
484+
let function = find_function(&db, file_id.file_id());
484485

485486
let scopes = db.expr_scopes(function.into());
486487
let (body, source_map) = db.body_with_source_map(function.into());

src/tools/rust-analyzer/crates/hir-def/src/db.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Defines database & queries for name resolution.
2-
use base_db::{salsa, CrateId, FileId, SourceDatabase, Upcast};
2+
use base_db::{salsa, CrateId, SourceDatabase, Upcast};
33
use either::Either;
44
use hir_expand::{db::ExpandDatabase, HirFileId, MacroDefId};
55
use intern::{sym, Interned};
66
use la_arena::ArenaMap;
7-
use span::MacroCallId;
7+
use span::{EditionedFileId, MacroCallId};
88
use syntax::{ast, AstPtr};
99
use triomphe::Arc;
1010

@@ -239,11 +239,14 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
239239

240240
fn crate_supports_no_std(&self, crate_id: CrateId) -> bool;
241241

242-
fn include_macro_invoc(&self, crate_id: CrateId) -> Vec<(MacroCallId, FileId)>;
242+
fn include_macro_invoc(&self, crate_id: CrateId) -> Vec<(MacroCallId, EditionedFileId)>;
243243
}
244244

245245
// return: macro call id and include file id
246-
fn include_macro_invoc(db: &dyn DefDatabase, krate: CrateId) -> Vec<(MacroCallId, FileId)> {
246+
fn include_macro_invoc(
247+
db: &dyn DefDatabase,
248+
krate: CrateId,
249+
) -> Vec<(MacroCallId, EditionedFileId)> {
247250
db.crate_def_map(krate)
248251
.modules
249252
.values()
@@ -257,7 +260,7 @@ fn include_macro_invoc(db: &dyn DefDatabase, krate: CrateId) -> Vec<(MacroCallId
257260
}
258261

259262
fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: CrateId) -> bool {
260-
let file = db.crate_graph()[crate_id].root_file_id;
263+
let file = db.crate_graph()[crate_id].root_file_id();
261264
let item_tree = db.file_item_tree(file.into());
262265
let attrs = item_tree.raw_attrs(AttrOwner::TopLevel);
263266
for attr in &**attrs {

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mbe.rs

+56
Original file line numberDiff line numberDiff line change
@@ -1921,3 +1921,59 @@ fn f() {
19211921
"#]],
19221922
);
19231923
}
1924+
1925+
#[test]
1926+
fn test_edition_handling_out() {
1927+
check(
1928+
r#"
1929+
//- /main.rs crate:main deps:old edition:2021
1930+
macro_rules! r#try {
1931+
($it:expr) => {
1932+
$it?
1933+
};
1934+
}
1935+
fn f() {
1936+
old::invoke_bare_try!(0);
1937+
}
1938+
//- /old.rs crate:old edition:2015
1939+
#[macro_export]
1940+
macro_rules! invoke_bare_try {
1941+
($it:expr) => {
1942+
try!($it)
1943+
};
1944+
}
1945+
"#,
1946+
expect![[r#"
1947+
macro_rules! r#try {
1948+
($it:expr) => {
1949+
$it?
1950+
};
1951+
}
1952+
fn f() {
1953+
try!(0);
1954+
}
1955+
"#]],
1956+
);
1957+
}
1958+
1959+
#[test]
1960+
fn test_edition_handling_in() {
1961+
check(
1962+
r#"
1963+
//- /main.rs crate:main deps:old edition:2021
1964+
fn f() {
1965+
old::parse_try_old!(try!{});
1966+
}
1967+
//- /old.rs crate:old edition:2015
1968+
#[macro_export]
1969+
macro_rules! parse_try_old {
1970+
($it:expr) => {};
1971+
}
1972+
"#,
1973+
expect![[r#"
1974+
fn f() {
1975+
;
1976+
}
1977+
"#]],
1978+
);
1979+
}

src/tools/rust-analyzer/crates/hir-def/src/nameres.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ mod tests;
5959

6060
use std::ops::Deref;
6161

62-
use base_db::{CrateId, FileId};
62+
use base_db::CrateId;
6363
use hir_expand::{
6464
name::Name, proc_macro::ProcMacroKind, ErasedAstId, HirFileId, InFile, MacroCallId, MacroDefId,
6565
};
6666
use intern::Symbol;
6767
use itertools::Itertools;
6868
use la_arena::Arena;
6969
use rustc_hash::{FxHashMap, FxHashSet};
70-
use span::{Edition, FileAstId, ROOT_ERASED_FILE_AST_ID};
70+
use span::{Edition, EditionedFileId, FileAstId, FileId, ROOT_ERASED_FILE_AST_ID};
7171
use stdx::format_to;
7272
use syntax::{ast, SmolStr};
7373
use triomphe::Arc;
@@ -244,14 +244,14 @@ impl std::ops::Index<LocalModuleId> for DefMap {
244244
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
245245
pub enum ModuleOrigin {
246246
CrateRoot {
247-
definition: FileId,
247+
definition: EditionedFileId,
248248
},
249249
/// Note that non-inline modules, by definition, live inside non-macro file.
250250
File {
251251
is_mod_rs: bool,
252252
declaration: FileAstId<ast::Module>,
253253
declaration_tree_id: ItemTreeId<Mod>,
254-
definition: FileId,
254+
definition: EditionedFileId,
255255
},
256256
Inline {
257257
definition_tree_id: ItemTreeId<Mod>,
@@ -277,7 +277,7 @@ impl ModuleOrigin {
277277
}
278278
}
279279

280-
pub fn file_id(&self) -> Option<FileId> {
280+
pub fn file_id(&self) -> Option<EditionedFileId> {
281281
match self {
282282
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
283283
Some(*definition)
@@ -339,7 +339,7 @@ impl DefMap {
339339
let _p = tracing::info_span!("crate_def_map_query", ?name).entered();
340340

341341
let module_data = ModuleData::new(
342-
ModuleOrigin::CrateRoot { definition: krate.root_file_id },
342+
ModuleOrigin::CrateRoot { definition: krate.root_file_id() },
343343
Visibility::Public,
344344
);
345345

@@ -350,7 +350,7 @@ impl DefMap {
350350
None,
351351
);
352352
let def_map =
353-
collector::collect_defs(db, def_map, TreeId::new(krate.root_file_id.into(), None));
353+
collector::collect_defs(db, def_map, TreeId::new(krate.root_file_id().into(), None));
354354

355355
Arc::new(def_map)
356356
}
@@ -433,7 +433,9 @@ impl DefMap {
433433
pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ {
434434
self.modules
435435
.iter()
436-
.filter(move |(_id, data)| data.origin.file_id() == Some(file_id))
436+
.filter(move |(_id, data)| {
437+
data.origin.file_id().map(EditionedFileId::file_id) == Some(file_id)
438+
})
437439
.map(|(id, _data)| id)
438440
}
439441

src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use std::{cmp::Ordering, iter, mem, ops::Not};
77

8-
use base_db::{CrateId, CrateOrigin, Dependency, FileId, LangCrateOrigin};
8+
use base_db::{CrateId, CrateOrigin, Dependency, LangCrateOrigin};
99
use cfg::{CfgExpr, CfgOptions};
1010
use either::Either;
1111
use hir_expand::{
@@ -22,7 +22,7 @@ use itertools::{izip, Itertools};
2222
use la_arena::Idx;
2323
use limit::Limit;
2424
use rustc_hash::{FxHashMap, FxHashSet};
25-
use span::{Edition, ErasedFileAstId, FileAstId, SyntaxContextId};
25+
use span::{Edition, EditionedFileId, ErasedFileAstId, FileAstId, SyntaxContextId};
2626
use syntax::ast;
2727
use triomphe::Arc;
2828

@@ -272,7 +272,7 @@ impl DefCollector<'_> {
272272
let _p = tracing::info_span!("seed_with_top_level").entered();
273273

274274
let crate_graph = self.db.crate_graph();
275-
let file_id = crate_graph[self.def_map.krate].root_file_id;
275+
let file_id = crate_graph[self.def_map.krate].root_file_id();
276276
let item_tree = self.db.file_item_tree(file_id.into());
277277
let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
278278
let crate_data = Arc::get_mut(&mut self.def_map.data).unwrap();
@@ -2003,7 +2003,7 @@ impl ModCollector<'_, '_> {
20032003
&mut self,
20042004
name: Name,
20052005
declaration: FileAstId<ast::Module>,
2006-
definition: Option<(FileId, bool)>,
2006+
definition: Option<(EditionedFileId, bool)>,
20072007
visibility: &crate::visibility::RawVisibility,
20082008
mod_tree_id: FileItemTreeId<Mod>,
20092009
) -> LocalModuleId {

src/tools/rust-analyzer/crates/hir-def/src/nameres/mod_resolution.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! This module resolves `mod foo;` declaration to file.
22
use arrayvec::ArrayVec;
3-
use base_db::{AnchoredPath, FileId};
3+
use base_db::AnchoredPath;
44
use hir_expand::{name::Name, HirFileIdExt, MacroFileIdExt};
55
use limit::Limit;
6+
use span::EditionedFileId;
67
use syntax::ToSmolStr as _;
78

89
use crate::{db::DefDatabase, HirFileId};
@@ -64,7 +65,7 @@ impl ModDir {
6465
file_id: HirFileId,
6566
name: &Name,
6667
attr_path: Option<&str>,
67-
) -> Result<(FileId, bool, ModDir), Box<[String]>> {
68+
) -> Result<(EditionedFileId, bool, ModDir), Box<[String]>> {
6869
let name = name.unescaped();
6970

7071
let mut candidate_files = ArrayVec::<_, 2>::new();
@@ -92,7 +93,7 @@ impl ModDir {
9293

9394
let orig_file_id = file_id.original_file_respecting_includes(db.upcast());
9495
for candidate in candidate_files.iter() {
95-
let path = AnchoredPath { anchor: orig_file_id, path: candidate.as_str() };
96+
let path = AnchoredPath { anchor: orig_file_id.file_id(), path: candidate.as_str() };
9697
if let Some(file_id) = db.resolve_path(path) {
9798
let is_mod_rs = candidate.ends_with("/mod.rs");
9899

@@ -103,7 +104,12 @@ impl ModDir {
103104
DirPath::new(format!("{}/", name.display(db.upcast())))
104105
};
105106
if let Some(mod_dir) = self.child(dir_path, !root_dir_owner) {
106-
return Ok((file_id, is_mod_rs, mod_dir));
107+
return Ok((
108+
// FIXME: Edition, is this rightr?
109+
EditionedFileId::new(file_id, orig_file_id.edition()),
110+
is_mod_rs,
111+
mod_dir,
112+
));
107113
}
108114
}
109115
}

src/tools/rust-analyzer/crates/hir-def/src/nameres/tests/incremental.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn check_def_map_is_not_recomputed(ra_fixture_initial: &str, ra_fixture_change:
1616
});
1717
assert!(format!("{events:?}").contains("crate_def_map"), "{events:#?}")
1818
}
19-
db.set_file_text(pos.file_id, ra_fixture_change);
19+
db.set_file_text(pos.file_id.file_id(), ra_fixture_change);
2020

2121
{
2222
let events = db.log_executed(|| {
@@ -266,7 +266,7 @@ fn quux() { 92 }
266266
m!(Y);
267267
m!(Z);
268268
"#;
269-
db.set_file_text(pos.file_id, new_text);
269+
db.set_file_text(pos.file_id.file_id(), new_text);
270270

271271
{
272272
let events = db.log_executed(|| {

0 commit comments

Comments
 (0)