aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/cfg/src/lib.rs2
-rw-r--r--crates/hir/Cargo.toml1
-rw-r--r--crates/hir/src/attrs.rs2
-rw-r--r--crates/hir/src/lib.rs15
-rw-r--r--crates/hir/src/semantics.rs12
-rw-r--r--crates/hir/src/source_analyzer.rs2
-rw-r--r--crates/hir_def/src/attr.rs175
-rw-r--r--crates/hir_def/src/body.rs6
-rw-r--r--crates/hir_def/src/body/lower.rs28
-rw-r--r--crates/hir_def/src/find_path.rs2
-rw-r--r--crates/hir_def/src/item_tree.rs5
-rw-r--r--crates/hir_def/src/item_tree/lower.rs37
-rw-r--r--crates/hir_def/src/lib.rs18
-rw-r--r--crates/hir_def/src/nameres.rs3
-rw-r--r--crates/hir_def/src/nameres/collector.rs22
-rw-r--r--crates/hir_def/src/nameres/tests/incremental.rs58
-rw-r--r--crates/hir_def/src/path.rs9
-rw-r--r--crates/hir_def/src/path/lower.rs4
-rw-r--r--crates/hir_def/src/path/lower/lower_use.rs23
-rw-r--r--crates/hir_def/src/visibility.rs8
-rw-r--r--crates/hir_expand/src/builtin_derive.rs4
-rw-r--r--crates/hir_expand/src/builtin_macro.rs5
-rw-r--r--crates/hir_expand/src/db.rs469
-rw-r--r--crates/hir_expand/src/eager.rs7
-rw-r--r--crates/hir_expand/src/hygiene.rs73
-rw-r--r--crates/hir_expand/src/input.rs94
-rw-r--r--crates/hir_expand/src/lib.rs106
-rw-r--r--crates/hir_expand/src/proc_macro.rs102
-rw-r--r--crates/hir_expand/src/quote.rs4
-rw-r--r--crates/hir_ty/src/chalk_ext.rs36
-rw-r--r--crates/hir_ty/src/display.rs2
-rw-r--r--crates/ide/Cargo.toml1
-rw-r--r--crates/ide/src/diagnostics.rs63
-rw-r--r--crates/ide/src/diagnostics/fixes.rs51
-rw-r--r--crates/ide/src/diagnostics/unlinked_file.rs7
-rwxr-xr-x[-rw-r--r--]crates/ide/src/folding_ranges.rs45
-rw-r--r--crates/ide/src/goto_type_definition.rs12
-rw-r--r--crates/ide/src/inlay_hints.rs4
-rw-r--r--crates/ide/src/join_lines.rs69
-rw-r--r--crates/ide/src/lib.rs44
-rw-r--r--crates/ide/src/references.rs64
-rw-r--r--crates/ide/src/references/rename.rs17
-rw-r--r--crates/ide/src/ssr.rs54
-rw-r--r--crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html2
-rw-r--r--crates/ide/src/syntax_highlighting/tests.rs2
-rw-r--r--crates/ide/src/typing.rs150
-rw-r--r--crates/ide/src/typing/on_enter.rs115
-rw-r--r--crates/ide/src/view_crate_graph.rs111
-rw-r--r--crates/ide_assists/src/assist_context.rs57
-rw-r--r--crates/ide_assists/src/handlers/convert_tuple_struct_to_named_struct.rs2
-rw-r--r--crates/ide_assists/src/handlers/early_return.rs4
-rw-r--r--crates/ide_assists/src/handlers/expand_glob_import.rs89
-rw-r--r--crates/ide_assists/src/handlers/extract_function.rs197
-rw-r--r--crates/ide_assists/src/handlers/extract_variable.rs2
-rw-r--r--crates/ide_assists/src/handlers/generate_function.rs2
-rw-r--r--crates/ide_assists/src/handlers/introduce_named_lifetime.rs46
-rw-r--r--crates/ide_assists/src/handlers/merge_imports.rs4
-rw-r--r--crates/ide_assists/src/handlers/move_bounds.rs6
-rw-r--r--crates/ide_assists/src/handlers/pull_assignment_up.rs212
-rw-r--r--crates/ide_assists/src/handlers/reorder_impl.rs40
-rw-r--r--crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs8
-rw-r--r--crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs120
-rw-r--r--crates/ide_assists/src/handlers/replace_let_with_if_let.rs21
-rw-r--r--crates/ide_assists/src/handlers/replace_unwrap_with_match.rs21
-rw-r--r--crates/ide_assists/src/handlers/wrap_return_type_in_result.rs4
-rw-r--r--crates/ide_assists/src/lib.rs68
-rw-r--r--crates/ide_assists/src/tests.rs264
-rw-r--r--crates/ide_assists/src/tests/generated.rs2
-rw-r--r--crates/ide_assists/src/utils.rs4
-rw-r--r--crates/ide_assists/src/utils/suggest_name.rs15
-rw-r--r--crates/ide_completion/src/completions.rs56
-rw-r--r--crates/ide_completion/src/completions/pattern.rs6
-rw-r--r--crates/ide_completion/src/completions/postfix.rs5
-rw-r--r--crates/ide_completion/src/completions/qualified_path.rs73
-rw-r--r--crates/ide_completion/src/completions/trait_impl.rs69
-rw-r--r--crates/ide_completion/src/completions/unqualified_path.rs6
-rw-r--r--crates/ide_completion/src/context.rs229
-rw-r--r--crates/ide_completion/src/lib.rs2
-rw-r--r--crates/ide_completion/src/test_utils.rs2
-rw-r--r--crates/ide_db/src/helpers/insert_use/tests.rs84
-rw-r--r--crates/ide_db/src/helpers/merge_imports.rs16
-rw-r--r--crates/ide_db/src/line_index.rs7
-rw-r--r--crates/ide_db/src/search.rs177
-rw-r--r--crates/ide_db/src/ty_filter.rs6
-rw-r--r--crates/parser/src/grammar/patterns.rs2
-rw-r--r--crates/proc_macro_api/Cargo.toml2
-rw-r--r--crates/proc_macro_api/src/version.rs2
-rw-r--r--crates/profile/Cargo.toml2
-rw-r--r--crates/project_model/src/cargo_workspace.rs67
-rw-r--r--crates/project_model/src/cfg_flag.rs3
-rw-r--r--crates/project_model/src/rustc_cfg.rs45
-rw-r--r--crates/project_model/src/workspace.rs7
-rw-r--r--crates/rust-analyzer/Cargo.toml2
-rw-r--r--crates/rust-analyzer/build.rs19
-rw-r--r--crates/rust-analyzer/src/benchmarks.rs74
-rw-r--r--crates/rust-analyzer/src/cli/diagnostics.rs7
-rw-r--r--crates/rust-analyzer/src/config.rs20
-rw-r--r--crates/rust-analyzer/src/dispatch.rs21
-rw-r--r--crates/rust-analyzer/src/handlers.rs78
-rw-r--r--crates/rust-analyzer/src/integrated_benchmarks.rs184
-rw-r--r--crates/rust-analyzer/src/lib.rs2
-rw-r--r--crates/rust-analyzer/src/lsp_ext.rs8
-rw-r--r--crates/rust-analyzer/src/main_loop.rs1
-rw-r--r--crates/rust-analyzer/src/to_proto.rs3
-rw-r--r--crates/stdx/src/lib.rs78
-rw-r--r--crates/syntax/Cargo.toml2
-rw-r--r--crates/syntax/src/algo.rs124
-rw-r--r--crates/syntax/src/ast/edit.rs12
-rw-r--r--crates/syntax/src/ast/edit_in_place.rs19
-rw-r--r--crates/syntax/src/ast/make.rs128
-rw-r--r--crates/syntax/src/ast/node_ext.rs25
-rw-r--r--crates/syntax/src/ted.rs5
-rw-r--r--crates/syntax/src/token_text.rs50
-rw-r--r--crates/test_utils/src/fixture.rs10
-rw-r--r--crates/test_utils/src/lib.rs4
115 files changed, 3253 insertions, 1824 deletions
diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs
index d88ecf8b0..59fd38880 100644
--- a/crates/cfg/src/lib.rs
+++ b/crates/cfg/src/lib.rs
@@ -13,7 +13,7 @@ use tt::SmolStr;
13pub use cfg_expr::{CfgAtom, CfgExpr}; 13pub use cfg_expr::{CfgAtom, CfgExpr};
14pub use dnf::DnfExpr; 14pub use dnf::DnfExpr;
15 15
16/// Configuration options used for conditional compilition on items with `cfg` attributes. 16/// Configuration options used for conditional compilation on items with `cfg` attributes.
17/// We have two kind of options in different namespaces: atomic options like `unix`, and 17/// We have two kind of options in different namespaces: atomic options like `unix`, and
18/// key-value options like `target_arch="x86"`. 18/// key-value options like `target_arch="x86"`.
19/// 19///
diff --git a/crates/hir/Cargo.toml b/crates/hir/Cargo.toml
index 9e329656f..560b15238 100644
--- a/crates/hir/Cargo.toml
+++ b/crates/hir/Cargo.toml
@@ -25,3 +25,4 @@ hir_expand = { path = "../hir_expand", version = "0.0.0" }
25hir_def = { path = "../hir_def", version = "0.0.0" } 25hir_def = { path = "../hir_def", version = "0.0.0" }
26hir_ty = { path = "../hir_ty", version = "0.0.0" } 26hir_ty = { path = "../hir_ty", version = "0.0.0" }
27tt = { path = "../tt", version = "0.0.0" } 27tt = { path = "../tt", version = "0.0.0" }
28cfg = { path = "../cfg", version = "0.0.0" }
diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs
index 4a11622fc..e8fa3c56e 100644
--- a/crates/hir/src/attrs.rs
+++ b/crates/hir/src/attrs.rs
@@ -112,7 +112,7 @@ fn resolve_doc_path(
112 AttrDefId::MacroDefId(_) => return None, 112 AttrDefId::MacroDefId(_) => return None,
113 }; 113 };
114 let path = ast::Path::parse(link).ok()?; 114 let path = ast::Path::parse(link).ok()?;
115 let modpath = ModPath::from_src(path, &Hygiene::new_unhygienic()).unwrap(); 115 let modpath = ModPath::from_src(db.upcast(), path, &Hygiene::new_unhygienic()).unwrap();
116 let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath); 116 let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
117 if resolved == PerNs::none() { 117 if resolved == PerNs::none() {
118 if let Some(trait_id) = resolver.resolve_module_path_in_trait_items(db.upcast(), &modpath) { 118 if let Some(trait_id) = resolver.resolve_module_path_in_trait_items(db.upcast(), &modpath) {
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index d8ccfde0c..c9ef4b420 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -89,6 +89,7 @@ pub use crate::{
89// Generally, a refactoring which *removes* a name from this list is a good 89// Generally, a refactoring which *removes* a name from this list is a good
90// idea! 90// idea!
91pub use { 91pub use {
92 cfg::{CfgAtom, CfgExpr, CfgOptions},
92 hir_def::{ 93 hir_def::{
93 adt::StructKind, 94 adt::StructKind,
94 attr::{Attr, Attrs, AttrsWithOwner, Documentation}, 95 attr::{Attr, Attrs, AttrsWithOwner, Documentation},
@@ -215,6 +216,10 @@ impl Crate {
215 216
216 doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/") 217 doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/")
217 } 218 }
219
220 pub fn cfg(&self, db: &dyn HirDatabase) -> CfgOptions {
221 db.crate_graph()[self.id].cfg_options.clone()
222 }
218} 223}
219 224
220#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 225#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -1666,7 +1671,7 @@ impl Impl {
1666 .value 1671 .value
1667 .attrs() 1672 .attrs()
1668 .filter_map(|it| { 1673 .filter_map(|it| {
1669 let path = ModPath::from_src(it.path()?, &hygenic)?; 1674 let path = ModPath::from_src(db.upcast(), it.path()?, &hygenic)?;
1670 if path.as_ident()?.to_string() == "derive" { 1675 if path.as_ident()?.to_string() == "derive" {
1671 Some(it) 1676 Some(it)
1672 } else { 1677 } else {
@@ -1744,6 +1749,10 @@ impl Type {
1744 } 1749 }
1745 } 1750 }
1746 1751
1752 pub fn strip_references(&self) -> Type {
1753 self.derived(self.ty.strip_references().clone())
1754 }
1755
1747 pub fn is_unknown(&self) -> bool { 1756 pub fn is_unknown(&self) -> bool {
1748 self.ty.is_unknown() 1757 self.ty.is_unknown()
1749 } 1758 }
@@ -2062,6 +2071,10 @@ impl Type {
2062 Some(adt.into()) 2071 Some(adt.into())
2063 } 2072 }
2064 2073
2074 pub fn as_builtin(&self) -> Option<BuiltinType> {
2075 self.ty.as_builtin().map(|inner| BuiltinType { inner })
2076 }
2077
2065 pub fn as_dyn_trait(&self) -> Option<Trait> { 2078 pub fn as_dyn_trait(&self) -> Option<Trait> {
2066 self.ty.dyn_trait().map(Into::into) 2079 self.ty.dyn_trait().map(Into::into)
2067 } 2080 }
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs
index 62500602a..38bd376bc 100644
--- a/crates/hir/src/semantics.rs
+++ b/crates/hir/src/semantics.rs
@@ -196,6 +196,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
196 self.imp.resolve_label(lifetime) 196 self.imp.resolve_label(lifetime)
197 } 197 }
198 198
199 pub fn resolve_type(&self, ty: &ast::Type) -> Option<Type> {
200 self.imp.resolve_type(ty)
201 }
202
199 pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> { 203 pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
200 self.imp.type_of_expr(expr) 204 self.imp.type_of_expr(expr)
201 } 205 }
@@ -476,6 +480,14 @@ impl<'db> SemanticsImpl<'db> {
476 ToDef::to_def(self, src) 480 ToDef::to_def(self, src)
477 } 481 }
478 482
483 fn resolve_type(&self, ty: &ast::Type) -> Option<Type> {
484 let scope = self.scope(ty.syntax());
485 let ctx = body::LowerCtx::new(self.db.upcast(), scope.file_id);
486 let ty = hir_ty::TyLoweringContext::new(self.db, &scope.resolver)
487 .lower_ty(&crate::TypeRef::from_ast(&ctx, ty.clone()));
488 Type::new_with_resolver(self.db, &scope.resolver, ty)
489 }
490
479 fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> { 491 fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
480 self.analyze(expr.syntax()).type_of_expr(self.db, expr) 492 self.analyze(expr.syntax()).type_of_expr(self.db, expr)
481 } 493 }
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs
index 0895bd6f1..b5c65808e 100644
--- a/crates/hir/src/source_analyzer.rs
+++ b/crates/hir/src/source_analyzer.rs
@@ -283,7 +283,7 @@ impl SourceAnalyzer {
283 283
284 // This must be a normal source file rather than macro file. 284 // This must be a normal source file rather than macro file.
285 let hygiene = Hygiene::new(db.upcast(), self.file_id); 285 let hygiene = Hygiene::new(db.upcast(), self.file_id);
286 let ctx = body::LowerCtx::with_hygiene(&hygiene); 286 let ctx = body::LowerCtx::with_hygiene(db.upcast(), &hygiene);
287 let hir_path = Path::from_src(path.clone(), &ctx)?; 287 let hir_path = Path::from_src(path.clone(), &ctx)?;
288 288
289 // Case where path is a qualifier of another path, e.g. foo::bar::Baz where we 289 // Case where path is a qualifier of another path, e.g. foo::bar::Baz where we
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs
index d9294d93a..aadd4e44a 100644
--- a/crates/hir_def/src/attr.rs
+++ b/crates/hir_def/src/attr.rs
@@ -9,7 +9,7 @@ use std::{
9use base_db::CrateId; 9use base_db::CrateId;
10use cfg::{CfgExpr, CfgOptions}; 10use cfg::{CfgExpr, CfgOptions};
11use either::Either; 11use either::Either;
12use hir_expand::{hygiene::Hygiene, name::AsName, AstId, AttrId, InFile}; 12use hir_expand::{hygiene::Hygiene, name::AsName, AstId, InFile};
13use itertools::Itertools; 13use itertools::Itertools;
14use la_arena::ArenaMap; 14use la_arena::ArenaMap;
15use mbe::ast_to_token_tree; 15use mbe::ast_to_token_tree;
@@ -95,19 +95,19 @@ impl ops::Deref for AttrsWithOwner {
95impl RawAttrs { 95impl RawAttrs {
96 pub(crate) const EMPTY: Self = Self { entries: None }; 96 pub(crate) const EMPTY: Self = Self { entries: None };
97 97
98 pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self { 98 pub(crate) fn new(
99 db: &dyn DefDatabase,
100 owner: &dyn ast::AttrsOwner,
101 hygiene: &Hygiene,
102 ) -> Self {
99 let entries = collect_attrs(owner) 103 let entries = collect_attrs(owner)
100 .enumerate() 104 .flat_map(|(id, attr)| match attr {
101 .flat_map(|(i, attr)| { 105 Either::Left(attr) => Attr::from_src(db, attr, hygiene, id),
102 let index = AttrId(i as u32); 106 Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
103 match attr { 107 id,
104 Either::Left(attr) => Attr::from_src(attr, hygiene, index), 108 input: Some(AttrInput::Literal(SmolStr::new(doc))),
105 Either::Right(comment) => comment.doc_comment().map(|doc| Attr { 109 path: Interned::new(ModPath::from(hir_expand::name!(doc))),
106 id: index, 110 }),
107 input: Some(AttrInput::Literal(SmolStr::new(doc))),
108 path: Interned::new(ModPath::from(hir_expand::name!(doc))),
109 }),
110 }
111 }) 111 })
112 .collect::<Arc<_>>(); 112 .collect::<Arc<_>>();
113 113
@@ -116,10 +116,11 @@ impl RawAttrs {
116 116
117 fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn ast::AttrsOwner>) -> Self { 117 fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn ast::AttrsOwner>) -> Self {
118 let hygiene = Hygiene::new(db.upcast(), owner.file_id); 118 let hygiene = Hygiene::new(db.upcast(), owner.file_id);
119 Self::new(owner.value, &hygiene) 119 Self::new(db, owner.value, &hygiene)
120 } 120 }
121 121
122 pub(crate) fn merge(&self, other: Self) -> Self { 122 pub(crate) fn merge(&self, other: Self) -> Self {
123 // FIXME: This needs to fixup `AttrId`s
123 match (&self.entries, &other.entries) { 124 match (&self.entries, &other.entries) {
124 (None, None) => Self::EMPTY, 125 (None, None) => Self::EMPTY,
125 (Some(entries), None) | (None, Some(entries)) => { 126 (Some(entries), None) | (None, Some(entries)) => {
@@ -170,7 +171,7 @@ impl RawAttrs {
170 let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?; 171 let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?;
171 // FIXME hygiene 172 // FIXME hygiene
172 let hygiene = Hygiene::new_unhygienic(); 173 let hygiene = Hygiene::new_unhygienic();
173 Attr::from_src(attr, &hygiene, index) 174 Attr::from_src(db, attr, &hygiene, index)
174 }); 175 });
175 176
176 let cfg_options = &crate_graph[krate].cfg_options; 177 let cfg_options = &crate_graph[krate].cfg_options;
@@ -371,39 +372,26 @@ impl AttrsWithOwner {
371 372
372 let def_map = module.def_map(db); 373 let def_map = module.def_map(db);
373 let mod_data = &def_map[module.local_id]; 374 let mod_data = &def_map[module.local_id];
374 let attrs = match mod_data.declaration_source(db) { 375 match mod_data.declaration_source(db) {
375 Some(it) => { 376 Some(it) => {
376 let mut attrs: Vec<_> = collect_attrs(&it.value as &dyn ast::AttrsOwner) 377 let mut map = AttrSourceMap::new(InFile::new(it.file_id, &it.value));
377 .map(|attr| InFile::new(it.file_id, attr))
378 .collect();
379 if let InFile { file_id, value: ModuleSource::SourceFile(file) } = 378 if let InFile { file_id, value: ModuleSource::SourceFile(file) } =
380 mod_data.definition_source(db) 379 mod_data.definition_source(db)
381 { 380 {
382 attrs.extend( 381 map.merge(AttrSourceMap::new(InFile::new(file_id, &file)));
383 collect_attrs(&file as &dyn ast::AttrsOwner)
384 .map(|attr| InFile::new(file_id, attr)),
385 )
386 } 382 }
387 attrs 383 return map;
388 } 384 }
389 None => { 385 None => {
390 let InFile { file_id, value } = mod_data.definition_source(db); 386 let InFile { file_id, value } = mod_data.definition_source(db);
391 match &value { 387 let attrs_owner = match &value {
392 ModuleSource::SourceFile(file) => { 388 ModuleSource::SourceFile(file) => file as &dyn ast::AttrsOwner,
393 collect_attrs(file as &dyn ast::AttrsOwner) 389 ModuleSource::Module(module) => module as &dyn ast::AttrsOwner,
394 } 390 ModuleSource::BlockExpr(block) => block as &dyn ast::AttrsOwner,
395 ModuleSource::Module(module) => { 391 };
396 collect_attrs(module as &dyn ast::AttrsOwner) 392 return AttrSourceMap::new(InFile::new(file_id, attrs_owner));
397 }
398 ModuleSource::BlockExpr(block) => {
399 collect_attrs(block as &dyn ast::AttrsOwner)
400 }
401 }
402 .map(|attr| InFile::new(file_id, attr))
403 .collect()
404 } 393 }
405 }; 394 }
406 return AttrSourceMap { attrs };
407 } 395 }
408 AttrDefId::FieldId(id) => { 396 AttrDefId::FieldId(id) => {
409 let map = db.fields_attrs_source_map(id.parent); 397 let map = db.fields_attrs_source_map(id.parent);
@@ -458,11 +446,7 @@ impl AttrsWithOwner {
458 }, 446 },
459 }; 447 };
460 448
461 AttrSourceMap { 449 AttrSourceMap::new(owner.as_ref().map(|node| node as &dyn AttrsOwner))
462 attrs: collect_attrs(&owner.value)
463 .map(|attr| InFile::new(owner.file_id, attr))
464 .collect(),
465 }
466 } 450 }
467 451
468 pub fn docs_with_rangemap( 452 pub fn docs_with_rangemap(
@@ -484,10 +468,10 @@ impl AttrsWithOwner {
484 let mut buf = String::new(); 468 let mut buf = String::new();
485 let mut mapping = Vec::new(); 469 let mut mapping = Vec::new();
486 for (doc, idx) in docs { 470 for (doc, idx) in docs {
487 // str::lines doesn't yield anything for the empty string
488 if !doc.is_empty() { 471 if !doc.is_empty() {
489 for line in doc.split('\n') { 472 let mut base_offset = 0;
490 let line = line.trim_end(); 473 for raw_line in doc.split('\n') {
474 let line = raw_line.trim_end();
491 let line_len = line.len(); 475 let line_len = line.len();
492 let (offset, line) = match line.char_indices().nth(indent) { 476 let (offset, line) = match line.char_indices().nth(indent) {
493 Some((offset, _)) => (offset, &line[offset..]), 477 Some((offset, _)) => (offset, &line[offset..]),
@@ -498,9 +482,13 @@ impl AttrsWithOwner {
498 mapping.push(( 482 mapping.push((
499 TextRange::new(buf_offset.try_into().ok()?, buf.len().try_into().ok()?), 483 TextRange::new(buf_offset.try_into().ok()?, buf.len().try_into().ok()?),
500 idx, 484 idx,
501 TextRange::new(offset.try_into().ok()?, line_len.try_into().ok()?), 485 TextRange::at(
486 (base_offset + offset).try_into().ok()?,
487 line_len.try_into().ok()?,
488 ),
502 )); 489 ));
503 buf.push('\n'); 490 buf.push('\n');
491 base_offset += raw_line.len() + 1;
504 } 492 }
505 } else { 493 } else {
506 buf.push('\n'); 494 buf.push('\n');
@@ -510,7 +498,7 @@ impl AttrsWithOwner {
510 if buf.is_empty() { 498 if buf.is_empty() {
511 None 499 None
512 } else { 500 } else {
513 Some((Documentation(buf), DocsRangeMap { mapping, source: self.source_map(db).attrs })) 501 Some((Documentation(buf), DocsRangeMap { mapping, source_map: self.source_map(db) }))
514 } 502 }
515 } 503 }
516} 504}
@@ -551,27 +539,59 @@ fn inner_attributes(
551} 539}
552 540
553pub struct AttrSourceMap { 541pub struct AttrSourceMap {
554 attrs: Vec<InFile<Either<ast::Attr, ast::Comment>>>, 542 attrs: Vec<InFile<ast::Attr>>,
543 doc_comments: Vec<InFile<ast::Comment>>,
555} 544}
556 545
557impl AttrSourceMap { 546impl AttrSourceMap {
547 fn new(owner: InFile<&dyn ast::AttrsOwner>) -> Self {
548 let mut attrs = Vec::new();
549 let mut doc_comments = Vec::new();
550 for (_, attr) in collect_attrs(owner.value) {
551 match attr {
552 Either::Left(attr) => attrs.push(owner.with_value(attr)),
553 Either::Right(comment) => doc_comments.push(owner.with_value(comment)),
554 }
555 }
556
557 Self { attrs, doc_comments }
558 }
559
560 fn merge(&mut self, other: Self) {
561 self.attrs.extend(other.attrs);
562 self.doc_comments.extend(other.doc_comments);
563 }
564
558 /// Maps the lowered `Attr` back to its original syntax node. 565 /// Maps the lowered `Attr` back to its original syntax node.
559 /// 566 ///
560 /// `attr` must come from the `owner` used for AttrSourceMap 567 /// `attr` must come from the `owner` used for AttrSourceMap
561 /// 568 ///
562 /// Note that the returned syntax node might be a `#[cfg_attr]`, or a doc comment, instead of 569 /// Note that the returned syntax node might be a `#[cfg_attr]`, or a doc comment, instead of
563 /// the attribute represented by `Attr`. 570 /// the attribute represented by `Attr`.
564 pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> { 571 pub fn source_of(&self, attr: &Attr) -> InFile<Either<ast::Attr, ast::Comment>> {
565 self.attrs 572 self.source_of_id(attr.id)
566 .get(attr.id.0 as usize) 573 }
567 .unwrap_or_else(|| panic!("cannot find `Attr` at index {:?}", attr.id)) 574
568 .as_ref() 575 fn source_of_id(&self, id: AttrId) -> InFile<Either<ast::Attr, ast::Comment>> {
576 if id.is_doc_comment {
577 self.doc_comments
578 .get(id.ast_index as usize)
579 .unwrap_or_else(|| panic!("cannot find doc comment at index {:?}", id))
580 .clone()
581 .map(|attr| Either::Right(attr))
582 } else {
583 self.attrs
584 .get(id.ast_index as usize)
585 .unwrap_or_else(|| panic!("cannot find `Attr` at index {:?}", id))
586 .clone()
587 .map(|attr| Either::Left(attr))
588 }
569 } 589 }
570} 590}
571 591
572/// A struct to map text ranges from [`Documentation`] back to TextRanges in the syntax tree. 592/// A struct to map text ranges from [`Documentation`] back to TextRanges in the syntax tree.
573pub struct DocsRangeMap { 593pub struct DocsRangeMap {
574 source: Vec<InFile<Either<ast::Attr, ast::Comment>>>, 594 source_map: AttrSourceMap,
575 // (docstring-line-range, attr_index, attr-string-range) 595 // (docstring-line-range, attr_index, attr-string-range)
576 // a mapping from the text range of a line of the [`Documentation`] to the attribute index and 596 // a mapping from the text range of a line of the [`Documentation`] to the attribute index and
577 // the original (untrimmed) syntax doc line 597 // the original (untrimmed) syntax doc line
@@ -588,7 +608,7 @@ impl DocsRangeMap {
588 608
589 let relative_range = range - line_docs_range.start(); 609 let relative_range = range - line_docs_range.start();
590 610
591 let &InFile { file_id, value: ref source } = &self.source[idx.0 as usize]; 611 let &InFile { file_id, value: ref source } = &self.source_map.source_of_id(idx);
592 match source { 612 match source {
593 Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here 613 Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here
594 // as well as for whats done in syntax highlight doc injection 614 // as well as for whats done in syntax highlight doc injection
@@ -607,6 +627,12 @@ impl DocsRangeMap {
607 } 627 }
608} 628}
609 629
630#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
631pub(crate) struct AttrId {
632 is_doc_comment: bool,
633 pub(crate) ast_index: u32,
634}
635
610#[derive(Debug, Clone, PartialEq, Eq)] 636#[derive(Debug, Clone, PartialEq, Eq)]
611pub struct Attr { 637pub struct Attr {
612 pub(crate) id: AttrId, 638 pub(crate) id: AttrId,
@@ -623,8 +649,13 @@ pub enum AttrInput {
623} 649}
624 650
625impl Attr { 651impl Attr {
626 fn from_src(ast: ast::Attr, hygiene: &Hygiene, id: AttrId) -> Option<Attr> { 652 fn from_src(
627 let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?); 653 db: &dyn DefDatabase,
654 ast: ast::Attr,
655 hygiene: &Hygiene,
656 id: AttrId,
657 ) -> Option<Attr> {
658 let path = Interned::new(ModPath::from_src(db, ast.path()?, hygiene)?);
628 let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { 659 let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
629 let value = match lit.kind() { 660 let value = match lit.kind() {
630 ast::LiteralKind::String(string) => string.value()?.into(), 661 ast::LiteralKind::String(string) => string.value()?.into(),
@@ -736,22 +767,32 @@ fn attrs_from_item_tree<N: ItemTreeNode>(id: ItemTreeId<N>, db: &dyn DefDatabase
736 767
737fn collect_attrs( 768fn collect_attrs(
738 owner: &dyn ast::AttrsOwner, 769 owner: &dyn ast::AttrsOwner,
739) -> impl Iterator<Item = Either<ast::Attr, ast::Comment>> { 770) -> impl Iterator<Item = (AttrId, Either<ast::Attr, ast::Comment>)> {
740 let (inner_attrs, inner_docs) = inner_attributes(owner.syntax()) 771 let (inner_attrs, inner_docs) = inner_attributes(owner.syntax())
741 .map_or((None, None), |(attrs, docs)| (Some(attrs), Some(docs))); 772 .map_or((None, None), |(attrs, docs)| (Some(attrs), Some(docs)));
742 773
743 let outer_attrs = owner.attrs().filter(|attr| attr.kind().is_outer()); 774 let outer_attrs = owner.attrs().filter(|attr| attr.kind().is_outer());
744 let attrs = outer_attrs 775 let attrs =
745 .chain(inner_attrs.into_iter().flatten()) 776 outer_attrs.chain(inner_attrs.into_iter().flatten()).enumerate().map(|(idx, attr)| {
746 .map(|attr| (attr.syntax().text_range().start(), Either::Left(attr))); 777 (
778 AttrId { ast_index: idx as u32, is_doc_comment: false },
779 attr.syntax().text_range().start(),
780 Either::Left(attr),
781 )
782 });
747 783
748 let outer_docs = 784 let outer_docs =
749 ast::CommentIter::from_syntax_node(owner.syntax()).filter(ast::Comment::is_outer); 785 ast::CommentIter::from_syntax_node(owner.syntax()).filter(ast::Comment::is_outer);
750 let docs = outer_docs 786 let docs =
751 .chain(inner_docs.into_iter().flatten()) 787 outer_docs.chain(inner_docs.into_iter().flatten()).enumerate().map(|(idx, docs_text)| {
752 .map(|docs_text| (docs_text.syntax().text_range().start(), Either::Right(docs_text))); 788 (
789 AttrId { ast_index: idx as u32, is_doc_comment: true },
790 docs_text.syntax().text_range().start(),
791 Either::Right(docs_text),
792 )
793 });
753 // sort here by syntax node offset because the source can have doc attributes and doc strings be interleaved 794 // sort here by syntax node offset because the source can have doc attributes and doc strings be interleaved
754 docs.chain(attrs).sorted_by_key(|&(offset, _)| offset).map(|(_, attr)| attr) 795 docs.chain(attrs).sorted_by_key(|&(_, offset, _)| offset).map(|(id, _, attr)| (id, attr))
755} 796}
756 797
757pub(crate) fn variants_attrs_source_map( 798pub(crate) fn variants_attrs_source_map(
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs
index 131f424cc..8360426f1 100644
--- a/crates/hir_def/src/body.rs
+++ b/crates/hir_def/src/body.rs
@@ -72,7 +72,7 @@ impl CfgExpander {
72 } 72 }
73 73
74 pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::AttrsOwner) -> Attrs { 74 pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::AttrsOwner) -> Attrs {
75 RawAttrs::new(owner, &self.hygiene).filter(db, self.krate) 75 RawAttrs::new(db, owner, &self.hygiene).filter(db, self.krate)
76 } 76 }
77 77
78 pub(crate) fn is_cfg_enabled(&self, db: &dyn DefDatabase, owner: &dyn ast::AttrsOwner) -> bool { 78 pub(crate) fn is_cfg_enabled(&self, db: &dyn DefDatabase, owner: &dyn ast::AttrsOwner) -> bool {
@@ -192,8 +192,8 @@ impl Expander {
192 self.current_file_id 192 self.current_file_id
193 } 193 }
194 194
195 fn parse_path(&mut self, path: ast::Path) -> Option<Path> { 195 fn parse_path(&mut self, db: &dyn DefDatabase, path: ast::Path) -> Option<Path> {
196 let ctx = LowerCtx::with_hygiene(&self.cfg_expander.hygiene); 196 let ctx = LowerCtx::with_hygiene(db, &self.cfg_expander.hygiene);
197 Path::from_src(path, &ctx) 197 Path::from_src(path, &ctx)
198 } 198 }
199 199
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs
index 820d5c17e..9f278d35b 100644
--- a/crates/hir_def/src/body/lower.rs
+++ b/crates/hir_def/src/body/lower.rs
@@ -40,23 +40,25 @@ use crate::{
40 40
41use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; 41use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};
42 42
43pub struct LowerCtx { 43pub struct LowerCtx<'a> {
44 pub db: &'a dyn DefDatabase,
44 hygiene: Hygiene, 45 hygiene: Hygiene,
45 file_id: Option<HirFileId>, 46 file_id: Option<HirFileId>,
46 source_ast_id_map: Option<Arc<AstIdMap>>, 47 source_ast_id_map: Option<Arc<AstIdMap>>,
47} 48}
48 49
49impl LowerCtx { 50impl<'a> LowerCtx<'a> {
50 pub fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self { 51 pub fn new(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
51 LowerCtx { 52 LowerCtx {
53 db,
52 hygiene: Hygiene::new(db.upcast(), file_id), 54 hygiene: Hygiene::new(db.upcast(), file_id),
53 file_id: Some(file_id), 55 file_id: Some(file_id),
54 source_ast_id_map: Some(db.ast_id_map(file_id)), 56 source_ast_id_map: Some(db.ast_id_map(file_id)),
55 } 57 }
56 } 58 }
57 59
58 pub fn with_hygiene(hygiene: &Hygiene) -> Self { 60 pub fn with_hygiene(db: &'a dyn DefDatabase, hygiene: &Hygiene) -> Self {
59 LowerCtx { hygiene: hygiene.clone(), file_id: None, source_ast_id_map: None } 61 LowerCtx { db, hygiene: hygiene.clone(), file_id: None, source_ast_id_map: None }
60 } 62 }
61 63
62 pub(crate) fn hygiene(&self) -> &Hygiene { 64 pub(crate) fn hygiene(&self) -> &Hygiene {
@@ -145,7 +147,7 @@ impl ExprCollector<'_> {
145 (self.body, self.source_map) 147 (self.body, self.source_map)
146 } 148 }
147 149
148 fn ctx(&self) -> LowerCtx { 150 fn ctx(&self) -> LowerCtx<'_> {
149 LowerCtx::new(self.db, self.expander.current_file_id) 151 LowerCtx::new(self.db, self.expander.current_file_id)
150 } 152 }
151 153
@@ -376,7 +378,7 @@ impl ExprCollector<'_> {
376 ast::Expr::PathExpr(e) => { 378 ast::Expr::PathExpr(e) => {
377 let path = e 379 let path = e
378 .path() 380 .path()
379 .and_then(|path| self.expander.parse_path(path)) 381 .and_then(|path| self.expander.parse_path(self.db, path))
380 .map(Expr::Path) 382 .map(Expr::Path)
381 .unwrap_or(Expr::Missing); 383 .unwrap_or(Expr::Missing);
382 self.alloc_expr(path, syntax_ptr) 384 self.alloc_expr(path, syntax_ptr)
@@ -408,7 +410,8 @@ impl ExprCollector<'_> {
408 self.alloc_expr(Expr::Yield { expr }, syntax_ptr) 410 self.alloc_expr(Expr::Yield { expr }, syntax_ptr)
409 } 411 }
410 ast::Expr::RecordExpr(e) => { 412 ast::Expr::RecordExpr(e) => {
411 let path = e.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); 413 let path =
414 e.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
412 let record_lit = if let Some(nfl) = e.record_expr_field_list() { 415 let record_lit = if let Some(nfl) = e.record_expr_field_list() {
413 let fields = nfl 416 let fields = nfl
414 .fields() 417 .fields()
@@ -801,7 +804,8 @@ impl ExprCollector<'_> {
801 } 804 }
802 } 805 }
803 ast::Pat::TupleStructPat(p) => { 806 ast::Pat::TupleStructPat(p) => {
804 let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); 807 let path =
808 p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
805 let (args, ellipsis) = self.collect_tuple_pat(p.fields()); 809 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
806 Pat::TupleStruct { path, args, ellipsis } 810 Pat::TupleStruct { path, args, ellipsis }
807 } 811 }
@@ -811,7 +815,8 @@ impl ExprCollector<'_> {
811 Pat::Ref { pat, mutability } 815 Pat::Ref { pat, mutability }
812 } 816 }
813 ast::Pat::PathPat(p) => { 817 ast::Pat::PathPat(p) => {
814 let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); 818 let path =
819 p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
815 path.map(Pat::Path).unwrap_or(Pat::Missing) 820 path.map(Pat::Path).unwrap_or(Pat::Missing)
816 } 821 }
817 ast::Pat::OrPat(p) => { 822 ast::Pat::OrPat(p) => {
@@ -825,7 +830,8 @@ impl ExprCollector<'_> {
825 } 830 }
826 ast::Pat::WildcardPat(_) => Pat::Wild, 831 ast::Pat::WildcardPat(_) => Pat::Wild,
827 ast::Pat::RecordPat(p) => { 832 ast::Pat::RecordPat(p) => {
828 let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); 833 let path =
834 p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
829 let args: Vec<_> = p 835 let args: Vec<_> = p
830 .record_pat_field_list() 836 .record_pat_field_list()
831 .expect("every struct should have a field list") 837 .expect("every struct should have a field list")
diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs
index c06a37294..858e88038 100644
--- a/crates/hir_def/src/find_path.rs
+++ b/crates/hir_def/src/find_path.rs
@@ -386,7 +386,7 @@ mod tests {
386 let parsed_path_file = syntax::SourceFile::parse(&format!("use {};", path)); 386 let parsed_path_file = syntax::SourceFile::parse(&format!("use {};", path));
387 let ast_path = 387 let ast_path =
388 parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap(); 388 parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap();
389 let mod_path = ModPath::from_src(ast_path, &Hygiene::new_unhygienic()).unwrap(); 389 let mod_path = ModPath::from_src(&db, ast_path, &Hygiene::new_unhygienic()).unwrap();
390 390
391 let def_map = module.def_map(&db); 391 let def_map = module.def_map(&db);
392 let resolved = def_map 392 let resolved = def_map
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs
index eaeca01bd..cad8a7479 100644
--- a/crates/hir_def/src/item_tree.rs
+++ b/crates/hir_def/src/item_tree.rs
@@ -18,7 +18,7 @@ use hir_expand::{
18 ast_id_map::FileAstId, 18 ast_id_map::FileAstId,
19 hygiene::Hygiene, 19 hygiene::Hygiene,
20 name::{name, AsName, Name}, 20 name::{name, AsName, Name},
21 HirFileId, InFile, 21 FragmentKind, HirFileId, InFile,
22}; 22};
23use la_arena::{Arena, Idx, RawIdx}; 23use la_arena::{Arena, Idx, RawIdx};
24use profile::Count; 24use profile::Count;
@@ -88,7 +88,7 @@ impl ItemTree {
88 let mut item_tree = match_ast! { 88 let mut item_tree = match_ast! {
89 match syntax { 89 match syntax {
90 ast::SourceFile(file) => { 90 ast::SourceFile(file) => {
91 top_attrs = Some(RawAttrs::new(&file, &hygiene)); 91 top_attrs = Some(RawAttrs::new(db, &file, &hygiene));
92 ctx.lower_module_items(&file) 92 ctx.lower_module_items(&file)
93 }, 93 },
94 ast::MacroItems(items) => { 94 ast::MacroItems(items) => {
@@ -656,6 +656,7 @@ pub struct MacroCall {
656 /// Path to the called macro. 656 /// Path to the called macro.
657 pub path: Interned<ModPath>, 657 pub path: Interned<ModPath>,
658 pub ast_id: FileAstId<ast::MacroCall>, 658 pub ast_id: FileAstId<ast::MacroCall>,
659 pub fragment: FragmentKind,
659} 660}
660 661
661#[derive(Debug, Clone, Eq, PartialEq)] 662#[derive(Debug, Clone, Eq, PartialEq)]
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs
index 45b099cf3..fe348091d 100644
--- a/crates/hir_def/src/item_tree/lower.rs
+++ b/crates/hir_def/src/item_tree/lower.rs
@@ -31,18 +31,20 @@ where
31 } 31 }
32} 32}
33 33
34pub(super) struct Ctx { 34pub(super) struct Ctx<'a> {
35 db: &'a dyn DefDatabase,
35 tree: ItemTree, 36 tree: ItemTree,
36 hygiene: Hygiene, 37 hygiene: Hygiene,
37 file: HirFileId, 38 file: HirFileId,
38 source_ast_id_map: Arc<AstIdMap>, 39 source_ast_id_map: Arc<AstIdMap>,
39 body_ctx: crate::body::LowerCtx, 40 body_ctx: crate::body::LowerCtx<'a>,
40 forced_visibility: Option<RawVisibilityId>, 41 forced_visibility: Option<RawVisibilityId>,
41} 42}
42 43
43impl Ctx { 44impl<'a> Ctx<'a> {
44 pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self { 45 pub(super) fn new(db: &'a dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
45 Self { 46 Self {
47 db,
46 tree: ItemTree::default(), 48 tree: ItemTree::default(),
47 hygiene, 49 hygiene,
48 file, 50 file,
@@ -126,7 +128,7 @@ impl Ctx {
126 | ast::Item::MacroDef(_) => {} 128 | ast::Item::MacroDef(_) => {}
127 }; 129 };
128 130
129 let attrs = RawAttrs::new(item, &self.hygiene); 131 let attrs = RawAttrs::new(self.db, item, &self.hygiene);
130 let items = match item { 132 let items = match item {
131 ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into), 133 ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into),
132 ast::Item::Union(ast) => self.lower_union(ast).map(Into::into), 134 ast::Item::Union(ast) => self.lower_union(ast).map(Into::into),
@@ -256,7 +258,7 @@ impl Ctx {
256 for field in fields.fields() { 258 for field in fields.fields() {
257 if let Some(data) = self.lower_record_field(&field) { 259 if let Some(data) = self.lower_record_field(&field) {
258 let idx = self.data().fields.alloc(data); 260 let idx = self.data().fields.alloc(data);
259 self.add_attrs(idx.into(), RawAttrs::new(&field, &self.hygiene)); 261 self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene));
260 } 262 }
261 } 263 }
262 let end = self.next_field_idx(); 264 let end = self.next_field_idx();
@@ -276,7 +278,7 @@ impl Ctx {
276 for (i, field) in fields.fields().enumerate() { 278 for (i, field) in fields.fields().enumerate() {
277 let data = self.lower_tuple_field(i, &field); 279 let data = self.lower_tuple_field(i, &field);
278 let idx = self.data().fields.alloc(data); 280 let idx = self.data().fields.alloc(data);
279 self.add_attrs(idx.into(), RawAttrs::new(&field, &self.hygiene)); 281 self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene));
280 } 282 }
281 let end = self.next_field_idx(); 283 let end = self.next_field_idx();
282 IdRange::new(start..end) 284 IdRange::new(start..end)
@@ -321,7 +323,7 @@ impl Ctx {
321 for variant in variants.variants() { 323 for variant in variants.variants() {
322 if let Some(data) = self.lower_variant(&variant) { 324 if let Some(data) = self.lower_variant(&variant) {
323 let idx = self.data().variants.alloc(data); 325 let idx = self.data().variants.alloc(data);
324 self.add_attrs(idx.into(), RawAttrs::new(&variant, &self.hygiene)); 326 self.add_attrs(idx.into(), RawAttrs::new(self.db, &variant, &self.hygiene));
325 } 327 }
326 } 328 }
327 let end = self.next_variant_idx(); 329 let end = self.next_variant_idx();
@@ -364,7 +366,7 @@ impl Ctx {
364 }; 366 };
365 let ty = Interned::new(self_type); 367 let ty = Interned::new(self_type);
366 let idx = self.data().params.alloc(Param::Normal(ty)); 368 let idx = self.data().params.alloc(Param::Normal(ty));
367 self.add_attrs(idx.into(), RawAttrs::new(&self_param, &self.hygiene)); 369 self.add_attrs(idx.into(), RawAttrs::new(self.db, &self_param, &self.hygiene));
368 has_self_param = true; 370 has_self_param = true;
369 } 371 }
370 for param in param_list.params() { 372 for param in param_list.params() {
@@ -376,7 +378,7 @@ impl Ctx {
376 self.data().params.alloc(Param::Normal(ty)) 378 self.data().params.alloc(Param::Normal(ty))
377 } 379 }
378 }; 380 };
379 self.add_attrs(idx.into(), RawAttrs::new(&param, &self.hygiene)); 381 self.add_attrs(idx.into(), RawAttrs::new(self.db, &param, &self.hygiene));
380 } 382 }
381 } 383 }
382 let end_param = self.next_param_idx(); 384 let end_param = self.next_param_idx();
@@ -522,10 +524,11 @@ impl Ctx {
522 let is_unsafe = trait_def.unsafe_token().is_some(); 524 let is_unsafe = trait_def.unsafe_token().is_some();
523 let bounds = self.lower_type_bounds(trait_def); 525 let bounds = self.lower_type_bounds(trait_def);
524 let items = trait_def.assoc_item_list().map(|list| { 526 let items = trait_def.assoc_item_list().map(|list| {
527 let db = self.db;
525 self.with_inherited_visibility(visibility, |this| { 528 self.with_inherited_visibility(visibility, |this| {
526 list.assoc_items() 529 list.assoc_items()
527 .filter_map(|item| { 530 .filter_map(|item| {
528 let attrs = RawAttrs::new(&item, &this.hygiene); 531 let attrs = RawAttrs::new(db, &item, &this.hygiene);
529 this.collect_inner_items(item.syntax()); 532 this.collect_inner_items(item.syntax());
530 this.lower_assoc_item(&item).map(|item| { 533 this.lower_assoc_item(&item).map(|item| {
531 this.add_attrs(ModItem::from(item).into(), attrs); 534 this.add_attrs(ModItem::from(item).into(), attrs);
@@ -567,7 +570,7 @@ impl Ctx {
567 .filter_map(|item| { 570 .filter_map(|item| {
568 self.collect_inner_items(item.syntax()); 571 self.collect_inner_items(item.syntax());
569 let assoc = self.lower_assoc_item(&item)?; 572 let assoc = self.lower_assoc_item(&item)?;
570 let attrs = RawAttrs::new(&item, &self.hygiene); 573 let attrs = RawAttrs::new(self.db, &item, &self.hygiene);
571 self.add_attrs(ModItem::from(assoc).into(), attrs); 574 self.add_attrs(ModItem::from(assoc).into(), attrs);
572 Some(assoc) 575 Some(assoc)
573 }) 576 })
@@ -585,6 +588,7 @@ impl Ctx {
585 let mut imports = Vec::new(); 588 let mut imports = Vec::new();
586 let tree = self.tree.data_mut(); 589 let tree = self.tree.data_mut();
587 ModPath::expand_use_item( 590 ModPath::expand_use_item(
591 self.db,
588 InFile::new(self.file, use_item.clone()), 592 InFile::new(self.file, use_item.clone()),
589 &self.hygiene, 593 &self.hygiene,
590 |path, _use_tree, is_glob, alias| { 594 |path, _use_tree, is_glob, alias| {
@@ -618,9 +622,10 @@ impl Ctx {
618 } 622 }
619 623
620 fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> { 624 fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
621 let path = Interned::new(ModPath::from_src(m.path()?, &self.hygiene)?); 625 let path = Interned::new(ModPath::from_src(self.db, m.path()?, &self.hygiene)?);
622 let ast_id = self.source_ast_id_map.ast_id(m); 626 let ast_id = self.source_ast_id_map.ast_id(m);
623 let res = MacroCall { path, ast_id }; 627 let fragment = hir_expand::to_fragment_kind(m);
628 let res = MacroCall { path, ast_id, fragment };
624 Some(id(self.data().macro_calls.alloc(res))) 629 Some(id(self.data().macro_calls.alloc(res)))
625 } 630 }
626 631
@@ -647,7 +652,7 @@ impl Ctx {
647 list.extern_items() 652 list.extern_items()
648 .filter_map(|item| { 653 .filter_map(|item| {
649 self.collect_inner_items(item.syntax()); 654 self.collect_inner_items(item.syntax());
650 let attrs = RawAttrs::new(&item, &self.hygiene); 655 let attrs = RawAttrs::new(self.db, &item, &self.hygiene);
651 let id: ModItem = match item { 656 let id: ModItem = match item {
652 ast::ExternItem::Fn(ast) => { 657 ast::ExternItem::Fn(ast) => {
653 let func_id = self.lower_function(&ast)?; 658 let func_id = self.lower_function(&ast)?;
@@ -755,7 +760,7 @@ impl Ctx {
755 fn lower_visibility(&mut self, item: &impl ast::VisibilityOwner) -> RawVisibilityId { 760 fn lower_visibility(&mut self, item: &impl ast::VisibilityOwner) -> RawVisibilityId {
756 let vis = match self.forced_visibility { 761 let vis = match self.forced_visibility {
757 Some(vis) => return vis, 762 Some(vis) => return vis,
758 None => RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene), 763 None => RawVisibility::from_ast_with_hygiene(self.db, item.visibility(), &self.hygiene),
759 }; 764 };
760 765
761 self.data().vis.alloc(vis) 766 self.data().vis.alloc(vis)
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs
index 25694f037..a82ea5957 100644
--- a/crates/hir_def/src/lib.rs
+++ b/crates/hir_def/src/lib.rs
@@ -62,13 +62,14 @@ use hir_expand::{
62 ast_id_map::FileAstId, 62 ast_id_map::FileAstId,
63 eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, 63 eager::{expand_eager_macro, ErrorEmitted, ErrorSink},
64 hygiene::Hygiene, 64 hygiene::Hygiene,
65 AstId, AttrId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, 65 AstId, FragmentKind, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
66}; 66};
67use la_arena::Idx; 67use la_arena::Idx;
68use nameres::DefMap; 68use nameres::DefMap;
69use path::ModPath; 69use path::ModPath;
70use syntax::ast; 70use syntax::ast;
71 71
72use crate::attr::AttrId;
72use crate::builtin_type::BuiltinType; 73use crate::builtin_type::BuiltinType;
73use item_tree::{ 74use item_tree::{
74 Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait, 75 Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait,
@@ -652,9 +653,10 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
652 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, 653 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
653 mut error_sink: &mut dyn FnMut(mbe::ExpandError), 654 mut error_sink: &mut dyn FnMut(mbe::ExpandError),
654 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> { 655 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
656 let fragment = hir_expand::to_fragment_kind(self.value);
655 let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value)); 657 let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
656 let h = Hygiene::new(db.upcast(), self.file_id); 658 let h = Hygiene::new(db.upcast(), self.file_id);
657 let path = self.value.path().and_then(|path| path::ModPath::from_src(path, &h)); 659 let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
658 660
659 let path = match error_sink 661 let path = match error_sink
660 .option(path, || mbe::ExpandError::Other("malformed macro invocation".into())) 662 .option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
@@ -667,6 +669,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
667 669
668 macro_call_as_call_id( 670 macro_call_as_call_id(
669 &AstIdWithPath::new(ast_id.file_id, ast_id.value, path), 671 &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
672 fragment,
670 db, 673 db,
671 krate, 674 krate,
672 resolver, 675 resolver,
@@ -695,6 +698,7 @@ pub struct UnresolvedMacro {
695 698
696fn macro_call_as_call_id( 699fn macro_call_as_call_id(
697 call: &AstIdWithPath<ast::MacroCall>, 700 call: &AstIdWithPath<ast::MacroCall>,
701 fragment: FragmentKind,
698 db: &dyn db::DefDatabase, 702 db: &dyn db::DefDatabase,
699 krate: CrateId, 703 krate: CrateId,
700 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, 704 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
@@ -712,13 +716,17 @@ fn macro_call_as_call_id(
712 krate, 716 krate,
713 macro_call, 717 macro_call,
714 def, 718 def,
715 &|path: ast::Path| resolver(path::ModPath::from_src(path, &hygiene)?), 719 &|path: ast::Path| resolver(path::ModPath::from_src(db, path, &hygiene)?),
716 error_sink, 720 error_sink,
717 ) 721 )
718 .map(MacroCallId::from) 722 .map(MacroCallId::from)
719 } else { 723 } else {
720 Ok(def 724 Ok(def
721 .as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike { ast_id: call.ast_id }) 725 .as_lazy_macro(
726 db.upcast(),
727 krate,
728 MacroCallKind::FnLike { ast_id: call.ast_id, fragment },
729 )
722 .into()) 730 .into())
723 }; 731 };
724 Ok(res) 732 Ok(res)
@@ -745,7 +753,7 @@ fn derive_macro_as_call_id(
745 MacroCallKind::Derive { 753 MacroCallKind::Derive {
746 ast_id: item_attr.ast_id, 754 ast_id: item_attr.ast_id,
747 derive_name: last_segment.to_string(), 755 derive_name: last_segment.to_string(),
748 derive_attr, 756 derive_attr_index: derive_attr.ast_index,
749 }, 757 },
750 ) 758 )
751 .into(); 759 .into();
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs
index ba027c44a..249af6fc8 100644
--- a/crates/hir_def/src/nameres.rs
+++ b/crates/hir_def/src/nameres.rs
@@ -599,6 +599,7 @@ mod diagnostics {
599 let mut cur = 0; 599 let mut cur = 0;
600 let mut tree = None; 600 let mut tree = None;
601 ModPath::expand_use_item( 601 ModPath::expand_use_item(
602 db,
602 InFile::new(ast.file_id, use_item), 603 InFile::new(ast.file_id, use_item),
603 &hygiene, 604 &hygiene,
604 |_mod_path, use_tree, _is_glob, _alias| { 605 |_mod_path, use_tree, _is_glob, _alias| {
@@ -628,7 +629,7 @@ mod diagnostics {
628 DiagnosticKind::UnresolvedProcMacro { ast } => { 629 DiagnosticKind::UnresolvedProcMacro { ast } => {
629 let mut precise_location = None; 630 let mut precise_location = None;
630 let (file, ast, name) = match ast { 631 let (file, ast, name) = match ast {
631 MacroCallKind::FnLike { ast_id } => { 632 MacroCallKind::FnLike { ast_id, .. } => {
632 let node = ast_id.to_node(db.upcast()); 633 let node = ast_id.to_node(db.upcast());
633 (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) 634 (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None)
634 } 635 }
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs
index 05ceb1efb..adfb78c94 100644
--- a/crates/hir_def/src/nameres/collector.rs
+++ b/crates/hir_def/src/nameres/collector.rs
@@ -13,14 +13,14 @@ use hir_expand::{
13 builtin_macro::find_builtin_macro, 13 builtin_macro::find_builtin_macro,
14 name::{AsName, Name}, 14 name::{AsName, Name},
15 proc_macro::ProcMacroExpander, 15 proc_macro::ProcMacroExpander,
16 AttrId, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, 16 FragmentKind, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
17}; 17};
18use hir_expand::{InFile, MacroCallLoc}; 18use hir_expand::{InFile, MacroCallLoc};
19use rustc_hash::{FxHashMap, FxHashSet}; 19use rustc_hash::{FxHashMap, FxHashSet};
20use syntax::ast; 20use syntax::ast;
21 21
22use crate::{ 22use crate::{
23 attr::Attrs, 23 attr::{AttrId, Attrs},
24 db::DefDatabase, 24 db::DefDatabase,
25 derive_macro_as_call_id, 25 derive_macro_as_call_id,
26 intern::Interned, 26 intern::Interned,
@@ -215,7 +215,7 @@ struct MacroDirective {
215 215
216#[derive(Clone, Debug, Eq, PartialEq)] 216#[derive(Clone, Debug, Eq, PartialEq)]
217enum MacroDirectiveKind { 217enum MacroDirectiveKind {
218 FnLike { ast_id: AstIdWithPath<ast::MacroCall> }, 218 FnLike { ast_id: AstIdWithPath<ast::MacroCall>, fragment: FragmentKind },
219 Derive { ast_id: AstIdWithPath<ast::Item>, derive_attr: AttrId }, 219 Derive { ast_id: AstIdWithPath<ast::Item>, derive_attr: AttrId },
220} 220}
221 221
@@ -807,9 +807,10 @@ impl DefCollector<'_> {
807 let mut res = ReachedFixedPoint::Yes; 807 let mut res = ReachedFixedPoint::Yes;
808 macros.retain(|directive| { 808 macros.retain(|directive| {
809 match &directive.kind { 809 match &directive.kind {
810 MacroDirectiveKind::FnLike { ast_id } => { 810 MacroDirectiveKind::FnLike { ast_id, fragment } => {
811 match macro_call_as_call_id( 811 match macro_call_as_call_id(
812 ast_id, 812 ast_id,
813 *fragment,
813 self.db, 814 self.db,
814 self.def_map.krate, 815 self.def_map.krate,
815 |path| { 816 |path| {
@@ -926,8 +927,9 @@ impl DefCollector<'_> {
926 927
927 for directive in &self.unexpanded_macros { 928 for directive in &self.unexpanded_macros {
928 match &directive.kind { 929 match &directive.kind {
929 MacroDirectiveKind::FnLike { ast_id, .. } => match macro_call_as_call_id( 930 MacroDirectiveKind::FnLike { ast_id, fragment } => match macro_call_as_call_id(
930 ast_id, 931 ast_id,
932 *fragment,
931 self.db, 933 self.db,
932 self.def_map.krate, 934 self.def_map.krate,
933 |path| { 935 |path| {
@@ -1496,6 +1498,7 @@ impl ModCollector<'_, '_> {
1496 let mut error = None; 1498 let mut error = None;
1497 match macro_call_as_call_id( 1499 match macro_call_as_call_id(
1498 &ast_id, 1500 &ast_id,
1501 mac.fragment,
1499 self.def_collector.db, 1502 self.def_collector.db,
1500 self.def_collector.def_map.krate, 1503 self.def_collector.def_map.krate,
1501 |path| { 1504 |path| {
@@ -1524,9 +1527,14 @@ impl ModCollector<'_, '_> {
1524 } 1527 }
1525 Ok(Err(_)) => { 1528 Ok(Err(_)) => {
1526 // Built-in macro failed eager expansion. 1529 // Built-in macro failed eager expansion.
1530
1531 // FIXME: don't parse the file here
1532 let fragment = hir_expand::to_fragment_kind(
1533 &ast_id.ast_id.to_node(self.def_collector.db.upcast()),
1534 );
1527 self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error( 1535 self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
1528 self.module_id, 1536 self.module_id,
1529 MacroCallKind::FnLike { ast_id: ast_id.ast_id }, 1537 MacroCallKind::FnLike { ast_id: ast_id.ast_id, fragment },
1530 error.unwrap().to_string(), 1538 error.unwrap().to_string(),
1531 )); 1539 ));
1532 return; 1540 return;
@@ -1543,7 +1551,7 @@ impl ModCollector<'_, '_> {
1543 self.def_collector.unexpanded_macros.push(MacroDirective { 1551 self.def_collector.unexpanded_macros.push(MacroDirective {
1544 module_id: self.module_id, 1552 module_id: self.module_id,
1545 depth: self.macro_depth + 1, 1553 depth: self.macro_depth + 1,
1546 kind: MacroDirectiveKind::FnLike { ast_id }, 1554 kind: MacroDirectiveKind::FnLike { ast_id, fragment: mac.fragment },
1547 }); 1555 });
1548 } 1556 }
1549 1557
diff --git a/crates/hir_def/src/nameres/tests/incremental.rs b/crates/hir_def/src/nameres/tests/incremental.rs
index 509e1bbbc..d884a6eb4 100644
--- a/crates/hir_def/src/nameres/tests/incremental.rs
+++ b/crates/hir_def/src/nameres/tests/incremental.rs
@@ -105,3 +105,61 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() {
105 assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) 105 assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
106 } 106 }
107} 107}
108
109#[test]
110fn typing_inside_a_function_should_not_invalidate_expansions() {
111 let (mut db, pos) = TestDB::with_position(
112 r#"
113//- /lib.rs
114macro_rules! m {
115 ($ident:ident) => {
116 fn $ident() { };
117 }
118}
119mod foo;
120
121//- /foo/mod.rs
122pub mod bar;
123
124//- /foo/bar.rs
125m!(X);
126fn quux() { 1$0 }
127m!(Y);
128m!(Z);
129"#,
130 );
131 let krate = db.test_crate();
132 {
133 let events = db.log_executed(|| {
134 let crate_def_map = db.crate_def_map(krate);
135 let (_, module_data) = crate_def_map.modules.iter().last().unwrap();
136 assert_eq!(module_data.scope.resolutions().count(), 4);
137 });
138 let n_recalculated_item_trees = events.iter().filter(|it| it.contains("item_tree")).count();
139 assert_eq!(n_recalculated_item_trees, 6);
140 let n_reparsed_macros =
141 events.iter().filter(|it| it.contains("parse_macro_expansion")).count();
142 assert_eq!(n_reparsed_macros, 3);
143 }
144
145 let new_text = r#"
146m!(X);
147fn quux() { 92 }
148m!(Y);
149m!(Z);
150"#;
151 db.set_file_text(pos.file_id, Arc::new(new_text.to_string()));
152
153 {
154 let events = db.log_executed(|| {
155 let crate_def_map = db.crate_def_map(krate);
156 let (_, module_data) = crate_def_map.modules.iter().last().unwrap();
157 assert_eq!(module_data.scope.resolutions().count(), 4);
158 });
159 let n_recalculated_item_trees = events.iter().filter(|it| it.contains("item_tree")).count();
160 assert_eq!(n_recalculated_item_trees, 1);
161 let n_reparsed_macros =
162 events.iter().filter(|it| it.contains("parse_macro_expansion")).count();
163 assert_eq!(n_reparsed_macros, 0);
164 }
165}
diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs
index 509f77850..a43441b1c 100644
--- a/crates/hir_def/src/path.rs
+++ b/crates/hir_def/src/path.rs
@@ -7,7 +7,7 @@ use std::{
7 sync::Arc, 7 sync::Arc,
8}; 8};
9 9
10use crate::{body::LowerCtx, intern::Interned, type_ref::LifetimeRef}; 10use crate::{body::LowerCtx, db::DefDatabase, intern::Interned, type_ref::LifetimeRef};
11use base_db::CrateId; 11use base_db::CrateId;
12use hir_expand::{ 12use hir_expand::{
13 hygiene::Hygiene, 13 hygiene::Hygiene,
@@ -47,8 +47,8 @@ pub enum ImportAlias {
47} 47}
48 48
49impl ModPath { 49impl ModPath {
50 pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> { 50 pub fn from_src(db: &dyn DefDatabase, path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
51 let ctx = LowerCtx::with_hygiene(hygiene); 51 let ctx = LowerCtx::with_hygiene(db, hygiene);
52 lower::lower_path(path, &ctx).map(|it| (*it.mod_path).clone()) 52 lower::lower_path(path, &ctx).map(|it| (*it.mod_path).clone())
53 } 53 }
54 54
@@ -64,12 +64,13 @@ impl ModPath {
64 64
65 /// Calls `cb` with all paths, represented by this use item. 65 /// Calls `cb` with all paths, represented by this use item.
66 pub(crate) fn expand_use_item( 66 pub(crate) fn expand_use_item(
67 db: &dyn DefDatabase,
67 item_src: InFile<ast::Use>, 68 item_src: InFile<ast::Use>,
68 hygiene: &Hygiene, 69 hygiene: &Hygiene,
69 mut cb: impl FnMut(ModPath, &ast::UseTree, /* is_glob */ bool, Option<ImportAlias>), 70 mut cb: impl FnMut(ModPath, &ast::UseTree, /* is_glob */ bool, Option<ImportAlias>),
70 ) { 71 ) {
71 if let Some(tree) = item_src.value.use_tree() { 72 if let Some(tree) = item_src.value.use_tree() {
72 lower::lower_use_tree(None, tree, hygiene, &mut cb); 73 lower::lower_use_tree(db, None, tree, hygiene, &mut cb);
73 } 74 }
74 } 75 }
75 76
diff --git a/crates/hir_def/src/path/lower.rs b/crates/hir_def/src/path/lower.rs
index 1df6db525..a873325b2 100644
--- a/crates/hir_def/src/path/lower.rs
+++ b/crates/hir_def/src/path/lower.rs
@@ -36,7 +36,7 @@ pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx) -> Option<Path> {
36 match segment.kind()? { 36 match segment.kind()? {
37 ast::PathSegmentKind::Name(name_ref) => { 37 ast::PathSegmentKind::Name(name_ref) => {
38 // FIXME: this should just return name 38 // FIXME: this should just return name
39 match hygiene.name_ref_to_name(name_ref) { 39 match hygiene.name_ref_to_name(ctx.db.upcast(), name_ref) {
40 Either::Left(name) => { 40 Either::Left(name) => {
41 let args = segment 41 let args = segment
42 .generic_arg_list() 42 .generic_arg_list()
@@ -133,7 +133,7 @@ pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx) -> Option<Path> {
133 // We follow what it did anyway :) 133 // We follow what it did anyway :)
134 if segments.len() == 1 && kind == PathKind::Plain { 134 if segments.len() == 1 && kind == PathKind::Plain {
135 if let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) { 135 if let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) {
136 if let Some(crate_id) = hygiene.local_inner_macros(path) { 136 if let Some(crate_id) = hygiene.local_inner_macros(ctx.db.upcast(), path) {
137 kind = PathKind::DollarCrate(crate_id); 137 kind = PathKind::DollarCrate(crate_id);
138 } 138 }
139 } 139 }
diff --git a/crates/hir_def/src/path/lower/lower_use.rs b/crates/hir_def/src/path/lower/lower_use.rs
index e2965b033..ee80e3df3 100644
--- a/crates/hir_def/src/path/lower/lower_use.rs
+++ b/crates/hir_def/src/path/lower/lower_use.rs
@@ -7,9 +7,13 @@ use either::Either;
7use hir_expand::{hygiene::Hygiene, name::AsName}; 7use hir_expand::{hygiene::Hygiene, name::AsName};
8use syntax::ast::{self, NameOwner}; 8use syntax::ast::{self, NameOwner};
9 9
10use crate::path::{ImportAlias, ModPath, PathKind}; 10use crate::{
11 db::DefDatabase,
12 path::{ImportAlias, ModPath, PathKind},
13};
11 14
12pub(crate) fn lower_use_tree( 15pub(crate) fn lower_use_tree(
16 db: &dyn DefDatabase,
13 prefix: Option<ModPath>, 17 prefix: Option<ModPath>,
14 tree: ast::UseTree, 18 tree: ast::UseTree,
15 hygiene: &Hygiene, 19 hygiene: &Hygiene,
@@ -21,13 +25,13 @@ pub(crate) fn lower_use_tree(
21 None => prefix, 25 None => prefix,
22 // E.g. `use something::{inner}` (prefix is `None`, path is `something`) 26 // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
23 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`) 27 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
24 Some(path) => match convert_path(prefix, path, hygiene) { 28 Some(path) => match convert_path(db, prefix, path, hygiene) {
25 Some(it) => Some(it), 29 Some(it) => Some(it),
26 None => return, // FIXME: report errors somewhere 30 None => return, // FIXME: report errors somewhere
27 }, 31 },
28 }; 32 };
29 for child_tree in use_tree_list.use_trees() { 33 for child_tree in use_tree_list.use_trees() {
30 lower_use_tree(prefix.clone(), child_tree, hygiene, cb); 34 lower_use_tree(db, prefix.clone(), child_tree, hygiene, cb);
31 } 35 }
32 } else { 36 } else {
33 let alias = tree.rename().map(|a| { 37 let alias = tree.rename().map(|a| {
@@ -47,7 +51,7 @@ pub(crate) fn lower_use_tree(
47 } 51 }
48 } 52 }
49 } 53 }
50 if let Some(path) = convert_path(prefix, ast_path, hygiene) { 54 if let Some(path) = convert_path(db, prefix, ast_path, hygiene) {
51 cb(path, &tree, is_glob, alias) 55 cb(path, &tree, is_glob, alias)
52 } 56 }
53 // FIXME: report errors somewhere 57 // FIXME: report errors somewhere
@@ -61,9 +65,14 @@ pub(crate) fn lower_use_tree(
61 } 65 }
62} 66}
63 67
64fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> { 68fn convert_path(
69 db: &dyn DefDatabase,
70 prefix: Option<ModPath>,
71 path: ast::Path,
72 hygiene: &Hygiene,
73) -> Option<ModPath> {
65 let prefix = if let Some(qual) = path.qualifier() { 74 let prefix = if let Some(qual) = path.qualifier() {
66 Some(convert_path(prefix, qual, hygiene)?) 75 Some(convert_path(db, prefix, qual, hygiene)?)
67 } else { 76 } else {
68 prefix 77 prefix
69 }; 78 };
@@ -71,7 +80,7 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) ->
71 let segment = path.segment()?; 80 let segment = path.segment()?;
72 let res = match segment.kind()? { 81 let res = match segment.kind()? {
73 ast::PathSegmentKind::Name(name_ref) => { 82 ast::PathSegmentKind::Name(name_ref) => {
74 match hygiene.name_ref_to_name(name_ref) { 83 match hygiene.name_ref_to_name(db.upcast(), name_ref) {
75 Either::Left(name) => { 84 Either::Left(name) => {
76 // no type args in use 85 // no type args in use
77 let mut res = prefix.unwrap_or_else(|| { 86 let mut res = prefix.unwrap_or_else(|| {
diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs
index d4b7c9970..83500f54e 100644
--- a/crates/hir_def/src/visibility.rs
+++ b/crates/hir_def/src/visibility.rs
@@ -33,17 +33,19 @@ impl RawVisibility {
33 db: &dyn DefDatabase, 33 db: &dyn DefDatabase,
34 node: InFile<Option<ast::Visibility>>, 34 node: InFile<Option<ast::Visibility>>,
35 ) -> RawVisibility { 35 ) -> RawVisibility {
36 Self::from_ast_with_hygiene(node.value, &Hygiene::new(db.upcast(), node.file_id)) 36 Self::from_ast_with_hygiene(db, node.value, &Hygiene::new(db.upcast(), node.file_id))
37 } 37 }
38 38
39 pub(crate) fn from_ast_with_hygiene( 39 pub(crate) fn from_ast_with_hygiene(
40 db: &dyn DefDatabase,
40 node: Option<ast::Visibility>, 41 node: Option<ast::Visibility>,
41 hygiene: &Hygiene, 42 hygiene: &Hygiene,
42 ) -> RawVisibility { 43 ) -> RawVisibility {
43 Self::from_ast_with_hygiene_and_default(node, RawVisibility::private(), hygiene) 44 Self::from_ast_with_hygiene_and_default(db, node, RawVisibility::private(), hygiene)
44 } 45 }
45 46
46 pub(crate) fn from_ast_with_hygiene_and_default( 47 pub(crate) fn from_ast_with_hygiene_and_default(
48 db: &dyn DefDatabase,
47 node: Option<ast::Visibility>, 49 node: Option<ast::Visibility>,
48 default: RawVisibility, 50 default: RawVisibility,
49 hygiene: &Hygiene, 51 hygiene: &Hygiene,
@@ -54,7 +56,7 @@ impl RawVisibility {
54 }; 56 };
55 match node.kind() { 57 match node.kind() {
56 ast::VisibilityKind::In(path) => { 58 ast::VisibilityKind::In(path) => {
57 let path = ModPath::from_src(path, hygiene); 59 let path = ModPath::from_src(db, path, hygiene);
58 let path = match path { 60 let path = match path {
59 None => return RawVisibility::private(), 61 None => return RawVisibility::private(),
60 Some(path) => path, 62 Some(path) => path,
diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs
index 537c03028..b6a6d602f 100644
--- a/crates/hir_expand/src/builtin_derive.rs
+++ b/crates/hir_expand/src/builtin_derive.rs
@@ -269,7 +269,7 @@ mod tests {
269 use expect_test::{expect, Expect}; 269 use expect_test::{expect, Expect};
270 use name::AsName; 270 use name::AsName;
271 271
272 use crate::{test_db::TestDB, AstId, AttrId, MacroCallId, MacroCallKind, MacroCallLoc}; 272 use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
273 273
274 use super::*; 274 use super::*;
275 275
@@ -320,7 +320,7 @@ $0
320 kind: MacroCallKind::Derive { 320 kind: MacroCallKind::Derive {
321 ast_id, 321 ast_id,
322 derive_name: name.to_string(), 322 derive_name: name.to_string(),
323 derive_attr: AttrId(0), 323 derive_attr_index: 0,
324 }, 324 },
325 }; 325 };
326 326
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs
index 179de61f9..af9802144 100644
--- a/crates/hir_expand/src/builtin_macro.rs
+++ b/crates/hir_expand/src/builtin_macro.rs
@@ -578,6 +578,7 @@ mod tests {
578 krate, 578 krate,
579 kind: MacroCallKind::FnLike { 579 kind: MacroCallKind::FnLike {
580 ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)), 580 ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)),
581 fragment: FragmentKind::Expr,
581 }, 582 },
582 }; 583 };
583 584
@@ -788,9 +789,9 @@ mod tests {
788 r##" 789 r##"
789 #[rustc_builtin_macro] 790 #[rustc_builtin_macro]
790 macro_rules! concat {} 791 macro_rules! concat {}
791 concat!("foo", "r", 0, r#"bar"#, false); 792 concat!("foo", "r", 0, r#"bar"#, "\n", false);
792 "##, 793 "##,
793 expect![[r#""foor0barfalse""#]], 794 expect![[r#""foor0bar\nfalse""#]],
794 ); 795 );
795 } 796 }
796} 797}
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index 1e4b0cc19..9fa419fcf 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -3,20 +3,18 @@
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use base_db::{salsa, SourceDatabase}; 5use base_db::{salsa, SourceDatabase};
6use mbe::{ExpandError, ExpandResult, MacroDef, MacroRules}; 6use mbe::{ExpandError, ExpandResult};
7use parser::FragmentKind; 7use parser::FragmentKind;
8use syntax::{ 8use syntax::{
9 algo::diff, 9 algo::diff,
10 ast::{MacroStmts, NameOwner}, 10 ast::{self, NameOwner},
11 AstNode, GreenNode, Parse, 11 AstNode, GreenNode, Parse, SyntaxNode, SyntaxToken,
12 SyntaxKind::*,
13 SyntaxNode,
14}; 12};
15 13
16use crate::{ 14use crate::{
17 ast_id_map::AstIdMap, hygiene::HygieneFrame, BuiltinDeriveExpander, BuiltinFnLikeExpander, 15 ast_id_map::AstIdMap, hygiene::HygieneFrame, input::process_macro_input, BuiltinDeriveExpander,
18 EagerCallLoc, EagerMacroId, HirFileId, HirFileIdRepr, LazyMacroId, MacroCallId, MacroCallLoc, 16 BuiltinFnLikeExpander, EagerCallLoc, EagerMacroId, HirFileId, HirFileIdRepr, LazyMacroId,
19 MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander, 17 MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander,
20}; 18};
21 19
22/// Total limit on the number of tokens produced by any macro invocation. 20/// Total limit on the number of tokens produced by any macro invocation.
@@ -27,23 +25,28 @@ const TOKEN_LIMIT: usize = 524288;
27 25
28#[derive(Debug, Clone, Eq, PartialEq)] 26#[derive(Debug, Clone, Eq, PartialEq)]
29pub enum TokenExpander { 27pub enum TokenExpander {
30 MacroRules(mbe::MacroRules), 28 /// Old-style `macro_rules`.
31 MacroDef(mbe::MacroDef), 29 MacroRules { mac: mbe::MacroRules, def_site_token_map: mbe::TokenMap },
30 /// AKA macros 2.0.
31 MacroDef { mac: mbe::MacroDef, def_site_token_map: mbe::TokenMap },
32 /// Stuff like `line!` and `file!`.
32 Builtin(BuiltinFnLikeExpander), 33 Builtin(BuiltinFnLikeExpander),
34 /// `derive(Copy)` and such.
33 BuiltinDerive(BuiltinDeriveExpander), 35 BuiltinDerive(BuiltinDeriveExpander),
36 /// The thing we love the most here in rust-analyzer -- procedural macros.
34 ProcMacro(ProcMacroExpander), 37 ProcMacro(ProcMacroExpander),
35} 38}
36 39
37impl TokenExpander { 40impl TokenExpander {
38 pub fn expand( 41 fn expand(
39 &self, 42 &self,
40 db: &dyn AstDatabase, 43 db: &dyn AstDatabase,
41 id: LazyMacroId, 44 id: LazyMacroId,
42 tt: &tt::Subtree, 45 tt: &tt::Subtree,
43 ) -> mbe::ExpandResult<tt::Subtree> { 46 ) -> mbe::ExpandResult<tt::Subtree> {
44 match self { 47 match self {
45 TokenExpander::MacroRules(it) => it.expand(tt), 48 TokenExpander::MacroRules { mac, .. } => mac.expand(tt),
46 TokenExpander::MacroDef(it) => it.expand(tt), 49 TokenExpander::MacroDef { mac, .. } => mac.expand(tt),
47 TokenExpander::Builtin(it) => it.expand(db, id, tt), 50 TokenExpander::Builtin(it) => it.expand(db, id, tt),
48 // FIXME switch these to ExpandResult as well 51 // FIXME switch these to ExpandResult as well
49 TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(), 52 TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(),
@@ -56,23 +59,23 @@ impl TokenExpander {
56 } 59 }
57 } 60 }
58 61
59 pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId { 62 pub(crate) fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
60 match self { 63 match self {
61 TokenExpander::MacroRules(it) => it.map_id_down(id), 64 TokenExpander::MacroRules { mac, .. } => mac.map_id_down(id),
62 TokenExpander::MacroDef(it) => it.map_id_down(id), 65 TokenExpander::MacroDef { mac, .. } => mac.map_id_down(id),
63 TokenExpander::Builtin(..) => id, 66 TokenExpander::Builtin(..)
64 TokenExpander::BuiltinDerive(..) => id, 67 | TokenExpander::BuiltinDerive(..)
65 TokenExpander::ProcMacro(..) => id, 68 | TokenExpander::ProcMacro(..) => id,
66 } 69 }
67 } 70 }
68 71
69 pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) { 72 pub(crate) fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
70 match self { 73 match self {
71 TokenExpander::MacroRules(it) => it.map_id_up(id), 74 TokenExpander::MacroRules { mac, .. } => mac.map_id_up(id),
72 TokenExpander::MacroDef(it) => it.map_id_up(id), 75 TokenExpander::MacroDef { mac, .. } => mac.map_id_up(id),
73 TokenExpander::Builtin(..) => (id, mbe::Origin::Call), 76 TokenExpander::Builtin(..)
74 TokenExpander::BuiltinDerive(..) => (id, mbe::Origin::Call), 77 | TokenExpander::BuiltinDerive(..)
75 TokenExpander::ProcMacro(..) => (id, mbe::Origin::Call), 78 | TokenExpander::ProcMacro(..) => (id, mbe::Origin::Call),
76 } 79 }
77 } 80 }
78} 81}
@@ -82,28 +85,48 @@ impl TokenExpander {
82pub trait AstDatabase: SourceDatabase { 85pub trait AstDatabase: SourceDatabase {
83 fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>; 86 fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
84 87
88 /// Main public API -- parsis a hir file, not caring whether it's a real
89 /// file or a macro expansion.
85 #[salsa::transparent] 90 #[salsa::transparent]
86 fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>; 91 fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
87 92 /// Implementation for the macro case.
88 #[salsa::interned]
89 fn intern_macro(&self, macro_call: MacroCallLoc) -> LazyMacroId;
90 fn macro_arg_text(&self, id: MacroCallId) -> Option<GreenNode>;
91 #[salsa::transparent]
92 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
93 fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
94 fn parse_macro_expansion( 93 fn parse_macro_expansion(
95 &self, 94 &self,
96 macro_file: MacroFile, 95 macro_file: MacroFile,
97 ) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>>; 96 ) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>>;
98 fn macro_expand(&self, macro_call: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>>;
99
100 /// Firewall query that returns the error from the `macro_expand` query.
101 fn macro_expand_error(&self, macro_call: MacroCallId) -> Option<ExpandError>;
102 97
98 /// Macro ids. That's probably the tricksiest bit in rust-analyzer, and the
99 /// reason why we use salsa at all.
100 ///
101 /// We encode macro definitions into ids of macro calls, this what allows us
102 /// to be incremental.
103 #[salsa::interned]
104 fn intern_macro(&self, macro_call: MacroCallLoc) -> LazyMacroId;
105 /// Certain built-in macros are eager (`format!(concat!("file: ", file!(), "{}"")), 92`).
106 /// For them, we actually want to encode the whole token tree as an argument.
103 #[salsa::interned] 107 #[salsa::interned]
104 fn intern_eager_expansion(&self, eager: EagerCallLoc) -> EagerMacroId; 108 fn intern_eager_expansion(&self, eager: EagerCallLoc) -> EagerMacroId;
105 109
110 /// Lowers syntactic macro call to a token tree representation.
111 #[salsa::transparent]
112 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
113 /// Extracts syntax node, corresponding to a macro call. That's a firewall
114 /// query, only typing in the macro call itself changes the returned
115 /// subtree.
116 fn macro_arg_text(&self, id: MacroCallId) -> Option<GreenNode>;
117 /// Gets the expander for this macro. This compiles declarative macros, and
118 /// just fetches procedural ones.
119 fn macro_def(&self, id: MacroDefId) -> Option<Arc<TokenExpander>>;
120
121 /// Expand macro call to a token tree. This query is LRUed (we keep 128 or so results in memory)
122 fn macro_expand(&self, macro_call: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>>;
123 /// Special case of the previous query for procedural macros. We can't LRU
124 /// proc macros, since they are not deterministic in general, and
125 /// non-determinism breaks salsa in a very, very, very bad way. @edwin0cheng
126 /// heroically debugged this once!
106 fn expand_proc_macro(&self, call: MacroCallId) -> Result<tt::Subtree, mbe::ExpandError>; 127 fn expand_proc_macro(&self, call: MacroCallId) -> Result<tt::Subtree, mbe::ExpandError>;
128 /// Firewall query that returns the error from the `macro_expand` query.
129 fn macro_expand_error(&self, macro_call: MacroCallId) -> Option<ExpandError>;
107 130
108 fn hygiene_frame(&self, file_id: HirFileId) -> Arc<HygieneFrame>; 131 fn hygiene_frame(&self, file_id: HirFileId) -> Arc<HygieneFrame>;
109} 132}
@@ -115,36 +138,160 @@ pub trait AstDatabase: SourceDatabase {
115pub fn expand_hypothetical( 138pub fn expand_hypothetical(
116 db: &dyn AstDatabase, 139 db: &dyn AstDatabase,
117 actual_macro_call: MacroCallId, 140 actual_macro_call: MacroCallId,
118 hypothetical_args: &syntax::ast::TokenTree, 141 hypothetical_args: &ast::TokenTree,
119 token_to_map: syntax::SyntaxToken, 142 token_to_map: SyntaxToken,
120) -> Option<(SyntaxNode, syntax::SyntaxToken)> { 143) -> Option<(SyntaxNode, SyntaxToken)> {
121 let macro_file = MacroFile { macro_call_id: actual_macro_call };
122 let (tt, tmap_1) = mbe::syntax_node_to_token_tree(hypothetical_args.syntax()); 144 let (tt, tmap_1) = mbe::syntax_node_to_token_tree(hypothetical_args.syntax());
123 let range = 145 let range =
124 token_to_map.text_range().checked_sub(hypothetical_args.syntax().text_range().start())?; 146 token_to_map.text_range().checked_sub(hypothetical_args.syntax().text_range().start())?;
125 let token_id = tmap_1.token_by_range(range)?; 147 let token_id = tmap_1.token_by_range(range)?;
126 let macro_def = expander(db, actual_macro_call)?; 148
149 let lazy_id = match actual_macro_call {
150 MacroCallId::LazyMacro(id) => id,
151 MacroCallId::EagerMacro(_) => return None,
152 };
153
154 let macro_def = {
155 let loc = db.lookup_intern_macro(lazy_id);
156 db.macro_def(loc.def)?
157 };
158
159 let hypothetical_expansion = macro_def.expand(db, lazy_id, &tt);
160
161 let fragment_kind = macro_fragment_kind(db, actual_macro_call);
162
127 let (node, tmap_2) = 163 let (node, tmap_2) =
128 parse_macro_with_arg(db, macro_file, Some(std::sync::Arc::new((tt, tmap_1)))).value?; 164 mbe::token_tree_to_syntax_node(&hypothetical_expansion.value, fragment_kind).ok()?;
129 let token_id = macro_def.0.map_id_down(token_id); 165
166 let token_id = macro_def.map_id_down(token_id);
130 let range = tmap_2.range_by_token(token_id)?.by_kind(token_to_map.kind())?; 167 let range = tmap_2.range_by_token(token_id)?.by_kind(token_to_map.kind())?;
131 let token = node.syntax_node().covering_element(range).into_token()?; 168 let token = node.syntax_node().covering_element(range).into_token()?;
132 Some((node.syntax_node(), token)) 169 Some((node.syntax_node(), token))
133} 170}
134 171
135fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> { 172fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
136 let map = 173 let map = db.parse_or_expand(file_id).map(|it| AstIdMap::from_source(&it)).unwrap_or_default();
137 db.parse_or_expand(file_id).map_or_else(AstIdMap::default, |it| AstIdMap::from_source(&it));
138 Arc::new(map) 174 Arc::new(map)
139} 175}
140 176
141fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> { 177fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
178 match file_id.0 {
179 HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
180 HirFileIdRepr::MacroFile(macro_file) => {
181 db.parse_macro_expansion(macro_file).value.map(|(it, _)| it.syntax_node())
182 }
183 }
184}
185
186fn parse_macro_expansion(
187 db: &dyn AstDatabase,
188 macro_file: MacroFile,
189) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
190 let _p = profile::span("parse_macro_expansion");
191 let result = db.macro_expand(macro_file.macro_call_id);
192
193 if let Some(err) = &result.err {
194 // Note:
195 // The final goal we would like to make all parse_macro success,
196 // such that the following log will not call anyway.
197 match macro_file.macro_call_id {
198 MacroCallId::LazyMacro(id) => {
199 let loc: MacroCallLoc = db.lookup_intern_macro(id);
200 let node = loc.kind.node(db);
201
202 // collect parent information for warning log
203 let parents = std::iter::successors(loc.kind.file_id().call_node(db), |it| {
204 it.file_id.call_node(db)
205 })
206 .map(|n| format!("{:#}", n.value))
207 .collect::<Vec<_>>()
208 .join("\n");
209
210 log::warn!(
211 "fail on macro_parse: (reason: {:?} macro_call: {:#}) parents: {}",
212 err,
213 node.value,
214 parents
215 );
216 }
217 _ => {
218 log::warn!("fail on macro_parse: (reason: {:?})", err);
219 }
220 }
221 }
222 let tt = match result.value {
223 Some(tt) => tt,
224 None => return ExpandResult { value: None, err: result.err },
225 };
226
227 let fragment_kind = macro_fragment_kind(db, macro_file.macro_call_id);
228
229 log::debug!("expanded = {}", tt.as_debug_string());
230 log::debug!("kind = {:?}", fragment_kind);
231
232 let (parse, rev_token_map) = match mbe::token_tree_to_syntax_node(&tt, fragment_kind) {
233 Ok(it) => it,
234 Err(err) => {
235 log::debug!(
236 "failed to parse expanstion to {:?} = {}",
237 fragment_kind,
238 tt.as_debug_string()
239 );
240 return ExpandResult::only_err(err);
241 }
242 };
243
244 match result.err {
245 Some(err) => {
246 // Safety check for recursive identity macro.
247 let node = parse.syntax_node();
248 let file: HirFileId = macro_file.into();
249 let call_node = match file.call_node(db) {
250 Some(it) => it,
251 None => {
252 return ExpandResult::only_err(err);
253 }
254 };
255 if is_self_replicating(&node, &call_node.value) {
256 return ExpandResult::only_err(err);
257 } else {
258 ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: Some(err) }
259 }
260 }
261 None => {
262 log::debug!("parse = {:?}", parse.syntax_node().kind());
263 ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: None }
264 }
265 }
266}
267
268fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
269 let arg = db.macro_arg_text(id)?;
270 let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg));
271 Some(Arc::new((tt, tmap)))
272}
273
274fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
275 let id = match id {
276 MacroCallId::LazyMacro(id) => id,
277 MacroCallId::EagerMacro(_id) => {
278 // FIXME: support macro_arg for eager macro
279 return None;
280 }
281 };
282 let loc = db.lookup_intern_macro(id);
283 let arg = loc.kind.arg(db)?;
284 let arg = process_macro_input(db, arg, id);
285 Some(arg.green().into())
286}
287
288fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<TokenExpander>> {
142 match id.kind { 289 match id.kind {
143 MacroDefKind::Declarative(ast_id) => match ast_id.to_node(db) { 290 MacroDefKind::Declarative(ast_id) => match ast_id.to_node(db) {
144 syntax::ast::Macro::MacroRules(macro_rules) => { 291 ast::Macro::MacroRules(macro_rules) => {
145 let arg = macro_rules.token_tree()?; 292 let arg = macro_rules.token_tree()?;
146 let (tt, tmap) = mbe::ast_to_token_tree(&arg); 293 let (tt, def_site_token_map) = mbe::ast_to_token_tree(&arg);
147 let rules = match MacroRules::parse(&tt) { 294 let mac = match mbe::MacroRules::parse(&tt) {
148 Ok(it) => it, 295 Ok(it) => it,
149 Err(err) => { 296 Err(err) => {
150 let name = macro_rules.name().map(|n| n.to_string()).unwrap_or_default(); 297 let name = macro_rules.name().map(|n| n.to_string()).unwrap_or_default();
@@ -152,12 +299,12 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander,
152 return None; 299 return None;
153 } 300 }
154 }; 301 };
155 Some(Arc::new((TokenExpander::MacroRules(rules), tmap))) 302 Some(Arc::new(TokenExpander::MacroRules { mac, def_site_token_map }))
156 } 303 }
157 syntax::ast::Macro::MacroDef(macro_def) => { 304 ast::Macro::MacroDef(macro_def) => {
158 let arg = macro_def.body()?; 305 let arg = macro_def.body()?;
159 let (tt, tmap) = mbe::ast_to_token_tree(&arg); 306 let (tt, def_site_token_map) = mbe::ast_to_token_tree(&arg);
160 let rules = match MacroDef::parse(&tt) { 307 let mac = match mbe::MacroDef::parse(&tt) {
161 Ok(it) => it, 308 Ok(it) => it,
162 Err(err) => { 309 Err(err) => {
163 let name = macro_def.name().map(|n| n.to_string()).unwrap_or_default(); 310 let name = macro_def.name().map(|n| n.to_string()).unwrap_or_default();
@@ -165,41 +312,18 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander,
165 return None; 312 return None;
166 } 313 }
167 }; 314 };
168 Some(Arc::new((TokenExpander::MacroDef(rules), tmap))) 315 Some(Arc::new(TokenExpander::MacroDef { mac, def_site_token_map }))
169 } 316 }
170 }, 317 },
171 MacroDefKind::BuiltIn(expander, _) => { 318 MacroDefKind::BuiltIn(expander, _) => Some(Arc::new(TokenExpander::Builtin(expander))),
172 Some(Arc::new((TokenExpander::Builtin(expander), mbe::TokenMap::default())))
173 }
174 MacroDefKind::BuiltInDerive(expander, _) => { 319 MacroDefKind::BuiltInDerive(expander, _) => {
175 Some(Arc::new((TokenExpander::BuiltinDerive(expander), mbe::TokenMap::default()))) 320 Some(Arc::new(TokenExpander::BuiltinDerive(expander)))
176 } 321 }
177 MacroDefKind::BuiltInEager(..) => None, 322 MacroDefKind::BuiltInEager(..) => None,
178 MacroDefKind::ProcMacro(expander, ..) => { 323 MacroDefKind::ProcMacro(expander, ..) => Some(Arc::new(TokenExpander::ProcMacro(expander))),
179 Some(Arc::new((TokenExpander::ProcMacro(expander), mbe::TokenMap::default())))
180 }
181 } 324 }
182} 325}
183 326
184fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
185 let id = match id {
186 MacroCallId::LazyMacro(id) => id,
187 MacroCallId::EagerMacro(_id) => {
188 // FIXME: support macro_arg for eager macro
189 return None;
190 }
191 };
192 let loc = db.lookup_intern_macro(id);
193 let arg = loc.kind.arg(db)?;
194 Some(arg.green())
195}
196
197fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
198 let arg = db.macro_arg_text(id)?;
199 let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg));
200 Some(Arc::new((tt, tmap)))
201}
202
203fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>> { 327fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>> {
204 macro_expand_with_arg(db, id, None) 328 macro_expand_with_arg(db, id, None)
205} 329}
@@ -208,19 +332,6 @@ fn macro_expand_error(db: &dyn AstDatabase, macro_call: MacroCallId) -> Option<E
208 db.macro_expand(macro_call).err 332 db.macro_expand(macro_call).err
209} 333}
210 334
211fn expander(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
212 let lazy_id = match id {
213 MacroCallId::LazyMacro(id) => id,
214 MacroCallId::EagerMacro(_id) => {
215 return None;
216 }
217 };
218
219 let loc = db.lookup_intern_macro(lazy_id);
220 let macro_rules = db.macro_def(loc.def)?;
221 Some(macro_rules)
222}
223
224fn macro_expand_with_arg( 335fn macro_expand_with_arg(
225 db: &dyn AstDatabase, 336 db: &dyn AstDatabase,
226 id: MacroCallId, 337 id: MacroCallId,
@@ -254,7 +365,7 @@ fn macro_expand_with_arg(
254 Some(it) => it, 365 Some(it) => it,
255 None => return ExpandResult::str_err("Fail to find macro definition".into()), 366 None => return ExpandResult::str_err("Fail to find macro definition".into()),
256 }; 367 };
257 let ExpandResult { value: tt, err } = macro_rules.0.expand(db, lazy_id, &macro_arg.0); 368 let ExpandResult { value: tt, err } = macro_rules.expand(db, lazy_id, &macro_arg.0);
258 // Set a hard limit for the expanded tt 369 // Set a hard limit for the expanded tt
259 let count = tt.count(); 370 let count = tt.count();
260 if count > TOKEN_LIMIT { 371 if count > TOKEN_LIMIT {
@@ -294,116 +405,11 @@ fn expand_proc_macro(
294 expander.expand(db, loc.krate, &macro_arg.0) 405 expander.expand(db, loc.krate, &macro_arg.0)
295} 406}
296 407
297fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
298 match file_id.0 {
299 HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
300 HirFileIdRepr::MacroFile(macro_file) => {
301 db.parse_macro_expansion(macro_file).value.map(|(it, _)| it.syntax_node())
302 }
303 }
304}
305
306fn parse_macro_expansion(
307 db: &dyn AstDatabase,
308 macro_file: MacroFile,
309) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
310 parse_macro_with_arg(db, macro_file, None)
311}
312
313fn parse_macro_with_arg(
314 db: &dyn AstDatabase,
315 macro_file: MacroFile,
316 arg: Option<Arc<(tt::Subtree, mbe::TokenMap)>>,
317) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
318 let macro_call_id = macro_file.macro_call_id;
319 let result = if let Some(arg) = arg {
320 macro_expand_with_arg(db, macro_call_id, Some(arg))
321 } else {
322 db.macro_expand(macro_call_id)
323 };
324
325 let _p = profile::span("parse_macro_expansion");
326
327 if let Some(err) = &result.err {
328 // Note:
329 // The final goal we would like to make all parse_macro success,
330 // such that the following log will not call anyway.
331 match macro_call_id {
332 MacroCallId::LazyMacro(id) => {
333 let loc: MacroCallLoc = db.lookup_intern_macro(id);
334 let node = loc.kind.node(db);
335
336 // collect parent information for warning log
337 let parents = std::iter::successors(loc.kind.file_id().call_node(db), |it| {
338 it.file_id.call_node(db)
339 })
340 .map(|n| format!("{:#}", n.value))
341 .collect::<Vec<_>>()
342 .join("\n");
343
344 log::warn!(
345 "fail on macro_parse: (reason: {:?} macro_call: {:#}) parents: {}",
346 err,
347 node.value,
348 parents
349 );
350 }
351 _ => {
352 log::warn!("fail on macro_parse: (reason: {:?})", err);
353 }
354 }
355 }
356 let tt = match result.value {
357 Some(tt) => tt,
358 None => return ExpandResult { value: None, err: result.err },
359 };
360
361 let fragment_kind = to_fragment_kind(db, macro_call_id);
362
363 log::debug!("expanded = {}", tt.as_debug_string());
364 log::debug!("kind = {:?}", fragment_kind);
365
366 let (parse, rev_token_map) = match mbe::token_tree_to_syntax_node(&tt, fragment_kind) {
367 Ok(it) => it,
368 Err(err) => {
369 log::debug!(
370 "failed to parse expanstion to {:?} = {}",
371 fragment_kind,
372 tt.as_debug_string()
373 );
374 return ExpandResult::only_err(err);
375 }
376 };
377
378 match result.err {
379 Some(err) => {
380 // Safety check for recursive identity macro.
381 let node = parse.syntax_node();
382 let file: HirFileId = macro_file.into();
383 let call_node = match file.call_node(db) {
384 Some(it) => it,
385 None => {
386 return ExpandResult::only_err(err);
387 }
388 };
389 if is_self_replicating(&node, &call_node.value) {
390 return ExpandResult::only_err(err);
391 } else {
392 ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: Some(err) }
393 }
394 }
395 None => {
396 log::debug!("parse = {:?}", parse.syntax_node().kind());
397 ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: None }
398 }
399 }
400}
401
402fn is_self_replicating(from: &SyntaxNode, to: &SyntaxNode) -> bool { 408fn is_self_replicating(from: &SyntaxNode, to: &SyntaxNode) -> bool {
403 if diff(from, to).is_empty() { 409 if diff(from, to).is_empty() {
404 return true; 410 return true;
405 } 411 }
406 if let Some(stmts) = MacroStmts::cast(from.clone()) { 412 if let Some(stmts) = ast::MacroStmts::cast(from.clone()) {
407 if stmts.statements().any(|stmt| diff(stmt.syntax(), to).is_empty()) { 413 if stmts.statements().any(|stmt| diff(stmt.syntax(), to).is_empty()) {
408 return true; 414 return true;
409 } 415 }
@@ -420,62 +426,15 @@ fn hygiene_frame(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<HygieneFrame>
420 Arc::new(HygieneFrame::new(db, file_id)) 426 Arc::new(HygieneFrame::new(db, file_id))
421} 427}
422 428
423/// Given a `MacroCallId`, return what `FragmentKind` it belongs to. 429fn macro_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind {
424/// FIXME: Not completed 430 match id {
425fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind { 431 MacroCallId::LazyMacro(id) => {
426 let lazy_id = match id { 432 let loc: MacroCallLoc = db.lookup_intern_macro(id);
427 MacroCallId::LazyMacro(id) => id, 433 loc.kind.fragment_kind()
428 MacroCallId::EagerMacro(id) => {
429 return db.lookup_intern_eager_expansion(id).fragment;
430 }
431 };
432 let syn = db.lookup_intern_macro(lazy_id).kind.node(db).value;
433
434 let parent = match syn.parent() {
435 Some(it) => it,
436 None => return FragmentKind::Statements,
437 };
438
439 match parent.kind() {
440 MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items,
441 MACRO_STMTS => FragmentKind::Statements,
442 MACRO_PAT => FragmentKind::Pattern,
443 MACRO_TYPE => FragmentKind::Type,
444 ITEM_LIST => FragmentKind::Items,
445 LET_STMT => {
446 // FIXME: Handle LHS Pattern
447 FragmentKind::Expr
448 } 434 }
449 EXPR_STMT => FragmentKind::Statements, 435 MacroCallId::EagerMacro(id) => {
450 BLOCK_EXPR => FragmentKind::Statements, 436 let loc: EagerCallLoc = db.lookup_intern_eager_expansion(id);
451 ARG_LIST => FragmentKind::Expr, 437 loc.fragment
452 TRY_EXPR => FragmentKind::Expr,
453 TUPLE_EXPR => FragmentKind::Expr,
454 PAREN_EXPR => FragmentKind::Expr,
455 ARRAY_EXPR => FragmentKind::Expr,
456 FOR_EXPR => FragmentKind::Expr,
457 PATH_EXPR => FragmentKind::Expr,
458 CLOSURE_EXPR => FragmentKind::Expr,
459 CONDITION => FragmentKind::Expr,
460 BREAK_EXPR => FragmentKind::Expr,
461 RETURN_EXPR => FragmentKind::Expr,
462 MATCH_EXPR => FragmentKind::Expr,
463 MATCH_ARM => FragmentKind::Expr,
464 MATCH_GUARD => FragmentKind::Expr,
465 RECORD_EXPR_FIELD => FragmentKind::Expr,
466 CALL_EXPR => FragmentKind::Expr,
467 INDEX_EXPR => FragmentKind::Expr,
468 METHOD_CALL_EXPR => FragmentKind::Expr,
469 FIELD_EXPR => FragmentKind::Expr,
470 AWAIT_EXPR => FragmentKind::Expr,
471 CAST_EXPR => FragmentKind::Expr,
472 REF_EXPR => FragmentKind::Expr,
473 PREFIX_EXPR => FragmentKind::Expr,
474 RANGE_EXPR => FragmentKind::Expr,
475 BIN_EXPR => FragmentKind::Expr,
476 _ => {
477 // Unknown , Just guess it is `Items`
478 FragmentKind::Items
479 } 438 }
480 } 439 }
481} 440}
diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs
index f12132f84..85491fe8b 100644
--- a/crates/hir_expand/src/eager.rs
+++ b/crates/hir_expand/src/eager.rs
@@ -175,8 +175,13 @@ fn lazy_expand(
175) -> ExpandResult<Option<InFile<SyntaxNode>>> { 175) -> ExpandResult<Option<InFile<SyntaxNode>>> {
176 let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value); 176 let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);
177 177
178 let fragment = crate::to_fragment_kind(&macro_call.value);
178 let id: MacroCallId = def 179 let id: MacroCallId = def
179 .as_lazy_macro(db, krate, MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id) }) 180 .as_lazy_macro(
181 db,
182 krate,
183 MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id), fragment },
184 )
180 .into(); 185 .into();
181 186
182 let err = db.macro_expand_error(id); 187 let err = db.macro_expand_error(id);
diff --git a/crates/hir_expand/src/hygiene.rs b/crates/hir_expand/src/hygiene.rs
index 779725629..aca69e35a 100644
--- a/crates/hir_expand/src/hygiene.rs
+++ b/crates/hir_expand/src/hygiene.rs
@@ -5,6 +5,7 @@
5use std::sync::Arc; 5use std::sync::Arc;
6 6
7use base_db::CrateId; 7use base_db::CrateId;
8use db::TokenExpander;
8use either::Either; 9use either::Either;
9use mbe::Origin; 10use mbe::Origin;
10use parser::SyntaxKind; 11use parser::SyntaxKind;
@@ -31,10 +32,14 @@ impl Hygiene {
31 } 32 }
32 33
33 // FIXME: this should just return name 34 // FIXME: this should just return name
34 pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either<Name, CrateId> { 35 pub fn name_ref_to_name(
36 &self,
37 db: &dyn AstDatabase,
38 name_ref: ast::NameRef,
39 ) -> Either<Name, CrateId> {
35 if let Some(frames) = &self.frames { 40 if let Some(frames) = &self.frames {
36 if name_ref.text() == "$crate" { 41 if name_ref.text() == "$crate" {
37 if let Some(krate) = frames.root_crate(name_ref.syntax()) { 42 if let Some(krate) = frames.root_crate(db, name_ref.syntax()) {
38 return Either::Right(krate); 43 return Either::Right(krate);
39 } 44 }
40 } 45 }
@@ -43,15 +48,19 @@ impl Hygiene {
43 Either::Left(name_ref.as_name()) 48 Either::Left(name_ref.as_name())
44 } 49 }
45 50
46 pub fn local_inner_macros(&self, path: ast::Path) -> Option<CrateId> { 51 pub fn local_inner_macros(&self, db: &dyn AstDatabase, path: ast::Path) -> Option<CrateId> {
47 let mut token = path.syntax().first_token()?.text_range(); 52 let mut token = path.syntax().first_token()?.text_range();
48 let frames = self.frames.as_ref()?; 53 let frames = self.frames.as_ref()?;
49 let mut current = frames.0.clone(); 54 let mut current = frames.0.clone();
50 55
51 loop { 56 loop {
52 let (mapped, origin) = current.expansion.as_ref()?.map_ident_up(token)?; 57 let (mapped, origin) = current.expansion.as_ref()?.map_ident_up(db, token)?;
53 if origin == Origin::Def { 58 if origin == Origin::Def {
54 return if current.local_inner { frames.root_crate(path.syntax()) } else { None }; 59 return if current.local_inner {
60 frames.root_crate(db, path.syntax())
61 } else {
62 None
63 };
55 } 64 }
56 current = current.call_site.as_ref()?.clone(); 65 current = current.call_site.as_ref()?.clone();
57 token = mapped.value; 66 token = mapped.value;
@@ -81,13 +90,13 @@ impl HygieneFrames {
81 HygieneFrames(Arc::new(HygieneFrame::new(db, file_id))) 90 HygieneFrames(Arc::new(HygieneFrame::new(db, file_id)))
82 } 91 }
83 92
84 fn root_crate(&self, node: &SyntaxNode) -> Option<CrateId> { 93 fn root_crate(&self, db: &dyn AstDatabase, node: &SyntaxNode) -> Option<CrateId> {
85 let mut token = node.first_token()?.text_range(); 94 let mut token = node.first_token()?.text_range();
86 let mut result = self.0.krate; 95 let mut result = self.0.krate;
87 let mut current = self.0.clone(); 96 let mut current = self.0.clone();
88 97
89 while let Some((mapped, origin)) = 98 while let Some((mapped, origin)) =
90 current.expansion.as_ref().and_then(|it| it.map_ident_up(token)) 99 current.expansion.as_ref().and_then(|it| it.map_ident_up(db, token))
91 { 100 {
92 result = current.krate; 101 result = current.krate;
93 102
@@ -111,26 +120,41 @@ impl HygieneFrames {
111 120
112#[derive(Debug, Clone, PartialEq, Eq)] 121#[derive(Debug, Clone, PartialEq, Eq)]
113struct HygieneInfo { 122struct HygieneInfo {
114 arg_start: InFile<TextSize>, 123 file: MacroFile,
115 /// The `macro_rules!` arguments. 124 /// The `macro_rules!` arguments.
116 def_start: Option<InFile<TextSize>>, 125 def_start: Option<InFile<TextSize>>,
117 126
118 macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>, 127 macro_def: Arc<TokenExpander>,
119 macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, 128 macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>,
120 exp_map: Arc<mbe::TokenMap>, 129 exp_map: Arc<mbe::TokenMap>,
121} 130}
122 131
123impl HygieneInfo { 132impl HygieneInfo {
124 fn map_ident_up(&self, token: TextRange) -> Option<(InFile<TextRange>, Origin)> { 133 fn map_ident_up(
134 &self,
135 db: &dyn AstDatabase,
136 token: TextRange,
137 ) -> Option<(InFile<TextRange>, Origin)> {
125 let token_id = self.exp_map.token_by_range(token)?; 138 let token_id = self.exp_map.token_by_range(token)?;
126 139
127 let (token_id, origin) = self.macro_def.0.map_id_up(token_id); 140 let (token_id, origin) = self.macro_def.map_id_up(token_id);
128 let (token_map, tt) = match origin { 141 let (token_map, tt) = match origin {
129 mbe::Origin::Call => (&self.macro_arg.1, self.arg_start), 142 mbe::Origin::Call => {
130 mbe::Origin::Def => ( 143 let call_id = match self.file.macro_call_id {
131 &self.macro_def.1, 144 MacroCallId::LazyMacro(lazy) => lazy,
132 *self.def_start.as_ref().expect("`Origin::Def` used with non-`macro_rules!` macro"), 145 MacroCallId::EagerMacro(_) => unreachable!(),
133 ), 146 };
147 let loc: MacroCallLoc = db.lookup_intern_macro(call_id);
148 let arg_start = loc.kind.arg(db)?.text_range().start();
149 (&self.macro_arg.1, InFile::new(loc.kind.file_id(), arg_start))
150 }
151 mbe::Origin::Def => match (&*self.macro_def, self.def_start) {
152 (TokenExpander::MacroDef { def_site_token_map, .. }, Some(tt))
153 | (TokenExpander::MacroRules { def_site_token_map, .. }, Some(tt)) => {
154 (def_site_token_map, tt)
155 }
156 _ => panic!("`Origin::Def` used with non-`macro_rules!` macro"),
157 },
134 }; 158 };
135 159
136 let range = token_map.range_by_token(token_id)?.by_kind(SyntaxKind::IDENT)?; 160 let range = token_map.range_by_token(token_id)?.by_kind(SyntaxKind::IDENT)?;
@@ -143,8 +167,6 @@ fn make_hygiene_info(
143 macro_file: MacroFile, 167 macro_file: MacroFile,
144 loc: &MacroCallLoc, 168 loc: &MacroCallLoc,
145) -> Option<HygieneInfo> { 169) -> Option<HygieneInfo> {
146 let arg_tt = loc.kind.arg(db)?;
147
148 let def_offset = loc.def.ast_id().left().and_then(|id| { 170 let def_offset = loc.def.ast_id().left().and_then(|id| {
149 let def_tt = match id.to_node(db) { 171 let def_tt = match id.to_node(db) {
150 ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(), 172 ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(),
@@ -157,13 +179,7 @@ fn make_hygiene_info(
157 let (_, exp_map) = db.parse_macro_expansion(macro_file).value?; 179 let (_, exp_map) = db.parse_macro_expansion(macro_file).value?;
158 let macro_arg = db.macro_arg(macro_file.macro_call_id)?; 180 let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
159 181
160 Some(HygieneInfo { 182 Some(HygieneInfo { file: macro_file, def_start: def_offset, macro_arg, macro_def, exp_map })
161 arg_start: InFile::new(loc.kind.file_id(), arg_tt.text_range().start()),
162 def_start: def_offset,
163 macro_arg,
164 macro_def,
165 exp_map,
166 })
167} 183}
168 184
169impl HygieneFrame { 185impl HygieneFrame {
@@ -174,7 +190,8 @@ impl HygieneFrame {
174 MacroCallId::EagerMacro(_id) => (None, None, false), 190 MacroCallId::EagerMacro(_id) => (None, None, false),
175 MacroCallId::LazyMacro(id) => { 191 MacroCallId::LazyMacro(id) => {
176 let loc = db.lookup_intern_macro(id); 192 let loc = db.lookup_intern_macro(id);
177 let info = make_hygiene_info(db, macro_file, &loc); 193 let info = make_hygiene_info(db, macro_file, &loc)
194 .map(|info| (loc.kind.file_id(), info));
178 match loc.def.kind { 195 match loc.def.kind {
179 MacroDefKind::Declarative(_) => { 196 MacroDefKind::Declarative(_) => {
180 (info, Some(loc.def.krate), loc.def.local_inner) 197 (info, Some(loc.def.krate), loc.def.local_inner)
@@ -188,7 +205,7 @@ impl HygieneFrame {
188 }, 205 },
189 }; 206 };
190 207
191 let info = match info { 208 let (calling_file, info) = match info {
192 None => { 209 None => {
193 return HygieneFrame { 210 return HygieneFrame {
194 expansion: None, 211 expansion: None,
@@ -202,7 +219,7 @@ impl HygieneFrame {
202 }; 219 };
203 220
204 let def_site = info.def_start.map(|it| db.hygiene_frame(it.file_id)); 221 let def_site = info.def_start.map(|it| db.hygiene_frame(it.file_id));
205 let call_site = Some(db.hygiene_frame(info.arg_start.file_id)); 222 let call_site = Some(db.hygiene_frame(calling_file));
206 223
207 HygieneFrame { expansion: Some(info), local_inner, krate, call_site, def_site } 224 HygieneFrame { expansion: Some(info), local_inner, krate, call_site, def_site }
208 } 225 }
diff --git a/crates/hir_expand/src/input.rs b/crates/hir_expand/src/input.rs
new file mode 100644
index 000000000..112216859
--- /dev/null
+++ b/crates/hir_expand/src/input.rs
@@ -0,0 +1,94 @@
1//! Macro input conditioning.
2
3use syntax::{
4 ast::{self, AttrsOwner},
5 AstNode, SyntaxNode,
6};
7
8use crate::{
9 db::AstDatabase,
10 name::{name, AsName},
11 LazyMacroId, MacroCallKind, MacroCallLoc,
12};
13
14pub(crate) fn process_macro_input(
15 db: &dyn AstDatabase,
16 node: SyntaxNode,
17 id: LazyMacroId,
18) -> SyntaxNode {
19 let loc: MacroCallLoc = db.lookup_intern_macro(id);
20
21 match loc.kind {
22 MacroCallKind::FnLike { .. } => node,
23 MacroCallKind::Derive { derive_attr_index, .. } => {
24 let item = match ast::Item::cast(node.clone()) {
25 Some(item) => item,
26 None => return node,
27 };
28
29 remove_derives_up_to(item, derive_attr_index as usize).syntax().clone()
30 }
31 }
32}
33
34/// Removes `#[derive]` attributes from `item`, up to `attr_index`.
35fn remove_derives_up_to(item: ast::Item, attr_index: usize) -> ast::Item {
36 let item = item.clone_for_update();
37 for attr in item.attrs().take(attr_index + 1) {
38 if let Some(name) =
39 attr.path().and_then(|path| path.as_single_segment()).and_then(|seg| seg.name_ref())
40 {
41 if name.as_name() == name![derive] {
42 attr.syntax().detach();
43 }
44 }
45 }
46 item
47}
48
49#[cfg(test)]
50mod tests {
51 use base_db::fixture::WithFixture;
52 use base_db::SourceDatabase;
53 use expect_test::{expect, Expect};
54
55 use crate::test_db::TestDB;
56
57 use super::*;
58
59 fn test_remove_derives_up_to(attr: usize, ra_fixture: &str, expect: Expect) {
60 let (db, file_id) = TestDB::with_single_file(&ra_fixture);
61 let parsed = db.parse(file_id);
62
63 let mut items: Vec<_> =
64 parsed.syntax_node().descendants().filter_map(ast::Item::cast).collect();
65 assert_eq!(items.len(), 1);
66
67 let item = remove_derives_up_to(items.pop().unwrap(), attr);
68 expect.assert_eq(&item.to_string());
69 }
70
71 #[test]
72 fn remove_derive() {
73 test_remove_derives_up_to(
74 2,
75 r#"
76#[allow(unused)]
77#[derive(Copy)]
78#[derive(Hello)]
79#[derive(Clone)]
80struct A {
81 bar: u32
82}
83 "#,
84 expect![[r#"
85#[allow(unused)]
86
87
88#[derive(Clone)]
89struct A {
90 bar: u32
91}"#]],
92 );
93 }
94}
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index a0e6aec62..5df11856e 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -14,9 +14,12 @@ pub mod builtin_macro;
14pub mod proc_macro; 14pub mod proc_macro;
15pub mod quote; 15pub mod quote;
16pub mod eager; 16pub mod eager;
17mod input;
17 18
18use either::Either; 19use either::Either;
20
19pub use mbe::{ExpandError, ExpandResult}; 21pub use mbe::{ExpandError, ExpandResult};
22pub use parser::FragmentKind;
20 23
21use std::hash::Hash; 24use std::hash::Hash;
22use std::sync::Arc; 25use std::sync::Arc;
@@ -290,13 +293,21 @@ pub struct MacroCallLoc {
290 293
291#[derive(Debug, Clone, PartialEq, Eq, Hash)] 294#[derive(Debug, Clone, PartialEq, Eq, Hash)]
292pub enum MacroCallKind { 295pub enum MacroCallKind {
293 FnLike { ast_id: AstId<ast::MacroCall> }, 296 FnLike {
294 Derive { ast_id: AstId<ast::Item>, derive_name: String, derive_attr: AttrId }, 297 ast_id: AstId<ast::MacroCall>,
298 fragment: FragmentKind,
299 },
300 Derive {
301 ast_id: AstId<ast::Item>,
302 derive_name: String,
303 /// Syntactical index of the invoking `#[derive]` attribute.
304 ///
305 /// Outer attributes are counted first, then inner attributes. This does not support
306 /// out-of-line modules, which may have attributes spread across 2 files!
307 derive_attr_index: u32,
308 },
295} 309}
296 310
297#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
298pub struct AttrId(pub u32);
299
300impl MacroCallKind { 311impl MacroCallKind {
301 fn file_id(&self) -> HirFileId { 312 fn file_id(&self) -> HirFileId {
302 match self { 313 match self {
@@ -324,6 +335,13 @@ impl MacroCallKind {
324 MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()), 335 MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()),
325 } 336 }
326 } 337 }
338
339 fn fragment_kind(&self) -> FragmentKind {
340 match self {
341 MacroCallKind::FnLike { fragment, .. } => *fragment,
342 MacroCallKind::Derive { .. } => FragmentKind::Items,
343 }
344 }
327} 345}
328 346
329impl MacroCallId { 347impl MacroCallId {
@@ -351,13 +369,12 @@ pub struct ExpansionInfo {
351 /// The `macro_rules!` arguments. 369 /// The `macro_rules!` arguments.
352 def: Option<InFile<ast::TokenTree>>, 370 def: Option<InFile<ast::TokenTree>>,
353 371
354 macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>, 372 macro_def: Arc<db::TokenExpander>,
355 macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, 373 macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>,
356 exp_map: Arc<mbe::TokenMap>, 374 exp_map: Arc<mbe::TokenMap>,
357} 375}
358 376
359pub use mbe::Origin; 377pub use mbe::Origin;
360use parser::FragmentKind;
361 378
362impl ExpansionInfo { 379impl ExpansionInfo {
363 pub fn call_node(&self) -> Option<InFile<SyntaxNode>> { 380 pub fn call_node(&self) -> Option<InFile<SyntaxNode>> {
@@ -368,7 +385,7 @@ impl ExpansionInfo {
368 assert_eq!(token.file_id, self.arg.file_id); 385 assert_eq!(token.file_id, self.arg.file_id);
369 let range = token.value.text_range().checked_sub(self.arg.value.text_range().start())?; 386 let range = token.value.text_range().checked_sub(self.arg.value.text_range().start())?;
370 let token_id = self.macro_arg.1.token_by_range(range)?; 387 let token_id = self.macro_arg.1.token_by_range(range)?;
371 let token_id = self.macro_def.0.map_id_down(token_id); 388 let token_id = self.macro_def.map_id_down(token_id);
372 389
373 let range = self.exp_map.range_by_token(token_id)?.by_kind(token.value.kind())?; 390 let range = self.exp_map.range_by_token(token_id)?.by_kind(token.value.kind())?;
374 391
@@ -383,17 +400,16 @@ impl ExpansionInfo {
383 ) -> Option<(InFile<SyntaxToken>, Origin)> { 400 ) -> Option<(InFile<SyntaxToken>, Origin)> {
384 let token_id = self.exp_map.token_by_range(token.value.text_range())?; 401 let token_id = self.exp_map.token_by_range(token.value.text_range())?;
385 402
386 let (token_id, origin) = self.macro_def.0.map_id_up(token_id); 403 let (token_id, origin) = self.macro_def.map_id_up(token_id);
387 let (token_map, tt) = match origin { 404 let (token_map, tt) = match origin {
388 mbe::Origin::Call => (&self.macro_arg.1, self.arg.clone()), 405 mbe::Origin::Call => (&self.macro_arg.1, self.arg.clone()),
389 mbe::Origin::Def => ( 406 mbe::Origin::Def => match (&*self.macro_def, self.def.as_ref()) {
390 &self.macro_def.1, 407 (db::TokenExpander::MacroRules { def_site_token_map, .. }, Some(tt))
391 self.def 408 | (db::TokenExpander::MacroDef { def_site_token_map, .. }, Some(tt)) => {
392 .as_ref() 409 (def_site_token_map, tt.as_ref().map(|tt| tt.syntax().clone()))
393 .expect("`Origin::Def` used with non-`macro_rules!` macro") 410 }
394 .as_ref() 411 _ => panic!("`Origin::Def` used with non-`macro_rules!` macro"),
395 .map(|tt| tt.syntax().clone()), 412 },
396 ),
397 }; 413 };
398 414
399 let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?; 415 let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?;
@@ -563,3 +579,59 @@ impl<N: AstNode> InFile<N> {
563 self.with_value(self.value.syntax()) 579 self.with_value(self.value.syntax())
564 } 580 }
565} 581}
582
583/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
584/// FIXME: Not completed
585pub fn to_fragment_kind(call: &ast::MacroCall) -> FragmentKind {
586 use syntax::SyntaxKind::*;
587
588 let syn = call.syntax();
589
590 let parent = match syn.parent() {
591 Some(it) => it,
592 None => return FragmentKind::Statements,
593 };
594
595 match parent.kind() {
596 MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items,
597 MACRO_STMTS => FragmentKind::Statements,
598 MACRO_PAT => FragmentKind::Pattern,
599 MACRO_TYPE => FragmentKind::Type,
600 ITEM_LIST => FragmentKind::Items,
601 LET_STMT => {
602 // FIXME: Handle LHS Pattern
603 FragmentKind::Expr
604 }
605 EXPR_STMT => FragmentKind::Statements,
606 BLOCK_EXPR => FragmentKind::Statements,
607 ARG_LIST => FragmentKind::Expr,
608 TRY_EXPR => FragmentKind::Expr,
609 TUPLE_EXPR => FragmentKind::Expr,
610 PAREN_EXPR => FragmentKind::Expr,
611 ARRAY_EXPR => FragmentKind::Expr,
612 FOR_EXPR => FragmentKind::Expr,
613 PATH_EXPR => FragmentKind::Expr,
614 CLOSURE_EXPR => FragmentKind::Expr,
615 CONDITION => FragmentKind::Expr,
616 BREAK_EXPR => FragmentKind::Expr,
617 RETURN_EXPR => FragmentKind::Expr,
618 MATCH_EXPR => FragmentKind::Expr,
619 MATCH_ARM => FragmentKind::Expr,
620 MATCH_GUARD => FragmentKind::Expr,
621 RECORD_EXPR_FIELD => FragmentKind::Expr,
622 CALL_EXPR => FragmentKind::Expr,
623 INDEX_EXPR => FragmentKind::Expr,
624 METHOD_CALL_EXPR => FragmentKind::Expr,
625 FIELD_EXPR => FragmentKind::Expr,
626 AWAIT_EXPR => FragmentKind::Expr,
627 CAST_EXPR => FragmentKind::Expr,
628 REF_EXPR => FragmentKind::Expr,
629 PREFIX_EXPR => FragmentKind::Expr,
630 RANGE_EXPR => FragmentKind::Expr,
631 BIN_EXPR => FragmentKind::Expr,
632 _ => {
633 // Unknown , Just guess it is `Items`
634 FragmentKind::Items
635 }
636 }
637}
diff --git a/crates/hir_expand/src/proc_macro.rs b/crates/hir_expand/src/proc_macro.rs
index 75e950816..d5643393a 100644
--- a/crates/hir_expand/src/proc_macro.rs
+++ b/crates/hir_expand/src/proc_macro.rs
@@ -2,7 +2,6 @@
2 2
3use crate::db::AstDatabase; 3use crate::db::AstDatabase;
4use base_db::{CrateId, ProcMacroId}; 4use base_db::{CrateId, ProcMacroId};
5use tt::buffer::{Cursor, TokenBuffer};
6 5
7#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] 6#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
8pub struct ProcMacroExpander { 7pub struct ProcMacroExpander {
@@ -44,9 +43,6 @@ impl ProcMacroExpander {
44 .clone() 43 .clone()
45 .ok_or_else(|| err!("No derive macro found."))?; 44 .ok_or_else(|| err!("No derive macro found."))?;
46 45
47 let tt = remove_derive_attrs(tt)
48 .ok_or_else(|| err!("Fail to remove derive for custom derive"))?;
49
50 // Proc macros have access to the environment variables of the invoking crate. 46 // Proc macros have access to the environment variables of the invoking crate.
51 let env = &krate_graph[calling_crate].env; 47 let env = &krate_graph[calling_crate].env;
52 48
@@ -56,101 +52,3 @@ impl ProcMacroExpander {
56 } 52 }
57 } 53 }
58} 54}
59
60fn eat_punct(cursor: &mut Cursor, c: char) -> bool {
61 if let Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Punct(punct), _)) = cursor.token_tree() {
62 if punct.char == c {
63 *cursor = cursor.bump();
64 return true;
65 }
66 }
67 false
68}
69
70fn eat_subtree(cursor: &mut Cursor, kind: tt::DelimiterKind) -> bool {
71 if let Some(tt::buffer::TokenTreeRef::Subtree(subtree, _)) = cursor.token_tree() {
72 if Some(kind) == subtree.delimiter_kind() {
73 *cursor = cursor.bump_subtree();
74 return true;
75 }
76 }
77 false
78}
79
80fn eat_ident(cursor: &mut Cursor, t: &str) -> bool {
81 if let Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Ident(ident), _)) = cursor.token_tree() {
82 if t == ident.text.as_str() {
83 *cursor = cursor.bump();
84 return true;
85 }
86 }
87 false
88}
89
90fn remove_derive_attrs(tt: &tt::Subtree) -> Option<tt::Subtree> {
91 let buffer = TokenBuffer::from_tokens(&tt.token_trees);
92 let mut p = buffer.begin();
93 let mut result = tt::Subtree::default();
94
95 while !p.eof() {
96 let curr = p;
97
98 if eat_punct(&mut p, '#') {
99 eat_punct(&mut p, '!');
100 let parent = p;
101 if eat_subtree(&mut p, tt::DelimiterKind::Bracket) {
102 if eat_ident(&mut p, "derive") {
103 p = parent.bump();
104 continue;
105 }
106 }
107 }
108
109 result.token_trees.push(curr.token_tree()?.cloned());
110 p = curr.bump();
111 }
112
113 Some(result)
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119 use test_utils::assert_eq_text;
120
121 #[test]
122 fn test_remove_derive_attrs() {
123 let tt = mbe::parse_to_token_tree(
124 r#"
125 #[allow(unused)]
126 #[derive(Copy)]
127 #[derive(Hello)]
128 struct A {
129 bar: u32
130 }
131"#,
132 )
133 .unwrap()
134 .0;
135 let result = format!("{:#?}", remove_derive_attrs(&tt).unwrap());
136
137 assert_eq_text!(
138 r#"
139SUBTREE $
140 PUNCH # [alone] 0
141 SUBTREE [] 1
142 IDENT allow 2
143 SUBTREE () 3
144 IDENT unused 4
145 IDENT struct 15
146 IDENT A 16
147 SUBTREE {} 17
148 IDENT bar 18
149 PUNCH : [alone] 19
150 IDENT u32 20
151"#
152 .trim(),
153 &result
154 );
155 }
156}
diff --git a/crates/hir_expand/src/quote.rs b/crates/hir_expand/src/quote.rs
index c82487ef0..230a59964 100644
--- a/crates/hir_expand/src/quote.rs
+++ b/crates/hir_expand/src/quote.rs
@@ -196,8 +196,8 @@ impl_to_to_tokentrees! {
196 tt::Literal => self { self }; 196 tt::Literal => self { self };
197 tt::Ident => self { self }; 197 tt::Ident => self { self };
198 tt::Punct => self { self }; 198 tt::Punct => self { self };
199 &str => self { tt::Literal{text: format!("{:?}", self.escape_default().to_string()).into(), id: tt::TokenId::unspecified()}}; 199 &str => self { tt::Literal{text: format!("\"{}\"", self.escape_debug()).into(), id: tt::TokenId::unspecified()}};
200 String => self { tt::Literal{text: format!("{:?}", self.escape_default().to_string()).into(), id: tt::TokenId::unspecified()}} 200 String => self { tt::Literal{text: format!("\"{}\"", self.escape_debug()).into(), id: tt::TokenId::unspecified()}}
201} 201}
202 202
203#[cfg(test)] 203#[cfg(test)]
diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs
index 8c4542956..5232a7d80 100644
--- a/crates/hir_ty/src/chalk_ext.rs
+++ b/crates/hir_ty/src/chalk_ext.rs
@@ -1,8 +1,10 @@
1//! Various extensions traits for Chalk types. 1//! Various extensions traits for Chalk types.
2 2
3use chalk_ir::Mutability; 3use chalk_ir::{FloatTy, IntTy, Mutability, Scalar, UintTy};
4use hir_def::{ 4use hir_def::{
5 type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, TraitId, 5 builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType, BuiltinUint},
6 type_ref::Rawness,
7 AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, TraitId,
6}; 8};
7 9
8use crate::{ 10use crate::{
@@ -18,6 +20,7 @@ pub trait TyExt {
18 fn is_unknown(&self) -> bool; 20 fn is_unknown(&self) -> bool;
19 21
20 fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)>; 22 fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)>;
23 fn as_builtin(&self) -> Option<BuiltinType>;
21 fn as_tuple(&self) -> Option<&Substitution>; 24 fn as_tuple(&self) -> Option<&Substitution>;
22 fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId>; 25 fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId>;
23 fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)>; 26 fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)>;
@@ -59,6 +62,35 @@ impl TyExt for Ty {
59 } 62 }
60 } 63 }
61 64
65 fn as_builtin(&self) -> Option<BuiltinType> {
66 match self.kind(&Interner) {
67 TyKind::Str => Some(BuiltinType::Str),
68 TyKind::Scalar(Scalar::Bool) => Some(BuiltinType::Bool),
69 TyKind::Scalar(Scalar::Char) => Some(BuiltinType::Char),
70 TyKind::Scalar(Scalar::Float(fty)) => Some(BuiltinType::Float(match fty {
71 FloatTy::F64 => BuiltinFloat::F64,
72 FloatTy::F32 => BuiltinFloat::F32,
73 })),
74 TyKind::Scalar(Scalar::Int(ity)) => Some(BuiltinType::Int(match ity {
75 IntTy::Isize => BuiltinInt::Isize,
76 IntTy::I8 => BuiltinInt::I8,
77 IntTy::I16 => BuiltinInt::I16,
78 IntTy::I32 => BuiltinInt::I32,
79 IntTy::I64 => BuiltinInt::I64,
80 IntTy::I128 => BuiltinInt::I128,
81 })),
82 TyKind::Scalar(Scalar::Uint(ity)) => Some(BuiltinType::Uint(match ity {
83 UintTy::Usize => BuiltinUint::Usize,
84 UintTy::U8 => BuiltinUint::U8,
85 UintTy::U16 => BuiltinUint::U16,
86 UintTy::U32 => BuiltinUint::U32,
87 UintTy::U64 => BuiltinUint::U64,
88 UintTy::U128 => BuiltinUint::U128,
89 })),
90 _ => None,
91 }
92 }
93
62 fn as_tuple(&self) -> Option<&Substitution> { 94 fn as_tuple(&self) -> Option<&Substitution> {
63 match self.kind(&Interner) { 95 match self.kind(&Interner) {
64 TyKind::Tuple(_, substs) => Some(substs), 96 TyKind::Tuple(_, substs) => Some(substs),
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index 4fb7d9cf2..1f6edf7a2 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -1000,7 +1000,7 @@ impl HirDisplay for TypeRef {
1000 } 1000 }
1001 TypeRef::Macro(macro_call) => { 1001 TypeRef::Macro(macro_call) => {
1002 let macro_call = macro_call.to_node(f.db.upcast()); 1002 let macro_call = macro_call.to_node(f.db.upcast());
1003 let ctx = body::LowerCtx::with_hygiene(&Hygiene::new_unhygienic()); 1003 let ctx = body::LowerCtx::with_hygiene(f.db.upcast(), &Hygiene::new_unhygienic());
1004 match macro_call.path() { 1004 match macro_call.path() {
1005 Some(path) => match Path::from_src(path, &ctx) { 1005 Some(path) => match Path::from_src(path, &ctx) {
1006 Some(path) => path.hir_fmt(f)?, 1006 Some(path) => path.hir_fmt(f)?,
diff --git a/crates/ide/Cargo.toml b/crates/ide/Cargo.toml
index f04bcf531..88f3d09d3 100644
--- a/crates/ide/Cargo.toml
+++ b/crates/ide/Cargo.toml
@@ -20,6 +20,7 @@ oorandom = "11.1.2"
20pulldown-cmark-to-cmark = "6.0.0" 20pulldown-cmark-to-cmark = "6.0.0"
21pulldown-cmark = { version = "0.8.0", default-features = false } 21pulldown-cmark = { version = "0.8.0", default-features = false }
22url = "2.1.1" 22url = "2.1.1"
23dot = "0.1.4"
23 24
24stdx = { path = "../stdx", version = "0.0.0" } 25stdx = { path = "../stdx", version = "0.0.0" }
25syntax = { path = "../syntax", version = "0.0.0" } 26syntax = { path = "../syntax", version = "0.0.0" }
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index 1c911a8b2..273d8cfbb 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -15,6 +15,7 @@ use hir::{
15 diagnostics::{Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder}, 15 diagnostics::{Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder},
16 InFile, Semantics, 16 InFile, Semantics,
17}; 17};
18use ide_assists::AssistResolveStrategy;
18use ide_db::{base_db::SourceDatabase, RootDatabase}; 19use ide_db::{base_db::SourceDatabase, RootDatabase};
19use itertools::Itertools; 20use itertools::Itertools;
20use rustc_hash::FxHashSet; 21use rustc_hash::FxHashSet;
@@ -84,7 +85,7 @@ pub struct DiagnosticsConfig {
84pub(crate) fn diagnostics( 85pub(crate) fn diagnostics(
85 db: &RootDatabase, 86 db: &RootDatabase,
86 config: &DiagnosticsConfig, 87 config: &DiagnosticsConfig,
87 resolve: bool, 88 resolve: &AssistResolveStrategy,
88 file_id: FileId, 89 file_id: FileId,
89) -> Vec<Diagnostic> { 90) -> Vec<Diagnostic> {
90 let _p = profile::span("diagnostics"); 91 let _p = profile::span("diagnostics");
@@ -212,7 +213,7 @@ pub(crate) fn diagnostics(
212fn diagnostic_with_fix<D: DiagnosticWithFix>( 213fn diagnostic_with_fix<D: DiagnosticWithFix>(
213 d: &D, 214 d: &D,
214 sema: &Semantics<RootDatabase>, 215 sema: &Semantics<RootDatabase>,
215 resolve: bool, 216 resolve: &AssistResolveStrategy,
216) -> Diagnostic { 217) -> Diagnostic {
217 Diagnostic::error(sema.diagnostics_display_range(d.display_source()).range, d.message()) 218 Diagnostic::error(sema.diagnostics_display_range(d.display_source()).range, d.message())
218 .with_fix(d.fix(&sema, resolve)) 219 .with_fix(d.fix(&sema, resolve))
@@ -222,7 +223,7 @@ fn diagnostic_with_fix<D: DiagnosticWithFix>(
222fn warning_with_fix<D: DiagnosticWithFix>( 223fn warning_with_fix<D: DiagnosticWithFix>(
223 d: &D, 224 d: &D,
224 sema: &Semantics<RootDatabase>, 225 sema: &Semantics<RootDatabase>,
225 resolve: bool, 226 resolve: &AssistResolveStrategy,
226) -> Diagnostic { 227) -> Diagnostic {
227 Diagnostic::hint(sema.diagnostics_display_range(d.display_source()).range, d.message()) 228 Diagnostic::hint(sema.diagnostics_display_range(d.display_source()).range, d.message())
228 .with_fix(d.fix(&sema, resolve)) 229 .with_fix(d.fix(&sema, resolve))
@@ -299,6 +300,7 @@ fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
299#[cfg(test)] 300#[cfg(test)]
300mod tests { 301mod tests {
301 use expect_test::{expect, Expect}; 302 use expect_test::{expect, Expect};
303 use ide_assists::AssistResolveStrategy;
302 use stdx::trim_indent; 304 use stdx::trim_indent;
303 use test_utils::assert_eq_text; 305 use test_utils::assert_eq_text;
304 306
@@ -314,7 +316,11 @@ mod tests {
314 316
315 let (analysis, file_position) = fixture::position(ra_fixture_before); 317 let (analysis, file_position) = fixture::position(ra_fixture_before);
316 let diagnostic = analysis 318 let diagnostic = analysis
317 .diagnostics(&DiagnosticsConfig::default(), true, file_position.file_id) 319 .diagnostics(
320 &DiagnosticsConfig::default(),
321 AssistResolveStrategy::All,
322 file_position.file_id,
323 )
318 .unwrap() 324 .unwrap()
319 .pop() 325 .pop()
320 .unwrap(); 326 .unwrap();
@@ -343,7 +349,11 @@ mod tests {
343 fn check_no_fix(ra_fixture: &str) { 349 fn check_no_fix(ra_fixture: &str) {
344 let (analysis, file_position) = fixture::position(ra_fixture); 350 let (analysis, file_position) = fixture::position(ra_fixture);
345 let diagnostic = analysis 351 let diagnostic = analysis
346 .diagnostics(&DiagnosticsConfig::default(), true, file_position.file_id) 352 .diagnostics(
353 &DiagnosticsConfig::default(),
354 AssistResolveStrategy::All,
355 file_position.file_id,
356 )
347 .unwrap() 357 .unwrap()
348 .pop() 358 .pop()
349 .unwrap(); 359 .unwrap();
@@ -357,7 +367,9 @@ mod tests {
357 let diagnostics = files 367 let diagnostics = files
358 .into_iter() 368 .into_iter()
359 .flat_map(|file_id| { 369 .flat_map(|file_id| {
360 analysis.diagnostics(&DiagnosticsConfig::default(), true, file_id).unwrap() 370 analysis
371 .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id)
372 .unwrap()
361 }) 373 })
362 .collect::<Vec<_>>(); 374 .collect::<Vec<_>>();
363 assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics); 375 assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics);
@@ -365,8 +377,9 @@ mod tests {
365 377
366 fn check_expect(ra_fixture: &str, expect: Expect) { 378 fn check_expect(ra_fixture: &str, expect: Expect) {
367 let (analysis, file_id) = fixture::file(ra_fixture); 379 let (analysis, file_id) = fixture::file(ra_fixture);
368 let diagnostics = 380 let diagnostics = analysis
369 analysis.diagnostics(&DiagnosticsConfig::default(), true, file_id).unwrap(); 381 .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id)
382 .unwrap();
370 expect.assert_debug_eq(&diagnostics) 383 expect.assert_debug_eq(&diagnostics)
371 } 384 }
372 385
@@ -641,6 +654,26 @@ fn test_fn() {
641 } 654 }
642 655
643 #[test] 656 #[test]
657 fn test_fill_struct_fields_raw_ident() {
658 check_fix(
659 r#"
660struct TestStruct { r#type: u8 }
661
662fn test_fn() {
663 TestStruct { $0 };
664}
665"#,
666 r"
667struct TestStruct { r#type: u8 }
668
669fn test_fn() {
670 TestStruct { r#type: () };
671}
672",
673 );
674 }
675
676 #[test]
644 fn test_fill_struct_fields_no_diagnostic() { 677 fn test_fill_struct_fields_no_diagnostic() {
645 check_no_diagnostics( 678 check_no_diagnostics(
646 r" 679 r"
@@ -911,11 +944,13 @@ struct Foo {
911 944
912 let (analysis, file_id) = fixture::file(r#"mod foo;"#); 945 let (analysis, file_id) = fixture::file(r#"mod foo;"#);
913 946
914 let diagnostics = analysis.diagnostics(&config, true, file_id).unwrap(); 947 let diagnostics =
948 analysis.diagnostics(&config, AssistResolveStrategy::All, file_id).unwrap();
915 assert!(diagnostics.is_empty()); 949 assert!(diagnostics.is_empty());
916 950
917 let diagnostics = 951 let diagnostics = analysis
918 analysis.diagnostics(&DiagnosticsConfig::default(), true, file_id).unwrap(); 952 .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id)
953 .unwrap();
919 assert!(!diagnostics.is_empty()); 954 assert!(!diagnostics.is_empty());
920 } 955 }
921 956
@@ -1022,7 +1057,11 @@ impl TestStruct {
1022 1057
1023 let (analysis, file_position) = fixture::position(input); 1058 let (analysis, file_position) = fixture::position(input);
1024 let diagnostics = analysis 1059 let diagnostics = analysis
1025 .diagnostics(&DiagnosticsConfig::default(), true, file_position.file_id) 1060 .diagnostics(
1061 &DiagnosticsConfig::default(),
1062 AssistResolveStrategy::All,
1063 file_position.file_id,
1064 )
1026 .unwrap(); 1065 .unwrap();
1027 assert_eq!(diagnostics.len(), 1); 1066 assert_eq!(diagnostics.len(), 1);
1028 1067
diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs
index 7be8b3459..15821500f 100644
--- a/crates/ide/src/diagnostics/fixes.rs
+++ b/crates/ide/src/diagnostics/fixes.rs
@@ -8,6 +8,7 @@ use hir::{
8 }, 8 },
9 HasSource, HirDisplay, InFile, Semantics, VariantDef, 9 HasSource, HirDisplay, InFile, Semantics, VariantDef,
10}; 10};
11use ide_assists::AssistResolveStrategy;
11use ide_db::{ 12use ide_db::{
12 base_db::{AnchoredPathBuf, FileId}, 13 base_db::{AnchoredPathBuf, FileId},
13 source_change::{FileSystemEdit, SourceChange}, 14 source_change::{FileSystemEdit, SourceChange},
@@ -35,11 +36,19 @@ pub(crate) trait DiagnosticWithFix: Diagnostic {
35 /// 36 ///
36 /// If `resolve` is false, the edit will be computed later, on demand, and 37 /// If `resolve` is false, the edit will be computed later, on demand, and
37 /// can be omitted. 38 /// can be omitted.
38 fn fix(&self, sema: &Semantics<RootDatabase>, _resolve: bool) -> Option<Assist>; 39 fn fix(
40 &self,
41 sema: &Semantics<RootDatabase>,
42 _resolve: &AssistResolveStrategy,
43 ) -> Option<Assist>;
39} 44}
40 45
41impl DiagnosticWithFix for UnresolvedModule { 46impl DiagnosticWithFix for UnresolvedModule {
42 fn fix(&self, sema: &Semantics<RootDatabase>, _resolve: bool) -> Option<Assist> { 47 fn fix(
48 &self,
49 sema: &Semantics<RootDatabase>,
50 _resolve: &AssistResolveStrategy,
51 ) -> Option<Assist> {
43 let root = sema.db.parse_or_expand(self.file)?;