aboutsummaryrefslogtreecommitdiff
path: root/crates/hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir')
-rw-r--r--crates/hir/src/attrs.rs8
-rw-r--r--crates/hir/src/display.rs2
-rw-r--r--crates/hir/src/has_source.rs8
-rw-r--r--crates/hir/src/lib.rs197
-rw-r--r--crates/hir/src/semantics.rs4
-rw-r--r--crates/hir/src/semantics/source_to_def.rs8
6 files changed, 120 insertions, 107 deletions
diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs
index dab8da7bb..4a11622fc 100644
--- a/crates/hir/src/attrs.rs
+++ b/crates/hir/src/attrs.rs
@@ -1,6 +1,6 @@
1//! Attributes & documentation for hir types. 1//! Attributes & documentation for hir types.
2use hir_def::{ 2use hir_def::{
3 attr::{Attrs, Documentation}, 3 attr::{AttrsWithOwner, Documentation},
4 path::ModPath, 4 path::ModPath,
5 per_ns::PerNs, 5 per_ns::PerNs,
6 resolver::HasResolver, 6 resolver::HasResolver,
@@ -16,7 +16,7 @@ use crate::{
16}; 16};
17 17
18pub trait HasAttrs { 18pub trait HasAttrs {
19 fn attrs(self, db: &dyn HirDatabase) -> Attrs; 19 fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner;
20 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>; 20 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>;
21 fn resolve_doc_path( 21 fn resolve_doc_path(
22 self, 22 self,
@@ -36,7 +36,7 @@ pub enum Namespace {
36macro_rules! impl_has_attrs { 36macro_rules! impl_has_attrs {
37 ($(($def:ident, $def_id:ident),)*) => {$( 37 ($(($def:ident, $def_id:ident),)*) => {$(
38 impl HasAttrs for $def { 38 impl HasAttrs for $def {
39 fn attrs(self, db: &dyn HirDatabase) -> Attrs { 39 fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
40 let def = AttrDefId::$def_id(self.into()); 40 let def = AttrDefId::$def_id(self.into());
41 db.attrs(def) 41 db.attrs(def)
42 } 42 }
@@ -70,7 +70,7 @@ impl_has_attrs![
70macro_rules! impl_has_attrs_enum { 70macro_rules! impl_has_attrs_enum {
71 ($($variant:ident),* for $enum:ident) => {$( 71 ($($variant:ident),* for $enum:ident) => {$(
72 impl HasAttrs for $variant { 72 impl HasAttrs for $variant {
73 fn attrs(self, db: &dyn HirDatabase) -> Attrs { 73 fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
74 $enum::$variant(self).attrs(db) 74 $enum::$variant(self).attrs(db)
75 } 75 }
76 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> { 76 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs
index 9f6d7be48..97a78ca25 100644
--- a/crates/hir/src/display.rs
+++ b/crates/hir/src/display.rs
@@ -217,7 +217,7 @@ impl HirDisplay for Variant {
217 217
218impl HirDisplay for Type { 218impl HirDisplay for Type {
219 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { 219 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
220 self.ty.value.hir_fmt(f) 220 self.ty.hir_fmt(f)
221 } 221 }
222} 222}
223 223
diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs
index 262002671..dc10a4d0f 100644
--- a/crates/hir/src/has_source.rs
+++ b/crates/hir/src/has_source.rs
@@ -111,10 +111,12 @@ impl HasSource for TypeAlias {
111 } 111 }
112} 112}
113impl HasSource for MacroDef { 113impl HasSource for MacroDef {
114 type Ast = ast::Macro; 114 type Ast = Either<ast::Macro, ast::Fn>;
115 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> { 115 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
116 let ast_id = self.id.ast_id?; 116 Some(self.id.ast_id().either(
117 Some(InFile { file_id: ast_id.file_id, value: ast_id.to_node(db.upcast()) }) 117 |id| id.with_value(Either::Left(id.to_node(db.upcast()))),
118 |id| id.with_value(Either::Right(id.to_node(db.upcast()))),
119 ))
118 } 120 }
119} 121}
120impl HasSource for Impl { 122impl HasSource for Impl {
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index b41a36a78..30b96d7e2 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -56,9 +56,9 @@ use hir_ty::{
56 primitive::UintTy, 56 primitive::UintTy,
57 to_assoc_type_id, 57 to_assoc_type_id,
58 traits::{FnTrait, Solution, SolutionVariables}, 58 traits::{FnTrait, Solution, SolutionVariables},
59 AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, 59 AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast,
60 InEnvironment, Interner, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substitution, 60 DebruijnIndex, InEnvironment, Interner, ProjectionTy, QuantifiedWhereClause, Scalar,
61 Ty, TyDefId, TyKind, TyVariableKind, 61 Substitution, TraitEnvironment, Ty, TyDefId, TyKind, TyVariableKind, WhereClause,
62}; 62};
63use itertools::Itertools; 63use itertools::Itertools;
64use rustc_hash::FxHashSet; 64use rustc_hash::FxHashSet;
@@ -89,7 +89,7 @@ pub use crate::{
89pub use { 89pub use {
90 hir_def::{ 90 hir_def::{
91 adt::StructKind, 91 adt::StructKind,
92 attr::{Attr, Attrs, Documentation}, 92 attr::{Attr, Attrs, AttrsWithOwner, Documentation},
93 body::scope::ExprScopes, 93 body::scope::ExprScopes,
94 find_path::PrefixKind, 94 find_path::PrefixKind,
95 import_map, 95 import_map,
@@ -213,7 +213,7 @@ impl Crate {
213 Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text), 213 Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text),
214 _ => None 214 _ => None
215 } 215 }
216 }).flat_map(|t| t).next(); 216 }).flatten().next();
217 217
218 doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/") 218 doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/")
219 } 219 }
@@ -851,13 +851,7 @@ impl Function {
851 .iter() 851 .iter()
852 .enumerate() 852 .enumerate()
853 .map(|(idx, type_ref)| { 853 .map(|(idx, type_ref)| {
854 let ty = Type { 854 let ty = Type { krate, env: environment.clone(), ty: ctx.lower_ty(type_ref) };
855 krate,
856 ty: InEnvironment {
857 value: ctx.lower_ty(type_ref),
858 environment: environment.clone(),
859 },
860 };
861 Param { func: self, ty, idx } 855 Param { func: self, ty, idx }
862 }) 856 })
863 .collect() 857 .collect()
@@ -1144,17 +1138,21 @@ impl MacroDef {
1144 1138
1145 /// XXX: this parses the file 1139 /// XXX: this parses the file
1146 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { 1140 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
1147 self.source(db)?.value.name().map(|it| it.as_name()) 1141 match self.source(db)?.value {
1142 Either::Left(it) => it.name().map(|it| it.as_name()),
1143 Either::Right(it) => it.name().map(|it| it.as_name()),
1144 }
1148 } 1145 }
1149 1146
1150 /// Indicate it is a proc-macro 1147 /// Indicate it is a proc-macro
1151 pub fn is_proc_macro(&self) -> bool { 1148 pub fn is_proc_macro(&self) -> bool {
1152 matches!(self.id.kind, MacroDefKind::ProcMacro(_)) 1149 matches!(self.id.kind, MacroDefKind::ProcMacro(..))
1153 } 1150 }
1154 1151
1155 /// Indicate it is a derive macro 1152 /// Indicate it is a derive macro
1156 pub fn is_derive_macro(&self) -> bool { 1153 pub fn is_derive_macro(&self) -> bool {
1157 matches!(self.id.kind, MacroDefKind::ProcMacro(_) | MacroDefKind::BuiltInDerive(_)) 1154 // FIXME: wrong for `ProcMacro`
1155 matches!(self.id.kind, MacroDefKind::ProcMacro(..) | MacroDefKind::BuiltInDerive(..))
1158 } 1156 }
1159} 1157}
1160 1158
@@ -1456,9 +1454,9 @@ impl TypeParam {
1456 pub fn trait_bounds(self, db: &dyn HirDatabase) -> Vec<Trait> { 1454 pub fn trait_bounds(self, db: &dyn HirDatabase) -> Vec<Trait> {
1457 db.generic_predicates_for_param(self.id) 1455 db.generic_predicates_for_param(self.id)
1458 .into_iter() 1456 .into_iter()
1459 .filter_map(|pred| match &pred.value { 1457 .filter_map(|pred| match &pred.skip_binders().skip_binders() {
1460 hir_ty::GenericPredicate::Implemented(trait_ref) => { 1458 hir_ty::WhereClause::Implemented(trait_ref) => {
1461 Some(Trait::from(trait_ref.trait_)) 1459 Some(Trait::from(trait_ref.hir_trait_id()))
1462 } 1460 }
1463 _ => None, 1461 _ => None,
1464 }) 1462 })
@@ -1536,8 +1534,8 @@ impl Impl {
1536 inherent.all_impls().chain(trait_.all_impls()).map(Self::from).collect() 1534 inherent.all_impls().chain(trait_.all_impls()).map(Self::from).collect()
1537 } 1535 }
1538 1536
1539 pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty }: Type) -> Vec<Impl> { 1537 pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec<Impl> {
1540 let def_crates = match ty.value.def_crates(db, krate) { 1538 let def_crates = match ty.def_crates(db, krate) {
1541 Some(def_crates) => def_crates, 1539 Some(def_crates) => def_crates,
1542 None => return Vec::new(), 1540 None => return Vec::new(),
1543 }; 1541 };
@@ -1545,14 +1543,14 @@ impl Impl {
1545 let filter = |impl_def: &Impl| { 1543 let filter = |impl_def: &Impl| {
1546 let target_ty = impl_def.target_ty(db); 1544 let target_ty = impl_def.target_ty(db);
1547 let rref = target_ty.remove_ref(); 1545 let rref = target_ty.remove_ref();
1548 ty.value.equals_ctor(rref.as_ref().map_or(&target_ty.ty.value, |it| &it.ty.value)) 1546 ty.equals_ctor(rref.as_ref().map_or(&target_ty.ty, |it| &it.ty))
1549 }; 1547 };
1550 1548
1551 let mut all = Vec::new(); 1549 let mut all = Vec::new();
1552 def_crates.iter().for_each(|&id| { 1550 def_crates.iter().for_each(|&id| {
1553 all.extend(db.inherent_impls_in_crate(id).all_impls().map(Self::from).filter(filter)) 1551 all.extend(db.inherent_impls_in_crate(id).all_impls().map(Self::from).filter(filter))
1554 }); 1552 });
1555 let fp = TyFingerprint::for_impl(&ty.value); 1553 let fp = TyFingerprint::for_impl(&ty);
1556 for id in def_crates 1554 for id in def_crates
1557 .iter() 1555 .iter()
1558 .flat_map(|&id| Crate { id }.transitive_reverse_dependencies(db)) 1556 .flat_map(|&id| Crate { id }.transitive_reverse_dependencies(db))
@@ -1639,7 +1637,8 @@ impl Impl {
1639#[derive(Clone, PartialEq, Eq, Debug)] 1637#[derive(Clone, PartialEq, Eq, Debug)]
1640pub struct Type { 1638pub struct Type {
1641 krate: CrateId, 1639 krate: CrateId,
1642 ty: InEnvironment<Ty>, 1640 env: Arc<TraitEnvironment>,
1641 ty: Ty,
1643} 1642}
1644 1643
1645impl Type { 1644impl Type {
@@ -1659,14 +1658,14 @@ impl Type {
1659 ) -> Type { 1658 ) -> Type {
1660 let environment = 1659 let environment =
1661 resolver.generic_def().map_or_else(Default::default, |d| db.trait_environment(d)); 1660 resolver.generic_def().map_or_else(Default::default, |d| db.trait_environment(d));
1662 Type { krate, ty: InEnvironment { value: ty, environment } } 1661 Type { krate, env: environment, ty }
1663 } 1662 }
1664 1663
1665 fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { 1664 fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type {
1666 let resolver = lexical_env.resolver(db.upcast()); 1665 let resolver = lexical_env.resolver(db.upcast());
1667 let environment = 1666 let environment =
1668 resolver.generic_def().map_or_else(Default::default, |d| db.trait_environment(d)); 1667 resolver.generic_def().map_or_else(Default::default, |d| db.trait_environment(d));
1669 Type { krate, ty: InEnvironment { value: ty, environment } } 1668 Type { krate, env: environment, ty }
1670 } 1669 }
1671 1670
1672 fn from_def( 1671 fn from_def(
@@ -1680,29 +1679,29 @@ impl Type {
1680 } 1679 }
1681 1680
1682 pub fn is_unit(&self) -> bool { 1681 pub fn is_unit(&self) -> bool {
1683 matches!(self.ty.value.interned(&Interner), TyKind::Tuple(0, ..)) 1682 matches!(self.ty.interned(&Interner), TyKind::Tuple(0, ..))
1684 } 1683 }
1685 pub fn is_bool(&self) -> bool { 1684 pub fn is_bool(&self) -> bool {
1686 matches!(self.ty.value.interned(&Interner), TyKind::Scalar(Scalar::Bool)) 1685 matches!(self.ty.interned(&Interner), TyKind::Scalar(Scalar::Bool))
1687 } 1686 }
1688 1687
1689 pub fn is_mutable_reference(&self) -> bool { 1688 pub fn is_mutable_reference(&self) -> bool {
1690 matches!(self.ty.value.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..)) 1689 matches!(self.ty.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..))
1691 } 1690 }
1692 1691
1693 pub fn is_usize(&self) -> bool { 1692 pub fn is_usize(&self) -> bool {
1694 matches!(self.ty.value.interned(&Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize))) 1693 matches!(self.ty.interned(&Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize)))
1695 } 1694 }
1696 1695
1697 pub fn remove_ref(&self) -> Option<Type> { 1696 pub fn remove_ref(&self) -> Option<Type> {
1698 match &self.ty.value.interned(&Interner) { 1697 match &self.ty.interned(&Interner) {
1699 TyKind::Ref(.., ty) => Some(self.derived(ty.clone())), 1698 TyKind::Ref(.., ty) => Some(self.derived(ty.clone())),
1700 _ => None, 1699 _ => None,
1701 } 1700 }
1702 } 1701 }
1703 1702
1704 pub fn is_unknown(&self) -> bool { 1703 pub fn is_unknown(&self) -> bool {
1705 self.ty.value.is_unknown() 1704 self.ty.is_unknown()
1706 } 1705 }
1707 1706
1708 /// Checks that particular type `ty` implements `std::future::Future`. 1707 /// Checks that particular type `ty` implements `std::future::Future`.
@@ -1719,11 +1718,12 @@ impl Type {
1719 None => return false, 1718 None => return false,
1720 }; 1719 };
1721 1720
1722 let canonical_ty = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; 1721 let canonical_ty =
1722 Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(&Interner) };
1723 method_resolution::implements_trait( 1723 method_resolution::implements_trait(
1724 &canonical_ty, 1724 &canonical_ty,
1725 db, 1725 db,
1726 self.ty.environment.clone(), 1726 self.env.clone(),
1727 krate, 1727 krate,
1728 std_future_trait, 1728 std_future_trait,
1729 ) 1729 )
@@ -1741,11 +1741,12 @@ impl Type {
1741 None => return false, 1741 None => return false,
1742 }; 1742 };
1743 1743
1744 let canonical_ty = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; 1744 let canonical_ty =
1745 Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(&Interner) };
1745 method_resolution::implements_trait_unique( 1746 method_resolution::implements_trait_unique(
1746 &canonical_ty, 1747 &canonical_ty,
1747 db, 1748 db,
1748 self.ty.environment.clone(), 1749 self.env.clone(),
1749 krate, 1750 krate,
1750 fnonce_trait, 1751 fnonce_trait,
1751 ) 1752 )
@@ -1753,19 +1754,16 @@ impl Type {
1753 1754
1754 pub fn impls_trait(&self, db: &dyn HirDatabase, trait_: Trait, args: &[Type]) -> bool { 1755 pub fn impls_trait(&self, db: &dyn HirDatabase, trait_: Trait, args: &[Type]) -> bool {
1755 let trait_ref = hir_ty::TraitRef { 1756 let trait_ref = hir_ty::TraitRef {
1756 trait_: trait_.id, 1757 trait_id: hir_ty::to_chalk_trait_id(trait_.id),
1757 substs: Substitution::build_for_def(db, trait_.id) 1758 substitution: Substitution::build_for_def(db, trait_.id)
1758 .push(self.ty.value.clone()) 1759 .push(self.ty.clone())
1759 .fill(args.iter().map(|t| t.ty.value.clone())) 1760 .fill(args.iter().map(|t| t.ty.clone()))
1760 .build(), 1761 .build(),
1761 }; 1762 };
1762 1763
1763 let goal = Canonical { 1764 let goal = Canonical {
1764 value: hir_ty::InEnvironment::new( 1765 value: hir_ty::InEnvironment::new(self.env.env.clone(), trait_ref.cast(&Interner)),
1765 self.ty.environment.clone(), 1766 binders: CanonicalVarKinds::empty(&Interner),
1766 hir_ty::Obligation::Trait(trait_ref),
1767 ),
1768 kinds: Arc::new([]),
1769 }; 1767 };
1770 1768
1771 db.trait_solve(self.krate, goal).is_some() 1769 db.trait_solve(self.krate, goal).is_some()
@@ -1779,23 +1777,24 @@ impl Type {
1779 alias: TypeAlias, 1777 alias: TypeAlias,
1780 ) -> Option<Type> { 1778 ) -> Option<Type> {
1781 let subst = Substitution::build_for_def(db, trait_.id) 1779 let subst = Substitution::build_for_def(db, trait_.id)
1782 .push(self.ty.value.clone()) 1780 .push(self.ty.clone())
1783 .fill(args.iter().map(|t| t.ty.value.clone())) 1781 .fill(args.iter().map(|t| t.ty.clone()))
1784 .build(); 1782 .build();
1785 let predicate = ProjectionPredicate { 1783 let goal = Canonical::new(
1786 projection_ty: ProjectionTy { 1784 InEnvironment::new(
1787 associated_ty_id: to_assoc_type_id(alias.id), 1785 self.env.env.clone(),
1788 substitution: subst, 1786 AliasEq {
1789 }, 1787 alias: AliasTy::Projection(ProjectionTy {
1790 ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)).intern(&Interner), 1788 associated_ty_id: to_assoc_type_id(alias.id),
1791 }; 1789 substitution: subst,
1792 let goal = Canonical { 1790 }),
1793 value: InEnvironment::new( 1791 ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))
1794 self.ty.environment.clone(), 1792 .intern(&Interner),
1795 Obligation::Projection(predicate), 1793 }
1794 .cast(&Interner),
1796 ), 1795 ),
1797 kinds: Arc::new([TyVariableKind::General]), 1796 [TyVariableKind::General].iter().copied(),
1798 }; 1797 );
1799 1798
1800 match db.trait_solve(self.krate, goal)? { 1799 match db.trait_solve(self.krate, goal)? {
1801 Solution::Unique(SolutionVariables(subst)) => { 1800 Solution::Unique(SolutionVariables(subst)) => {
@@ -1815,22 +1814,22 @@ impl Type {
1815 } 1814 }
1816 1815
1817 pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> { 1816 pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> {
1818 let def = self.ty.value.callable_def(db); 1817 let def = self.ty.callable_def(db);
1819 1818
1820 let sig = self.ty.value.callable_sig(db)?; 1819 let sig = self.ty.callable_sig(db)?;
1821 Some(Callable { ty: self.clone(), sig, def, is_bound_method: false }) 1820 Some(Callable { ty: self.clone(), sig, def, is_bound_method: false })
1822 } 1821 }
1823 1822
1824 pub fn is_closure(&self) -> bool { 1823 pub fn is_closure(&self) -> bool {
1825 matches!(&self.ty.value.interned(&Interner), TyKind::Closure { .. }) 1824 matches!(&self.ty.interned(&Interner), TyKind::Closure { .. })
1826 } 1825 }
1827 1826
1828 pub fn is_fn(&self) -> bool { 1827 pub fn is_fn(&self) -> bool {
1829 matches!(&self.ty.value.interned(&Interner), TyKind::FnDef(..) | TyKind::Function { .. }) 1828 matches!(&self.ty.interned(&Interner), TyKind::FnDef(..) | TyKind::Function { .. })
1830 } 1829 }
1831 1830
1832 pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { 1831 pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
1833 let adt_id = match self.ty.value.interned(&Interner) { 1832 let adt_id = match self.ty.interned(&Interner) {
1834 &TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, 1833 &TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id,
1835 _ => return false, 1834 _ => return false,
1836 }; 1835 };
@@ -1843,11 +1842,11 @@ impl Type {
1843 } 1842 }
1844 1843
1845 pub fn is_raw_ptr(&self) -> bool { 1844 pub fn is_raw_ptr(&self) -> bool {
1846 matches!(&self.ty.value.interned(&Interner), TyKind::Raw(..)) 1845 matches!(&self.ty.interned(&Interner), TyKind::Raw(..))
1847 } 1846 }
1848 1847
1849 pub fn contains_unknown(&self) -> bool { 1848 pub fn contains_unknown(&self) -> bool {
1850 return go(&self.ty.value); 1849 return go(&self.ty);
1851 1850
1852 fn go(ty: &Ty) -> bool { 1851 fn go(ty: &Ty) -> bool {
1853 match ty.interned(&Interner) { 1852 match ty.interned(&Interner) {
@@ -1879,7 +1878,7 @@ impl Type {
1879 } 1878 }
1880 1879
1881 pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { 1880 pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
1882 let (variant_id, substs) = match self.ty.value.interned(&Interner) { 1881 let (variant_id, substs) = match self.ty.interned(&Interner) {
1883 &TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs), 1882 &TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs),
1884 &TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs), 1883 &TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs),
1885 _ => return Vec::new(), 1884 _ => return Vec::new(),
@@ -1896,7 +1895,7 @@ impl Type {
1896 } 1895 }
1897 1896
1898 pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { 1897 pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> {
1899 if let TyKind::Tuple(_, substs) = &self.ty.value.interned(&Interner) { 1898 if let TyKind::Tuple(_, substs) = &self.ty.interned(&Interner) {
1900 substs.iter().map(|ty| self.derived(ty.clone())).collect() 1899 substs.iter().map(|ty| self.derived(ty.clone())).collect()
1901 } else { 1900 } else {
1902 Vec::new() 1901 Vec::new()
@@ -1906,9 +1905,10 @@ impl Type {
1906 pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a { 1905 pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a {
1907 // There should be no inference vars in types passed here 1906 // There should be no inference vars in types passed here
1908 // FIXME check that? 1907 // FIXME check that?
1909 let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; 1908 let canonical =
1910 let environment = self.ty.environment.clone(); 1909 Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(&Interner) };
1911 let ty = InEnvironment { value: canonical, environment }; 1910 let environment = self.env.env.clone();
1911 let ty = InEnvironment { goal: canonical, environment };
1912 autoderef(db, Some(self.krate), ty) 1912 autoderef(db, Some(self.krate), ty)
1913 .map(|canonical| canonical.value) 1913 .map(|canonical| canonical.value)
1914 .map(move |ty| self.derived(ty)) 1914 .map(move |ty| self.derived(ty))
@@ -1922,10 +1922,10 @@ impl Type {
1922 krate: Crate, 1922 krate: Crate,
1923 mut callback: impl FnMut(AssocItem) -> Option<T>, 1923 mut callback: impl FnMut(AssocItem) -> Option<T>,
1924 ) -> Option<T> { 1924 ) -> Option<T> {
1925 for krate in self.ty.value.def_crates(db, krate.id)? { 1925 for krate in self.ty.def_crates(db, krate.id)? {
1926 let impls = db.inherent_impls_in_crate(krate); 1926 let impls = db.inherent_impls_in_crate(krate);
1927 1927
1928 for impl_def in impls.for_self_ty(&self.ty.value) { 1928 for impl_def in impls.for_self_ty(&self.ty) {
1929 for &item in db.impl_data(*impl_def).items.iter() { 1929 for &item in db.impl_data(*impl_def).items.iter() {
1930 if let Some(result) = callback(item.into()) { 1930 if let Some(result) = callback(item.into()) {
1931 return Some(result); 1931 return Some(result);
@@ -1938,7 +1938,6 @@ impl Type {
1938 1938
1939 pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { 1939 pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ {
1940 self.ty 1940 self.ty
1941 .value
1942 .strip_references() 1941 .strip_references()
1943 .substs() 1942 .substs()
1944 .into_iter() 1943 .into_iter()
@@ -1957,9 +1956,10 @@ impl Type {
1957 // There should be no inference vars in types passed here 1956 // There should be no inference vars in types passed here
1958 // FIXME check that? 1957 // FIXME check that?
1959 // FIXME replace Unknown by bound vars here 1958 // FIXME replace Unknown by bound vars here
1960 let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; 1959 let canonical =
1960 Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(&Interner) };
1961 1961
1962 let env = self.ty.environment.clone(); 1962 let env = self.env.clone();
1963 let krate = krate.id; 1963 let krate = krate.id;
1964 1964
1965 method_resolution::iterate_method_candidates( 1965 method_resolution::iterate_method_candidates(
@@ -1988,9 +1988,10 @@ impl Type {
1988 // There should be no inference vars in types passed here 1988 // There should be no inference vars in types passed here
1989 // FIXME check that? 1989 // FIXME check that?
1990 // FIXME replace Unknown by bound vars here 1990 // FIXME replace Unknown by bound vars here
1991 let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; 1991 let canonical =
1992 Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(&Interner) };
1992 1993
1993 let env = self.ty.environment.clone(); 1994 let env = self.env.clone();
1994 let krate = krate.id; 1995 let krate = krate.id;
1995 1996
1996 method_resolution::iterate_method_candidates( 1997 method_resolution::iterate_method_candidates(
@@ -2006,20 +2007,20 @@ impl Type {
2006 } 2007 }
2007 2008
2008 pub fn as_adt(&self) -> Option<Adt> { 2009 pub fn as_adt(&self) -> Option<Adt> {
2009 let (adt, _subst) = self.ty.value.as_adt()?; 2010 let (adt, _subst) = self.ty.as_adt()?;
2010 Some(adt.into()) 2011 Some(adt.into())
2011 } 2012 }
2012 2013
2013 pub fn as_dyn_trait(&self) -> Option<Trait> { 2014 pub fn as_dyn_trait(&self) -> Option<Trait> {
2014 self.ty.value.dyn_trait().map(Into::into) 2015 self.ty.dyn_trait().map(Into::into)
2015 } 2016 }
2016 2017
2017 pub fn as_impl_traits(&self, db: &dyn HirDatabase) -> Option<Vec<Trait>> { 2018 pub fn as_impl_traits(&self, db: &dyn HirDatabase) -> Option<Vec<Trait>> {
2018 self.ty.value.impl_trait_bounds(db).map(|it| { 2019 self.ty.impl_trait_bounds(db).map(|it| {
2019 it.into_iter() 2020 it.into_iter()
2020 .filter_map(|pred| match pred { 2021 .filter_map(|pred| match pred.skip_binders() {
2021 hir_ty::GenericPredicate::Implemented(trait_ref) => { 2022 hir_ty::WhereClause::Implemented(trait_ref) => {
2022 Some(Trait::from(trait_ref.trait_)) 2023 Some(Trait::from(trait_ref.hir_trait_id()))
2023 } 2024 }
2024 _ => None, 2025 _ => None,
2025 }) 2026 })
@@ -2028,14 +2029,11 @@ impl Type {
2028 } 2029 }
2029 2030
2030 pub fn as_associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<Trait> { 2031 pub fn as_associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<Trait> {
2031 self.ty.value.associated_type_parent_trait(db).map(Into::into) 2032 self.ty.associated_type_parent_trait(db).map(Into::into)
2032 } 2033 }
2033 2034
2034 fn derived(&self, ty: Ty) -> Type { 2035 fn derived(&self, ty: Ty) -> Type {
2035 Type { 2036 Type { krate: self.krate, env: self.env.clone(), ty }
2036 krate: self.krate,
2037 ty: InEnvironment { value: ty, environment: self.ty.environment.clone() },
2038 }
2039 } 2037 }
2040 2038
2041 pub fn walk(&self, db: &dyn HirDatabase, mut cb: impl FnMut(Type)) { 2039 pub fn walk(&self, db: &dyn HirDatabase, mut cb: impl FnMut(Type)) {
@@ -2056,14 +2054,17 @@ impl Type {
2056 fn walk_bounds( 2054 fn walk_bounds(
2057 db: &dyn HirDatabase, 2055 db: &dyn HirDatabase,
2058 type_: &Type, 2056 type_: &Type,
2059 bounds: &[GenericPredicate], 2057 bounds: &[QuantifiedWhereClause],
2060 cb: &mut impl FnMut(Type), 2058 cb: &mut impl FnMut(Type),
2061 ) { 2059 ) {
2062 for pred in bounds { 2060 for pred in bounds {
2063 match pred { 2061 match pred.skip_binders() {
2064 GenericPredicate::Implemented(trait_ref) => { 2062 WhereClause::Implemented(trait_ref) => {
2065 cb(type_.clone()); 2063 cb(type_.clone());
2066 walk_substs(db, type_, &trait_ref.substs, cb); 2064 // skip the self type. it's likely the type we just got the bounds from
2065 for ty in trait_ref.substitution.iter().skip(1) {
2066 walk_type(db, &type_.derived(ty.clone()), cb);
2067 }
2067 } 2068 }
2068 _ => (), 2069 _ => (),
2069 } 2070 }
@@ -2071,7 +2072,7 @@ impl Type {
2071 } 2072 }
2072 2073
2073 fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { 2074 fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) {
2074 let ty = type_.ty.value.strip_references(); 2075 let ty = type_.ty.strip_references();
2075 match ty.interned(&Interner) { 2076 match ty.interned(&Interner) {
2076 TyKind::Adt(..) => { 2077 TyKind::Adt(..) => {
2077 cb(type_.derived(ty.clone())); 2078 cb(type_.derived(ty.clone()));
@@ -2099,7 +2100,12 @@ impl Type {
2099 } 2100 }
2100 } 2101 }
2101 TyKind::Dyn(bounds) => { 2102 TyKind::Dyn(bounds) => {
2102 walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb); 2103 walk_bounds(
2104 db,
2105 &type_.derived(ty.clone()),
2106 bounds.bounds.skip_binders().interned(),
2107 cb,
2108 );
2103 } 2109 }
2104 2110
2105 TyKind::Ref(_, ty) | TyKind::Raw(_, ty) | TyKind::Array(ty) | TyKind::Slice(ty) => { 2111 TyKind::Ref(_, ty) | TyKind::Raw(_, ty) | TyKind::Array(ty) | TyKind::Slice(ty) => {
@@ -2194,6 +2200,7 @@ pub enum ScopeDef {
2194 ImplSelfType(Impl), 2200 ImplSelfType(Impl),
2195 AdtSelfType(Adt), 2201 AdtSelfType(Adt),
2196 Local(Local), 2202 Local(Local),
2203 Label(Label),
2197 Unknown, 2204 Unknown,
2198} 2205}
2199 2206
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs
index 15651bb22..1198e3f0b 100644
--- a/crates/hir/src/semantics.rs
+++ b/crates/hir/src/semantics.rs
@@ -839,6 +839,10 @@ impl<'a> SemanticsScope<'a> {
839 let parent = resolver.body_owner().unwrap(); 839 let parent = resolver.body_owner().unwrap();
840 ScopeDef::Local(Local { parent, pat_id }) 840 ScopeDef::Local(Local { parent, pat_id })
841 } 841 }
842 resolver::ScopeDef::Label(label_id) => {
843 let parent = resolver.body_owner().unwrap();
844 ScopeDef::Label(Label { parent, label_id })
845 }
842 }; 846 };
843 f(name, def) 847 f(name, def)
844 }) 848 })
diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs
index c6ad5ecb5..762809fcd 100644
--- a/crates/hir/src/semantics/source_to_def.rs
+++ b/crates/hir/src/semantics/source_to_def.rs
@@ -195,12 +195,12 @@ impl SourceToDefCtx<'_, '_> {
195 &mut self, 195 &mut self,
196 src: InFile<ast::MacroRules>, 196 src: InFile<ast::MacroRules>,
197 ) -> Option<MacroDefId> { 197 ) -> Option<MacroDefId> {
198 let kind = MacroDefKind::Declarative; 198 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());
200 let kind = MacroDefKind::Declarative(ast_id);
199 let file_id = src.file_id.original_file(self.db.upcast()); 201 let file_id = src.file_id.original_file(self.db.upcast());
200 let krate = self.file_to_def(file_id).get(0).copied()?.krate(); 202 let krate = self.file_to_def(file_id).get(0).copied()?.krate();
201 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value); 203 Some(MacroDefId { krate, kind, local_inner: false })
202 let ast_id = Some(AstId::new(src.file_id, file_ast_id.upcast()));
203 Some(MacroDefId { krate, ast_id, kind, local_inner: false })
204 } 204 }
205 205
206 pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> { 206 pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {