diff options
Diffstat (limited to 'crates')
201 files changed, 7298 insertions, 4050 deletions
diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index 8d4641355..04e2be390 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs | |||
@@ -1,62 +1,4 @@ | |||
1 | //! Fixtures are strings containing rust source code with optional metadata. | 1 | //! A set of high-level utility fixture methods to use in tests. |
2 | //! A fixture without metadata is parsed into a single source file. | ||
3 | //! Use this to test functionality local to one file. | ||
4 | //! | ||
5 | //! Simple Example: | ||
6 | //! ``` | ||
7 | //! r#" | ||
8 | //! fn main() { | ||
9 | //! println!("Hello World") | ||
10 | //! } | ||
11 | //! "# | ||
12 | //! ``` | ||
13 | //! | ||
14 | //! Metadata can be added to a fixture after a `//-` comment. | ||
15 | //! The basic form is specifying filenames, | ||
16 | //! which is also how to define multiple files in a single test fixture | ||
17 | //! | ||
18 | //! Example using two files in the same crate: | ||
19 | //! ``` | ||
20 | //! " | ||
21 | //! //- /main.rs | ||
22 | //! mod foo; | ||
23 | //! fn main() { | ||
24 | //! foo::bar(); | ||
25 | //! } | ||
26 | //! | ||
27 | //! //- /foo.rs | ||
28 | //! pub fn bar() {} | ||
29 | //! " | ||
30 | //! ``` | ||
31 | //! | ||
32 | //! Example using two crates with one file each, with one crate depending on the other: | ||
33 | //! ``` | ||
34 | //! r#" | ||
35 | //! //- /main.rs crate:a deps:b | ||
36 | //! fn main() { | ||
37 | //! b::foo(); | ||
38 | //! } | ||
39 | //! //- /lib.rs crate:b | ||
40 | //! pub fn b() { | ||
41 | //! println!("Hello World") | ||
42 | //! } | ||
43 | //! "# | ||
44 | //! ``` | ||
45 | //! | ||
46 | //! Metadata allows specifying all settings and variables | ||
47 | //! that are available in a real rust project: | ||
48 | //! - crate names via `crate:cratename` | ||
49 | //! - dependencies via `deps:dep1,dep2` | ||
50 | //! - configuration settings via `cfg:dbg=false,opt_level=2` | ||
51 | //! - environment variables via `env:PATH=/bin,RUST_LOG=debug` | ||
52 | //! | ||
53 | //! Example using all available metadata: | ||
54 | //! ``` | ||
55 | //! " | ||
56 | //! //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo | ||
57 | //! fn insert_source_code_here() {} | ||
58 | //! " | ||
59 | //! ``` | ||
60 | use std::{mem, str::FromStr, sync::Arc}; | 2 | use std::{mem, str::FromStr, sync::Arc}; |
61 | 3 | ||
62 | use cfg::CfgOptions; | 4 | use cfg::CfgOptions; |
diff --git a/crates/base_db/src/lib.rs b/crates/base_db/src/lib.rs index 5f77a0b1f..980a0ed98 100644 --- a/crates/base_db/src/lib.rs +++ b/crates/base_db/src/lib.rs | |||
@@ -59,6 +59,8 @@ pub trait CheckCanceled { | |||
59 | Self: Sized + panic::RefUnwindSafe, | 59 | Self: Sized + panic::RefUnwindSafe, |
60 | F: FnOnce(&Self) -> T + panic::UnwindSafe, | 60 | F: FnOnce(&Self) -> T + panic::UnwindSafe, |
61 | { | 61 | { |
62 | // Uncomment to debug missing cancellations. | ||
63 | // let _span = profile::heartbeat_span(); | ||
62 | panic::catch_unwind(|| f(self)).map_err(|err| match err.downcast::<Canceled>() { | 64 | panic::catch_unwind(|| f(self)).map_err(|err| match err.downcast::<Canceled>() { |
63 | Ok(canceled) => *canceled, | 65 | Ok(canceled) => *canceled, |
64 | Err(payload) => panic::resume_unwind(payload), | 66 | Err(payload) => panic::resume_unwind(payload), |
@@ -68,6 +70,7 @@ pub trait CheckCanceled { | |||
68 | 70 | ||
69 | impl<T: salsa::Database> CheckCanceled for T { | 71 | impl<T: salsa::Database> CheckCanceled for T { |
70 | fn check_canceled(&self) { | 72 | fn check_canceled(&self) { |
73 | // profile::heartbeat(); | ||
71 | if self.salsa_runtime().is_current_revision_canceled() { | 74 | if self.salsa_runtime().is_current_revision_canceled() { |
72 | Canceled::throw() | 75 | Canceled::throw() |
73 | } | 76 | } |
diff --git a/crates/cfg/src/tests.rs b/crates/cfg/src/tests.rs index bd0f9ec48..d8736c893 100644 --- a/crates/cfg/src/tests.rs +++ b/crates/cfg/src/tests.rs | |||
@@ -8,7 +8,7 @@ fn assert_parse_result(input: &str, expected: CfgExpr) { | |||
8 | let (tt, _) = { | 8 | let (tt, _) = { |
9 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); | 9 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); |
10 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | 10 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); |
11 | ast_to_token_tree(&tt).unwrap() | 11 | ast_to_token_tree(&tt) |
12 | }; | 12 | }; |
13 | let cfg = CfgExpr::parse(&tt); | 13 | let cfg = CfgExpr::parse(&tt); |
14 | assert_eq!(cfg, expected); | 14 | assert_eq!(cfg, expected); |
@@ -18,7 +18,7 @@ fn check_dnf(input: &str, expect: Expect) { | |||
18 | let (tt, _) = { | 18 | let (tt, _) = { |
19 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); | 19 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); |
20 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | 20 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); |
21 | ast_to_token_tree(&tt).unwrap() | 21 | ast_to_token_tree(&tt) |
22 | }; | 22 | }; |
23 | let cfg = CfgExpr::parse(&tt); | 23 | let cfg = CfgExpr::parse(&tt); |
24 | let actual = format!("#![cfg({})]", DnfExpr::new(cfg)); | 24 | let actual = format!("#![cfg({})]", DnfExpr::new(cfg)); |
@@ -29,7 +29,7 @@ fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) { | |||
29 | let (tt, _) = { | 29 | let (tt, _) = { |
30 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); | 30 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); |
31 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | 31 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); |
32 | ast_to_token_tree(&tt).unwrap() | 32 | ast_to_token_tree(&tt) |
33 | }; | 33 | }; |
34 | let cfg = CfgExpr::parse(&tt); | 34 | let cfg = CfgExpr::parse(&tt); |
35 | let dnf = DnfExpr::new(cfg); | 35 | let dnf = DnfExpr::new(cfg); |
@@ -42,7 +42,7 @@ fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) { | |||
42 | let (tt, _) = { | 42 | let (tt, _) = { |
43 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); | 43 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); |
44 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | 44 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); |
45 | ast_to_token_tree(&tt).unwrap() | 45 | ast_to_token_tree(&tt) |
46 | }; | 46 | }; |
47 | let cfg = CfgExpr::parse(&tt); | 47 | let cfg = CfgExpr::parse(&tt); |
48 | let dnf = DnfExpr::new(cfg); | 48 | let dnf = DnfExpr::new(cfg); |
diff --git a/crates/hir/Cargo.toml b/crates/hir/Cargo.toml index 2ef5bcbc9..9e329656f 100644 --- a/crates/hir/Cargo.toml +++ b/crates/hir/Cargo.toml | |||
@@ -13,7 +13,7 @@ doctest = false | |||
13 | log = "0.4.8" | 13 | log = "0.4.8" |
14 | rustc-hash = "1.1.0" | 14 | rustc-hash = "1.1.0" |
15 | either = "1.5.3" | 15 | either = "1.5.3" |
16 | arrayvec = "0.6" | 16 | arrayvec = "0.7" |
17 | itertools = "0.10.0" | 17 | itertools = "0.10.0" |
18 | smallvec = "1.4.0" | 18 | smallvec = "1.4.0" |
19 | 19 | ||
diff --git a/crates/hir/src/db.rs b/crates/hir/src/db.rs index 1902a8d16..df5758342 100644 --- a/crates/hir/src/db.rs +++ b/crates/hir/src/db.rs | |||
@@ -1,14 +1,6 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | pub use hir_def::db::{ | 3 | pub use hir_def::db::*; |
4 | AttrsQuery, BlockDefMapQuery, BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, | ||
5 | CrateDefMapQueryQuery, CrateLangItemsQuery, DefDatabase, DefDatabaseStorage, EnumDataQuery, | ||
6 | ExprScopesQuery, FileItemTreeQuery, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, | ||
7 | ImportMapQuery, InternConstQuery, InternDatabase, InternDatabaseStorage, InternEnumQuery, | ||
8 | InternFunctionQuery, InternImplQuery, InternStaticQuery, InternStructQuery, InternTraitQuery, | ||
9 | InternTypeAliasQuery, InternUnionQuery, LangItemQuery, StaticDataQuery, StructDataQuery, | ||
10 | TraitDataQuery, TypeAliasDataQuery, UnionDataQuery, | ||
11 | }; | ||
12 | pub use hir_expand::db::{ | 4 | pub use hir_expand::db::{ |
13 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, HygieneFrameQuery, InternEagerExpansionQuery, | 5 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, HygieneFrameQuery, InternEagerExpansionQuery, |
14 | InternMacroQuery, MacroArgTextQuery, MacroDefQuery, MacroExpandQuery, ParseMacroExpansionQuery, | 6 | InternMacroQuery, MacroArgTextQuery, MacroDefQuery, MacroExpandQuery, ParseMacroExpansionQuery, |
diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 97a78ca25..01a4d205f 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs | |||
@@ -9,33 +9,33 @@ use hir_ty::display::{ | |||
9 | write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError, | 9 | write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError, |
10 | HirFormatter, | 10 | HirFormatter, |
11 | }; | 11 | }; |
12 | use hir_ty::Interner; | ||
12 | use syntax::ast::{self, NameOwner}; | 13 | use syntax::ast::{self, NameOwner}; |
13 | 14 | ||
14 | use crate::{ | 15 | use crate::{ |
15 | Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasVisibility, LifetimeParam, | 16 | Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasVisibility, LifetimeParam, |
16 | Module, Static, Struct, Substitution, Trait, Type, TypeAlias, TypeParam, Union, Variant, | 17 | Module, Static, Struct, Trait, TyBuilder, Type, TypeAlias, TypeParam, Union, Variant, |
17 | }; | 18 | }; |
18 | 19 | ||
19 | impl HirDisplay for Function { | 20 | impl HirDisplay for Function { |
20 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | 21 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { |
21 | let data = f.db.function_data(self.id); | 22 | let data = f.db.function_data(self.id); |
22 | write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; | 23 | write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; |
23 | let qual = &data.qualifier; | 24 | if data.is_default() { |
24 | if qual.is_default { | ||
25 | write!(f, "default ")?; | 25 | write!(f, "default ")?; |
26 | } | 26 | } |
27 | if qual.is_const { | 27 | if data.is_const() { |
28 | write!(f, "const ")?; | 28 | write!(f, "const ")?; |
29 | } | 29 | } |
30 | if qual.is_async { | 30 | if data.is_async() { |
31 | write!(f, "async ")?; | 31 | write!(f, "async ")?; |
32 | } | 32 | } |
33 | if qual.is_unsafe { | 33 | if data.is_unsafe() { |
34 | write!(f, "unsafe ")?; | 34 | write!(f, "unsafe ")?; |
35 | } | 35 | } |
36 | if let Some(abi) = &qual.abi { | 36 | if let Some(abi) = &data.abi { |
37 | // FIXME: String escape? | 37 | // FIXME: String escape? |
38 | write!(f, "extern \"{}\" ", abi)?; | 38 | write!(f, "extern \"{}\" ", &**abi)?; |
39 | } | 39 | } |
40 | write!(f, "fn {}", data.name)?; | 40 | write!(f, "fn {}", data.name)?; |
41 | 41 | ||
@@ -68,7 +68,7 @@ impl HirDisplay for Function { | |||
68 | write!(f, ", ")?; | 68 | write!(f, ", ")?; |
69 | } else { | 69 | } else { |
70 | first = false; | 70 | first = false; |
71 | if data.has_self_param { | 71 | if data.has_self_param() { |
72 | write_self_param(type_ref, f)?; | 72 | write_self_param(type_ref, f)?; |
73 | continue; | 73 | continue; |
74 | } | 74 | } |
@@ -88,10 +88,10 @@ impl HirDisplay for Function { | |||
88 | // `FunctionData::ret_type` will be `::core::future::Future<Output = ...>` for async fns. | 88 | // `FunctionData::ret_type` will be `::core::future::Future<Output = ...>` for async fns. |
89 | // Use ugly pattern match to strip the Future trait. | 89 | // Use ugly pattern match to strip the Future trait. |
90 | // Better way? | 90 | // Better way? |
91 | let ret_type = if !qual.is_async { | 91 | let ret_type = if !data.is_async() { |
92 | &data.ret_type | 92 | &data.ret_type |
93 | } else { | 93 | } else { |
94 | match &data.ret_type { | 94 | match &*data.ret_type { |
95 | TypeRef::ImplTrait(bounds) => match &bounds[0] { | 95 | TypeRef::ImplTrait(bounds) => match &bounds[0] { |
96 | TypeBound::Path(path) => { | 96 | TypeBound::Path(path) => { |
97 | path.segments().iter().last().unwrap().args_and_bindings.unwrap().bindings | 97 | path.segments().iter().last().unwrap().args_and_bindings.unwrap().bindings |
@@ -235,8 +235,9 @@ impl HirDisplay for TypeParam { | |||
235 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | 235 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { |
236 | write!(f, "{}", self.name(f.db))?; | 236 | write!(f, "{}", self.name(f.db))?; |
237 | let bounds = f.db.generic_predicates_for_param(self.id); | 237 | let bounds = f.db.generic_predicates_for_param(self.id); |
238 | let substs = Substitution::type_params(f.db, self.id.parent); | 238 | let substs = TyBuilder::type_params_subst(f.db, self.id.parent); |
239 | let predicates = bounds.iter().cloned().map(|b| b.subst(&substs)).collect::<Vec<_>>(); | 239 | let predicates = |
240 | bounds.iter().cloned().map(|b| b.substitute(&Interner, &substs)).collect::<Vec<_>>(); | ||
240 | if !(predicates.is_empty() || f.omit_verbose_types()) { | 241 | if !(predicates.is_empty() || f.omit_verbose_types()) { |
241 | write_bounds_like_dyn_trait_with_prefix(":", &predicates, f)?; | 242 | write_bounds_like_dyn_trait_with_prefix(":", &predicates, f)?; |
242 | } | 243 | } |
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 05a60e158..0afc06906 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -44,6 +44,7 @@ use hir_def::{ | |||
44 | per_ns::PerNs, | 44 | per_ns::PerNs, |
45 | resolver::{HasResolver, Resolver}, | 45 | resolver::{HasResolver, Resolver}, |
46 | src::HasSource as _, | 46 | src::HasSource as _, |
47 | type_ref::TraitRef, | ||
47 | AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, | 48 | AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, |
48 | DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, | 49 | DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, |
49 | LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, | 50 | LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, |
@@ -51,14 +52,15 @@ use hir_def::{ | |||
51 | }; | 52 | }; |
52 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; | 53 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; |
53 | use hir_ty::{ | 54 | use hir_ty::{ |
54 | autoderef, | 55 | autoderef, could_unify, |
55 | method_resolution::{self, TyFingerprint}, | 56 | method_resolution::{self, def_crates, TyFingerprint}, |
56 | primitive::UintTy, | 57 | primitive::UintTy, |
57 | to_assoc_type_id, | 58 | subst_prefix, |
58 | traits::{FnTrait, Solution, SolutionVariables}, | 59 | traits::FnTrait, |
59 | AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, | 60 | AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, |
60 | DebruijnIndex, InEnvironment, Interner, ProjectionTy, QuantifiedWhereClause, Scalar, | 61 | DebruijnIndex, InEnvironment, Interner, QuantifiedWhereClause, Scalar, Solution, Substitution, |
61 | Substitution, TraitEnvironment, Ty, TyDefId, TyKind, TyVariableKind, WhereClause, | 62 | TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, TyVariableKind, |
63 | WhereClause, | ||
62 | }; | 64 | }; |
63 | use itertools::Itertools; | 65 | use itertools::Itertools; |
64 | use rustc_hash::FxHashSet; | 66 | use rustc_hash::FxHashSet; |
@@ -514,8 +516,8 @@ impl Field { | |||
514 | VariantDef::Union(it) => it.id.into(), | 516 | VariantDef::Union(it) => it.id.into(), |
515 | VariantDef::Variant(it) => it.parent.id.into(), | 517 | VariantDef::Variant(it) => it.parent.id.into(), |
516 | }; | 518 | }; |
517 | let substs = Substitution::type_params(db, generic_def_id); | 519 | let substs = TyBuilder::type_params_subst(db, generic_def_id); |
518 | let ty = db.field_types(var_id)[self.id].clone().subst(&substs); | 520 | let ty = db.field_types(var_id)[self.id].clone().substitute(&Interner, &substs); |
519 | Type::new(db, self.parent.module(db).id.krate(), var_id, ty) | 521 | Type::new(db, self.parent.module(db).id.krate(), var_id, ty) |
520 | } | 522 | } |
521 | 523 | ||
@@ -701,7 +703,7 @@ impl_from!(Struct, Union, Enum for Adt); | |||
701 | impl Adt { | 703 | impl Adt { |
702 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { | 704 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { |
703 | let subst = db.generic_defaults(self.into()); | 705 | let subst = db.generic_defaults(self.into()); |
704 | subst.iter().any(|ty| ty.value.is_unknown()) | 706 | subst.iter().any(|ty| ty.skip_binders().is_unknown()) |
705 | } | 707 | } |
706 | 708 | ||
707 | /// Turns this ADT into a type. Any type parameters of the ADT will be | 709 | /// Turns this ADT into a type. Any type parameters of the ADT will be |
@@ -831,7 +833,7 @@ impl Function { | |||
831 | } | 833 | } |
832 | 834 | ||
833 | pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> { | 835 | pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> { |
834 | if !db.function_data(self.id).has_self_param { | 836 | if !db.function_data(self.id).has_self_param() { |
835 | return None; | 837 | return None; |
836 | } | 838 | } |
837 | Some(SelfParam { func: self.id }) | 839 | Some(SelfParam { func: self.id }) |
@@ -863,7 +865,7 @@ impl Function { | |||
863 | } | 865 | } |
864 | 866 | ||
865 | pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool { | 867 | pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool { |
866 | db.function_data(self.id).qualifier.is_unsafe | 868 | db.function_data(self.id).is_unsafe() |
867 | } | 869 | } |
868 | 870 | ||
869 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | 871 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { |
@@ -877,7 +879,7 @@ impl Function { | |||
877 | /// | 879 | /// |
878 | /// This is false in the case of required (not provided) trait methods. | 880 | /// This is false in the case of required (not provided) trait methods. |
879 | pub fn has_body(self, db: &dyn HirDatabase) -> bool { | 881 | pub fn has_body(self, db: &dyn HirDatabase) -> bool { |
880 | db.function_data(self.id).has_body | 882 | db.function_data(self.id).has_body() |
881 | } | 883 | } |
882 | 884 | ||
883 | /// A textual representation of the HIR of this function for debugging purposes. | 885 | /// A textual representation of the HIR of this function for debugging purposes. |
@@ -956,7 +958,7 @@ impl SelfParam { | |||
956 | func_data | 958 | func_data |
957 | .params | 959 | .params |
958 | .first() | 960 | .first() |
959 | .map(|param| match *param { | 961 | .map(|param| match &**param { |
960 | TypeRef::Reference(.., mutability) => match mutability { | 962 | TypeRef::Reference(.., mutability) => match mutability { |
961 | hir_def::type_ref::Mutability::Shared => Access::Shared, | 963 | hir_def::type_ref::Mutability::Shared => Access::Shared, |
962 | hir_def::type_ref::Mutability::Mut => Access::Exclusive, | 964 | hir_def::type_ref::Mutability::Mut => Access::Exclusive, |
@@ -1010,7 +1012,7 @@ impl Const { | |||
1010 | } | 1012 | } |
1011 | 1013 | ||
1012 | pub fn type_ref(self, db: &dyn HirDatabase) -> TypeRef { | 1014 | pub fn type_ref(self, db: &dyn HirDatabase) -> TypeRef { |
1013 | db.const_data(self.id).type_ref.clone() | 1015 | db.const_data(self.id).type_ref.as_ref().clone() |
1014 | } | 1016 | } |
1015 | } | 1017 | } |
1016 | 1018 | ||
@@ -1088,7 +1090,7 @@ pub struct TypeAlias { | |||
1088 | impl TypeAlias { | 1090 | impl TypeAlias { |
1089 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { | 1091 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { |
1090 | let subst = db.generic_defaults(self.id.into()); | 1092 | let subst = db.generic_defaults(self.id.into()); |
1091 | subst.iter().any(|ty| ty.value.is_unknown()) | 1093 | subst.iter().any(|ty| ty.skip_binders().is_unknown()) |
1092 | } | 1094 | } |
1093 | 1095 | ||
1094 | pub fn module(self, db: &dyn HirDatabase) -> Module { | 1096 | pub fn module(self, db: &dyn HirDatabase) -> Module { |
@@ -1100,7 +1102,7 @@ impl TypeAlias { | |||
1100 | } | 1102 | } |
1101 | 1103 | ||
1102 | pub fn type_ref(self, db: &dyn HirDatabase) -> Option<TypeRef> { | 1104 | pub fn type_ref(self, db: &dyn HirDatabase) -> Option<TypeRef> { |
1103 | db.type_alias_data(self.id).type_ref.clone() | 1105 | db.type_alias_data(self.id).type_ref.as_deref().cloned() |
1104 | } | 1106 | } |
1105 | 1107 | ||
1106 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | 1108 | pub fn ty(self, db: &dyn HirDatabase) -> Type { |
@@ -1128,7 +1130,7 @@ pub struct BuiltinType { | |||
1128 | impl BuiltinType { | 1130 | impl BuiltinType { |
1129 | pub fn ty(self, db: &dyn HirDatabase, module: Module) -> Type { | 1131 | pub fn ty(self, db: &dyn HirDatabase, module: Module) -> Type { |
1130 | let resolver = module.id.resolver(db.upcast()); | 1132 | let resolver = module.id.resolver(db.upcast()); |
1131 | Type::new_with_resolver(db, &resolver, Ty::builtin(self.inner)) | 1133 | Type::new_with_resolver(db, &resolver, TyBuilder::builtin(self.inner)) |
1132 | .expect("crate not present in resolver") | 1134 | .expect("crate not present in resolver") |
1133 | } | 1135 | } |
1134 | 1136 | ||
@@ -1501,8 +1503,8 @@ impl TypeParam { | |||
1501 | let resolver = self.id.parent.resolver(db.upcast()); | 1503 | let resolver = self.id.parent.resolver(db.upcast()); |
1502 | let krate = self.id.parent.module(db.upcast()).krate(); | 1504 | let krate = self.id.parent.module(db.upcast()).krate(); |
1503 | let ty = params.get(local_idx)?.clone(); | 1505 | let ty = params.get(local_idx)?.clone(); |
1504 | let subst = Substitution::type_params(db, self.id.parent); | 1506 | let subst = TyBuilder::type_params_subst(db, self.id.parent); |
1505 | let ty = ty.subst(&subst.prefix(local_idx)); | 1507 | let ty = ty.substitute(&Interner, &subst_prefix(&subst, local_idx)); |
1506 | Some(Type::new_with_resolver_inner(db, krate, &resolver, ty)) | 1508 | Some(Type::new_with_resolver_inner(db, krate, &resolver, ty)) |
1507 | } | 1509 | } |
1508 | } | 1510 | } |
@@ -1567,15 +1569,15 @@ impl Impl { | |||
1567 | } | 1569 | } |
1568 | 1570 | ||
1569 | pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec<Impl> { | 1571 | pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec<Impl> { |
1570 | let def_crates = match ty.def_crates(db, krate) { | 1572 | let def_crates = match def_crates(db, &ty, krate) { |
1571 | Some(def_crates) => def_crates, | 1573 | Some(def_crates) => def_crates, |
1572 | None => return Vec::new(), | 1574 | None => return Vec::new(), |
1573 | }; | 1575 | }; |
1574 | 1576 | ||
1575 | let filter = |impl_def: &Impl| { | 1577 | let filter = |impl_def: &Impl| { |
1576 | let target_ty = impl_def.target_ty(db); | 1578 | let self_ty = impl_def.self_ty(db); |
1577 | let rref = target_ty.remove_ref(); | 1579 | let rref = self_ty.remove_ref(); |
1578 | ty.equals_ctor(rref.as_ref().map_or(&target_ty.ty, |it| &it.ty)) | 1580 | ty.equals_ctor(rref.as_ref().map_or(&self_ty.ty, |it| &it.ty)) |
1579 | }; | 1581 | }; |
1580 | 1582 | ||
1581 | let mut all = Vec::new(); | 1583 | let mut all = Vec::new(); |
@@ -1613,16 +1615,16 @@ impl Impl { | |||
1613 | 1615 | ||
1614 | // FIXME: the return type is wrong. This should be a hir version of | 1616 | // FIXME: the return type is wrong. This should be a hir version of |
1615 | // `TraitRef` (ie, resolved `TypeRef`). | 1617 | // `TraitRef` (ie, resolved `TypeRef`). |
1616 | pub fn target_trait(self, db: &dyn HirDatabase) -> Option<TypeRef> { | 1618 | pub fn trait_(self, db: &dyn HirDatabase) -> Option<TraitRef> { |
1617 | db.impl_data(self.id).target_trait.clone() | 1619 | db.impl_data(self.id).target_trait.as_deref().cloned() |
1618 | } | 1620 | } |
1619 | 1621 | ||
1620 | pub fn target_ty(self, db: &dyn HirDatabase) -> Type { | 1622 | pub fn self_ty(self, db: &dyn HirDatabase) -> Type { |
1621 | let impl_data = db.impl_data(self.id); | 1623 | let impl_data = db.impl_data(self.id); |
1622 | let resolver = self.id.resolver(db.upcast()); | 1624 | let resolver = self.id.resolver(db.upcast()); |
1623 | let krate = self.id.lookup(db.upcast()).container.krate(); | 1625 | let krate = self.id.lookup(db.upcast()).container.krate(); |
1624 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | 1626 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); |
1625 | let ty = ctx.lower_ty(&impl_data.target_type); | 1627 | let ty = ctx.lower_ty(&impl_data.self_ty); |
1626 | Type::new_with_resolver_inner(db, krate, &resolver, ty) | 1628 | Type::new_with_resolver_inner(db, krate, &resolver, ty) |
1627 | } | 1629 | } |
1628 | 1630 | ||
@@ -1702,30 +1704,29 @@ impl Type { | |||
1702 | fn from_def( | 1704 | fn from_def( |
1703 | db: &dyn HirDatabase, | 1705 | db: &dyn HirDatabase, |
1704 | krate: CrateId, | 1706 | krate: CrateId, |
1705 | def: impl HasResolver + Into<TyDefId> + Into<GenericDefId>, | 1707 | def: impl HasResolver + Into<TyDefId>, |
1706 | ) -> Type { | 1708 | ) -> Type { |
1707 | let substs = Substitution::build_for_def(db, def).fill_with_unknown().build(); | 1709 | let ty = TyBuilder::def_ty(db, def.into()).fill_with_unknown().build(); |
1708 | let ty = db.ty(def.into()).subst(&substs); | ||
1709 | Type::new(db, krate, def, ty) | 1710 | Type::new(db, krate, def, ty) |
1710 | } | 1711 | } |
1711 | 1712 | ||
1712 | pub fn is_unit(&self) -> bool { | 1713 | pub fn is_unit(&self) -> bool { |
1713 | matches!(self.ty.interned(&Interner), TyKind::Tuple(0, ..)) | 1714 | matches!(self.ty.kind(&Interner), TyKind::Tuple(0, ..)) |
1714 | } | 1715 | } |
1715 | pub fn is_bool(&self) -> bool { | 1716 | pub fn is_bool(&self) -> bool { |
1716 | matches!(self.ty.interned(&Interner), TyKind::Scalar(Scalar::Bool)) | 1717 | matches!(self.ty.kind(&Interner), TyKind::Scalar(Scalar::Bool)) |
1717 | } | 1718 | } |
1718 | 1719 | ||
1719 | pub fn is_mutable_reference(&self) -> bool { | 1720 | pub fn is_mutable_reference(&self) -> bool { |
1720 | matches!(self.ty.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..)) | 1721 | matches!(self.ty.kind(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..)) |
1721 | } | 1722 | } |
1722 | 1723 | ||
1723 | pub fn is_usize(&self) -> bool { | 1724 | pub fn is_usize(&self) -> bool { |
1724 | matches!(self.ty.interned(&Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize))) | 1725 | matches!(self.ty.kind(&Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize))) |
1725 | } | 1726 | } |
1726 | 1727 | ||
1727 | pub fn remove_ref(&self) -> Option<Type> { | 1728 | pub fn remove_ref(&self) -> Option<Type> { |
1728 | match &self.ty.interned(&Interner) { | 1729 | match &self.ty.kind(&Interner) { |
1729 | TyKind::Ref(.., ty) => Some(self.derived(ty.clone())), | 1730 | TyKind::Ref(.., ty) => Some(self.derived(ty.clone())), |
1730 | _ => None, | 1731 | _ => None, |
1731 | } | 1732 | } |
@@ -1784,16 +1785,13 @@ impl Type { | |||
1784 | } | 1785 | } |
1785 | 1786 | ||
1786 | pub fn impls_trait(&self, db: &dyn HirDatabase, trait_: Trait, args: &[Type]) -> bool { | 1787 | pub fn impls_trait(&self, db: &dyn HirDatabase, trait_: Trait, args: &[Type]) -> bool { |
1787 | let trait_ref = hir_ty::TraitRef { | 1788 | let trait_ref = TyBuilder::trait_ref(db, trait_.id) |
1788 | trait_id: hir_ty::to_chalk_trait_id(trait_.id), | 1789 | .push(self.ty.clone()) |
1789 | substitution: Substitution::build_for_def(db, trait_.id) | 1790 | .fill(args.iter().map(|t| t.ty.clone())) |
1790 | .push(self.ty.clone()) | 1791 | .build(); |
1791 | .fill(args.iter().map(|t| t.ty.clone())) | ||
1792 | .build(), | ||
1793 | }; | ||
1794 | 1792 | ||
1795 | let goal = Canonical { | 1793 | let goal = Canonical { |
1796 | value: hir_ty::InEnvironment::new(self.env.env.clone(), trait_ref.cast(&Interner)), | 1794 | value: hir_ty::InEnvironment::new(&self.env.env, trait_ref.cast(&Interner)), |
1797 | binders: CanonicalVarKinds::empty(&Interner), | 1795 | binders: CanonicalVarKinds::empty(&Interner), |
1798 | }; | 1796 | }; |
1799 | 1797 | ||
@@ -1803,22 +1801,18 @@ impl Type { | |||
1803 | pub fn normalize_trait_assoc_type( | 1801 | pub fn normalize_trait_assoc_type( |
1804 | &self, | 1802 | &self, |
1805 | db: &dyn HirDatabase, | 1803 | db: &dyn HirDatabase, |
1806 | trait_: Trait, | ||
1807 | args: &[Type], | 1804 | args: &[Type], |
1808 | alias: TypeAlias, | 1805 | alias: TypeAlias, |
1809 | ) -> Option<Type> { | 1806 | ) -> Option<Type> { |
1810 | let subst = Substitution::build_for_def(db, trait_.id) | 1807 | let projection = TyBuilder::assoc_type_projection(db, alias.id) |
1811 | .push(self.ty.clone()) | 1808 | .push(self.ty.clone()) |
1812 | .fill(args.iter().map(|t| t.ty.clone())) | 1809 | .fill(args.iter().map(|t| t.ty.clone())) |
1813 | .build(); | 1810 | .build(); |
1814 | let goal = Canonical::new( | 1811 | let goal = hir_ty::make_canonical( |
1815 | InEnvironment::new( | 1812 | InEnvironment::new( |
1816 | self.env.env.clone(), | 1813 | &self.env.env, |
1817 | AliasEq { | 1814 | AliasEq { |
1818 | alias: AliasTy::Projection(ProjectionTy { | 1815 | alias: AliasTy::Projection(projection), |
1819 | associated_ty_id: to_assoc_type_id(alias.id), | ||
1820 | substitution: subst, | ||
1821 | }), | ||
1822 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) | 1816 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) |
1823 | .intern(&Interner), | 1817 | .intern(&Interner), |
1824 | } | 1818 | } |
@@ -1828,9 +1822,12 @@ impl Type { | |||
1828 | ); | 1822 | ); |
1829 | 1823 | ||
1830 | match db.trait_solve(self.krate, goal)? { | 1824 | match db.trait_solve(self.krate, goal)? { |
1831 | Solution::Unique(SolutionVariables(subst)) => { | 1825 | Solution::Unique(s) => s |
1832 | subst.value.first().map(|ty| self.derived(ty.clone())) | 1826 | .value |
1833 | } | 1827 | .subst |
1828 | .interned() | ||
1829 | .first() | ||
1830 | .map(|ty| self.derived(ty.assert_ty_ref(&Interner).clone())), | ||
1834 | Solution::Ambig(_) => None, | 1831 | Solution::Ambig(_) => None, |
1835 | } | 1832 | } |
1836 | } | 1833 | } |
@@ -1852,15 +1849,15 @@ impl Type { | |||
1852 | } | 1849 | } |
1853 | 1850 | ||
1854 | pub fn is_closure(&self) -> bool { | 1851 | pub fn is_closure(&self) -> bool { |
1855 | matches!(&self.ty.interned(&Interner), TyKind::Closure { .. }) | 1852 | matches!(&self.ty.kind(&Interner), TyKind::Closure { .. }) |
1856 | } | 1853 | } |
1857 | 1854 | ||
1858 | pub fn is_fn(&self) -> bool { | 1855 | pub fn is_fn(&self) -> bool { |
1859 | matches!(&self.ty.interned(&Interner), TyKind::FnDef(..) | TyKind::Function { .. }) | 1856 | matches!(&self.ty.kind(&Interner), TyKind::FnDef(..) | TyKind::Function { .. }) |
1860 | } | 1857 | } |
1861 | 1858 | ||
1862 | pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { | 1859 | pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { |
1863 | let adt_id = match self.ty.interned(&Interner) { | 1860 | let adt_id = match self.ty.kind(&Interner) { |
1864 | &TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, | 1861 | &TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, |
1865 | _ => return false, | 1862 | _ => return false, |
1866 | }; | 1863 | }; |
@@ -1873,27 +1870,30 @@ impl Type { | |||
1873 | } | 1870 | } |
1874 | 1871 | ||
1875 | pub fn is_raw_ptr(&self) -> bool { | 1872 | pub fn is_raw_ptr(&self) -> bool { |
1876 | matches!(&self.ty.interned(&Interner), TyKind::Raw(..)) | 1873 | matches!(&self.ty.kind(&Interner), TyKind::Raw(..)) |
1877 | } | 1874 | } |
1878 | 1875 | ||
1879 | pub fn contains_unknown(&self) -> bool { | 1876 | pub fn contains_unknown(&self) -> bool { |
1880 | return go(&self.ty); | 1877 | return go(&self.ty); |
1881 | 1878 | ||
1882 | fn go(ty: &Ty) -> bool { | 1879 | fn go(ty: &Ty) -> bool { |
1883 | match ty.interned(&Interner) { | 1880 | match ty.kind(&Interner) { |
1884 | TyKind::Unknown => true, | 1881 | TyKind::Error => true, |
1885 | 1882 | ||
1886 | TyKind::Adt(_, substs) | 1883 | TyKind::Adt(_, substs) |
1887 | | TyKind::AssociatedType(_, substs) | 1884 | | TyKind::AssociatedType(_, substs) |
1888 | | TyKind::Tuple(_, substs) | 1885 | | TyKind::Tuple(_, substs) |
1889 | | TyKind::OpaqueType(_, substs) | 1886 | | TyKind::OpaqueType(_, substs) |
1890 | | TyKind::FnDef(_, substs) | 1887 | | TyKind::FnDef(_, substs) |
1891 | | TyKind::Closure(_, substs) => substs.iter().any(go), | 1888 | | TyKind::Closure(_, substs) => { |
1892 | 1889 | substs.iter(&Interner).filter_map(|a| a.ty(&Interner)).any(go) | |
1893 | TyKind::Array(ty) | TyKind::Slice(ty) | TyKind::Raw(_, ty) | TyKind::Ref(_, ty) => { | ||
1894 | go(ty) | ||
1895 | } | 1890 | } |
1896 | 1891 | ||
1892 | TyKind::Array(ty, _) | ||
1893 | | TyKind::Slice(ty) | ||
1894 | | TyKind::Raw(_, ty) | ||
1895 | | TyKind::Ref(_, _, ty) => go(ty), | ||
1896 | |||
1897 | TyKind::Scalar(_) | 1897 | TyKind::Scalar(_) |
1898 | | TyKind::Str | 1898 | | TyKind::Str |
1899 | | TyKind::Never | 1899 | | TyKind::Never |
@@ -1903,13 +1903,13 @@ impl Type { | |||
1903 | | TyKind::Dyn(_) | 1903 | | TyKind::Dyn(_) |
1904 | | TyKind::Function(_) | 1904 | | TyKind::Function(_) |
1905 | | TyKind::Alias(_) | 1905 | | TyKind::Alias(_) |
1906 | | TyKind::ForeignType(_) => false, | 1906 | | TyKind::Foreign(_) => false, |
1907 | } | 1907 | } |
1908 | } | 1908 | } |
1909 | } | 1909 | } |
1910 | 1910 | ||
1911 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { | 1911 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { |
1912 | let (variant_id, substs) = match self.ty.interned(&Interner) { | 1912 | let (variant_id, substs) = match self.ty.kind(&Interner) { |
1913 | &TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs), | 1913 | &TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs), |
1914 | &TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs), | 1914 | &TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs), |
1915 | _ => return Vec::new(), | 1915 | _ => return Vec::new(), |
@@ -1919,15 +1919,18 @@ impl Type { | |||
1919 | .iter() | 1919 | .iter() |
1920 | .map(|(local_id, ty)| { | 1920 | .map(|(local_id, ty)| { |
1921 | let def = Field { parent: variant_id.into(), id: local_id }; | 1921 | let def = Field { parent: variant_id.into(), id: local_id }; |
1922 | let ty = ty.clone().subst(substs); | 1922 | let ty = ty.clone().substitute(&Interner, substs); |
1923 | (def, self.derived(ty)) | 1923 | (def, self.derived(ty)) |
1924 | }) | 1924 | }) |
1925 | .collect() | 1925 | .collect() |
1926 | } | 1926 | } |
1927 | 1927 | ||
1928 | pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { | 1928 | pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { |
1929 | if let TyKind::Tuple(_, substs) = &self.ty.interned(&Interner) { | 1929 | if let TyKind::Tuple(_, substs) = &self.ty.kind(&Interner) { |
1930 | substs.iter().map(|ty| self.derived(ty.clone())).collect() | 1930 | substs |
1931 | .iter(&Interner) | ||
1932 | .map(|ty| self.derived(ty.assert_ty_ref(&Interner).clone())) | ||
1933 | .collect() | ||
1931 | } else { | 1934 | } else { |
1932 | Vec::new() | 1935 | Vec::new() |
1933 | } | 1936 | } |
@@ -1953,7 +1956,7 @@ impl Type { | |||
1953 | krate: Crate, | 1956 | krate: Crate, |
1954 | mut callback: impl FnMut(AssocItem) -> Option<T>, | 1957 | mut callback: impl FnMut(AssocItem) -> Option<T>, |
1955 | ) -> Option<T> { | 1958 | ) -> Option<T> { |
1956 | for krate in self.ty.def_crates(db, krate.id)? { | 1959 | for krate in def_crates(db, &self.ty, krate.id)? { |
1957 | let impls = db.inherent_impls_in_crate(krate); | 1960 | let impls = db.inherent_impls_in_crate(krate); |
1958 | 1961 | ||
1959 | for impl_def in impls.for_self_ty(&self.ty) { | 1962 | for impl_def in impls.for_self_ty(&self.ty) { |
@@ -1970,10 +1973,11 @@ impl Type { | |||
1970 | pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { | 1973 | pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { |
1971 | self.ty | 1974 | self.ty |
1972 | .strip_references() | 1975 | .strip_references() |
1973 | .substs() | 1976 | .as_adt() |
1974 | .into_iter() | 1977 | .into_iter() |
1975 | .flat_map(|substs| substs.iter()) | 1978 | .flat_map(|(_, substs)| substs.iter(&Interner)) |
1976 | .map(move |ty| self.derived(ty.clone())) | 1979 | .filter_map(|arg| arg.ty(&Interner).cloned()) |
1980 | .map(move |ty| self.derived(ty)) | ||
1977 | } | 1981 | } |
1978 | 1982 | ||
1979 | pub fn iterate_method_candidates<T>( | 1983 | pub fn iterate_method_candidates<T>( |
@@ -2079,7 +2083,7 @@ impl Type { | |||
2079 | substs: &Substitution, | 2083 | substs: &Substitution, |
2080 | cb: &mut impl FnMut(Type), | 2084 | cb: &mut impl FnMut(Type), |
2081 | ) { | 2085 | ) { |
2082 | for ty in substs.iter() { | 2086 | for ty in substs.iter(&Interner).filter_map(|a| a.ty(&Interner)) { |
2083 | walk_type(db, &type_.derived(ty.clone()), cb); | 2087 | walk_type(db, &type_.derived(ty.clone()), cb); |
2084 | } | 2088 | } |
2085 | } | 2089 | } |
@@ -2095,7 +2099,12 @@ impl Type { | |||
2095 | WhereClause::Implemented(trait_ref) => { | 2099 | WhereClause::Implemented(trait_ref) => { |
2096 | cb(type_.clone()); | 2100 | cb(type_.clone()); |
2097 | // skip the self type. it's likely the type we just got the bounds from | 2101 | // skip the self type. it's likely the type we just got the bounds from |
2098 | for ty in trait_ref.substitution.iter().skip(1) { | 2102 | for ty in trait_ref |
2103 | .substitution | ||
2104 | .iter(&Interner) | ||
2105 | .skip(1) | ||
2106 | .filter_map(|a| a.ty(&Interner)) | ||
2107 | { | ||
2099 | walk_type(db, &type_.derived(ty.clone()), cb); | 2108 | walk_type(db, &type_.derived(ty.clone()), cb); |
2100 | } | 2109 | } |
2101 | } | 2110 | } |
@@ -2106,19 +2115,23 @@ impl Type { | |||
2106 | 2115 | ||
2107 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { | 2116 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { |
2108 | let ty = type_.ty.strip_references(); | 2117 | let ty = type_.ty.strip_references(); |
2109 | match ty.interned(&Interner) { | 2118 | match ty.kind(&Interner) { |
2110 | TyKind::Adt(..) => { | 2119 | TyKind::Adt(_, substs) => { |
2111 | cb(type_.derived(ty.clone())); | 2120 | cb(type_.derived(ty.clone())); |
2121 | walk_substs(db, type_, &substs, cb); | ||
2112 | } | 2122 | } |
2113 | TyKind::AssociatedType(..) => { | 2123 | TyKind::AssociatedType(_, substs) => { |
2114 | if let Some(_) = ty.associated_type_parent_trait(db) { | 2124 | if let Some(_) = ty.associated_type_parent_trait(db) { |
2115 | cb(type_.derived(ty.clone())); | 2125 | cb(type_.derived(ty.clone())); |
2116 | } | 2126 | } |
2127 | walk_substs(db, type_, &substs, cb); | ||
2117 | } | 2128 | } |
2118 | TyKind::OpaqueType(..) => { | 2129 | TyKind::OpaqueType(_, subst) => { |
2119 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 2130 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
2120 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | 2131 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); |
2121 | } | 2132 | } |
2133 | |||
2134 | walk_substs(db, type_, subst, cb); | ||
2122 | } | 2135 | } |
2123 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { | 2136 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { |
2124 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 2137 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
@@ -2141,19 +2154,32 @@ impl Type { | |||
2141 | ); | 2154 | ); |
2142 | } | 2155 | } |
2143 | 2156 | ||
2144 | TyKind::Ref(_, ty) | TyKind::Raw(_, ty) | TyKind::Array(ty) | TyKind::Slice(ty) => { | 2157 | TyKind::Ref(_, _, ty) |
2158 | | TyKind::Raw(_, ty) | ||
2159 | | TyKind::Array(ty, _) | ||
2160 | | TyKind::Slice(ty) => { | ||
2145 | walk_type(db, &type_.derived(ty.clone()), cb); | 2161 | walk_type(db, &type_.derived(ty.clone()), cb); |
2146 | } | 2162 | } |
2147 | 2163 | ||
2164 | TyKind::FnDef(_, substs) | ||
2165 | | TyKind::Tuple(_, substs) | ||
2166 | | TyKind::Closure(.., substs) => { | ||
2167 | walk_substs(db, type_, &substs, cb); | ||
2168 | } | ||
2169 | TyKind::Function(hir_ty::FnPointer { substitution, .. }) => { | ||
2170 | walk_substs(db, type_, &substitution.0, cb); | ||
2171 | } | ||
2172 | |||
2148 | _ => {} | 2173 | _ => {} |
2149 | } | 2174 | } |
2150 | if let Some(substs) = ty.substs() { | ||
2151 | walk_substs(db, type_, &substs, cb); | ||
2152 | } | ||
2153 | } | 2175 | } |
2154 | 2176 | ||
2155 | walk_type(db, self, &mut cb); | 2177 | walk_type(db, self, &mut cb); |
2156 | } | 2178 | } |
2179 | |||
2180 | pub fn could_unify_with(&self, other: &Type) -> bool { | ||
2181 | could_unify(&self.ty, &other.ty) | ||
2182 | } | ||
2157 | } | 2183 | } |
2158 | 2184 | ||
2159 | // FIXME: closures | 2185 | // FIXME: closures |
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 1198e3f0b..7955bf0b5 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs | |||
@@ -76,9 +76,11 @@ impl PathResolution { | |||
76 | pub fn assoc_type_shorthand_candidates<R>( | 76 | pub fn assoc_type_shorthand_candidates<R>( |
77 | &self, | 77 | &self, |
78 | db: &dyn HirDatabase, | 78 | db: &dyn HirDatabase, |
79 | mut cb: impl FnMut(TypeAlias) -> Option<R>, | 79 | mut cb: impl FnMut(&Name, TypeAlias) -> Option<R>, |
80 | ) -> Option<R> { | 80 | ) -> Option<R> { |
81 | associated_type_shorthand_candidates(db, self.in_type_ns()?, |_, _, id| cb(id.into())) | 81 | associated_type_shorthand_candidates(db, self.in_type_ns()?, |name, _, id| { |
82 | cb(name, id.into()) | ||
83 | }) | ||
82 | } | 84 | } |
83 | } | 85 | } |
84 | 86 | ||
@@ -445,7 +447,7 @@ impl<'db> SemanticsImpl<'db> { | |||
445 | } | 447 | } |
446 | }; | 448 | }; |
447 | gpl.lifetime_params() | 449 | gpl.lifetime_params() |
448 | .find(|tp| tp.lifetime().as_ref().map(|lt| lt.text()) == Some(text)) | 450 | .find(|tp| tp.lifetime().as_ref().map(|lt| lt.text()).as_ref() == Some(&text)) |
449 | })?; | 451 | })?; |
450 | let src = self.find_file(lifetime_param.syntax().clone()).with_value(lifetime_param); | 452 | let src = self.find_file(lifetime_param.syntax().clone()).with_value(lifetime_param); |
451 | ToDef::to_def(self, src) | 453 | ToDef::to_def(self, src) |
@@ -492,9 +494,9 @@ impl<'db> SemanticsImpl<'db> { | |||
492 | fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> { | 494 | fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> { |
493 | // FIXME: this erases Substs | 495 | // FIXME: this erases Substs |
494 | let func = self.resolve_method_call(call)?; | 496 | let func = self.resolve_method_call(call)?; |
495 | let ty = self.db.value_ty(func.into()); | 497 | let (ty, _) = self.db.value_ty(func.into()).into_value_and_skipped_binders(); |
496 | let resolver = self.analyze(call.syntax()).resolver; | 498 | let resolver = self.analyze(call.syntax()).resolver; |
497 | let ty = Type::new_with_resolver(self.db, &resolver, ty.value)?; | 499 | let ty = Type::new_with_resolver(self.db, &resolver, ty)?; |
498 | let mut res = ty.as_callable(self.db)?; | 500 | let mut res = ty.as_callable(self.db)?; |
499 | res.is_bound_method = true; | 501 | res.is_bound_method = true; |
500 | Some(res) | 502 | Some(res) |
@@ -768,7 +770,7 @@ to_def_impls![ | |||
768 | (crate::TypeParam, ast::TypeParam, type_param_to_def), | 770 | (crate::TypeParam, ast::TypeParam, type_param_to_def), |
769 | (crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def), | 771 | (crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def), |
770 | (crate::ConstParam, ast::ConstParam, const_param_to_def), | 772 | (crate::ConstParam, ast::ConstParam, const_param_to_def), |
771 | (crate::MacroDef, ast::MacroRules, macro_rules_to_def), | 773 | (crate::MacroDef, ast::Macro, macro_to_def), |
772 | (crate::Local, ast::IdentPat, bind_pat_to_def), | 774 | (crate::Local, ast::IdentPat, bind_pat_to_def), |
773 | (crate::Local, ast::SelfParam, self_param_to_def), | 775 | (crate::Local, ast::SelfParam, self_param_to_def), |
774 | (crate::Label, ast::Label, label_to_def), | 776 | (crate::Label, ast::Label, label_to_def), |
diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index 762809fcd..9a5a2255f 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs | |||
@@ -191,10 +191,7 @@ impl SourceToDefCtx<'_, '_> { | |||
191 | } | 191 | } |
192 | 192 | ||
193 | // FIXME: use DynMap as well? | 193 | // FIXME: use DynMap as well? |
194 | pub(super) fn macro_rules_to_def( | 194 | pub(super) fn macro_to_def(&mut self, src: InFile<ast::Macro>) -> Option<MacroDefId> { |
195 | &mut self, | ||
196 | src: InFile<ast::MacroRules>, | ||
197 | ) -> Option<MacroDefId> { | ||
198 | let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value); | 195 | let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value); |
199 | let ast_id = AstId::new(src.file_id, file_ast_id.upcast()); | 196 | let ast_id = AstId::new(src.file_id, file_ast_id.upcast()); |
200 | let kind = MacroDefKind::Declarative(ast_id); | 197 | let kind = MacroDefKind::Declarative(ast_id); |
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index 37d162b32..847d2537d 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs | |||
@@ -20,7 +20,7 @@ use hir_def::{ | |||
20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; | 20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; |
21 | use hir_ty::{ | 21 | use hir_ty::{ |
22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, | 22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, |
23 | InferenceResult, Substitution, | 23 | InferenceResult, Interner, Substitution, TyExt, TyLoweringContext, |
24 | }; | 24 | }; |
25 | use syntax::{ | 25 | use syntax::{ |
26 | ast::{self, AstNode}, | 26 | ast::{self, AstNode}, |
@@ -161,14 +161,15 @@ impl SourceAnalyzer { | |||
161 | db: &dyn HirDatabase, | 161 | db: &dyn HirDatabase, |
162 | field: &ast::RecordExprField, | 162 | field: &ast::RecordExprField, |
163 | ) -> Option<(Field, Option<Local>)> { | 163 | ) -> Option<(Field, Option<Local>)> { |
164 | let expr_id = | 164 | let record_expr = ast::RecordExpr::cast(field.syntax().parent().and_then(|p| p.parent())?)?; |
165 | self.body_source_map.as_ref()?.node_field(InFile::new(self.file_id, field))?; | 165 | let expr = ast::Expr::from(record_expr); |
166 | let expr_id = self.body_source_map.as_ref()?.node_expr(InFile::new(self.file_id, &expr))?; | ||
166 | 167 | ||
168 | let local_name = field.field_name()?.as_name(); | ||
167 | let local = if field.name_ref().is_some() { | 169 | let local = if field.name_ref().is_some() { |
168 | None | 170 | None |
169 | } else { | 171 | } else { |
170 | let local_name = field.field_name()?.as_name(); | 172 | let path = ModPath::from_segments(PathKind::Plain, once(local_name.clone())); |
171 | let path = ModPath::from_segments(PathKind::Plain, once(local_name)); | ||
172 | match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) { | 173 | match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) { |
173 | Some(ValueNs::LocalBinding(pat_id)) => { | 174 | Some(ValueNs::LocalBinding(pat_id)) => { |
174 | Some(Local { pat_id, parent: self.resolver.body_owner()? }) | 175 | Some(Local { pat_id, parent: self.resolver.body_owner()? }) |
@@ -176,18 +177,24 @@ impl SourceAnalyzer { | |||
176 | _ => None, | 177 | _ => None, |
177 | } | 178 | } |
178 | }; | 179 | }; |
179 | let struct_field = self.infer.as_ref()?.record_field_resolution(expr_id)?; | 180 | let variant = self.infer.as_ref()?.variant_resolution_for_expr(expr_id)?; |
180 | Some((struct_field.into(), local)) | 181 | let variant_data = variant.variant_data(db.upcast()); |
182 | let field = FieldId { parent: variant, local_id: variant_data.field(&local_name)? }; | ||
183 | Some((field.into(), local)) | ||
181 | } | 184 | } |
182 | 185 | ||
183 | pub(crate) fn resolve_record_pat_field( | 186 | pub(crate) fn resolve_record_pat_field( |
184 | &self, | 187 | &self, |
185 | _db: &dyn HirDatabase, | 188 | db: &dyn HirDatabase, |
186 | field: &ast::RecordPatField, | 189 | field: &ast::RecordPatField, |
187 | ) -> Option<Field> { | 190 | ) -> Option<Field> { |
188 | let pat_id = self.pat_id(&field.pat()?)?; | 191 | let field_name = field.field_name()?.as_name(); |
189 | let struct_field = self.infer.as_ref()?.record_pat_field_resolution(pat_id)?; | 192 | let record_pat = ast::RecordPat::cast(field.syntax().parent().and_then(|p| p.parent())?)?; |
190 | Some(struct_field.into()) | 193 | let pat_id = self.pat_id(&record_pat.into())?; |
194 | let variant = self.infer.as_ref()?.variant_resolution_for_pat(pat_id)?; | ||
195 | let variant_data = variant.variant_data(db.upcast()); | ||
196 | let field = FieldId { parent: variant, local_id: variant_data.field(&field_name)? }; | ||
197 | Some(field.into()) | ||
191 | } | 198 | } |
192 | 199 | ||
193 | pub(crate) fn resolve_macro_call( | 200 | pub(crate) fn resolve_macro_call( |
@@ -299,7 +306,7 @@ impl SourceAnalyzer { | |||
299 | let infer = self.infer.as_ref()?; | 306 | let infer = self.infer.as_ref()?; |
300 | 307 | ||
301 | let expr_id = self.expr_id(db, &literal.clone().into())?; | 308 | let expr_id = self.expr_id(db, &literal.clone().into())?; |
302 | let substs = infer.type_of_expr[expr_id].substs()?; | 309 | let substs = infer.type_of_expr[expr_id].as_adt()?.1; |
303 | 310 | ||
304 | let (variant, missing_fields, _exhaustive) = | 311 | let (variant, missing_fields, _exhaustive) = |
305 | record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; | 312 | record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; |
@@ -317,7 +324,7 @@ impl SourceAnalyzer { | |||
317 | let infer = self.infer.as_ref()?; | 324 | let infer = self.infer.as_ref()?; |
318 | 325 | ||
319 | let pat_id = self.pat_id(&pattern.clone().into())?; | 326 | let pat_id = self.pat_id(&pattern.clone().into())?; |
320 | let substs = infer.type_of_pat[pat_id].substs()?; | 327 | let substs = infer.type_of_pat[pat_id].as_adt()?.1; |
321 | 328 | ||
322 | let (variant, missing_fields, _exhaustive) = | 329 | let (variant, missing_fields, _exhaustive) = |
323 | record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; | 330 | record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; |
@@ -339,7 +346,7 @@ impl SourceAnalyzer { | |||
339 | .into_iter() | 346 | .into_iter() |
340 | .map(|local_id| { | 347 | .map(|local_id| { |
341 | let field = FieldId { parent: variant, local_id }; | 348 | let field = FieldId { parent: variant, local_id }; |
342 | let ty = field_types[local_id].clone().subst(substs); | 349 | let ty = field_types[local_id].clone().substitute(&Interner, substs); |
343 | (field.into(), Type::new_with_resolver_inner(db, krate, &self.resolver, ty)) | 350 | (field.into(), Type::new_with_resolver_inner(db, krate, &self.resolver, ty)) |
344 | }) | 351 | }) |
345 | .collect() | 352 | .collect() |
@@ -466,7 +473,21 @@ fn resolve_hir_path_( | |||
466 | prefer_value_ns: bool, | 473 | prefer_value_ns: bool, |
467 | ) -> Option<PathResolution> { | 474 | ) -> Option<PathResolution> { |
468 | let types = || { | 475 | let types = || { |
469 | resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty { | 476 | let (ty, unresolved) = match path.type_anchor() { |
477 | Some(type_ref) => { | ||
478 | let (_, res) = TyLoweringContext::new(db, resolver).lower_ty_ext(type_ref); | ||
479 | res.map(|ty_ns| (ty_ns, path.segments().first())) | ||
480 | } | ||
481 | None => { | ||
482 | let (ty, remaining) = | ||
483 | resolver.resolve_path_in_type_ns(db.upcast(), path.mod_path())?; | ||
484 | match remaining { | ||
485 | Some(remaining) if remaining > 1 => None, | ||
486 | _ => Some((ty, path.segments().get(1))), | ||
487 | } | ||
488 | } | ||
489 | }?; | ||
490 | let res = match ty { | ||
470 | TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), | 491 | TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), |
471 | TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), | 492 | TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), |
472 | TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { | 493 | TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { |
@@ -476,7 +497,17 @@ fn resolve_hir_path_( | |||
476 | TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), | 497 | TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), |
477 | TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), | 498 | TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), |
478 | TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), | 499 | TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), |
479 | }) | 500 | }; |
501 | match unresolved { | ||
502 | Some(unresolved) => res | ||
503 | .assoc_type_shorthand_candidates(db, |name, alias| { | ||
504 | (name == unresolved.name).then(|| alias) | ||
505 | }) | ||
506 | .map(TypeAlias::from) | ||
507 | .map(Into::into) | ||
508 | .map(PathResolution::Def), | ||
509 | None => Some(res), | ||
510 | } | ||
480 | }; | 511 | }; |
481 | 512 | ||
482 | let body_owner = resolver.body_owner(); | 513 | let body_owner = resolver.body_owner(); |
diff --git a/crates/hir_def/Cargo.toml b/crates/hir_def/Cargo.toml index 475d337f3..43324d8d9 100644 --- a/crates/hir_def/Cargo.toml +++ b/crates/hir_def/Cargo.toml | |||
@@ -11,6 +11,7 @@ doctest = false | |||
11 | 11 | ||
12 | [dependencies] | 12 | [dependencies] |
13 | cov-mark = { version = "1.1", features = ["thread-local"] } | 13 | cov-mark = { version = "1.1", features = ["thread-local"] } |
14 | dashmap = { version = "4.0.2", features = ["raw-api"] } | ||
14 | log = "0.4.8" | 15 | log = "0.4.8" |
15 | once_cell = "1.3.1" | 16 | once_cell = "1.3.1" |
16 | rustc-hash = "1.1.0" | 17 | rustc-hash = "1.1.0" |
diff --git a/crates/hir_def/src/adt.rs b/crates/hir_def/src/adt.rs index 58e35353b..402fb1d8d 100644 --- a/crates/hir_def/src/adt.rs +++ b/crates/hir_def/src/adt.rs | |||
@@ -15,6 +15,7 @@ use tt::{Delimiter, DelimiterKind, Leaf, Subtree, TokenTree}; | |||
15 | use crate::{ | 15 | use crate::{ |
16 | body::{CfgExpander, LowerCtx}, | 16 | body::{CfgExpander, LowerCtx}, |
17 | db::DefDatabase, | 17 | db::DefDatabase, |
18 | intern::Interned, | ||
18 | item_tree::{AttrOwner, Field, Fields, ItemTree, ModItem, RawVisibilityId}, | 19 | item_tree::{AttrOwner, Field, Fields, ItemTree, ModItem, RawVisibilityId}, |
19 | src::HasChildSource, | 20 | src::HasChildSource, |
20 | src::HasSource, | 21 | src::HasSource, |
@@ -58,7 +59,7 @@ pub enum VariantData { | |||
58 | #[derive(Debug, Clone, PartialEq, Eq)] | 59 | #[derive(Debug, Clone, PartialEq, Eq)] |
59 | pub struct FieldData { | 60 | pub struct FieldData { |
60 | pub name: Name, | 61 | pub name: Name, |
61 | pub type_ref: TypeRef, | 62 | pub type_ref: Interned<TypeRef>, |
62 | pub visibility: RawVisibility, | 63 | pub visibility: RawVisibility, |
63 | } | 64 | } |
64 | 65 | ||
@@ -292,7 +293,7 @@ fn lower_struct( | |||
292 | || Either::Left(fd.clone()), | 293 | || Either::Left(fd.clone()), |
293 | || FieldData { | 294 | || FieldData { |
294 | name: Name::new_tuple_field(i), | 295 | name: Name::new_tuple_field(i), |
295 | type_ref: TypeRef::from_ast_opt(&ctx, fd.ty()), | 296 | type_ref: Interned::new(TypeRef::from_ast_opt(&ctx, fd.ty())), |
296 | visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), | 297 | visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), |
297 | }, | 298 | }, |
298 | ); | 299 | ); |
@@ -309,7 +310,7 @@ fn lower_struct( | |||
309 | || Either::Right(fd.clone()), | 310 | || Either::Right(fd.clone()), |
310 | || FieldData { | 311 | || FieldData { |
311 | name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), | 312 | name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), |
312 | type_ref: TypeRef::from_ast_opt(&ctx, fd.ty()), | 313 | type_ref: Interned::new(TypeRef::from_ast_opt(&ctx, fd.ty())), |
313 | visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), | 314 | visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), |
314 | }, | 315 | }, |
315 | ); | 316 | ); |
@@ -358,7 +359,7 @@ fn lower_field( | |||
358 | ) -> FieldData { | 359 | ) -> FieldData { |
359 | FieldData { | 360 | FieldData { |
360 | name: field.name.clone(), | 361 | name: field.name.clone(), |
361 | type_ref: item_tree[field.type_ref].clone(), | 362 | type_ref: field.type_ref.clone(), |
362 | visibility: item_tree[override_visibility.unwrap_or(field.visibility)].clone(), | 363 | visibility: item_tree[override_visibility.unwrap_or(field.visibility)].clone(), |
363 | } | 364 | } |
364 | } | 365 | } |
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 52a2bce9b..d9df7564d 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs | |||
@@ -1,6 +1,10 @@ | |||
1 | //! A higher level attributes based on TokenTree, with also some shortcuts. | 1 | //! A higher level attributes based on TokenTree, with also some shortcuts. |
2 | 2 | ||
3 | use std::{ops, sync::Arc}; | 3 | use std::{ |
4 | convert::{TryFrom, TryInto}, | ||
5 | ops, | ||
6 | sync::Arc, | ||
7 | }; | ||
4 | 8 | ||
5 | use base_db::CrateId; | 9 | use base_db::CrateId; |
6 | use cfg::{CfgExpr, CfgOptions}; | 10 | use cfg::{CfgExpr, CfgOptions}; |
@@ -12,12 +16,13 @@ use mbe::ast_to_token_tree; | |||
12 | use smallvec::{smallvec, SmallVec}; | 16 | use smallvec::{smallvec, SmallVec}; |
13 | use syntax::{ | 17 | use syntax::{ |
14 | ast::{self, AstNode, AttrsOwner}, | 18 | ast::{self, AstNode, AttrsOwner}, |
15 | match_ast, AstToken, SmolStr, SyntaxNode, | 19 | match_ast, AstPtr, AstToken, SmolStr, SyntaxNode, TextRange, TextSize, |
16 | }; | 20 | }; |
17 | use tt::Subtree; | 21 | use tt::Subtree; |
18 | 22 | ||
19 | use crate::{ | 23 | use crate::{ |
20 | db::DefDatabase, | 24 | db::DefDatabase, |
25 | intern::Interned, | ||
21 | item_tree::{ItemTreeId, ItemTreeNode}, | 26 | item_tree::{ItemTreeId, ItemTreeNode}, |
22 | nameres::ModuleSource, | 27 | nameres::ModuleSource, |
23 | path::{ModPath, PathKind}, | 28 | path::{ModPath, PathKind}, |
@@ -98,7 +103,7 @@ impl RawAttrs { | |||
98 | Either::Right(comment) => comment.doc_comment().map(|doc| Attr { | 103 | Either::Right(comment) => comment.doc_comment().map(|doc| Attr { |
99 | index: i as u32, | 104 | index: i as u32, |
100 | input: Some(AttrInput::Literal(SmolStr::new(doc))), | 105 | input: Some(AttrInput::Literal(SmolStr::new(doc))), |
101 | path: ModPath::from(hir_expand::name!(doc)), | 106 | path: Interned::new(ModPath::from(hir_expand::name!(doc))), |
102 | }), | 107 | }), |
103 | }) | 108 | }) |
104 | .collect::<Arc<_>>(); | 109 | .collect::<Arc<_>>(); |
@@ -210,12 +215,11 @@ impl Attrs { | |||
210 | let mut res = ArenaMap::default(); | 215 | let mut res = ArenaMap::default(); |
211 | 216 | ||
212 | for (id, fld) in src.value.iter() { | 217 | for (id, fld) in src.value.iter() { |
213 | let attrs = match fld { | 218 | let owner: &dyn AttrsOwner = match fld { |
214 | Either::Left(_tuple) => Attrs::default(), | 219 | Either::Left(tuple) => tuple, |
215 | Either::Right(record) => { | 220 | Either::Right(record) => record, |
216 | RawAttrs::from_attrs_owner(db, src.with_value(record)).filter(db, krate) | ||
217 | } | ||
218 | }; | 221 | }; |
222 | let attrs = RawAttrs::from_attrs_owner(db, src.with_value(owner)).filter(db, krate); | ||
219 | 223 | ||
220 | res.insert(id, attrs); | 224 | res.insert(id, attrs); |
221 | } | 225 | } |
@@ -399,10 +403,14 @@ impl AttrsWithOwner { | |||
399 | return AttrSourceMap { attrs }; | 403 | return AttrSourceMap { attrs }; |
400 | } | 404 | } |
401 | AttrDefId::FieldId(id) => { | 405 | AttrDefId::FieldId(id) => { |
402 | id.parent.child_source(db).map(|source| match &source[id.local_id] { | 406 | let map = db.fields_attrs_source_map(id.parent); |
403 | Either::Left(field) => ast::AttrsOwnerNode::new(field.clone()), | 407 | let file_id = id.parent.file_id(db); |
404 | Either::Right(field) => ast::AttrsOwnerNode::new(field.clone()), | 408 | let root = db.parse_or_expand(file_id).unwrap(); |
405 | }) | 409 | let owner = match &map[id.local_id] { |
410 | Either::Left(it) => ast::AttrsOwnerNode::new(it.to_node(&root)), | ||
411 | Either::Right(it) => ast::AttrsOwnerNode::new(it.to_node(&root)), | ||
412 | }; | ||
413 | InFile::new(file_id, owner) | ||
406 | } | 414 | } |
407 | AttrDefId::AdtId(adt) => match adt { | 415 | AttrDefId::AdtId(adt) => match adt { |
408 | AdtId::StructId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 416 | AdtId::StructId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
@@ -410,10 +418,12 @@ impl AttrsWithOwner { | |||
410 | AdtId::EnumId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 418 | AdtId::EnumId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
411 | }, | 419 | }, |
412 | AttrDefId::FunctionId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 420 | AttrDefId::FunctionId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
413 | AttrDefId::EnumVariantId(id) => id | 421 | AttrDefId::EnumVariantId(id) => { |
414 | .parent | 422 | let map = db.variants_attrs_source_map(id.parent); |
415 | .child_source(db) | 423 | let file_id = id.parent.lookup(db).id.file_id(); |
416 | .map(|source| ast::AttrsOwnerNode::new(source[id.local_id].clone())), | 424 | let root = db.parse_or_expand(file_id).unwrap(); |
425 | InFile::new(file_id, ast::AttrsOwnerNode::new(map[id.local_id].to_node(&root))) | ||
426 | } | ||
417 | AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 427 | AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
418 | AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 428 | AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
419 | AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 429 | AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
@@ -451,6 +461,55 @@ impl AttrsWithOwner { | |||
451 | .collect(), | 461 | .collect(), |
452 | } | 462 | } |
453 | } | 463 | } |
464 | |||
465 | pub fn docs_with_rangemap( | ||
466 | &self, | ||
467 | db: &dyn DefDatabase, | ||
468 | ) -> Option<(Documentation, DocsRangeMap)> { | ||
469 | // FIXME: code duplication in `docs` above | ||
470 | let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? { | ||
471 | AttrInput::Literal(s) => Some((s, attr.index)), | ||
472 | AttrInput::TokenTree(_) => None, | ||
473 | }); | ||
474 | let indent = docs | ||
475 | .clone() | ||
476 | .flat_map(|(s, _)| s.lines()) | ||
477 | .filter(|line| !line.chars().all(|c| c.is_whitespace())) | ||
478 | .map(|line| line.chars().take_while(|c| c.is_whitespace()).count()) | ||
479 | .min() | ||
480 | .unwrap_or(0); | ||
481 | let mut buf = String::new(); | ||
482 | let mut mapping = Vec::new(); | ||
483 | for (doc, idx) in docs { | ||
484 | // str::lines doesn't yield anything for the empty string | ||
485 | if !doc.is_empty() { | ||
486 | for line in doc.split('\n') { | ||
487 | let line = line.trim_end(); | ||
488 | let line_len = line.len(); | ||
489 | let (offset, line) = match line.char_indices().nth(indent) { | ||
490 | Some((offset, _)) => (offset, &line[offset..]), | ||
491 | None => (0, line), | ||
492 | }; | ||
493 | let buf_offset = buf.len(); | ||
494 | buf.push_str(line); | ||
495 | mapping.push(( | ||
496 | TextRange::new(buf_offset.try_into().ok()?, buf.len().try_into().ok()?), | ||
497 | idx, | ||
498 | TextRange::new(offset.try_into().ok()?, line_len.try_into().ok()?), | ||
499 | )); | ||
500 | buf.push('\n'); | ||
501 | } | ||
502 | } else { | ||
503 | buf.push('\n'); | ||
504 | } | ||
505 | } | ||
506 | buf.pop(); | ||
507 | if buf.is_empty() { | ||
508 | None | ||
509 | } else { | ||
510 | Some((Documentation(buf), DocsRangeMap { mapping, source: self.source_map(db).attrs })) | ||
511 | } | ||
512 | } | ||
454 | } | 513 | } |
455 | 514 | ||
456 | fn inner_attributes( | 515 | fn inner_attributes( |
@@ -507,10 +566,48 @@ impl AttrSourceMap { | |||
507 | } | 566 | } |
508 | } | 567 | } |
509 | 568 | ||
569 | /// A struct to map text ranges from [`Documentation`] back to TextRanges in the syntax tree. | ||
570 | pub struct DocsRangeMap { | ||
571 | source: Vec<InFile<Either<ast::Attr, ast::Comment>>>, | ||
572 | // (docstring-line-range, attr_index, attr-string-range) | ||
573 | // a mapping from the text range of a line of the [`Documentation`] to the attribute index and | ||
574 | // the original (untrimmed) syntax doc line | ||
575 | mapping: Vec<(TextRange, u32, TextRange)>, | ||
576 | } | ||
577 | |||
578 | impl DocsRangeMap { | ||
579 | pub fn map(&self, range: TextRange) -> Option<InFile<TextRange>> { | ||
580 | let found = self.mapping.binary_search_by(|(probe, ..)| probe.ordering(range)).ok()?; | ||
581 | let (line_docs_range, idx, original_line_src_range) = self.mapping[found].clone(); | ||
582 | if !line_docs_range.contains_range(range) { | ||
583 | return None; | ||
584 | } | ||
585 | |||
586 | let relative_range = range - line_docs_range.start(); | ||
587 | |||
588 | let &InFile { file_id, value: ref source } = &self.source[idx as usize]; | ||
589 | match source { | ||
590 | Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here | ||
591 | // as well as for whats done in syntax highlight doc injection | ||
592 | Either::Right(comment) => { | ||
593 | let text_range = comment.syntax().text_range(); | ||
594 | let range = TextRange::at( | ||
595 | text_range.start() | ||
596 | + TextSize::try_from(comment.prefix().len()).ok()? | ||
597 | + original_line_src_range.start() | ||
598 | + relative_range.start(), | ||
599 | text_range.len().min(range.len()), | ||
600 | ); | ||
601 | Some(InFile { file_id, value: range }) | ||
602 | } | ||
603 | } | ||
604 | } | ||
605 | } | ||
606 | |||
510 | #[derive(Debug, Clone, PartialEq, Eq)] | 607 | #[derive(Debug, Clone, PartialEq, Eq)] |
511 | pub struct Attr { | 608 | pub struct Attr { |
512 | index: u32, | 609 | index: u32, |
513 | pub(crate) path: ModPath, | 610 | pub(crate) path: Interned<ModPath>, |
514 | pub(crate) input: Option<AttrInput>, | 611 | pub(crate) input: Option<AttrInput>, |
515 | } | 612 | } |
516 | 613 | ||
@@ -524,7 +621,7 @@ pub enum AttrInput { | |||
524 | 621 | ||
525 | impl Attr { | 622 | impl Attr { |
526 | fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> { | 623 | fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> { |
527 | let path = ModPath::from_src(ast.path()?, hygiene)?; | 624 | let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?); |
528 | let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { | 625 | let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { |
529 | let value = match lit.kind() { | 626 | let value = match lit.kind() { |
530 | ast::LiteralKind::String(string) => string.value()?.into(), | 627 | ast::LiteralKind::String(string) => string.value()?.into(), |
@@ -532,7 +629,7 @@ impl Attr { | |||
532 | }; | 629 | }; |
533 | Some(AttrInput::Literal(value)) | 630 | Some(AttrInput::Literal(value)) |
534 | } else if let Some(tt) = ast.token_tree() { | 631 | } else if let Some(tt) = ast.token_tree() { |
535 | Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0)) | 632 | Some(AttrInput::TokenTree(ast_to_token_tree(&tt).0)) |
536 | } else { | 633 | } else { |
537 | None | 634 | None |
538 | }; | 635 | }; |
@@ -655,3 +752,36 @@ fn collect_attrs( | |||
655 | 752 | ||
656 | attrs.into_iter().map(|(_, attr)| attr) | 753 | attrs.into_iter().map(|(_, attr)| attr) |
657 | } | 754 | } |
755 | |||
756 | pub(crate) fn variants_attrs_source_map( | ||
757 | db: &dyn DefDatabase, | ||
758 | def: EnumId, | ||
759 | ) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>> { | ||
760 | let mut res = ArenaMap::default(); | ||
761 | let child_source = def.child_source(db); | ||
762 | |||
763 | for (idx, variant) in child_source.value.iter() { | ||
764 | res.insert(idx, AstPtr::new(variant)); | ||
765 | } | ||
766 | |||
767 | Arc::new(res) | ||
768 | } | ||
769 | |||
770 | pub(crate) fn fields_attrs_source_map( | ||
771 | db: &dyn DefDatabase, | ||
772 | def: VariantId, | ||
773 | ) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>> { | ||
774 | let mut res = ArenaMap::default(); | ||
775 | let child_source = def.child_source(db); | ||
776 | |||
777 | for (idx, variant) in child_source.value.iter() { | ||
778 | res.insert( | ||
779 | idx, | ||
780 | variant | ||
781 | .as_ref() | ||
782 | .either(|l| Either::Left(AstPtr::new(l)), |r| Either::Right(AstPtr::new(r))), | ||
783 | ); | ||
784 | } | ||
785 | |||
786 | Arc::new(res) | ||
787 | } | ||
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs index 1080d9c2c..96b959967 100644 --- a/crates/hir_def/src/body.rs +++ b/crates/hir_def/src/body.rs | |||
@@ -226,7 +226,7 @@ pub struct Body { | |||
226 | /// The `ExprId` of the actual body expression. | 226 | /// The `ExprId` of the actual body expression. |
227 | pub body_expr: ExprId, | 227 | pub body_expr: ExprId, |
228 | /// Block expressions in this body that may contain inner items. | 228 | /// Block expressions in this body that may contain inner items. |
229 | pub block_scopes: Vec<BlockId>, | 229 | block_scopes: Vec<BlockId>, |
230 | _c: Count<Self>, | 230 | _c: Count<Self>, |
231 | } | 231 | } |
232 | 232 | ||
@@ -302,7 +302,8 @@ impl Body { | |||
302 | } | 302 | } |
303 | }; | 303 | }; |
304 | let expander = Expander::new(db, file_id, module); | 304 | let expander = Expander::new(db, file_id, module); |
305 | let (body, source_map) = Body::new(db, expander, params, body); | 305 | let (mut body, source_map) = Body::new(db, expander, params, body); |
306 | body.shrink_to_fit(); | ||
306 | (Arc::new(body), Arc::new(source_map)) | 307 | (Arc::new(body), Arc::new(source_map)) |
307 | } | 308 | } |
308 | 309 | ||
@@ -310,6 +311,16 @@ impl Body { | |||
310 | db.body_with_source_map(def).0 | 311 | db.body_with_source_map(def).0 |
311 | } | 312 | } |
312 | 313 | ||
314 | /// Returns an iterator over all block expressions in this body that define inner items. | ||
315 | pub fn blocks<'a>( | ||
316 | &'a self, | ||
317 | db: &'a dyn DefDatabase, | ||
318 | ) -> impl Iterator<Item = (BlockId, Arc<DefMap>)> + '_ { | ||
319 | self.block_scopes | ||
320 | .iter() | ||
321 | .map(move |block| (*block, db.block_def_map(*block).expect("block ID without DefMap"))) | ||
322 | } | ||
323 | |||
313 | fn new( | 324 | fn new( |
314 | db: &dyn DefDatabase, | 325 | db: &dyn DefDatabase, |
315 | expander: Expander, | 326 | expander: Expander, |
@@ -318,6 +329,15 @@ impl Body { | |||
318 | ) -> (Body, BodySourceMap) { | 329 | ) -> (Body, BodySourceMap) { |
319 | lower::lower(db, expander, params, body) | 330 | lower::lower(db, expander, params, body) |
320 | } | 331 | } |
332 | |||
333 | fn shrink_to_fit(&mut self) { | ||
334 | let Self { _c: _, body_expr: _, block_scopes, exprs, labels, params, pats } = self; | ||
335 | block_scopes.shrink_to_fit(); | ||
336 | exprs.shrink_to_fit(); | ||
337 | labels.shrink_to_fit(); | ||
338 | params.shrink_to_fit(); | ||
339 | pats.shrink_to_fit(); | ||
340 | } | ||
321 | } | 341 | } |
322 | 342 | ||
323 | impl Index<ExprId> for Body { | 343 | impl Index<ExprId> for Body { |
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 19f5065d1..bfb75a8a5 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs | |||
@@ -30,6 +30,7 @@ use crate::{ | |||
30 | LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, | 30 | LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, |
31 | Statement, | 31 | Statement, |
32 | }, | 32 | }, |
33 | intern::Interned, | ||
33 | item_scope::BuiltinShadowMode, | 34 | item_scope::BuiltinShadowMode, |
34 | path::{GenericArgs, Path}, | 35 | path::{GenericArgs, Path}, |
35 | type_ref::{Mutability, Rawness, TypeRef}, | 36 | type_ref::{Mutability, Rawness, TypeRef}, |
@@ -74,6 +75,7 @@ pub(super) fn lower( | |||
74 | _c: Count::new(), | 75 | _c: Count::new(), |
75 | }, | 76 | }, |
76 | expander, | 77 | expander, |
78 | statements_in_scope: Vec::new(), | ||
77 | } | 79 | } |
78 | .collect(params, body) | 80 | .collect(params, body) |
79 | } | 81 | } |
@@ -83,6 +85,7 @@ struct ExprCollector<'a> { | |||
83 | expander: Expander, | 85 | expander: Expander, |
84 | body: Body, | 86 | body: Body, |
85 | source_map: BodySourceMap, | 87 | source_map: BodySourceMap, |
88 | statements_in_scope: Vec<Statement>, | ||
86 | } | 89 | } |
87 | 90 | ||
88 | impl ExprCollector<'_> { | 91 | impl ExprCollector<'_> { |
@@ -320,8 +323,10 @@ impl ExprCollector<'_> { | |||
320 | Vec::new() | 323 | Vec::new() |
321 | }; | 324 | }; |
322 | let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | 325 | let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); |
323 | let generic_args = | 326 | let generic_args = e |
324 | e.generic_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it)); | 327 | .generic_arg_list() |
328 | .and_then(|it| GenericArgs::from_ast(&self.ctx(), it)) | ||
329 | .map(Box::new); | ||
325 | self.alloc_expr( | 330 | self.alloc_expr( |
326 | Expr::MethodCall { receiver, method_name, args, generic_args }, | 331 | Expr::MethodCall { receiver, method_name, args, generic_args }, |
327 | syntax_ptr, | 332 | syntax_ptr, |
@@ -383,7 +388,7 @@ impl ExprCollector<'_> { | |||
383 | self.alloc_expr(Expr::Yield { expr }, syntax_ptr) | 388 | self.alloc_expr(Expr::Yield { expr }, syntax_ptr) |
384 | } | 389 | } |
385 | ast::Expr::RecordExpr(e) => { | 390 | ast::Expr::RecordExpr(e) => { |
386 | let path = e.path().and_then(|path| self.expander.parse_path(path)); | 391 | let path = e.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); |
387 | let record_lit = if let Some(nfl) = e.record_expr_field_list() { | 392 | let record_lit = if let Some(nfl) = e.record_expr_field_list() { |
388 | let fields = nfl | 393 | let fields = nfl |
389 | .fields() | 394 | .fields() |
@@ -428,7 +433,7 @@ impl ExprCollector<'_> { | |||
428 | } | 433 | } |
429 | ast::Expr::CastExpr(e) => { | 434 | ast::Expr::CastExpr(e) => { |
430 | let expr = self.collect_expr_opt(e.expr()); | 435 | let expr = self.collect_expr_opt(e.expr()); |
431 | let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.ty()); | 436 | let type_ref = Interned::new(TypeRef::from_ast_opt(&self.ctx(), e.ty())); |
432 | self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) | 437 | self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) |
433 | } | 438 | } |
434 | ast::Expr::RefExpr(e) => { | 439 | ast::Expr::RefExpr(e) => { |
@@ -462,13 +467,16 @@ impl ExprCollector<'_> { | |||
462 | if let Some(pl) = e.param_list() { | 467 | if let Some(pl) = e.param_list() { |
463 | for param in pl.params() { | 468 | for param in pl.params() { |
464 | let pat = self.collect_pat_opt(param.pat()); | 469 | let pat = self.collect_pat_opt(param.pat()); |
465 | let type_ref = param.ty().map(|it| TypeRef::from_ast(&self.ctx(), it)); | 470 | let type_ref = |
471 | param.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it))); | ||
466 | args.push(pat); | 472 | args.push(pat); |
467 | arg_types.push(type_ref); | 473 | arg_types.push(type_ref); |
468 | } | 474 | } |
469 | } | 475 | } |
470 | let ret_type = | 476 | let ret_type = e |
471 | e.ret_type().and_then(|r| r.ty()).map(|it| TypeRef::from_ast(&self.ctx(), it)); | 477 | .ret_type() |
478 | .and_then(|r| r.ty()) | ||
479 | .map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it))); | ||
472 | let body = self.collect_expr_opt(e.body()); | 480 | let body = self.collect_expr_opt(e.body()); |
473 | self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr) | 481 | self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr) |
474 | } | 482 | } |
@@ -533,15 +541,13 @@ impl ExprCollector<'_> { | |||
533 | ids[0] | 541 | ids[0] |
534 | } | 542 | } |
535 | ast::Expr::MacroStmts(e) => { | 543 | ast::Expr::MacroStmts(e) => { |
536 | // FIXME: these statements should be held by some hir containter | 544 | e.statements().for_each(|s| self.collect_stmt(s)); |
537 | for stmt in e.statements() { | 545 | let tail = e |
538 | self.collect_stmt(stmt); | 546 | .expr() |
539 | } | 547 | .map(|e| self.collect_expr(e)) |
540 | if let Some(expr) = e.expr() { | 548 | .unwrap_or_else(|| self.alloc_expr(Expr::Missing, syntax_ptr.clone())); |
541 | self.collect_expr(expr) | 549 | |
542 | } else { | 550 | self.alloc_expr(Expr::MacroStmts { tail }, syntax_ptr) |
543 | self.alloc_expr(Expr::Missing, syntax_ptr) | ||
544 | } | ||
545 | } | 551 | } |
546 | }) | 552 | }) |
547 | } | 553 | } |
@@ -618,58 +624,55 @@ impl ExprCollector<'_> { | |||
618 | } | 624 | } |
619 | } | 625 | } |
620 | 626 | ||
621 | fn collect_stmt(&mut self, s: ast::Stmt) -> Option<Vec<Statement>> { | 627 | fn collect_stmt(&mut self, s: ast::Stmt) { |
622 | let stmt = match s { | 628 | match s { |
623 | ast::Stmt::LetStmt(stmt) => { | 629 | ast::Stmt::LetStmt(stmt) => { |
624 | self.check_cfg(&stmt)?; | 630 | if self.check_cfg(&stmt).is_none() { |
625 | 631 | return; | |
632 | } | ||
626 | let pat = self.collect_pat_opt(stmt.pat()); | 633 | let pat = self.collect_pat_opt(stmt.pat()); |
627 | let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it)); | 634 | let type_ref = |
635 | stmt.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it))); | ||
628 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); | 636 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); |
629 | vec![Statement::Let { pat, type_ref, initializer }] | 637 | self.statements_in_scope.push(Statement::Let { pat, type_ref, initializer }); |
630 | } | 638 | } |
631 | ast::Stmt::ExprStmt(stmt) => { | 639 | ast::Stmt::ExprStmt(stmt) => { |
632 | self.check_cfg(&stmt)?; | 640 | if self.check_cfg(&stmt).is_none() { |
641 | return; | ||
642 | } | ||
633 | 643 | ||
634 | // Note that macro could be expended to multiple statements | 644 | // Note that macro could be expended to multiple statements |
635 | if let Some(ast::Expr::MacroCall(m)) = stmt.expr() { | 645 | if let Some(ast::Expr::MacroCall(m)) = stmt.expr() { |
636 | let syntax_ptr = AstPtr::new(&stmt.expr().unwrap()); | 646 | let syntax_ptr = AstPtr::new(&stmt.expr().unwrap()); |
637 | let mut stmts = vec![]; | ||
638 | 647 | ||
639 | self.collect_macro_call(m, syntax_ptr.clone(), false, |this, expansion| { | 648 | self.collect_macro_call(m, syntax_ptr.clone(), false, |this, expansion| { |
640 | match expansion { | 649 | match expansion { |
641 | Some(expansion) => { | 650 | Some(expansion) => { |
642 | let statements: ast::MacroStmts = expansion; | 651 | let statements: ast::MacroStmts = expansion; |
643 | 652 | ||
644 | statements.statements().for_each(|stmt| { | 653 | statements.statements().for_each(|stmt| this.collect_stmt(stmt)); |
645 | if let Some(mut r) = this.collect_stmt(stmt) { | ||
646 | stmts.append(&mut r); | ||
647 | } | ||
648 | }); | ||
649 | if let Some(expr) = statements.expr() { | 654 | if let Some(expr) = statements.expr() { |
650 | stmts.push(Statement::Expr(this.collect_expr(expr))); | 655 | let expr = this.collect_expr(expr); |
656 | this.statements_in_scope.push(Statement::Expr(expr)); | ||
651 | } | 657 | } |
652 | } | 658 | } |
653 | None => { | 659 | None => { |
654 | stmts.push(Statement::Expr( | 660 | let expr = this.alloc_expr(Expr::Missing, syntax_ptr.clone()); |
655 | this.alloc_expr(Expr::Missing, syntax_ptr.clone()), | 661 | this.statements_in_scope.push(Statement::Expr(expr)); |
656 | )); | ||
657 | } | 662 | } |
658 | } | 663 | } |
659 | }); | 664 | }); |
660 | stmts | ||
661 | } else { | 665 | } else { |
662 | vec![Statement::Expr(self.collect_expr_opt(stmt.expr()))] | 666 | let expr = self.collect_expr_opt(stmt.expr()); |
667 | self.statements_in_scope.push(Statement::Expr(expr)); | ||
663 | } | 668 | } |
664 | } | 669 | } |
665 | ast::Stmt::Item(item) => { | 670 | ast::Stmt::Item(item) => { |
666 | self.check_cfg(&item)?; | 671 | if self.check_cfg(&item).is_none() { |
667 | 672 | return; | |
668 | return None; | 673 | } |
669 | } | 674 | } |
670 | }; | 675 | } |
671 | |||
672 | Some(stmt) | ||
673 | } | 676 | } |
674 | 677 | ||
675 | fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId { | 678 | fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId { |
@@ -677,18 +680,22 @@ impl ExprCollector<'_> { | |||
677 | let block_loc = | 680 | let block_loc = |
678 | BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) }; | 681 | BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) }; |
679 | let block_id = self.db.intern_block(block_loc); | 682 | let block_id = self.db.intern_block(block_loc); |
680 | self.body.block_scopes.push(block_id); | ||
681 | 683 | ||
682 | let opt_def_map = self.db.block_def_map(block_id); | 684 | let (module, def_map) = match self.db.block_def_map(block_id) { |
683 | let has_def_map = opt_def_map.is_some(); | 685 | Some(def_map) => { |
684 | let def_map = opt_def_map.unwrap_or_else(|| self.expander.def_map.clone()); | 686 | self.body.block_scopes.push(block_id); |
685 | let module = if has_def_map { def_map.root() } else { self.expander.module }; | 687 | (def_map.root(), def_map) |
688 | } | ||
689 | None => (self.expander.module, self.expander.def_map.clone()), | ||
690 | }; | ||
686 | let prev_def_map = mem::replace(&mut self.expander.def_map, def_map); | 691 | let prev_def_map = mem::replace(&mut self.expander.def_map, def_map); |
687 | let prev_local_module = mem::replace(&mut self.expander.module, module); | 692 | let prev_local_module = mem::replace(&mut self.expander.module, module); |
693 | let prev_statements = std::mem::take(&mut self.statements_in_scope); | ||
694 | |||
695 | block.statements().for_each(|s| self.collect_stmt(s)); | ||
688 | 696 | ||
689 | let statements = | ||
690 | block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect(); | ||
691 | let tail = block.tail_expr().map(|e| self.collect_expr(e)); | 697 | let tail = block.tail_expr().map(|e| self.collect_expr(e)); |
698 | let statements = std::mem::replace(&mut self.statements_in_scope, prev_statements); | ||
692 | let syntax_node_ptr = AstPtr::new(&block.into()); | 699 | let syntax_node_ptr = AstPtr::new(&block.into()); |
693 | let expr_id = self.alloc_expr( | 700 | let expr_id = self.alloc_expr( |
694 | Expr::Block { id: block_id, statements, tail, label: None }, | 701 | Expr::Block { id: block_id, statements, tail, label: None }, |
@@ -755,7 +762,7 @@ impl ExprCollector<'_> { | |||
755 | } | 762 | } |
756 | } | 763 | } |
757 | ast::Pat::TupleStructPat(p) => { | 764 | ast::Pat::TupleStructPat(p) => { |
758 | let path = p.path().and_then(|path| self.expander.parse_path(path)); | 765 | let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); |
759 | let (args, ellipsis) = self.collect_tuple_pat(p.fields()); | 766 | let (args, ellipsis) = self.collect_tuple_pat(p.fields()); |
760 | Pat::TupleStruct { path, args, ellipsis } | 767 | Pat::TupleStruct { path, args, ellipsis } |
761 | } | 768 | } |
@@ -765,7 +772,7 @@ impl ExprCollector<'_> { | |||
765 | Pat::Ref { pat, mutability } | 772 | Pat::Ref { pat, mutability } |
766 | } | 773 | } |
767 | ast::Pat::PathPat(p) => { | 774 | ast::Pat::PathPat(p) => { |
768 | let path = p.path().and_then(|path| self.expander.parse_path(path)); | 775 | let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); |
769 | path.map(Pat::Path).unwrap_or(Pat::Missing) | 776 | path.map(Pat::Path).unwrap_or(Pat::Missing) |
770 | } | 777 | } |
771 | ast::Pat::OrPat(p) => { | 778 | ast::Pat::OrPat(p) => { |
@@ -779,7 +786,7 @@ impl ExprCollector<'_> { | |||
779 | } | 786 | } |
780 | ast::Pat::WildcardPat(_) => Pat::Wild, | 787 | ast::Pat::WildcardPat(_) => Pat::Wild, |
781 | ast::Pat::RecordPat(p) => { | 788 | ast::Pat::RecordPat(p) => { |
782 | let path = p.path().and_then(|path| self.expander.parse_path(path)); | 789 | let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); |
783 | let args: Vec<_> = p | 790 | let args: Vec<_> = p |
784 | .record_pat_field_list() | 791 | .record_pat_field_list() |
785 | .expect("every struct should have a field list") | 792 | .expect("every struct should have a field list") |
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index faa133297..c1d3e998f 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs | |||
@@ -143,7 +143,7 @@ fn f() { | |||
143 | //^^^^^^^^^^^^^ could not convert tokens | 143 | //^^^^^^^^^^^^^ could not convert tokens |
144 | 144 | ||
145 | env!("OUT_DIR"); | 145 | env!("OUT_DIR"); |
146 | //^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "load out dirs from check" to fix | 146 | //^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "run build scripts" to fix |
147 | 147 | ||
148 | compile_error!("compile_error works"); | 148 | compile_error!("compile_error works"); |
149 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ compile_error works | 149 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ compile_error works |
diff --git a/crates/hir_def/src/child_by_source.rs b/crates/hir_def/src/child_by_source.rs index 2a331dcaf..f40a7f80d 100644 --- a/crates/hir_def/src/child_by_source.rs +++ b/crates/hir_def/src/child_by_source.rs | |||
@@ -160,7 +160,7 @@ impl ChildBySource for EnumId { | |||
160 | impl ChildBySource for DefWithBodyId { | 160 | impl ChildBySource for DefWithBodyId { |
161 | fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) { | 161 | fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) { |
162 | let body = db.body(*self); | 162 | let body = db.body(*self); |
163 | for def_map in body.block_scopes.iter().filter_map(|block| db.block_def_map(*block)) { | 163 | for (_, def_map) in body.blocks(db) { |
164 | // All block expressions are merged into the same map, because they logically all add | 164 | // All block expressions are merged into the same map, because they logically all add |
165 | // inner items to the containing `DefWithBodyId`. | 165 | // inner items to the containing `DefWithBodyId`. |
166 | def_map[def_map.root()].scope.child_by_source_to(db, res); | 166 | def_map[def_map.root()].scope.child_by_source_to(db, res); |
diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index 0be868ba2..135a6698e 100644 --- a/crates/hir_def/src/data.rs +++ b/crates/hir_def/src/data.rs | |||
@@ -9,8 +9,9 @@ use crate::{ | |||
9 | attr::Attrs, | 9 | attr::Attrs, |
10 | body::Expander, | 10 | body::Expander, |
11 | db::DefDatabase, | 11 | db::DefDatabase, |
12 | item_tree::{AssocItem, FunctionQualifier, ItemTreeId, ModItem, Param}, | 12 | intern::Interned, |
13 | type_ref::{TypeBound, TypeRef}, | 13 | item_tree::{AssocItem, FnFlags, ItemTreeId, ModItem, Param}, |
14 | type_ref::{TraitRef, TypeBound, TypeRef}, | ||
14 | visibility::RawVisibility, | 15 | visibility::RawVisibility, |
15 | AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId, | 16 | AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId, |
16 | Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, | 17 | Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, |
@@ -19,17 +20,12 @@ use crate::{ | |||
19 | #[derive(Debug, Clone, PartialEq, Eq)] | 20 | #[derive(Debug, Clone, PartialEq, Eq)] |
20 | pub struct FunctionData { | 21 | pub struct FunctionData { |
21 | pub name: Name, | 22 | pub name: Name, |
22 | pub params: Vec<TypeRef>, | 23 | pub params: Vec<Interned<TypeRef>>, |
23 | pub ret_type: TypeRef, | 24 | pub ret_type: Interned<TypeRef>, |
24 | pub attrs: Attrs, | 25 | pub attrs: Attrs, |
25 | /// True if the first param is `self`. This is relevant to decide whether this | ||
26 | /// can be called as a method. | ||
27 | pub has_self_param: bool, | ||
28 | pub has_body: bool, | ||
29 | pub qualifier: FunctionQualifier, | ||
30 | pub is_in_extern_block: bool, | ||
31 | pub is_varargs: bool, | ||
32 | pub visibility: RawVisibility, | 26 | pub visibility: RawVisibility, |
27 | pub abi: Option<Interned<str>>, | ||
28 | flags: FnFlags, | ||
33 | } | 29 | } |
34 | 30 | ||
35 | impl FunctionData { | 31 | impl FunctionData { |
@@ -52,31 +48,67 @@ impl FunctionData { | |||
52 | .next_back() | 48 | .next_back() |
53 | .map_or(false, |param| matches!(item_tree[param], Param::Varargs)); | 49 | .map_or(false, |param| matches!(item_tree[param], Param::Varargs)); |
54 | 50 | ||
51 | let mut flags = func.flags; | ||
52 | if is_varargs { | ||
53 | flags.bits |= FnFlags::IS_VARARGS; | ||
54 | } | ||
55 | |||
55 | Arc::new(FunctionData { | 56 | Arc::new(FunctionData { |
56 | name: func.name.clone(), | 57 | name: func.name.clone(), |
57 | params: enabled_params | 58 | params: enabled_params |
58 | .clone() | 59 | .clone() |
59 | .filter_map(|id| match &item_tree[id] { | 60 | .filter_map(|id| match &item_tree[id] { |
60 | Param::Normal(ty) => Some(item_tree[*ty].clone()), | 61 | Param::Normal(ty) => Some(ty.clone()), |
61 | Param::Varargs => None, | 62 | Param::Varargs => None, |
62 | }) | 63 | }) |
63 | .collect(), | 64 | .collect(), |
64 | ret_type: item_tree[func.ret_type].clone(), | 65 | ret_type: func.ret_type.clone(), |
65 | attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()), | 66 | attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()), |
66 | has_self_param: func.has_self_param, | ||
67 | has_body: func.has_body, | ||
68 | qualifier: func.qualifier.clone(), | ||
69 | is_in_extern_block: func.is_in_extern_block, | ||
70 | is_varargs, | ||
71 | visibility: item_tree[func.visibility].clone(), | 67 | visibility: item_tree[func.visibility].clone(), |
68 | abi: func.abi.clone(), | ||
69 | flags, | ||
72 | }) | 70 | }) |
73 | } | 71 | } |
72 | |||
73 | pub fn has_body(&self) -> bool { | ||
74 | self.flags.bits & FnFlags::HAS_BODY != 0 | ||
75 | } | ||
76 | |||
77 | /// True if the first param is `self`. This is relevant to decide whether this | ||
78 | /// can be called as a method. | ||
79 | pub fn has_self_param(&self) -> bool { | ||
80 | self.flags.bits & FnFlags::HAS_SELF_PARAM != 0 | ||
81 | } | ||
82 | |||
83 | pub fn is_default(&self) -> bool { | ||
84 | self.flags.bits & FnFlags::IS_DEFAULT != 0 | ||
85 | } | ||
86 | |||
87 | pub fn is_const(&self) -> bool { | ||
88 | self.flags.bits & FnFlags::IS_CONST != 0 | ||
89 | } | ||
90 | |||
91 | pub fn is_async(&self) -> bool { | ||
92 | self.flags.bits & FnFlags::IS_ASYNC != 0 | ||
93 | } | ||
94 | |||
95 | pub fn is_unsafe(&self) -> bool { | ||
96 | self.flags.bits & FnFlags::IS_UNSAFE != 0 | ||
97 | } | ||
98 | |||
99 | pub fn is_in_extern_block(&self) -> bool { | ||
100 | self.flags.bits & FnFlags::IS_IN_EXTERN_BLOCK != 0 | ||
101 | } | ||
102 | |||
103 | pub fn is_varargs(&self) -> bool { | ||
104 | self.flags.bits & FnFlags::IS_VARARGS != 0 | ||
105 | } | ||
74 | } | 106 | } |
75 | 107 | ||
76 | #[derive(Debug, Clone, PartialEq, Eq)] | 108 | #[derive(Debug, Clone, PartialEq, Eq)] |
77 | pub struct TypeAliasData { | 109 | pub struct TypeAliasData { |
78 | pub name: Name, | 110 | pub name: Name, |
79 | pub type_ref: Option<TypeRef>, | 111 | pub type_ref: Option<Interned<TypeRef>>, |
80 | pub visibility: RawVisibility, | 112 | pub visibility: RawVisibility, |
81 | pub is_extern: bool, | 113 | pub is_extern: bool, |
82 | /// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl). | 114 | /// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl). |
@@ -94,7 +126,7 @@ impl TypeAliasData { | |||
94 | 126 | ||
95 | Arc::new(TypeAliasData { | 127 | Arc::new(TypeAliasData { |
96 | name: typ.name.clone(), | 128 | name: typ.name.clone(), |
97 | type_ref: typ.type_ref.map(|id| item_tree[id].clone()), | 129 | type_ref: typ.type_ref.clone(), |
98 | visibility: item_tree[typ.visibility].clone(), | 130 | visibility: item_tree[typ.visibility].clone(), |
99 | is_extern: typ.is_extern, | 131 | is_extern: typ.is_extern, |
100 | bounds: typ.bounds.to_vec(), | 132 | bounds: typ.bounds.to_vec(), |
@@ -156,8 +188,8 @@ impl TraitData { | |||
156 | 188 | ||
157 | #[derive(Debug, Clone, PartialEq, Eq)] | 189 | #[derive(Debug, Clone, PartialEq, Eq)] |
158 | pub struct ImplData { | 190 | pub struct ImplData { |
159 | pub target_trait: Option<TypeRef>, | 191 | pub target_trait: Option<Interned<TraitRef>>, |
160 | pub target_type: TypeRef, | 192 | pub self_ty: Interned<TypeRef>, |
161 | pub items: Vec<AssocItemId>, | 193 | pub items: Vec<AssocItemId>, |
162 | pub is_negative: bool, | 194 | pub is_negative: bool, |
163 | } | 195 | } |
@@ -169,8 +201,8 @@ impl ImplData { | |||
169 | 201 | ||
170 | let item_tree = impl_loc.id.item_tree(db); | 202 | let item_tree = impl_loc.id.item_tree(db); |
171 | let impl_def = &item_tree[impl_loc.id.value]; | 203 | let impl_def = &item_tree[impl_loc.id.value]; |
172 | let target_trait = impl_def.target_trait.map(|id| item_tree[id].clone()); | 204 | let target_trait = impl_def.target_trait.clone(); |
173 | let target_type = item_tree[impl_def.target_type].clone(); | 205 | let self_ty = impl_def.self_ty.clone(); |
174 | let is_negative = impl_def.is_negative; | 206 | let is_negative = impl_def.is_negative; |
175 | let module_id = impl_loc.container; | 207 | let module_id = impl_loc.container; |
176 | let container = AssocContainerId::ImplId(id); | 208 | let container = AssocContainerId::ImplId(id); |
@@ -187,7 +219,7 @@ impl ImplData { | |||
187 | ); | 219 | ); |
188 | let items = items.into_iter().map(|(_, item)| item).collect(); | 220 | let items = items.into_iter().map(|(_, item)| item).collect(); |
189 | 221 | ||
190 | Arc::new(ImplData { target_trait, target_type, items, is_negative }) | 222 | Arc::new(ImplData { target_trait, self_ty, items, is_negative }) |
191 | } | 223 | } |
192 | } | 224 | } |
193 | 225 | ||
@@ -195,7 +227,7 @@ impl ImplData { | |||
195 | pub struct ConstData { | 227 | pub struct ConstData { |
196 | /// const _: () = (); | 228 | /// const _: () = (); |
197 | pub name: Option<Name>, | 229 | pub name: Option<Name>, |
198 | pub type_ref: TypeRef, | 230 | pub type_ref: Interned<TypeRef>, |
199 | pub visibility: RawVisibility, | 231 | pub visibility: RawVisibility, |
200 | } | 232 | } |
201 | 233 | ||
@@ -207,7 +239,7 @@ impl ConstData { | |||
207 | 239 | ||
208 | Arc::new(ConstData { | 240 | Arc::new(ConstData { |
209 | name: konst.name.clone(), | 241 | name: konst.name.clone(), |
210 | type_ref: item_tree[konst.type_ref].clone(), | 242 | type_ref: konst.type_ref.clone(), |
211 | visibility: item_tree[konst.visibility].clone(), | 243 | visibility: item_tree[konst.visibility].clone(), |
212 | }) | 244 | }) |
213 | } | 245 | } |
@@ -216,7 +248,7 @@ impl ConstData { | |||
216 | #[derive(Debug, Clone, PartialEq, Eq)] | 248 | #[derive(Debug, Clone, PartialEq, Eq)] |
217 | pub struct StaticData { | 249 | pub struct StaticData { |
218 | pub name: Option<Name>, | 250 | pub name: Option<Name>, |
219 | pub type_ref: TypeRef, | 251 | pub type_ref: Interned<TypeRef>, |
220 | pub visibility: RawVisibility, | 252 | pub visibility: RawVisibility, |
221 | pub mutable: bool, | 253 | pub mutable: bool, |
222 | pub is_extern: bool, | 254 | pub is_extern: bool, |
@@ -230,7 +262,7 @@ impl StaticData { | |||
230 | 262 | ||
231 | Arc::new(StaticData { | 263 | Arc::new(StaticData { |
232 | name: Some(statik.name.clone()), | 264 | name: Some(statik.name.clone()), |
233 | type_ref: item_tree[statik.type_ref].clone(), | 265 | type_ref: statik.type_ref.clone(), |
234 | visibility: item_tree[statik.visibility].clone(), | 266 | visibility: item_tree[statik.visibility].clone(), |
235 | mutable: statik.mutable, | 267 | mutable: statik.mutable, |
236 | is_extern: statik.is_extern, | 268 | is_extern: statik.is_extern, |
diff --git a/crates/hir_def/src/db.rs b/crates/hir_def/src/db.rs index 068b2ee38..7eadc8e0d 100644 --- a/crates/hir_def/src/db.rs +++ b/crates/hir_def/src/db.rs | |||
@@ -2,9 +2,10 @@ | |||
2 | use std::sync::Arc; | 2 | use std::sync::Arc; |
3 | 3 | ||
4 | use base_db::{salsa, CrateId, SourceDatabase, Upcast}; | 4 | use base_db::{salsa, CrateId, SourceDatabase, Upcast}; |
5 | use either::Either; | ||
5 | use hir_expand::{db::AstDatabase, HirFileId}; | 6 | use hir_expand::{db::AstDatabase, HirFileId}; |
6 | use la_arena::ArenaMap; | 7 | use la_arena::ArenaMap; |
7 | use syntax::SmolStr; | 8 | use syntax::{ast, AstPtr, SmolStr}; |
8 | 9 | ||
9 | use crate::{ | 10 | use crate::{ |
10 | adt::{EnumData, StructData}, | 11 | adt::{EnumData, StructData}, |
@@ -13,6 +14,7 @@ use crate::{ | |||
13 | data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData}, | 14 | data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData}, |
14 | generics::GenericParams, | 15 | generics::GenericParams, |
15 | import_map::ImportMap, | 16 | import_map::ImportMap, |
17 | intern::Interned, | ||
16 | item_tree::ItemTree, | 18 | item_tree::ItemTree, |
17 | lang_item::{LangItemTarget, LangItems}, | 19 | lang_item::{LangItemTarget, LangItems}, |
18 | nameres::DefMap, | 20 | nameres::DefMap, |
@@ -113,7 +115,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> { | |||
113 | fn expr_scopes(&self, def: DefWithBodyId) -> Arc<ExprScopes>; | 115 | fn expr_scopes(&self, def: DefWithBodyId) -> Arc<ExprScopes>; |
114 | 116 | ||
115 | #[salsa::invoke(GenericParams::generic_params_query)] | 117 | #[salsa::invoke(GenericParams::generic_params_query)] |
116 | fn generic_params(&self, def: GenericDefId) -> Arc<GenericParams>; | 118 | fn generic_params(&self, def: GenericDefId) -> Interned<GenericParams>; |
117 | 119 | ||
118 | #[salsa::invoke(Attrs::variants_attrs_query)] | 120 | #[salsa::invoke(Attrs::variants_attrs_query)] |
119 | fn variants_attrs(&self, def: EnumId) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>>; | 121 | fn variants_attrs(&self, def: EnumId) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>>; |
@@ -121,6 +123,18 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> { | |||
121 | #[salsa::invoke(Attrs::fields_attrs_query)] | 123 | #[salsa::invoke(Attrs::fields_attrs_query)] |
122 | fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>; | 124 | fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>; |
123 | 125 | ||
126 | #[salsa::invoke(crate::attr::variants_attrs_source_map)] | ||
127 | fn variants_attrs_source_map( | ||
128 | &self, | ||
129 | def: EnumId, | ||
130 | ) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>>; | ||
131 | |||
132 | #[salsa::invoke(crate::attr::fields_attrs_source_map)] | ||
133 | fn fields_attrs_source_map( | ||
134 | &self, | ||
135 | def: VariantId, | ||
136 | ) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>>; | ||
137 | |||
124 | #[salsa::invoke(AttrsWithOwner::attrs_query)] | 138 | #[salsa::invoke(AttrsWithOwner::attrs_query)] |
125 | fn attrs(&self, def: AttrDefId) -> AttrsWithOwner; | 139 | fn attrs(&self, def: AttrDefId) -> AttrsWithOwner; |
126 | 140 | ||
diff --git a/crates/hir_def/src/expr.rs b/crates/hir_def/src/expr.rs index 24be93773..b4ad984bd 100644 --- a/crates/hir_def/src/expr.rs +++ b/crates/hir_def/src/expr.rs | |||
@@ -18,6 +18,7 @@ use syntax::ast::RangeOp; | |||
18 | 18 | ||
19 | use crate::{ | 19 | use crate::{ |
20 | builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, | 20 | builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, |
21 | intern::Interned, | ||
21 | path::{GenericArgs, Path}, | 22 | path::{GenericArgs, Path}, |
22 | type_ref::{Mutability, Rawness, TypeRef}, | 23 | type_ref::{Mutability, Rawness, TypeRef}, |
23 | BlockId, | 24 | BlockId, |
@@ -86,7 +87,7 @@ pub enum Expr { | |||
86 | receiver: ExprId, | 87 | receiver: ExprId, |
87 | method_name: Name, | 88 | method_name: Name, |
88 | args: Vec<ExprId>, | 89 | args: Vec<ExprId>, |
89 | generic_args: Option<GenericArgs>, | 90 | generic_args: Option<Box<GenericArgs>>, |
90 | }, | 91 | }, |
91 | Match { | 92 | Match { |
92 | expr: ExprId, | 93 | expr: ExprId, |
@@ -106,7 +107,7 @@ pub enum Expr { | |||
106 | expr: Option<ExprId>, | 107 | expr: Option<ExprId>, |
107 | }, | 108 | }, |
108 | RecordLit { | 109 | RecordLit { |
109 | path: Option<Path>, | 110 | path: Option<Box<Path>>, |
110 | fields: Vec<RecordLitField>, | 111 | fields: Vec<RecordLitField>, |
111 | spread: Option<ExprId>, | 112 | spread: Option<ExprId>, |
112 | }, | 113 | }, |
@@ -131,7 +132,7 @@ pub enum Expr { | |||
131 | }, | 132 | }, |
132 | Cast { | 133 | Cast { |
133 | expr: ExprId, | 134 | expr: ExprId, |
134 | type_ref: TypeRef, | 135 | type_ref: Interned<TypeRef>, |
135 | }, | 136 | }, |
136 | Ref { | 137 | Ref { |
137 | expr: ExprId, | 138 | expr: ExprId, |
@@ -161,8 +162,8 @@ pub enum Expr { | |||
161 | }, | 162 | }, |
162 | Lambda { | 163 | Lambda { |
163 | args: Vec<PatId>, | 164 | args: Vec<PatId>, |
164 | arg_types: Vec<Option<TypeRef>>, | 165 | arg_types: Vec<Option<Interned<TypeRef>>>, |
165 | ret_type: Option<TypeRef>, | 166 | ret_type: Option<Interned<TypeRef>>, |
166 | body: ExprId, | 167 | body: ExprId, |
167 | }, | 168 | }, |
168 | Tuple { | 169 | Tuple { |
@@ -171,6 +172,9 @@ pub enum Expr { | |||
171 | Unsafe { | 172 | Unsafe { |
172 | body: ExprId, | 173 | body: ExprId, |
173 | }, | 174 | }, |
175 | MacroStmts { | ||
176 | tail: ExprId, | ||
177 | }, | ||
174 | Array(Array), | 178 | Array(Array), |
175 | Literal(Literal), | 179 | Literal(Literal), |
176 | } | 180 | } |
@@ -237,7 +241,7 @@ pub struct RecordLitField { | |||
237 | 241 | ||
238 | #[derive(Debug, Clone, Eq, PartialEq)] | 242 | #[derive(Debug, Clone, Eq, PartialEq)] |
239 | pub enum Statement { | 243 | pub enum Statement { |
240 | Let { pat: PatId, type_ref: Option<TypeRef>, initializer: Option<ExprId> }, | 244 | Let { pat: PatId, type_ref: Option<Interned<TypeRef>>, initializer: Option<ExprId> }, |
241 | Expr(ExprId), | 245 | Expr(ExprId), |
242 | } | 246 | } |
243 | 247 | ||
@@ -357,6 +361,7 @@ impl Expr { | |||
357 | f(*repeat) | 361 | f(*repeat) |
358 | } | 362 | } |
359 | }, | 363 | }, |
364 | Expr::MacroStmts { tail } => f(*tail), | ||
360 | Expr::Literal(_) => {} | 365 | Expr::Literal(_) => {} |
361 | } | 366 | } |
362 | } | 367 | } |
@@ -408,13 +413,13 @@ pub enum Pat { | |||
408 | Wild, | 413 | Wild, |
409 | Tuple { args: Vec<PatId>, ellipsis: Option<usize> }, | 414 | Tuple { args: Vec<PatId>, ellipsis: Option<usize> }, |
410 | Or(Vec<PatId>), | 415 | Or(Vec<PatId>), |
411 | Record { path: Option<Path>, args: Vec<RecordFieldPat>, ellipsis: bool }, | 416 | Record { path: Option<Box<Path>>, args: Vec<RecordFieldPat>, ellipsis: bool }, |
412 | Range { start: ExprId, end: ExprId }, | 417 | Range { start: ExprId, end: ExprId }, |
413 | Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> }, | 418 | Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> }, |
414 | Path(Path), | 419 | Path(Box<Path>), |
415 | Lit(ExprId), | 420 | Lit(ExprId), |
416 | Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> }, | 421 | Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> }, |
417 | TupleStruct { path: Option<Path>, args: Vec<PatId>, ellipsis: Option<usize> }, | 422 | TupleStruct { path: Option<Box<Path>>, args: Vec<PatId>, ellipsis: Option<usize> }, |
418 | Ref { pat: PatId, mutability: Mutability }, | 423 | Ref { pat: PatId, mutability: Mutability }, |
419 | Box { inner: PatId }, | 424 | Box { inner: PatId }, |
420 | ConstBlock(ExprId), | 425 | ConstBlock(ExprId), |
diff --git a/crates/hir_def/src/generics.rs b/crates/hir_def/src/generics.rs index 7c6cbff11..de5acced8 100644 --- a/crates/hir_def/src/generics.rs +++ b/crates/hir_def/src/generics.rs | |||
@@ -2,7 +2,6 @@ | |||
2 | //! structs, impls, traits, etc. This module provides a common HIR for these | 2 | //! structs, impls, traits, etc. This module provides a common HIR for these |
3 | //! generic parameters. See also the `Generics` type and the `generics_of` query | 3 | //! generic parameters. See also the `Generics` type and the `generics_of` query |
4 | //! in rustc. | 4 | //! in rustc. |
5 | use std::sync::Arc; | ||
6 | 5 | ||
7 | use base_db::FileId; | 6 | use base_db::FileId; |
8 | use either::Either; | 7 | use either::Either; |
@@ -18,6 +17,7 @@ use crate::{ | |||
18 | child_by_source::ChildBySource, | 17 | child_by_source::ChildBySource, |
19 | db::DefDatabase, | 18 | db::DefDatabase, |
20 | dyn_map::DynMap, | 19 | dyn_map::DynMap, |
20 | intern::Interned, | ||
21 | keys, | 21 | keys, |
22 | src::{HasChildSource, HasSource}, | 22 | src::{HasChildSource, HasSource}, |
23 | type_ref::{LifetimeRef, TypeBound, TypeRef}, | 23 | type_ref::{LifetimeRef, TypeBound, TypeRef}, |
@@ -26,27 +26,27 @@ use crate::{ | |||
26 | }; | 26 | }; |
27 | 27 | ||
28 | /// Data about a generic type parameter (to a function, struct, impl, ...). | 28 | /// Data about a generic type parameter (to a function, struct, impl, ...). |
29 | #[derive(Clone, PartialEq, Eq, Debug)] | 29 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
30 | pub struct TypeParamData { | 30 | pub struct TypeParamData { |
31 | pub name: Option<Name>, | 31 | pub name: Option<Name>, |
32 | pub default: Option<TypeRef>, | 32 | pub default: Option<Interned<TypeRef>>, |
33 | pub provenance: TypeParamProvenance, | 33 | pub provenance: TypeParamProvenance, |
34 | } | 34 | } |
35 | 35 | ||
36 | /// Data about a generic lifetime parameter (to a function, struct, impl, ...). | 36 | /// Data about a generic lifetime parameter (to a function, struct, impl, ...). |
37 | #[derive(Clone, PartialEq, Eq, Debug)] | 37 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
38 | pub struct LifetimeParamData { | 38 | pub struct LifetimeParamData { |
39 | pub name: Name, | 39 | pub name: Name, |
40 | } | 40 | } |
41 | 41 | ||
42 | /// Data about a generic const parameter (to a function, struct, impl, ...). | 42 | /// Data about a generic const parameter (to a function, struct, impl, ...). |
43 | #[derive(Clone, PartialEq, Eq, Debug)] | 43 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
44 | pub struct ConstParamData { | 44 | pub struct ConstParamData { |
45 | pub name: Name, | 45 | pub name: Name, |
46 | pub ty: TypeRef, | 46 | pub ty: Interned<TypeRef>, |
47 | } | 47 | } |
48 | 48 | ||
49 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] | 49 | #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] |
50 | pub enum TypeParamProvenance { | 50 | pub enum TypeParamProvenance { |
51 | TypeParamList, | 51 | TypeParamList, |
52 | TraitSelf, | 52 | TraitSelf, |
@@ -54,7 +54,7 @@ pub enum TypeParamProvenance { | |||
54 | } | 54 | } |
55 | 55 | ||
56 | /// Data about the generic parameters of a function, struct, impl, etc. | 56 | /// Data about the generic parameters of a function, struct, impl, etc. |
57 | #[derive(Clone, PartialEq, Eq, Debug, Default)] | 57 | #[derive(Clone, PartialEq, Eq, Debug, Default, Hash)] |
58 | pub struct GenericParams { | 58 | pub struct GenericParams { |
59 | pub types: Arena<TypeParamData>, | 59 | pub types: Arena<TypeParamData>, |
60 | pub lifetimes: Arena<LifetimeParamData>, | 60 | pub lifetimes: Arena<LifetimeParamData>, |
@@ -66,16 +66,16 @@ pub struct GenericParams { | |||
66 | /// where clauses like `where T: Foo + Bar` are turned into multiple of these. | 66 | /// where clauses like `where T: Foo + Bar` are turned into multiple of these. |
67 | /// It might still result in multiple actual predicates though, because of | 67 | /// It might still result in multiple actual predicates though, because of |
68 | /// associated type bindings like `Iterator<Item = u32>`. | 68 | /// associated type bindings like `Iterator<Item = u32>`. |
69 | #[derive(Clone, PartialEq, Eq, Debug)] | 69 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
70 | pub enum WherePredicate { | 70 | pub enum WherePredicate { |
71 | TypeBound { target: WherePredicateTypeTarget, bound: TypeBound }, | 71 | TypeBound { target: WherePredicateTypeTarget, bound: TypeBound }, |
72 | Lifetime { target: LifetimeRef, bound: LifetimeRef }, | 72 | Lifetime { target: LifetimeRef, bound: LifetimeRef }, |
73 | ForLifetime { lifetimes: Box<[Name]>, target: WherePredicateTypeTarget, bound: TypeBound }, | 73 | ForLifetime { lifetimes: Box<[Name]>, target: WherePredicateTypeTarget, bound: TypeBound }, |
74 | } | 74 | } |
75 | 75 | ||
76 | #[derive(Clone, PartialEq, Eq, Debug)] | 76 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
77 | pub enum WherePredicateTypeTarget { | 77 | pub enum WherePredicateTypeTarget { |
78 | TypeRef(TypeRef), | 78 | TypeRef(Interned<TypeRef>), |
79 | /// For desugared where predicates that can directly refer to a type param. | 79 | /// For desugared where predicates that can directly refer to a type param. |
80 | TypeParam(LocalTypeParamId), | 80 | TypeParam(LocalTypeParamId), |
81 | } | 81 | } |
@@ -91,7 +91,7 @@ impl GenericParams { | |||
91 | pub(crate) fn generic_params_query( | 91 | pub(crate) fn generic_params_query( |
92 | db: &dyn DefDatabase, | 92 | db: &dyn DefDatabase, |
93 | def: GenericDefId, | 93 | def: GenericDefId, |
94 | ) -> Arc<GenericParams> { | 94 | ) -> Interned<GenericParams> { |
95 | let _p = profile::span("generic_params_query"); | 95 | let _p = profile::span("generic_params_query"); |
96 | 96 | ||
97 | let generics = match def { | 97 | let generics = match def { |
@@ -99,47 +99,49 @@ impl GenericParams { | |||
99 | let id = id.lookup(db).id; | 99 | let id = id.lookup(db).id; |
100 | let tree = id.item_tree(db); | 100 | let tree = id.item_tree(db); |
101 | let item = &tree[id.value]; | 101 | let item = &tree[id.value]; |
102 | tree[item.generic_params].clone() | 102 | item.generic_params.clone() |
103 | } | 103 | } |
104 | GenericDefId::AdtId(AdtId::StructId(id)) => { | 104 | GenericDefId::AdtId(AdtId::StructId(id)) => { |
105 | let id = id.lookup(db).id; | 105 | let id = id.lookup(db).id; |
106 | let tree = id.item_tree(db); | 106 | let tree = id.item_tree(db); |
107 | let item = &tree[id.value]; | 107 | let item = &tree[id.value]; |
108 | tree[item.generic_params].clone() | 108 | item.generic_params.clone() |
109 | } | 109 | } |
110 | GenericDefId::AdtId(AdtId::EnumId(id)) => { | 110 | GenericDefId::AdtId(AdtId::EnumId(id)) => { |
111 | let id = id.lookup(db).id; | 111 | let id = id.lookup(db).id; |
112 | let tree = id.item_tree(db); | 112 | let tree = id.item_tree(db); |
113 | let item = &tree[id.value]; | 113 | let item = &tree[id.value]; |
114 | tree[item.generic_params].clone() | 114 | item.generic_params.clone() |
115 | } | 115 | } |
116 | GenericDefId::AdtId(AdtId::UnionId(id)) => { | 116 | GenericDefId::AdtId(AdtId::UnionId(id)) => { |
117 | let id = id.lookup(db).id; | 117 | let id = id.lookup(db).id; |
118 | let tree = id.item_tree(db); | 118 | let tree = id.item_tree(db); |
119 | let item = &tree[id.value]; | 119 | let item = &tree[id.value]; |
120 | tree[item.generic_params].clone() | 120 | item.generic_params.clone() |
121 | } | 121 | } |
122 | GenericDefId::TraitId(id) => { | 122 | GenericDefId::TraitId(id) => { |
123 | let id = id.lookup(db).id; | 123 | let id = id.lookup(db).id; |
124 | let tree = id.item_tree(db); | 124 | let tree = id.item_tree(db); |
125 | let item = &tree[id.value]; | 125 | let item = &tree[id.value]; |
126 | tree[item.generic_params].clone() | 126 | item.generic_params.clone() |
127 | } | 127 | } |
128 | GenericDefId::TypeAliasId(id) => { | 128 | GenericDefId::TypeAliasId(id) => { |
129 | let id = id.lookup(db).id; | 129 | let id = id.lookup(db).id; |
130 | let tree = id.item_tree(db); | 130 | let tree = id.item_tree(db); |
131 | let item = &tree[id.value]; | 131 | let item = &tree[id.value]; |
132 | tree[item.generic_params].clone() | 132 | item.generic_params.clone() |
133 | } | 133 | } |
134 | GenericDefId::ImplId(id) => { | 134 | GenericDefId::ImplId(id) => { |
135 | let id = id.lookup(db).id; | 135 | let id = id.lookup(db).id; |
136 | let tree = id.item_tree(db); | 136 | let tree = id.item_tree(db); |
137 | let item = &tree[id.value]; | 137 | let item = &tree[id.value]; |
138 | tree[item.generic_params].clone() | 138 | item.generic_params.clone() |
139 | } | ||
140 | GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => { | ||
141 | Interned::new(GenericParams::default()) | ||
139 | } | 142 | } |
140 | GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => GenericParams::default(), | ||
141 | }; | 143 | }; |
142 | Arc::new(generics) | 144 | generics |
143 | } | 145 | } |
144 | 146 | ||
145 | fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) { | 147 | fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) { |
@@ -217,6 +219,7 @@ impl GenericParams { | |||
217 | GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => FileId(!0).into(), | 219 | GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => FileId(!0).into(), |
218 | }; | 220 | }; |
219 | 221 | ||
222 | generics.shrink_to_fit(); | ||
220 | (generics, InFile::new(file_id, sm)) | 223 | (generics, InFile::new(file_id, sm)) |
221 | } | 224 | } |
222 | 225 | ||
@@ -256,7 +259,8 @@ impl GenericParams { | |||
256 | for type_param in params.type_params() { | 259 | for type_param in params.type_params() { |
257 | let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); | 260 | let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); |
258 | // FIXME: Use `Path::from_src` | 261 | // FIXME: Use `Path::from_src` |
259 | let default = type_param.default_type().map(|it| TypeRef::from_ast(lower_ctx, it)); | 262 | let default = |
263 | type_param.default_type().map(|it| Interned::new(TypeRef::from_ast(lower_ctx, it))); | ||
260 | let param = TypeParamData { | 264 | let param = TypeParamData { |
261 | name: Some(name.clone()), | 265 | name: Some(name.clone()), |
262 | default, | 266 | default, |
@@ -280,7 +284,7 @@ impl GenericParams { | |||
280 | for const_param in params.const_params() { | 284 | for const_param in params.const_params() { |
281 | let name = const_param.name().map_or_else(Name::missing, |it| it.as_name()); | 285 | let name = const_param.name().map_or_else(Name::missing, |it| it.as_name()); |
282 | let ty = const_param.ty().map_or(TypeRef::Error, |it| TypeRef::from_ast(lower_ctx, it)); | 286 | let ty = const_param.ty().map_or(TypeRef::Error, |it| TypeRef::from_ast(lower_ctx, it)); |
283 | let param = ConstParamData { name, ty }; | 287 | let param = ConstParamData { name, ty: Interned::new(ty) }; |
284 | let param_id = self.consts.alloc(param); | 288 | let param_id = self.consts.alloc(param); |
285 | sm.const_params.insert(param_id, const_param.clone()); | 289 | sm.const_params.insert(param_id, const_param.clone()); |
286 | } | 290 | } |
@@ -334,11 +338,11 @@ impl GenericParams { | |||
334 | (Either::Left(type_ref), bound) => match hrtb_lifetimes { | 338 | (Either::Left(type_ref), bound) => match hrtb_lifetimes { |
335 | Some(hrtb_lifetimes) => WherePredicate::ForLifetime { | 339 | Some(hrtb_lifetimes) => WherePredicate::ForLifetime { |
336 | lifetimes: hrtb_lifetimes.clone(), | 340 | lifetimes: hrtb_lifetimes.clone(), |
337 | target: WherePredicateTypeTarget::TypeRef(type_ref), | 341 | target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)), |
338 | bound, | 342 | bound, |
339 | }, | 343 | }, |
340 | None => WherePredicate::TypeBound { | 344 | None => WherePredicate::TypeBound { |
341 | target: WherePredicateTypeTarget::TypeRef(type_ref), | 345 | target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)), |
342 | bound, | 346 | bound, |
343 | }, | 347 | }, |
344 | }, | 348 | }, |
@@ -369,6 +373,14 @@ impl GenericParams { | |||
369 | }); | 373 | }); |
370 | } | 374 | } |
371 | 375 | ||
376 | pub(crate) fn shrink_to_fit(&mut self) { | ||
377 | let Self { consts, lifetimes, types, where_predicates } = self; | ||
378 | consts.shrink_to_fit(); | ||
379 | lifetimes.shrink_to_fit(); | ||
380 | types.shrink_to_fit(); | ||
381 | where_predicates.shrink_to_fit(); | ||
382 | } | ||
383 | |||
372 | pub fn find_type_by_name(&self, name: &Name) -> Option<LocalTypeParamId> { | 384 | pub fn find_type_by_name(&self, name: &Name) -> Option<LocalTypeParamId> { |
373 | self.types | 385 | self.types |
374 | .iter() | 386 | .iter() |
diff --git a/crates/hir_def/src/intern.rs b/crates/hir_def/src/intern.rs new file mode 100644 index 000000000..abc304ef0 --- /dev/null +++ b/crates/hir_def/src/intern.rs | |||
@@ -0,0 +1,216 @@ | |||
1 | //! Global `Arc`-based object interning infrastructure. | ||
2 | //! | ||
3 | //! Eventually this should probably be replaced with salsa-based interning. | ||
4 | |||
5 | use std::{ | ||
6 | collections::HashMap, | ||
7 | fmt::{self, Debug}, | ||
8 | hash::{BuildHasherDefault, Hash, Hasher}, | ||
9 | ops::Deref, | ||
10 | sync::Arc, | ||
11 | }; | ||
12 | |||
13 | use dashmap::{lock::RwLockWriteGuard, DashMap, SharedValue}; | ||
14 | use once_cell::sync::OnceCell; | ||
15 | use rustc_hash::FxHasher; | ||
16 | |||
17 | use crate::generics::GenericParams; | ||
18 | |||
19 | type InternMap<T> = DashMap<Arc<T>, (), BuildHasherDefault<FxHasher>>; | ||
20 | type Guard<T> = | ||
21 | RwLockWriteGuard<'static, HashMap<Arc<T>, SharedValue<()>, BuildHasherDefault<FxHasher>>>; | ||
22 | |||
23 | pub struct Interned<T: Internable + ?Sized> { | ||
24 | arc: Arc<T>, | ||
25 | } | ||
26 | |||
27 | impl<T: Internable> Interned<T> { | ||
28 | pub fn new(obj: T) -> Self { | ||
29 | match Interned::lookup(&obj) { | ||
30 | Ok(this) => this, | ||
31 | Err(shard) => { | ||
32 | let arc = Arc::new(obj); | ||
33 | Self::alloc(arc, shard) | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | |||
39 | impl<T: Internable + ?Sized> Interned<T> { | ||
40 | fn lookup(obj: &T) -> Result<Self, Guard<T>> { | ||
41 | let storage = T::storage().get(); | ||
42 | let shard_idx = storage.determine_map(obj); | ||
43 | let shard = &storage.shards()[shard_idx]; | ||
44 | let shard = shard.write(); | ||
45 | |||
46 | // Atomically, | ||
47 | // - check if `obj` is already in the map | ||
48 | // - if so, clone its `Arc` and return it | ||
49 | // - if not, box it up, insert it, and return a clone | ||
50 | // This needs to be atomic (locking the shard) to avoid races with other thread, which could | ||
51 | // insert the same object between us looking it up and inserting it. | ||
52 | |||
53 | // FIXME: avoid double lookup/hashing by using raw entry API (once stable, or when | ||
54 | // hashbrown can be plugged into dashmap) | ||
55 | match shard.get_key_value(obj) { | ||
56 | Some((arc, _)) => Ok(Self { arc: arc.clone() }), | ||
57 | None => Err(shard), | ||
58 | } | ||
59 | } | ||
60 | |||
61 | fn alloc(arc: Arc<T>, mut shard: Guard<T>) -> Self { | ||
62 | let arc2 = arc.clone(); | ||
63 | |||
64 | shard.insert(arc2, SharedValue::new(())); | ||
65 | |||
66 | Self { arc } | ||
67 | } | ||
68 | } | ||
69 | |||
70 | impl Interned<str> { | ||
71 | pub fn new_str(s: &str) -> Self { | ||
72 | match Interned::lookup(s) { | ||
73 | Ok(this) => this, | ||
74 | Err(shard) => { | ||
75 | let arc = Arc::<str>::from(s); | ||
76 | Self::alloc(arc, shard) | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | |||
82 | impl<T: Internable + ?Sized> Drop for Interned<T> { | ||
83 | #[inline] | ||
84 | fn drop(&mut self) { | ||
85 | // When the last `Ref` is dropped, remove the object from the global map. | ||
86 | if Arc::strong_count(&self.arc) == 2 { | ||
87 | // Only `self` and the global map point to the object. | ||
88 | |||
89 | self.drop_slow(); | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | |||
94 | impl<T: Internable + ?Sized> Interned<T> { | ||
95 | #[cold] | ||
96 | fn drop_slow(&mut self) { | ||
97 | let storage = T::storage().get(); | ||
98 | let shard_idx = storage.determine_map(&self.arc); | ||
99 | let shard = &storage.shards()[shard_idx]; | ||
100 | let mut shard = shard.write(); | ||
101 | |||
102 | // FIXME: avoid double lookup | ||
103 | let (arc, _) = shard.get_key_value(&self.arc).expect("interned value removed prematurely"); | ||
104 | |||
105 | if Arc::strong_count(arc) != 2 { | ||
106 | // Another thread has interned another copy | ||
107 | return; | ||
108 | } | ||
109 | |||
110 | shard.remove(&self.arc); | ||
111 | |||
112 | // Shrink the backing storage if the shard is less than 50% occupied. | ||
113 | if shard.len() * 2 < shard.capacity() { | ||
114 | shard.shrink_to_fit(); | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | |||
119 | /// Compares interned `Ref`s using pointer equality. | ||
120 | impl<T: Internable> PartialEq for Interned<T> { | ||
121 | // NOTE: No `?Sized` because `ptr_eq` doesn't work right with trait objects. | ||
122 | |||
123 | #[inline] | ||
124 | fn eq(&self, other: &Self) -> bool { | ||
125 | Arc::ptr_eq(&self.arc, &other.arc) | ||
126 | } | ||
127 | } | ||
128 | |||
129 | impl<T: Internable> Eq for Interned<T> {} | ||
130 | |||
131 | impl PartialEq for Interned<str> { | ||
132 | fn eq(&self, other: &Self) -> bool { | ||
133 | Arc::ptr_eq(&self.arc, &other.arc) | ||
134 | } | ||
135 | } | ||
136 | |||
137 | impl Eq for Interned<str> {} | ||
138 | |||
139 | impl<T: Internable + ?Sized> Hash for Interned<T> { | ||
140 | fn hash<H: Hasher>(&self, state: &mut H) { | ||
141 | // NOTE: Cast disposes vtable pointer / slice/str length. | ||
142 | state.write_usize(Arc::as_ptr(&self.arc) as *const () as usize) | ||
143 | } | ||
144 | } | ||
145 | |||
146 | impl<T: Internable + ?Sized> AsRef<T> for Interned<T> { | ||
147 | #[inline] | ||
148 | fn as_ref(&self) -> &T { | ||
149 | &self.arc | ||
150 | } | ||
151 | } | ||
152 | |||
153 | impl<T: Internable + ?Sized> Deref for Interned<T> { | ||
154 | type Target = T; | ||
155 | |||
156 | #[inline] | ||
157 | fn deref(&self) -> &Self::Target { | ||
158 | &self.arc | ||
159 | } | ||
160 | } | ||
161 | |||
162 | impl<T: Internable + ?Sized> Clone for Interned<T> { | ||
163 | fn clone(&self) -> Self { | ||
164 | Self { arc: self.arc.clone() } | ||
165 | } | ||
166 | } | ||
167 | |||
168 | impl<T: Debug + Internable + ?Sized> Debug for Interned<T> { | ||
169 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
170 | (*self.arc).fmt(f) | ||
171 | } | ||
172 | } | ||
173 | |||
174 | pub struct InternStorage<T: ?Sized> { | ||
175 | map: OnceCell<InternMap<T>>, | ||
176 | } | ||
177 | |||
178 | impl<T: ?Sized> InternStorage<T> { | ||
179 | pub const fn new() -> Self { | ||
180 | Self { map: OnceCell::new() } | ||
181 | } | ||
182 | } | ||
183 | |||
184 | impl<T: Internable + ?Sized> InternStorage<T> { | ||
185 | fn get(&self) -> &InternMap<T> { | ||
186 | self.map.get_or_init(DashMap::default) | ||
187 | } | ||
188 | } | ||
189 | |||
190 | pub trait Internable: Hash + Eq + 'static { | ||
191 | fn storage() -> &'static InternStorage<Self>; | ||
192 | } | ||
193 | |||
194 | /// Implements `Internable` for a given list of types, making them usable with `Interned`. | ||
195 | #[macro_export] | ||
196 | #[doc(hidden)] | ||
197 | macro_rules! _impl_internable { | ||
198 | ( $($t:path),+ $(,)? ) => { $( | ||
199 | impl Internable for $t { | ||
200 | fn storage() -> &'static InternStorage<Self> { | ||
201 | static STORAGE: InternStorage<$t> = InternStorage::new(); | ||
202 | &STORAGE | ||
203 | } | ||
204 | } | ||
205 | )+ }; | ||
206 | } | ||
207 | |||
208 | pub use crate::_impl_internable as impl_internable; | ||
209 | |||
210 | impl_internable!( | ||
211 | crate::type_ref::TypeRef, | ||
212 | crate::type_ref::TraitRef, | ||
213 | crate::path::ModPath, | ||
214 | GenericParams, | ||
215 | str, | ||
216 | ); | ||
diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs index f3ebe7c72..9014468ea 100644 --- a/crates/hir_def/src/item_scope.rs +++ b/crates/hir_def/src/item_scope.rs | |||
@@ -11,7 +11,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; | |||
11 | use stdx::format_to; | 11 | use stdx::format_to; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ImplId, | 14 | db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ConstId, ImplId, |
15 | LocalModuleId, MacroDefId, ModuleDefId, ModuleId, TraitId, | 15 | LocalModuleId, MacroDefId, ModuleDefId, ModuleId, TraitId, |
16 | }; | 16 | }; |
17 | 17 | ||
@@ -37,6 +37,7 @@ pub struct ItemScope { | |||
37 | 37 | ||
38 | defs: Vec<ModuleDefId>, | 38 | defs: Vec<ModuleDefId>, |
39 | impls: Vec<ImplId>, | 39 | impls: Vec<ImplId>, |
40 | unnamed_consts: Vec<ConstId>, | ||
40 | /// Traits imported via `use Trait as _;`. | 41 | /// Traits imported via `use Trait as _;`. |
41 | unnamed_trait_imports: FxHashMap<TraitId, Visibility>, | 42 | unnamed_trait_imports: FxHashMap<TraitId, Visibility>, |
42 | /// Macros visible in current module in legacy textual scope | 43 | /// Macros visible in current module in legacy textual scope |
@@ -106,6 +107,10 @@ impl ItemScope { | |||
106 | .map(|(_, v)| v) | 107 | .map(|(_, v)| v) |
107 | } | 108 | } |
108 | 109 | ||
110 | pub fn unnamed_consts(&self) -> impl Iterator<Item = ConstId> + '_ { | ||
111 | self.unnamed_consts.iter().copied() | ||
112 | } | ||
113 | |||
109 | /// Iterate over all module scoped macros | 114 | /// Iterate over all module scoped macros |
110 | pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { | 115 | pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { |
111 | self.entries().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_))) | 116 | self.entries().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_))) |
@@ -156,6 +161,10 @@ impl ItemScope { | |||
156 | self.impls.push(imp) | 161 | self.impls.push(imp) |
157 | } | 162 | } |
158 | 163 | ||
164 | pub(crate) fn define_unnamed_const(&mut self, konst: ConstId) { | ||
165 | self.unnamed_consts.push(konst); | ||
166 | } | ||
167 | |||
159 | pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) { | 168 | pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) { |
160 | self.legacy_macros.insert(name, mac); | 169 | self.legacy_macros.insert(name, mac); |
161 | } | 170 | } |
@@ -285,6 +294,30 @@ impl ItemScope { | |||
285 | buf.push('\n'); | 294 | buf.push('\n'); |
286 | } | 295 | } |
287 | } | 296 | } |
297 | |||
298 | pub(crate) fn shrink_to_fit(&mut self) { | ||
299 | // Exhaustive match to require handling new fields. | ||
300 | let Self { | ||
301 | types, | ||
302 | values, | ||
303 | macros, | ||
304 | unresolved, | ||
305 | defs, | ||
306 | impls, | ||
307 | unnamed_consts, | ||
308 | unnamed_trait_imports, | ||
309 | legacy_macros, | ||
310 | } = self; | ||
311 | types.shrink_to_fit(); | ||
312 | values.shrink_to_fit(); | ||
313 | macros.shrink_to_fit(); | ||
314 | unresolved.shrink_to_fit(); | ||
315 | defs.shrink_to_fit(); | ||
316 | impls.shrink_to_fit(); | ||
317 | unnamed_consts.shrink_to_fit(); | ||
318 | unnamed_trait_imports.shrink_to_fit(); | ||
319 | legacy_macros.shrink_to_fit(); | ||
320 | } | ||
288 | } | 321 | } |
289 | 322 | ||
290 | impl PerNs { | 323 | impl PerNs { |
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index ae2475b4e..240662486 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs | |||
@@ -24,14 +24,15 @@ use la_arena::{Arena, Idx, RawIdx}; | |||
24 | use profile::Count; | 24 | use profile::Count; |
25 | use rustc_hash::FxHashMap; | 25 | use rustc_hash::FxHashMap; |
26 | use smallvec::SmallVec; | 26 | use smallvec::SmallVec; |
27 | use syntax::{ast, match_ast, SmolStr, SyntaxKind}; | 27 | use syntax::{ast, match_ast, SyntaxKind}; |
28 | 28 | ||
29 | use crate::{ | 29 | use crate::{ |
30 | attr::{Attrs, RawAttrs}, | 30 | attr::{Attrs, RawAttrs}, |
31 | db::DefDatabase, | 31 | db::DefDatabase, |
32 | generics::GenericParams, | 32 | generics::GenericParams, |
33 | intern::Interned, | ||
33 | path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind}, | 34 | path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind}, |
34 | type_ref::{Mutability, TypeBound, TypeRef}, | 35 | type_ref::{Mutability, TraitRef, TypeBound, TypeRef}, |
35 | visibility::RawVisibility, | 36 | visibility::RawVisibility, |
36 | }; | 37 | }; |
37 | 38 | ||
@@ -57,13 +58,6 @@ impl fmt::Debug for RawVisibilityId { | |||
57 | } | 58 | } |
58 | } | 59 | } |
59 | 60 | ||
60 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] | ||
61 | pub struct GenericParamsId(u32); | ||
62 | |||
63 | impl GenericParamsId { | ||
64 | pub const EMPTY: Self = GenericParamsId(u32::max_value()); | ||
65 | } | ||
66 | |||
67 | /// The item tree of a source file. | 61 | /// The item tree of a source file. |
68 | #[derive(Debug, Default, Eq, PartialEq)] | 62 | #[derive(Debug, Default, Eq, PartialEq)] |
69 | pub struct ItemTree { | 63 | pub struct ItemTree { |
@@ -110,15 +104,6 @@ impl ItemTree { | |||
110 | // still need to collect inner items. | 104 | // still need to collect inner items. |
111 | ctx.lower_inner_items(e.syntax()) | 105 | ctx.lower_inner_items(e.syntax()) |
112 | }, | 106 | }, |
113 | ast::ExprStmt(stmt) => { | ||
114 | // Macros can expand to stmt. We return an empty item tree in this case, but | ||
115 | // still need to collect inner items. | ||
116 | ctx.lower_inner_items(stmt.syntax()) | ||
117 | }, | ||
118 | ast::Item(item) => { | ||
119 | // Macros can expand to stmt and other item, and we add it as top level item | ||
120 | ctx.lower_single_item(item) | ||
121 | }, | ||
122 | _ => { | 107 | _ => { |
123 | panic!("cannot create item tree from {:?} {}", syntax, syntax); | 108 | panic!("cannot create item tree from {:?} {}", syntax, syntax); |
124 | }, | 109 | }, |
@@ -154,8 +139,6 @@ impl ItemTree { | |||
154 | macro_rules, | 139 | macro_rules, |
155 | macro_defs, | 140 | macro_defs, |
156 | vis, | 141 | vis, |
157 | generics, | ||
158 | type_refs, | ||
159 | inner_items, | 142 | inner_items, |
160 | } = &mut **data; | 143 | } = &mut **data; |
161 | 144 | ||
@@ -179,9 +162,6 @@ impl ItemTree { | |||
179 | macro_defs.shrink_to_fit(); | 162 | macro_defs.shrink_to_fit(); |
180 | 163 | ||
181 | vis.arena.shrink_to_fit(); | 164 | vis.arena.shrink_to_fit(); |
182 | generics.arena.shrink_to_fit(); | ||
183 | type_refs.arena.shrink_to_fit(); | ||
184 | type_refs.map.shrink_to_fit(); | ||
185 | 165 | ||
186 | inner_items.shrink_to_fit(); | 166 | inner_items.shrink_to_fit(); |
187 | } | 167 | } |
@@ -253,58 +233,6 @@ static VIS_PRIV: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKi | |||
253 | static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Crate)); | 233 | static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Crate)); |
254 | 234 | ||
255 | #[derive(Default, Debug, Eq, PartialEq)] | 235 | #[derive(Default, Debug, Eq, PartialEq)] |
256 | struct GenericParamsStorage { | ||
257 | arena: Arena<GenericParams>, | ||
258 | } | ||
259 | |||
260 | impl GenericParamsStorage { | ||
261 | fn alloc(&mut self, params: GenericParams) -> GenericParamsId { | ||
262 | if params.types.is_empty() | ||
263 | && params.lifetimes.is_empty() | ||
264 | && params.consts.is_empty() | ||
265 | && params.where_predicates.is_empty() | ||
266 | { | ||
267 | return GenericParamsId::EMPTY; | ||
268 | } | ||
269 | |||
270 | GenericParamsId(self.arena.alloc(params).into_raw().into()) | ||
271 | } | ||
272 | } | ||
273 | |||
274 | static EMPTY_GENERICS: GenericParams = GenericParams { | ||
275 | types: Arena::new(), | ||
276 | lifetimes: Arena::new(), | ||
277 | consts: Arena::new(), | ||
278 | where_predicates: Vec::new(), | ||
279 | }; | ||
280 | |||
281 | /// `TypeRef` interner. | ||
282 | #[derive(Default, Debug, Eq, PartialEq)] | ||
283 | struct TypeRefStorage { | ||
284 | arena: Arena<Arc<TypeRef>>, | ||
285 | map: FxHashMap<Arc<TypeRef>, Idx<Arc<TypeRef>>>, | ||
286 | } | ||
287 | |||
288 | impl TypeRefStorage { | ||
289 | // Note: We lie about the `Idx<TypeRef>` to hide the interner details. | ||
290 | |||
291 | fn intern(&mut self, ty: TypeRef) -> Idx<TypeRef> { | ||
292 | if let Some(id) = self.map.get(&ty) { | ||
293 | return Idx::from_raw(id.into_raw()); | ||
294 | } | ||
295 | |||
296 | let ty = Arc::new(ty); | ||
297 | let idx = self.arena.alloc(ty.clone()); | ||
298 | self.map.insert(ty, idx); | ||
299 | Idx::from_raw(idx.into_raw()) | ||
300 | } | ||
301 | |||
302 | fn lookup(&self, id: Idx<TypeRef>) -> &TypeRef { | ||
303 | &self.arena[Idx::from_raw(id.into_raw())] | ||
304 | } | ||
305 | } | ||
306 | |||
307 | #[derive(Default, Debug, Eq, PartialEq)] | ||
308 | struct ItemTreeData { | 236 | struct ItemTreeData { |
309 | imports: Arena<Import>, | 237 | imports: Arena<Import>, |
310 | extern_crates: Arena<ExternCrate>, | 238 | extern_crates: Arena<ExternCrate>, |
@@ -326,8 +254,6 @@ struct ItemTreeData { | |||
326 | macro_defs: Arena<MacroDef>, | 254 | macro_defs: Arena<MacroDef>, |
327 | 255 | ||
328 | vis: ItemVisibilities, | 256 | vis: ItemVisibilities, |
329 | generics: GenericParamsStorage, | ||
330 | type_refs: TypeRefStorage, | ||
331 | 257 | ||
332 | inner_items: FxHashMap<FileAstId<ast::BlockExpr>, SmallVec<[ModItem; 1]>>, | 258 | inner_items: FxHashMap<FileAstId<ast::BlockExpr>, SmallVec<[ModItem; 1]>>, |
333 | } | 259 | } |
@@ -546,25 +472,6 @@ impl Index<RawVisibilityId> for ItemTree { | |||
546 | } | 472 | } |
547 | } | 473 | } |
548 | 474 | ||
549 | impl Index<GenericParamsId> for ItemTree { | ||
550 | type Output = GenericParams; | ||
551 | |||
552 | fn index(&self, index: GenericParamsId) -> &Self::Output { | ||
553 | match index { | ||
554 | GenericParamsId::EMPTY => &EMPTY_GENERICS, | ||
555 | _ => &self.data().generics.arena[Idx::from_raw(index.0.into())], | ||
556 | } | ||
557 | } | ||
558 | } | ||
559 | |||
560 | impl Index<Idx<TypeRef>> for ItemTree { | ||
561 | type Output = TypeRef; | ||
562 | |||
563 | fn index(&self, id: Idx<TypeRef>) -> &Self::Output { | ||
564 | self.data().type_refs.lookup(id) | ||
565 | } | ||
566 | } | ||
567 | |||
568 | impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree { | 475 | impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree { |
569 | type Output = N; | 476 | type Output = N; |
570 | fn index(&self, id: FileItemTreeId<N>) -& |