diff options
79 files changed, 2738 insertions, 1659 deletions
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1850068a3..0f68b234c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml | |||
@@ -76,7 +76,7 @@ jobs: | |||
76 | run: cargo test --no-run --locked | 76 | run: cargo test --no-run --locked |
77 | 77 | ||
78 | - name: Test | 78 | - name: Test |
79 | run: cargo test | 79 | run: cargo test -- --nocapture |
80 | 80 | ||
81 | - name: Prepare cache | 81 | - name: Prepare cache |
82 | run: cargo xtask pre-cache | 82 | run: cargo xtask pre-cache |
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 817a01db1..eba46a056 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -1580,11 +1580,24 @@ impl Impl { | |||
1580 | ty.equals_ctor(rref.as_ref().map_or(&self_ty.ty, |it| &it.ty)) | 1580 | ty.equals_ctor(rref.as_ref().map_or(&self_ty.ty, |it| &it.ty)) |
1581 | }; | 1581 | }; |
1582 | 1582 | ||
1583 | let fp = TyFingerprint::for_inherent_impl(&ty); | ||
1584 | let fp = if let Some(fp) = fp { | ||
1585 | fp | ||
1586 | } else { | ||
1587 | return Vec::new(); | ||
1588 | }; | ||
1589 | |||
1583 | let mut all = Vec::new(); | 1590 | let mut all = Vec::new(); |
1584 | def_crates.iter().for_each(|&id| { | 1591 | def_crates.iter().for_each(|&id| { |
1585 | all.extend(db.inherent_impls_in_crate(id).all_impls().map(Self::from).filter(filter)) | 1592 | all.extend( |
1593 | db.inherent_impls_in_crate(id) | ||
1594 | .for_self_ty(&ty) | ||
1595 | .into_iter() | ||
1596 | .cloned() | ||
1597 | .map(Self::from) | ||
1598 | .filter(filter), | ||
1599 | ) | ||
1586 | }); | 1600 | }); |
1587 | let fp = TyFingerprint::for_impl(&ty); | ||
1588 | for id in def_crates | 1601 | for id in def_crates |
1589 | .iter() | 1602 | .iter() |
1590 | .flat_map(|&id| Crate { id }.transitive_reverse_dependencies(db)) | 1603 | .flat_map(|&id| Crate { id }.transitive_reverse_dependencies(db)) |
@@ -1592,13 +1605,12 @@ impl Impl { | |||
1592 | .chain(def_crates.iter().copied()) | 1605 | .chain(def_crates.iter().copied()) |
1593 | .unique() | 1606 | .unique() |
1594 | { | 1607 | { |
1595 | match fp { | 1608 | all.extend( |
1596 | Some(fp) => all.extend( | 1609 | db.trait_impls_in_crate(id) |
1597 | db.trait_impls_in_crate(id).for_self_ty(fp).map(Self::from).filter(filter), | 1610 | .for_self_ty_without_blanket_impls(fp) |
1598 | ), | 1611 | .map(Self::from) |
1599 | None => all | 1612 | .filter(filter), |
1600 | .extend(db.trait_impls_in_crate(id).all_impls().map(Self::from).filter(filter)), | 1613 | ); |
1601 | } | ||
1602 | } | 1614 | } |
1603 | all | 1615 | all |
1604 | } | 1616 | } |
@@ -1825,7 +1837,7 @@ impl Type { | |||
1825 | Solution::Unique(s) => s | 1837 | Solution::Unique(s) => s |
1826 | .value | 1838 | .value |
1827 | .subst | 1839 | .subst |
1828 | .interned() | 1840 | .as_slice(&Interner) |
1829 | .first() | 1841 | .first() |
1830 | .map(|ty| self.derived(ty.assert_ty_ref(&Interner).clone())), | 1842 | .map(|ty| self.derived(ty.assert_ty_ref(&Interner).clone())), |
1831 | Solution::Ambig(_) => None, | 1843 | Solution::Ambig(_) => None, |
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index d9df7564d..786fad6e1 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs | |||
@@ -9,7 +9,7 @@ use std::{ | |||
9 | use base_db::CrateId; | 9 | use base_db::CrateId; |
10 | use cfg::{CfgExpr, CfgOptions}; | 10 | use cfg::{CfgExpr, CfgOptions}; |
11 | use either::Either; | 11 | use either::Either; |
12 | use hir_expand::{hygiene::Hygiene, name::AsName, AstId, InFile}; | 12 | use hir_expand::{hygiene::Hygiene, name::AsName, AstId, AttrId, InFile}; |
13 | use itertools::Itertools; | 13 | use itertools::Itertools; |
14 | use la_arena::ArenaMap; | 14 | use la_arena::ArenaMap; |
15 | use mbe::ast_to_token_tree; | 15 | use mbe::ast_to_token_tree; |
@@ -98,13 +98,16 @@ impl RawAttrs { | |||
98 | pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self { | 98 | pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self { |
99 | let entries = collect_attrs(owner) | 99 | let entries = collect_attrs(owner) |
100 | .enumerate() | 100 | .enumerate() |
101 | .flat_map(|(i, attr)| match attr { | 101 | .flat_map(|(i, attr)| { |
102 | Either::Left(attr) => Attr::from_src(attr, hygiene, i as u32), | 102 | let index = AttrId(i as u32); |
103 | Either::Right(comment) => comment.doc_comment().map(|doc| Attr { | 103 | match attr { |
104 | index: i as u32, | 104 | Either::Left(attr) => Attr::from_src(attr, hygiene, index), |
105 | input: Some(AttrInput::Literal(SmolStr::new(doc))), | 105 | Either::Right(comment) => comment.doc_comment().map(|doc| Attr { |
106 | path: Interned::new(ModPath::from(hir_expand::name!(doc))), | 106 | id: index, |
107 | }), | 107 | input: Some(AttrInput::Literal(SmolStr::new(doc))), |
108 | path: Interned::new(ModPath::from(hir_expand::name!(doc))), | ||
109 | }), | ||
110 | } | ||
108 | }) | 111 | }) |
109 | .collect::<Arc<_>>(); | 112 | .collect::<Arc<_>>(); |
110 | 113 | ||
@@ -161,7 +164,7 @@ impl RawAttrs { | |||
161 | let cfg = parts.next().unwrap(); | 164 | let cfg = parts.next().unwrap(); |
162 | let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() }; | 165 | let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() }; |
163 | let cfg = CfgExpr::parse(&cfg); | 166 | let cfg = CfgExpr::parse(&cfg); |
164 | let index = attr.index; | 167 | let index = attr.id; |
165 | let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| { | 168 | let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| { |
166 | let tree = Subtree { delimiter: None, token_trees: attr.to_vec() }; | 169 | let tree = Subtree { delimiter: None, token_trees: attr.to_vec() }; |
167 | let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?; | 170 | let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?; |
@@ -468,7 +471,7 @@ impl AttrsWithOwner { | |||
468 | ) -> Option<(Documentation, DocsRangeMap)> { | 471 | ) -> Option<(Documentation, DocsRangeMap)> { |
469 | // FIXME: code duplication in `docs` above | 472 | // FIXME: code duplication in `docs` above |
470 | let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? { | 473 | let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? { |
471 | AttrInput::Literal(s) => Some((s, attr.index)), | 474 | AttrInput::Literal(s) => Some((s, attr.id)), |
472 | AttrInput::TokenTree(_) => None, | 475 | AttrInput::TokenTree(_) => None, |
473 | }); | 476 | }); |
474 | let indent = docs | 477 | let indent = docs |
@@ -560,8 +563,8 @@ impl AttrSourceMap { | |||
560 | /// the attribute represented by `Attr`. | 563 | /// the attribute represented by `Attr`. |
561 | pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> { | 564 | pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> { |
562 | self.attrs | 565 | self.attrs |
563 | .get(attr.index as usize) | 566 | .get(attr.id.0 as usize) |
564 | .unwrap_or_else(|| panic!("cannot find `Attr` at index {}", attr.index)) | 567 | .unwrap_or_else(|| panic!("cannot find `Attr` at index {:?}", attr.id)) |
565 | .as_ref() | 568 | .as_ref() |
566 | } | 569 | } |
567 | } | 570 | } |
@@ -572,7 +575,7 @@ pub struct DocsRangeMap { | |||
572 | // (docstring-line-range, attr_index, attr-string-range) | 575 | // (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 | 576 | // a mapping from the text range of a line of the [`Documentation`] to the attribute index and |
574 | // the original (untrimmed) syntax doc line | 577 | // the original (untrimmed) syntax doc line |
575 | mapping: Vec<(TextRange, u32, TextRange)>, | 578 | mapping: Vec<(TextRange, AttrId, TextRange)>, |
576 | } | 579 | } |
577 | 580 | ||
578 | impl DocsRangeMap { | 581 | impl DocsRangeMap { |
@@ -585,7 +588,7 @@ impl DocsRangeMap { | |||
585 | 588 | ||
586 | let relative_range = range - line_docs_range.start(); | 589 | let relative_range = range - line_docs_range.start(); |
587 | 590 | ||
588 | let &InFile { file_id, value: ref source } = &self.source[idx as usize]; | 591 | let &InFile { file_id, value: ref source } = &self.source[idx.0 as usize]; |
589 | match source { | 592 | match source { |
590 | Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here | 593 | 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 | 594 | // as well as for whats done in syntax highlight doc injection |
@@ -606,7 +609,7 @@ impl DocsRangeMap { | |||
606 | 609 | ||
607 | #[derive(Debug, Clone, PartialEq, Eq)] | 610 | #[derive(Debug, Clone, PartialEq, Eq)] |
608 | pub struct Attr { | 611 | pub struct Attr { |
609 | index: u32, | 612 | pub(crate) id: AttrId, |
610 | pub(crate) path: Interned<ModPath>, | 613 | pub(crate) path: Interned<ModPath>, |
611 | pub(crate) input: Option<AttrInput>, | 614 | pub(crate) input: Option<AttrInput>, |
612 | } | 615 | } |
@@ -620,7 +623,7 @@ pub enum AttrInput { | |||
620 | } | 623 | } |
621 | 624 | ||
622 | impl Attr { | 625 | impl Attr { |
623 | fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> { | 626 | fn from_src(ast: ast::Attr, hygiene: &Hygiene, id: AttrId) -> Option<Attr> { |
624 | let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?); | 627 | let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?); |
625 | let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { | 628 | let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { |
626 | let value = match lit.kind() { | 629 | let value = match lit.kind() { |
@@ -633,7 +636,7 @@ impl Attr { | |||
633 | } else { | 636 | } else { |
634 | None | 637 | None |
635 | }; | 638 | }; |
636 | Some(Attr { index, path, input }) | 639 | Some(Attr { id, path, input }) |
637 | } | 640 | } |
638 | 641 | ||
639 | /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths | 642 | /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths |
@@ -748,9 +751,7 @@ fn collect_attrs( | |||
748 | .chain(inner_docs.into_iter().flatten()) | 751 | .chain(inner_docs.into_iter().flatten()) |
749 | .map(|docs_text| (docs_text.syntax().text_range().start(), Either::Right(docs_text))); | 752 | .map(|docs_text| (docs_text.syntax().text_range().start(), Either::Right(docs_text))); |
750 | // sort here by syntax node offset because the source can have doc attributes and doc strings be interleaved | 753 | // sort here by syntax node offset because the source can have doc attributes and doc strings be interleaved |
751 | let attrs: Vec<_> = docs.chain(attrs).sorted_by_key(|&(offset, _)| offset).collect(); | 754 | docs.chain(attrs).sorted_by_key(|&(offset, _)| offset).map(|(_, attr)| attr) |
752 | |||
753 | attrs.into_iter().map(|(_, attr)| attr) | ||
754 | } | 755 | } |
755 | 756 | ||
756 | pub(crate) fn variants_attrs_source_map( | 757 | pub(crate) fn variants_attrs_source_map( |
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index bfb75a8a5..ed07d6928 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs | |||
@@ -531,8 +531,9 @@ impl ExprCollector<'_> { | |||
531 | } | 531 | } |
532 | } | 532 | } |
533 | ast::Expr::MacroCall(e) => { | 533 | ast::Expr::MacroCall(e) => { |
534 | let macro_ptr = AstPtr::new(&e); | ||
534 | let mut ids = vec![]; | 535 | let mut ids = vec![]; |
535 | self.collect_macro_call(e, syntax_ptr.clone(), true, |this, expansion| { | 536 | self.collect_macro_call(e, macro_ptr, true, |this, expansion| { |
536 | ids.push(match expansion { | 537 | ids.push(match expansion { |
537 | Some(it) => this.collect_expr(it), | 538 | Some(it) => this.collect_expr(it), |
538 | None => this.alloc_expr(Expr::Missing, syntax_ptr.clone()), | 539 | None => this.alloc_expr(Expr::Missing, syntax_ptr.clone()), |
@@ -555,7 +556,7 @@ impl ExprCollector<'_> { | |||
555 | fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>( | 556 | fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>( |
556 | &mut self, | 557 | &mut self, |
557 | e: ast::MacroCall, | 558 | e: ast::MacroCall, |
558 | syntax_ptr: AstPtr<ast::Expr>, | 559 | syntax_ptr: AstPtr<ast::MacroCall>, |
559 | is_error_recoverable: bool, | 560 | is_error_recoverable: bool, |
560 | mut collector: F, | 561 | mut collector: F, |
561 | ) { | 562 | ) { |
@@ -643,10 +644,14 @@ impl ExprCollector<'_> { | |||
643 | 644 | ||
644 | // Note that macro could be expended to multiple statements | 645 | // Note that macro could be expended to multiple statements |
645 | if let Some(ast::Expr::MacroCall(m)) = stmt.expr() { | 646 | if let Some(ast::Expr::MacroCall(m)) = stmt.expr() { |
647 | let macro_ptr = AstPtr::new(&m); | ||
646 | let syntax_ptr = AstPtr::new(&stmt.expr().unwrap()); | 648 | let syntax_ptr = AstPtr::new(&stmt.expr().unwrap()); |
647 | 649 | ||
648 | self.collect_macro_call(m, syntax_ptr.clone(), false, |this, expansion| { | 650 | self.collect_macro_call( |
649 | match expansion { | 651 | m, |
652 | macro_ptr, | ||
653 | false, | ||
654 | |this, expansion| match expansion { | ||
650 | Some(expansion) => { | 655 | Some(expansion) => { |
651 | let statements: ast::MacroStmts = expansion; | 656 | let statements: ast::MacroStmts = expansion; |
652 | 657 | ||
@@ -660,8 +665,8 @@ impl ExprCollector<'_> { | |||
660 | let expr = this.alloc_expr(Expr::Missing, syntax_ptr.clone()); | 665 | let expr = this.alloc_expr(Expr::Missing, syntax_ptr.clone()); |
661 | this.statements_in_scope.push(Statement::Expr(expr)); | 666 | this.statements_in_scope.push(Statement::Expr(expr)); |
662 | } | 667 | } |
663 | } | 668 | }, |
664 | }); | 669 | ); |
665 | } else { | 670 | } else { |
666 | let expr = self.collect_expr_opt(stmt.expr()); | 671 | let expr = self.collect_expr_opt(stmt.expr()); |
667 | self.statements_in_scope.push(Statement::Expr(expr)); | 672 | self.statements_in_scope.push(Statement::Expr(expr)); |
@@ -848,8 +853,23 @@ impl ExprCollector<'_> { | |||
848 | Pat::Missing | 853 | Pat::Missing |
849 | } | 854 | } |
850 | } | 855 | } |
856 | ast::Pat::MacroPat(mac) => match mac.macro_call() { | ||
857 | Some(call) => { | ||
858 | let macro_ptr = AstPtr::new(&call); | ||
859 | let mut pat = None; | ||
860 | self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| { | ||
861 | pat = Some(this.collect_pat_opt(expanded_pat)); | ||
862 | }); | ||
863 | |||
864 | match pat { | ||
865 | Some(pat) => return pat, | ||
866 | None => Pat::Missing, | ||
867 | } | ||
868 | } | ||
869 | None => Pat::Missing, | ||
870 | }, | ||
851 | // FIXME: implement | 871 | // FIXME: implement |
852 | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, | 872 | ast::Pat::RangePat(_) => Pat::Missing, |
853 | }; | 873 | }; |
854 | let ptr = AstPtr::new(&pat); | 874 | let ptr = AstPtr::new(&pat); |
855 | self.alloc_pat(pattern, Either::Left(ptr)) | 875 | self.alloc_pat(pattern, Either::Left(ptr)) |
diff --git a/crates/hir_def/src/child_by_source.rs b/crates/hir_def/src/child_by_source.rs index f40a7f80d..f2e809ca9 100644 --- a/crates/hir_def/src/child_by_source.rs +++ b/crates/hir_def/src/child_by_source.rs | |||
@@ -80,6 +80,10 @@ impl ChildBySource for ModuleId { | |||
80 | impl ChildBySource for ItemScope { | 80 | impl ChildBySource for ItemScope { |
81 | fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) { | 81 | fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) { |
82 | self.declarations().for_each(|item| add_module_def(db, res, item)); | 82 | self.declarations().for_each(|item| add_module_def(db, res, item)); |
83 | self.unnamed_consts().for_each(|konst| { | ||
84 | let src = konst.lookup(db).source(db); | ||
85 | res[keys::CONST].insert(src, konst); | ||
86 | }); | ||
83 | self.impls().for_each(|imp| add_impl(db, res, imp)); | 87 | self.impls().for_each(|imp| add_impl(db, res, imp)); |
84 | 88 | ||
85 | fn add_module_def(db: &dyn DefDatabase, map: &mut DynMap, item: ModuleDefId) { | 89 | fn add_module_def(db: &dyn DefDatabase, map: &mut DynMap, item: ModuleDefId) { |
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 240662486..94e08f835 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs | |||
@@ -99,6 +99,11 @@ impl ItemTree { | |||
99 | // items. | 99 | // items. |
100 | ctx.lower_macro_stmts(stmts) | 100 | ctx.lower_macro_stmts(stmts) |
101 | }, | 101 | }, |
102 | ast::Pat(_pat) => { | ||
103 | // FIXME: This occurs because macros in pattern position are treated as inner | ||
104 | // items and expanded during block DefMap computation | ||
105 | return Default::default(); | ||
106 | }, | ||
102 | ast::Expr(e) => { | 107 | ast::Expr(e) => { |
103 | // Macros can expand to expressions. We return an empty item tree in this case, but | 108 | // Macros can expand to expressions. We return an empty item tree in this case, but |
104 | // still need to collect inner items. | 109 | // still need to collect inner items. |
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index c5629af24..45b099cf3 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs | |||
@@ -189,7 +189,7 @@ impl Ctx { | |||
189 | block_stack.push(self.source_ast_id_map.ast_id(&block)); | 189 | block_stack.push(self.source_ast_id_map.ast_id(&block)); |
190 | }, | 190 | }, |
191 | ast::Item(item) => { | 191 | ast::Item(item) => { |
192 | // FIXME: This triggers for macro calls in expression position | 192 | // FIXME: This triggers for macro calls in expression/pattern/type position |
193 | let mod_items = self.lower_mod_item(&item, true); | 193 | let mod_items = self.lower_mod_item(&item, true); |
194 | let current_block = block_stack.last(); | 194 | let current_block = block_stack.last(); |
195 | if let (Some(mod_items), Some(block)) = (mod_items, current_block) { | 195 | if let (Some(mod_items), Some(block)) = (mod_items, current_block) { |
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index e2af0e514..d69116d51 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -62,7 +62,7 @@ use hir_expand::{ | |||
62 | ast_id_map::FileAstId, | 62 | ast_id_map::FileAstId, |
63 | eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, | 63 | eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, |
64 | hygiene::Hygiene, | 64 | hygiene::Hygiene, |
65 | AstId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 65 | AstId, AttrId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, |
66 | }; | 66 | }; |
67 | use la_arena::Idx; | 67 | use la_arena::Idx; |
68 | use nameres::DefMap; | 68 | use nameres::DefMap; |
@@ -690,13 +690,16 @@ fn macro_call_as_call_id( | |||
690 | ) | 690 | ) |
691 | .map(MacroCallId::from) | 691 | .map(MacroCallId::from) |
692 | } else { | 692 | } else { |
693 | Ok(def.as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike(call.ast_id)).into()) | 693 | Ok(def |
694 | .as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike { ast_id: call.ast_id }) | ||
695 | .into()) | ||
694 | }; | 696 | }; |
695 | Ok(res) | 697 | Ok(res) |
696 | } | 698 | } |
697 | 699 | ||
698 | fn derive_macro_as_call_id( | 700 | fn derive_macro_as_call_id( |
699 | item_attr: &AstIdWithPath<ast::Item>, | 701 | item_attr: &AstIdWithPath<ast::Item>, |
702 | derive_attr: AttrId, | ||
700 | db: &dyn db::DefDatabase, | 703 | db: &dyn db::DefDatabase, |
701 | krate: CrateId, | 704 | krate: CrateId, |
702 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, | 705 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, |
@@ -707,7 +710,11 @@ fn derive_macro_as_call_id( | |||
707 | .as_lazy_macro( | 710 | .as_lazy_macro( |
708 | db.upcast(), | 711 | db.upcast(), |
709 | krate, | 712 | krate, |
710 | MacroCallKind::Derive(item_attr.ast_id, last_segment.to_string()), | 713 | MacroCallKind::Derive { |
714 | ast_id: item_attr.ast_id, | ||
715 | derive_name: last_segment.to_string(), | ||
716 | derive_attr, | ||
717 | }, | ||
711 | ) | 718 | ) |
712 | .into(); | 719 | .into(); |
713 | Ok(res) | 720 | Ok(res) |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 7dd68219f..9e181751c 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -613,12 +613,12 @@ mod diagnostics { | |||
613 | DiagnosticKind::UnresolvedProcMacro { ast } => { | 613 | DiagnosticKind::UnresolvedProcMacro { ast } => { |
614 | let mut precise_location = None; | 614 | let mut precise_location = None; |
615 | let (file, ast, name) = match ast { | 615 | let (file, ast, name) = match ast { |
616 | MacroCallKind::FnLike(ast) => { | 616 | MacroCallKind::FnLike { ast_id } => { |
617 | let node = ast.to_node(db.upcast()); | 617 | let node = ast_id.to_node(db.upcast()); |
618 | (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) | 618 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) |
619 | } | 619 | } |
620 | MacroCallKind::Derive(ast, name) => { | 620 | MacroCallKind::Derive { ast_id, derive_name, .. } => { |
621 | let node = ast.to_node(db.upcast()); | 621 | let node = ast_id.to_node(db.upcast()); |
622 | 622 | ||
623 | // Compute the precise location of the macro name's token in the derive | 623 | // Compute the precise location of the macro name's token in the derive |
624 | // list. | 624 | // list. |
@@ -639,7 +639,7 @@ mod diagnostics { | |||
639 | }); | 639 | }); |
640 | for token in tokens { | 640 | for token in tokens { |
641 | if token.kind() == SyntaxKind::IDENT | 641 | if token.kind() == SyntaxKind::IDENT |
642 | && token.text() == name.as_str() | 642 | && token.text() == derive_name.as_str() |
643 | { | 643 | { |
644 | precise_location = Some(token.text_range()); | 644 | precise_location = Some(token.text_range()); |
645 | break 'outer; | 645 | break 'outer; |
@@ -648,9 +648,9 @@ mod diagnostics { | |||
648 | } | 648 | } |
649 | 649 | ||
650 | ( | 650 | ( |
651 | ast.file_id, | 651 | ast_id.file_id, |
652 | SyntaxNodePtr::from(AstPtr::new(&node)), | 652 | SyntaxNodePtr::from(AstPtr::new(&node)), |
653 | Some(name.clone()), | 653 | Some(derive_name.clone()), |
654 | ) | 654 | ) |
655 | } | 655 | } |
656 | }; | 656 | }; |
@@ -669,13 +669,13 @@ mod diagnostics { | |||
669 | 669 | ||
670 | DiagnosticKind::MacroError { ast, message } => { | 670 | DiagnosticKind::MacroError { ast, message } => { |
671 | let (file, ast) = match ast { | 671 | let (file, ast) = match ast { |
672 | MacroCallKind::FnLike(ast) => { | 672 | MacroCallKind::FnLike { ast_id, .. } => { |
673 | let node = ast.to_node(db.upcast()); | 673 | let node = ast_id.to_node(db.upcast()); |
674 | (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) | 674 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) |
675 | } | 675 | } |
676 | MacroCallKind::Derive(ast, _) => { | 676 | MacroCallKind::Derive { ast_id, .. } => { |
677 | let node = ast.to_node(db.upcast()); | 677 | let node = ast_id.to_node(db.upcast()); |
678 | (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) | 678 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) |
679 | } | 679 | } |
680 | }; | 680 | }; |
681 | sink.push(MacroError { file, node: ast, message: message.clone() }); | 681 | sink.push(MacroError { file, node: ast, message: message.clone() }); |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 6dbbe2d05..fb4ddff5e 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -13,7 +13,7 @@ use hir_expand::{ | |||
13 | builtin_macro::find_builtin_macro, | 13 | builtin_macro::find_builtin_macro, |
14 | name::{AsName, Name}, | 14 | name::{AsName, Name}, |
15 | proc_macro::ProcMacroExpander, | 15 | proc_macro::ProcMacroExpander, |
16 | HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 16 | AttrId, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, |
17 | }; | 17 | }; |
18 | use hir_expand::{InFile, MacroCallLoc}; | 18 | use hir_expand::{InFile, MacroCallLoc}; |
19 | use rustc_hash::{FxHashMap, FxHashSet}; | 19 | use rustc_hash::{FxHashMap, FxHashSet}; |
@@ -216,7 +216,7 @@ struct MacroDirective { | |||
216 | #[derive(Clone, Debug, Eq, PartialEq)] | 216 | #[derive(Clone, Debug, Eq, PartialEq)] |
217 | enum MacroDirectiveKind { | 217 | enum MacroDirectiveKind { |
218 | FnLike { ast_id: AstIdWithPath<ast::MacroCall> }, | 218 | FnLike { ast_id: AstIdWithPath<ast::MacroCall> }, |
219 | Derive { ast_id: AstIdWithPath<ast::Item> }, | 219 | Derive { ast_id: AstIdWithPath<ast::Item>, derive_attr: AttrId }, |
220 | } | 220 | } |
221 | 221 | ||
222 | struct DefData<'a> { | 222 | struct DefData<'a> { |
@@ -478,7 +478,7 @@ impl DefCollector<'_> { | |||
478 | self.def_map.edition, | 478 | self.def_map.edition, |
479 | ); | 479 | ); |
480 | 480 | ||
481 | let res = self.def_map.resolve_name_in_extern_prelude(&extern_crate.name); | 481 | let res = self.def_map.resolve_name_in_extern_prelude(self.db, &extern_crate.name); |
482 | 482 | ||
483 | if let Some(ModuleDefId::ModuleId(m)) = res.take_types() { | 483 | if let Some(ModuleDefId::ModuleId(m)) = res.take_types() { |
484 | cov_mark::hit!(macro_rules_from_other_crates_are_visible_with_macro_use); | 484 | cov_mark::hit!(macro_rules_from_other_crates_are_visible_with_macro_use); |
@@ -534,6 +534,7 @@ impl DefCollector<'_> { | |||
534 | log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); | 534 | log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); |
535 | if import.is_extern_crate { | 535 | if import.is_extern_crate { |
536 | let res = self.def_map.resolve_name_in_extern_prelude( | 536 | let res = self.def_map.resolve_name_in_extern_prelude( |
537 | self.db, | ||
537 | &import | 538 | &import |
538 | .path | 539 | .path |
539 | .as_ident() | 540 | .as_ident() |
@@ -831,10 +832,14 @@ impl DefCollector<'_> { | |||
831 | Err(UnresolvedMacro) | Ok(Err(_)) => {} | 832 | Err(UnresolvedMacro) | Ok(Err(_)) => {} |
832 | } | 833 | } |
833 | } | 834 | } |
834 | MacroDirectiveKind::Derive { ast_id } => { | 835 | MacroDirectiveKind::Derive { ast_id, derive_attr } => { |
835 | match derive_macro_as_call_id(ast_id, self.db, self.def_map.krate, |path| { | 836 | match derive_macro_as_call_id( |
836 | self.resolve_derive_macro(directive.module_id, &path) | 837 | ast_id, |
837 | }) { | 838 | *derive_attr, |
839 | self.db, | ||
840 | self.def_map.krate, | ||
841 | |path| self.resolve_derive_macro(directive.module_id, &path), | ||
842 | ) { | ||
838 | Ok(call_id) => { | 843 | Ok(call_id) => { |
839 | resolved.push((directive.module_id, call_id, directive.depth)); | 844 | resolved.push((directive.module_id, call_id, directive.depth)); |
840 | res = ReachedFixedPoint::No; | 845 | res = ReachedFixedPoint::No; |
@@ -1368,7 +1373,7 @@ impl ModCollector<'_, '_> { | |||
1368 | self.def_collector.unexpanded_macros.push(MacroDirective { | 1373 | self.def_collector.unexpanded_macros.push(MacroDirective { |
1369 | module_id: self.module_id, | 1374 | module_id: self.module_id, |
1370 | depth: self.macro_depth + 1, | 1375 | depth: self.macro_depth + 1, |
1371 | kind: MacroDirectiveKind::Derive { ast_id }, | 1376 | kind: MacroDirectiveKind::Derive { ast_id, derive_attr: derive.id }, |
1372 | }); | 1377 | }); |
1373 | } | 1378 | } |
1374 | } | 1379 | } |
@@ -1520,7 +1525,7 @@ impl ModCollector<'_, '_> { | |||
1520 | // Built-in macro failed eager expansion. | 1525 | // Built-in macro failed eager expansion. |
1521 | self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error( | 1526 | self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error( |
1522 | self.module_id, | 1527 | self.module_id, |
1523 | MacroCallKind::FnLike(ast_id.ast_id), | 1528 | MacroCallKind::FnLike { ast_id: ast_id.ast_id }, |
1524 | error.unwrap().to_string(), | 1529 | error.unwrap().to_string(), |
1525 | )); | 1530 | )); |
1526 | return; | 1531 | return; |
diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index 60471937c..ccc9f22eb 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs | |||
@@ -60,12 +60,26 @@ impl ResolvePathResult { | |||
60 | } | 60 | } |
61 | 61 | ||
62 | impl DefMap { | 62 | impl DefMap { |
63 | pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { | 63 | pub(super) fn resolve_name_in_extern_prelude( |
64 | &self, | ||
65 | db: &dyn DefDatabase, | ||
66 | name: &Name, | ||
67 | ) -> PerNs { | ||
64 | if name == &name!(self) { | 68 | if name == &name!(self) { |
65 | cov_mark::hit!(extern_crate_self_as); | 69 | cov_mark::hit!(extern_crate_self_as); |
66 | return PerNs::types(self.module_id(self.root).into(), Visibility::Public); | 70 | return PerNs::types(self.module_id(self.root).into(), Visibility::Public); |
67 | } | 71 | } |
68 | self.extern_prelude | 72 | |
73 | let arc; | ||
74 | let root = match self.block { | ||
75 | Some(_) => { | ||
76 | arc = self.crate_root(db).def_map(db); | ||
77 | &*arc | ||
78 | } | ||
79 | None => self, | ||
80 | }; | ||
81 | |||
82 | root.extern_prelude | ||
69 | .get(name) | 83 | .get(name) |
70 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)) | 84 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)) |
71 | } | 85 | } |
@@ -191,7 +205,7 @@ impl DefMap { | |||
191 | None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), | 205 | None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), |
192 | }; | 206 | }; |
193 | log::debug!("resolving {:?} in crate root (+ extern prelude)", segment); | 207 | log::debug!("resolving {:?} in crate root (+ extern prelude)", segment); |
194 | self.resolve_name_in_crate_root_or_extern_prelude(&segment) | 208 | self.resolve_name_in_crate_root_or_extern_prelude(db, &segment) |
195 | } | 209 | } |
196 | PathKind::Plain => { | 210 | PathKind::Plain => { |
197 | let (_, segment) = match segments.next() { | 211 | let (_, segment) = match segments.next() { |
@@ -384,24 +398,31 @@ impl DefMap { | |||
384 | } | 398 | } |
385 | } | 399 | } |
386 | }; | 400 | }; |
387 | // Give precedence to names in outer `DefMap`s over the extern prelude; only check prelude | 401 | let from_extern_prelude = self |
388 | // from the crate DefMap. | 402 | .extern_prelude |
389 | let from_extern_prelude = match self.block { | 403 | .get(name) |
390 | Some(_) => PerNs::none(), | 404 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)); |
391 | None => self | ||
392 | .extern_prelude | ||
393 | .get(name) | ||
394 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)), | ||
395 | }; | ||
396 | 405 | ||
397 | let from_prelude = self.resolve_in_prelude(db, name); | 406 | let from_prelude = self.resolve_in_prelude(db, name); |
398 | 407 | ||
399 | from_legacy_macro.or(from_scope_or_builtin).or(from_extern_prelude).or(from_prelude) | 408 | from_legacy_macro.or(from_scope_or_builtin).or(from_extern_prelude).or(from_prelude) |
400 | } | 409 | } |
401 | 410 | ||
402 | fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs { | 411 | fn resolve_name_in_crate_root_or_extern_prelude( |
403 | let from_crate_root = self[self.root].scope.get(name); | 412 | &self, |
404 | let from_extern_prelude = self.resolve_name_in_extern_prelude(name); | 413 | db: &dyn DefDatabase, |
414 | name: &Name, | ||
415 | ) -> PerNs { | ||
416 | let arc; | ||
417 | let crate_def_map = match self.block { | ||
418 | Some(_) => { | ||
419 | arc = self.crate_root(db).def_map(db); | ||
420 | &arc | ||
421 | } | ||
422 | None => self, | ||
423 | }; | ||
424 | let from_crate_root = crate_def_map[crate_def_map.root].scope.get(name); | ||
425 | let from_extern_prelude = self.resolve_name_in_extern_prelude(db, name); | ||
405 | 426 | ||
406 | from_crate_root.or(from_extern_prelude) | 427 | from_crate_root.or(from_extern_prelude) |
407 | } | 428 | } |
diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs index 6ece4b289..537c03028 100644 --- a/crates/hir_expand/src/builtin_derive.rs +++ b/crates/hir_expand/src/builtin_derive.rs | |||
@@ -269,7 +269,7 @@ mod tests { | |||
269 | use expect_test::{expect, Expect}; | 269 | use expect_test::{expect, Expect}; |
270 | use name::AsName; | 270 | use name::AsName; |
271 | 271 | ||
272 | use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc}; | 272 | use crate::{test_db::TestDB, AstId, AttrId, MacroCallId, MacroCallKind, MacroCallLoc}; |
273 | 273 | ||
274 | use super::*; | 274 | use super::*; |
275 | 275 | ||
@@ -308,7 +308,7 @@ $0 | |||
308 | 308 | ||
309 | let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap(); | 309 | let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap(); |
310 | 310 | ||
311 | let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0])); | 311 | let ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0])); |
312 | 312 | ||
313 | let loc = MacroCallLoc { | 313 | let loc = MacroCallLoc { |
314 | def: MacroDefId { | 314 | def: MacroDefId { |
@@ -317,7 +317,11 @@ $0 | |||
317 | local_inner: false, | 317 | local_inner: false, |
318 | }, | 318 | }, |
319 | krate: CrateId(0), | 319 | krate: CrateId(0), |
320 | kind: MacroCallKind::Derive(attr_id, name.to_string()), | 320 | kind: MacroCallKind::Derive { |
321 | ast_id, | ||
322 | derive_name: name.to_string(), | ||
323 | derive_attr: AttrId(0), | ||
324 | }, | ||
321 | }; | 325 | }; |
322 | 326 | ||
323 | let id: MacroCallId = db.intern_macro(loc).into(); | 327 | let id: MacroCallId = db.intern_macro(loc).into(); |
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index a7d0f5b1f..80365fc16 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs | |||
@@ -566,10 +566,9 @@ mod tests { | |||
566 | let loc = MacroCallLoc { | 566 | let loc = MacroCallLoc { |
567 | def, | 567 | def, |
568 | krate, | 568 | krate, |
569 | kind: MacroCallKind::FnLike(AstId::new( | 569 | kind: MacroCallKind::FnLike { |
570 | file_id.into(), | 570 | ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(¯o_call)), |
571 | ast_id_map.ast_id(¯o_call), | 571 | }, |
572 | )), | ||
573 | }; | 572 | }; |
574 | 573 | ||
575 | let id: MacroCallId = db.intern_macro(loc).into(); | 574 | let id: MacroCallId = db.intern_macro(loc).into(); |
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index 10fe60821..ca705ee9d 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs | |||
@@ -439,6 +439,7 @@ fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind { | |||
439 | match parent.kind() { | 439 | match parent.kind() { |
440 | MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items, | 440 | MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items, |
441 | MACRO_STMTS => FragmentKind::Statements, | 441 | MACRO_STMTS => FragmentKind::Statements, |
442 | MACRO_PAT => FragmentKind::Pattern, | ||
442 | ITEM_LIST => FragmentKind::Items, | 443 | ITEM_LIST => FragmentKind::Items, |
443 | LET_STMT => { | 444 | LET_STMT => { |
444 | // FIXME: Handle LHS Pattern | 445 | // FIXME: Handle LHS Pattern |
diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index 9705526fa..ef126e4ad 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs | |||
@@ -174,8 +174,9 @@ fn lazy_expand( | |||
174 | ) -> ExpandResult<Option<InFile<SyntaxNode>>> { | 174 | ) -> ExpandResult<Option<InFile<SyntaxNode>>> { |
175 | let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value); | 175 | let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value); |
176 | 176 | ||
177 | let id: MacroCallId = | 177 | let id: MacroCallId = def |
178 | def.as_lazy_macro(db, krate, MacroCallKind::FnLike(macro_call.with_value(ast_id))).into(); | 178 | .as_lazy_macro(db, krate, MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id) }) |
179 | .into(); | ||
179 | 180 | ||
180 | let err = db.macro_expand_error(id); | 181 | let err = db.macro_expand_error(id); |
181 | let value = db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node)); | 182 | let value = db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node)); |
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 3e332ee47..a0e6aec62 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs | |||
@@ -290,22 +290,27 @@ pub struct MacroCallLoc { | |||
290 | 290 | ||
291 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 291 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
292 | pub enum MacroCallKind { | 292 | pub enum MacroCallKind { |
293 | FnLike(AstId<ast::MacroCall>), | 293 | FnLike { ast_id: AstId<ast::MacroCall> }, |
294 | Derive(AstId<ast::Item>, String), | 294 | Derive { ast_id: AstId<ast::Item>, derive_name: String, derive_attr: AttrId }, |
295 | } | 295 | } |
296 | 296 | ||
297 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
298 | pub struct AttrId(pub u32); | ||
299 | |||
297 | impl MacroCallKind { | 300 | impl MacroCallKind { |
298 | fn file_id(&self) -> HirFileId { | 301 | fn file_id(&self) -> HirFileId { |
299 | match self { | 302 | match self { |
300 | MacroCallKind::FnLike(ast_id) => ast_id.file_id, | 303 | MacroCallKind::FnLike { ast_id, .. } => ast_id.file_id, |
301 | MacroCallKind::Derive(ast_id, _) => ast_id.file_id, | 304 | MacroCallKind::Derive { ast_id, .. } => ast_id.file_id, |
302 | } | 305 | } |
303 | } | 306 | } |
304 | 307 | ||
305 | fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { | 308 | fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { |
306 | match self { | 309 | match self { |
307 | MacroCallKind::FnLike(ast_id) => ast_id.with_value(ast_id.to_node(db).syntax().clone()), | 310 | MacroCallKind::FnLike { ast_id, .. } => { |
308 | MacroCallKind::Derive(ast_id, _) => { | 311 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) |
312 | } | ||
313 | MacroCallKind::Derive { ast_id, .. } => { | ||
309 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) | 314 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) |
310 | } | 315 | } |
311 | } | 316 | } |
@@ -313,10 +318,10 @@ impl MacroCallKind { | |||
313 | 318 | ||
314 | fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> { | 319 | fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> { |
315 | match self { | 320 | match self { |
316 | MacroCallKind::FnLike(ast_id) => { | 321 | MacroCallKind::FnLike { ast_id, .. } => { |
317 | Some(ast_id.to_node(db).token_tree()?.syntax().clone()) | 322 | Some(ast_id.to_node(db).token_tree()?.syntax().clone()) |
318 | } | 323 | } |
319 | MacroCallKind::Derive(ast_id, _) => Some(ast_id.to_node(db).syntax().clone()), | 324 | MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()), |
320 | } | 325 | } |
321 | } | 326 | } |
322 | } | 327 | } |
diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index f8e9db9ae..71bc436e6 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs | |||
@@ -6,14 +6,15 @@ | |||
6 | use std::iter::successors; | 6 | use std::iter::successors; |
7 | 7 | ||
8 | use base_db::CrateId; | 8 | use base_db::CrateId; |
9 | use chalk_ir::cast::Cast; | 9 | use chalk_ir::{cast::Cast, fold::Fold, interner::HasInterner, VariableKind}; |
10 | use hir_def::lang_item::LangItemTarget; | 10 | use hir_def::lang_item::LangItemTarget; |
11 | use hir_expand::name::name; | 11 | use hir_expand::name::name; |
12 | use log::{info, warn}; | 12 | use log::{info, warn}; |
13 | 13 | ||
14 | use crate::{ | 14 | use crate::{ |
15 | db::HirDatabase, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, | 15 | db::HirDatabase, static_lifetime, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, |
16 | InEnvironment, Interner, ProjectionTyExt, Solution, Ty, TyBuilder, TyKind, | 16 | DebruijnIndex, InEnvironment, Interner, ProjectionTyExt, Solution, Substitution, Ty, TyBuilder, |
17 | TyKind, | ||
17 | }; | 18 | }; |
18 | 19 | ||
19 | const AUTODEREF_RECURSION_LIMIT: usize = 10; | 20 | const AUTODEREF_RECURSION_LIMIT: usize = 10; |
@@ -103,7 +104,7 @@ fn deref_by_trait( | |||
103 | binders: CanonicalVarKinds::from_iter( | 104 | binders: CanonicalVarKinds::from_iter( |
104 | &Interner, | 105 | &Interner, |
105 | ty.goal.binders.iter(&Interner).cloned().chain(Some(chalk_ir::WithKind::new( | 106 | ty.goal.binders.iter(&Interner).cloned().chain(Some(chalk_ir::WithKind::new( |
106 | chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General), | 107 | VariableKind::Ty(chalk_ir::TyVariableKind::General), |
107 | chalk_ir::UniverseIndex::ROOT, | 108 | chalk_ir::UniverseIndex::ROOT, |
108 | ))), | 109 | ))), |
109 | ), | 110 | ), |
@@ -136,7 +137,9 @@ fn deref_by_trait( | |||
136 | return None; | 137 | return None; |
137 | } | 138 | } |
138 | } | 139 | } |
139 | Some(Canonical { | 140 | // FIXME: we remove lifetime variables here since they can confuse |
141 | // the method resolution code later | ||
142 | Some(fixup_lifetime_variables(Canonical { | ||
140 | value: vars | 143 | value: vars |
141 | .value | 144 | .value |
142 | .subst | 145 | .subst |
@@ -144,7 +147,7 @@ fn deref_by_trait( | |||
144 | .assert_ty_ref(&Interner) | 147 | .assert_ty_ref(&Interner) |
145 | .clone(), | 148 | .clone(), |
146 | binders: vars.binders.clone(), | 149 | binders: vars.binders.clone(), |
147 | }) | 150 | })) |
148 | } | 151 | } |
149 | Solution::Ambig(_) => { | 152 | Solution::Ambig(_) => { |
150 | info!("Ambiguous solution for derefing {:?}: {:?}", ty.goal, solution); | 153 | info!("Ambiguous solution for derefing {:?}: {:?}", ty.goal, solution); |
@@ -152,3 +155,32 @@ fn deref_by_trait( | |||
152 | } | 155 | } |
153 | } | 156 | } |
154 | } | 157 | } |
158 | |||
159 | fn fixup_lifetime_variables<T: Fold<Interner, Result = T> + HasInterner<Interner = Interner>>( | ||
160 | c: Canonical<T>, | ||
161 | ) -> Canonical<T> { | ||
162 | // Removes lifetime variables from the Canonical, replacing them by static lifetimes. | ||
163 | let mut i = 0; | ||
164 | let subst = Substitution::from_iter( | ||
165 | &Interner, | ||
166 | c.binders.iter(&Interner).map(|vk| match vk.kind { | ||
167 | VariableKind::Ty(_) => { | ||
168 | let index = i; | ||
169 | i += 1; | ||
170 | BoundVar::new(DebruijnIndex::INNERMOST, index).to_ty(&Interner).cast(&Interner) | ||
171 | } | ||
172 | VariableKind::Lifetime => static_lifetime().cast(&Interner), | ||
173 | VariableKind::Const(_) => unimplemented!(), | ||
174 | }), | ||
175 | ); | ||
176 | let binders = CanonicalVarKinds::from_iter( | ||
177 | &Interner, | ||
178 | c.binders.iter(&Interner).filter(|vk| match vk.kind { | ||
179 | VariableKind::Ty(_) => true, | ||
180 | VariableKind::Lifetime => false, | ||
181 | VariableKind::Const(_) => true, | ||
182 | }), | ||
183 | ); | ||
184 | let value = subst.apply(c.value, &Interner); | ||
185 | Canonical { binders, value } | ||
186 | } | ||
diff --git a/crates/hir_ty/src/chalk_cast.rs b/crates/hir_ty/src/chalk_cast.rs deleted file mode 100644 index f27dee3fd..000000000 --- a/crates/hir_ty/src/chalk_cast.rs +++ /dev/null | |||
@@ -1,16 +0,0 @@ | |||
1 | //! Implementations of the Chalk `Cast` trait for our types. | ||
2 | |||
3 | use chalk_ir::interner::HasInterner; | ||
4 | |||
5 | use crate::{CallableSig, ReturnTypeImplTraits}; | ||
6 | |||
7 | macro_rules! has_interner { | ||
8 | ($t:ty) => { | ||
9 | impl HasInterner for $t { | ||
10 | type Interner = crate::Interner; | ||
11 | } | ||
12 | }; | ||
13 | } | ||
14 | |||
15 | has_interner!(CallableSig); | ||
16 | has_interner!(ReturnTypeImplTraits); | ||
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/chalk_db.rs index b8c390b2e..8f054d06b 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/chalk_db.rs | |||
@@ -1,50 +1,47 @@ | |||
1 | //! Conversion code from/to Chalk. | 1 | //! The implementation of `RustIrDatabase` for Chalk, which provides information |
2 | //! about the code that Chalk needs. | ||
2 | use std::sync::Arc; | 3 | use std::sync::Arc; |
3 | 4 | ||
4 | use log::debug; | 5 | use log::debug; |
5 | 6 | ||
6 | use chalk_ir::{fold::shift::Shift, CanonicalVarKinds}; | 7 | use chalk_ir::{cast::Cast, fold::shift::Shift, CanonicalVarKinds}; |
7 | use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; | 8 | use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; |
8 | 9 | ||
9 | use base_db::{salsa::InternKey, CrateId}; | 10 | use base_db::CrateId; |
10 | use hir_def::{ | 11 | use hir_def::{ |
11 | lang_item::{lang_attr, LangItemTarget}, | 12 | lang_item::{lang_attr, LangItemTarget}, |
12 | AssocContainerId, AssocItemId, HasModule, Lookup, TypeAliasId, | 13 | AssocContainerId, AssocItemId, GenericDefId, HasModule, Lookup, TypeAliasId, |
13 | }; | 14 | }; |
14 | use hir_expand::name::name; | 15 | use hir_expand::name::name; |
15 | 16 | ||
16 | use super::ChalkContext; | ||
17 | use crate::{ | 17 | use crate::{ |
18 | db::HirDatabase, | 18 | db::HirDatabase, |
19 | display::HirDisplay, | 19 | display::HirDisplay, |
20 | from_assoc_type_id, make_only_type_binders, | 20 | from_assoc_type_id, from_chalk_trait_id, make_only_type_binders, |
21 | mapping::{from_chalk, ToChalk, TypeAliasAsValue}, | ||
21 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, | 22 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, |
22 | to_assoc_type_id, to_chalk_trait_id, | 23 | to_assoc_type_id, to_chalk_trait_id, |
24 | traits::ChalkContext, | ||
23 | utils::generics, | 25 | utils::generics, |
24 | AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution, | 26 | AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, Interner, ProjectionTy, |
25 | TraitRef, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, WhereClause, | 27 | ProjectionTyExt, QuantifiedWhereClause, Substitution, TraitRef, TraitRefExt, Ty, TyBuilder, |
28 | TyExt, TyKind, WhereClause, | ||
26 | }; | 29 | }; |
27 | use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, TypeAliasAsValue}; | ||
28 | 30 | ||
29 | pub use self::interner::Interner; | 31 | pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>; |
30 | pub(crate) use self::interner::*; | 32 | pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>; |
31 | 33 | pub(crate) type StructDatum = chalk_solve::rust_ir::AdtDatum<Interner>; | |
32 | pub(super) mod tls; | 34 | pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>; |
33 | mod interner; | 35 | pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>; |
34 | mod mapping; | 36 | |
35 | 37 | pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>; | |
36 | pub(crate) trait ToChalk { | 38 | pub(crate) type TraitId = chalk_ir::TraitId<Interner>; |
37 | type Chalk; | 39 | pub(crate) type AdtId = chalk_ir::AdtId<Interner>; |
38 | fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk; | 40 | pub(crate) type ImplId = chalk_ir::ImplId<Interner>; |
39 | fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self; | 41 | pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>; |
40 | } | 42 | pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>; |
41 | 43 | pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>; | |
42 | pub(crate) fn from_chalk<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T | 44 | pub(crate) type Variances = chalk_ir::Variances<Interner>; |
43 | where | ||
44 | T: ToChalk<Chalk = ChalkT>, | ||
45 | { | ||
46 | T::from_chalk(db, chalk) | ||
47 | } | ||
48 | 45 | ||
49 | impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | 46 | impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { |
50 | fn associated_ty_data(&self, id: AssocTypeId) -> Arc<AssociatedTyDatum> { | 47 | fn associated_ty_data(&self, id: AssocTypeId) -> Arc<AssociatedTyDatum> { |
@@ -82,7 +79,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
82 | binders: &CanonicalVarKinds<Interner>, | 79 | binders: &CanonicalVarKinds<Interner>, |
83 | ) -> Vec<ImplId> { | 80 | ) -> Vec<ImplId> { |
84 | debug!("impls_for_trait {:?}", trait_id); | 81 | debug!("impls_for_trait {:?}", trait_id); |
85 | let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); | 82 | let trait_: hir_def::TraitId = from_chalk_trait_id(trait_id); |
86 | 83 | ||
87 | let ty: Ty = parameters[0].assert_ty_ref(&Interner).clone(); | 84 | let ty: Ty = parameters[0].assert_ty_ref(&Interner).clone(); |
88 | 85 | ||
@@ -101,7 +98,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
101 | None | 98 | None |
102 | } | 99 | } |
103 | 100 | ||
104 | let self_ty_fp = TyFingerprint::for_impl(&ty); | 101 | let self_ty_fp = TyFingerprint::for_trait_impl(&ty); |
105 | let fps: &[TyFingerprint] = match binder_kind(&ty, binders) { | 102 | let fps: &[TyFingerprint] = match binder_kind(&ty, binders) { |
106 | Some(chalk_ir::TyVariableKind::Integer) => &ALL_INT_FPS, | 103 | Some(chalk_ir::TyVariableKind::Integer) => &ALL_INT_FPS, |
107 | Some(chalk_ir::TyVariableKind::Float) => &ALL_FLOAT_FPS, | 104 | Some(chalk_ir::TyVariableKind::Float) => &ALL_FLOAT_FPS, |
@@ -164,7 +161,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
164 | Some(LangItemTarget::TraitId(trait_)) => trait_, | 161 | Some(LangItemTarget::TraitId(trait_)) => trait_, |
165 | _ => return None, | 162 | _ => return None, |
166 | }; | 163 | }; |
167 | Some(trait_.to_chalk(self.db)) | 164 | Some(to_chalk_trait_id(trait_)) |
168 | } | 165 | } |
169 | 166 | ||
170 | fn program_clauses_for_env( | 167 | fn program_clauses_for_env( |
@@ -311,7 +308,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
311 | } | 308 | } |
312 | 309 | ||
313 | fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String { | 310 | fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String { |
314 | let id = from_chalk(self.db, trait_id); | 311 | let id = from_chalk_trait_id(trait_id); |
315 | self.db.trait_data(id).name.to_string() | 312 | self.db.trait_data(id).name.to_string() |
316 | } | 313 | } |
317 | fn adt_name(&self, chalk_ir::AdtId(adt_id): AdtId) -> String { | 314 | fn adt_name(&self, chalk_ir::AdtId(adt_id): AdtId) -> String { |
@@ -416,7 +413,7 @@ pub(crate) fn trait_datum_query( | |||
416 | trait_id: TraitId, | 413 | trait_id: TraitId, |
417 | ) -> Arc<TraitDatum> { | 414 | ) -> Arc<TraitDatum> { |
418 | debug!("trait_datum {:?}", trait_id); | 415 | debug!("trait_datum {:?}", trait_id); |
419 | let trait_: hir_def::TraitId = from_chalk(db, trait_id); | 416 | let trait_ = from_chalk_trait_id(trait_id); |
420 | let trait_data = db.trait_data(trait_); | 417 | let trait_data = db.trait_data(trait_); |
421 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); | 418 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); |
422 | let generic_params = generics(db.upcast(), trait_.into()); | 419 | let generic_params = generics(db.upcast(), trait_.into()); |
@@ -679,38 +676,65 @@ pub(crate) fn adt_variance_query( | |||
679 | ) | 676 | ) |
680 | } | 677 | } |
681 | 678 | ||
682 | impl From<FnDefId> for crate::db::InternedCallableDefId { | 679 | pub(super) fn convert_where_clauses( |
683 | fn from(fn_def_id: FnDefId) -> Self { | 680 | db: &dyn HirDatabase, |
684 | InternKey::from_intern_id(fn_def_id.0) | 681 | def: GenericDefId, |
685 | } | 682 | substs: &Substitution, |
686 | } | 683 | ) -> Vec<chalk_ir::QuantifiedWhereClause<Interner>> { |
687 | 684 | let generic_predicates = db.generic_predicates(def); | |
688 | impl From<crate::db::InternedCallableDefId> for FnDefId { | 685 | let mut result = Vec::with_capacity(generic_predicates.len()); |
689 | fn from(callable_def_id: crate::db::InternedCallableDefId) -> Self { | 686 | for pred in generic_predicates.iter() { |
690 | chalk_ir::FnDefId(callable_def_id.as_intern_id()) | 687 | result.push(pred.clone().substitute(&Interner, substs)); |
691 | } | 688 | } |
692 | } | 689 | result |
693 | |||
694 | impl From<OpaqueTyId> for crate::db::InternedOpaqueTyId { | ||
695 | fn from(id: OpaqueTyId) -> Self { | ||
696 | InternKey::from_intern_id(id.0) | ||
697 | } | ||
698 | } | ||
699 | |||
700 | impl From<crate::db::InternedOpaqueTyId> for OpaqueTyId { | ||
701 | fn from(id: crate::db::InternedOpaqueTyId) -> Self { | ||
702 | chalk_ir::OpaqueTyId(id.as_intern_id()) | ||
703 | } | ||
704 | } | ||
705 | |||
706 | impl From<chalk_ir::ClosureId<Interner>> for crate::db::InternedClosureId { | ||
707 | fn from(id: chalk_ir::ClosureId<Interner>) -> Self { | ||
708 | Self::from_intern_id(id.0) | ||
709 | } | ||
710 | } | 690 | } |
711 | 691 | ||
712 | impl From<crate::db::InternedClosureId> for chalk_ir::ClosureId<Interner> { | 692 | pub(super) fn generic_predicate_to_inline_bound( |
713 | fn from(id: crate::db::InternedClosureId) -> Self { | 693 | db: &dyn HirDatabase, |
714 | chalk_ir::ClosureId(id.as_intern_id()) | 694 | pred: &QuantifiedWhereClause, |
695 | self_ty: &Ty, | ||
696 | ) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> { | ||
697 | // An InlineBound is like a GenericPredicate, except the self type is left out. | ||
698 | // We don't have a special type for this, but Chalk does. | ||
699 | let self_ty_shifted_in = self_ty.clone().shifted_in_from(&Interner, DebruijnIndex::ONE); | ||
700 | let (pred, binders) = pred.as_ref().into_value_and_skipped_binders(); | ||
701 | match pred { | ||
702 | WhereClause::Implemented(trait_ref) => { | ||
703 | if trait_ref.self_type_parameter(&Interner) != self_ty_shifted_in { | ||
704 | // we can only convert predicates back to type bounds if they | ||
705 | // have the expected self type | ||
706 | return None; | ||
707 | } | ||
708 | let args_no_self = trait_ref.substitution.as_slice(&Interner)[1..] | ||
709 | .iter() | ||
710 | .map(|ty| ty.clone().cast(&Interner)) | ||
711 | .collect(); | ||
712 | let trait_bound = rust_ir::TraitBound { trait_id: trait_ref.trait_id, args_no_self }; | ||
713 | Some(chalk_ir::Binders::new(binders, rust_ir::InlineBound::TraitBound(trait_bound))) | ||
714 | } | ||
715 | WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => { | ||
716 | if projection_ty.self_type_parameter(&Interner) != self_ty_shifted_in { | ||
717 | return None; | ||
718 | } | ||
719 | let trait_ = projection_ty.trait_(db); | ||
720 | let args_no_self = projection_ty.substitution.as_slice(&Interner)[1..] | ||
721 | .iter() | ||
722 | .map(|ty| ty.clone().cast(&Interner)) | ||
723 | .collect(); | ||
724 | let alias_eq_bound = rust_ir::AliasEqBound { | ||
725 | value: ty.clone(), | ||
726 | trait_bound: rust_ir::TraitBound { | ||
727 | trait_id: to_chalk_trait_id(trait_), | ||
728 | args_no_self, | ||
729 | }, | ||
730 | associated_ty_id: projection_ty.associated_ty_id, | ||
731 | parameters: Vec::new(), // FIXME we don't support generic associated types yet | ||
732 | }; | ||
733 | Some(chalk_ir::Binders::new( | ||
734 | binders, | ||
735 | rust_ir::InlineBound::AliasEqBound(alias_eq_bound), | ||
736 | )) | ||
737 | } | ||
738 | _ => None, | ||
715 | } | 739 | } |
716 | } | 740 | } |
diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index 28ed3aac6..8c4542956 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs | |||
@@ -75,7 +75,7 @@ impl TyExt for Ty { | |||
75 | } | 75 | } |
76 | fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> { | 76 | fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> { |
77 | match self.kind(&Interner) { | 77 | match self.kind(&Interner) { |
78 | TyKind::Ref(mutability, lifetime, ty) => Some((ty, *lifetime, *mutability)), | 78 | TyKind::Ref(mutability, lifetime, ty) => Some((ty, lifetime.clone(), *mutability)), |
79 | _ => None, | 79 | _ => None, |
80 | } | 80 | } |
81 | } | 81 | } |
diff --git a/crates/hir_ty/src/db.rs b/crates/hir_ty/src/db.rs index 326c20240..1690926ad 100644 --- a/crates/hir_ty/src/db.rs +++ b/crates/hir_ty/src/db.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! The home of `HirDatabase`, which is the Salsa database containing all the |
2 | //! type inference-related queries. | ||
2 | 3 | ||
3 | use std::sync::Arc; | 4 | use std::sync::Arc; |
4 | 5 | ||
@@ -10,9 +11,9 @@ use hir_def::{ | |||
10 | use la_arena::ArenaMap; | 11 | use la_arena::ArenaMap; |
11 | 12 | ||
12 | use crate::{ | 13 | use crate::{ |
14 | chalk_db, | ||
13 | method_resolution::{InherentImpls, TraitImpls}, | 15 | method_resolution::{InherentImpls, TraitImpls}, |
14 | traits::chalk, | 16 | Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, Interner, PolyFnSig, |
15 | Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, PolyFnSig, | ||
16 | QuantifiedWhereClause, ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId, | 17 | QuantifiedWhereClause, ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId, |
17 | }; | 18 | }; |
18 | use hir_expand::name::Name; | 19 | use hir_expand::name::Name; |
@@ -94,33 +95,38 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> { | |||
94 | #[salsa::interned] | 95 | #[salsa::interned] |
95 | fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId; | 96 | fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId; |
96 | 97 | ||
97 | #[salsa::invoke(chalk::associated_ty_data_query)] | 98 | #[salsa::invoke(chalk_db::associated_ty_data_query)] |
98 | fn associated_ty_data(&self, id: chalk::AssocTypeId) -> Arc<chalk::AssociatedTyDatum>; | 99 | fn associated_ty_data(&self, id: chalk_db::AssocTypeId) -> Arc<chalk_db::AssociatedTyDatum>; |
99 | 100 | ||
100 | #[salsa::invoke(chalk::trait_datum_query)] | 101 | #[salsa::invoke(chalk_db::trait_datum_query)] |
101 | fn trait_datum(&self, krate: CrateId, trait_id: chalk::TraitId) -> Arc<chalk::TraitDatum>; | 102 | fn trait_datum(&self, krate: CrateId, trait_id: chalk_db::TraitId) |
103 | -> Arc<chalk_db::TraitDatum>; | ||
102 | 104 | ||
103 | #[salsa::invoke(chalk::struct_datum_query)] | 105 | #[salsa::invoke(chalk_db::struct_datum_query)] |
104 | fn struct_datum(&self, krate: CrateId, struct_id: chalk::AdtId) -> Arc<chalk::StructDatum>; | 106 | fn struct_datum( |
107 | &self, | ||
108 | krate: CrateId, | ||
109 | struct_id: chalk_db::AdtId, | ||
110 | ) -> Arc<chalk_db::StructDatum>; | ||
105 | 111 | ||
106 | #[salsa::invoke(crate::traits::chalk::impl_datum_query)] | 112 | #[salsa::invoke(chalk_db::impl_datum_query)] |
107 | fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc<chalk::ImplDatum>; | 113 | fn impl_datum(&self, krate: CrateId, impl_id: chalk_db::ImplId) -> Arc<chalk_db::ImplDatum>; |
108 | 114 | ||
109 | #[salsa::invoke(crate::traits::chalk::fn_def_datum_query)] | 115 | #[salsa::invoke(chalk_db::fn_def_datum_query)] |
110 | fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk::FnDefDatum>; | 116 | fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk_db::FnDefDatum>; |
111 | 117 | ||
112 | #[salsa::invoke(crate::traits::chalk::fn_def_variance_query)] | 118 | #[salsa::invoke(chalk_db::fn_def_variance_query)] |
113 | fn fn_def_variance(&self, krate: CrateId, fn_def_id: FnDefId) -> chalk::Variances; | 119 | fn fn_def_variance(&self, krate: CrateId, fn_def_id: FnDefId) -> chalk_db::Variances; |
114 | 120 | ||
115 | #[salsa::invoke(crate::traits::chalk::adt_variance_query)] | 121 | #[salsa::invoke(chalk_db::adt_variance_query)] |
116 | fn adt_variance(&self, krate: CrateId, adt_id: chalk::AdtId) -> chalk::Variances; | 122 | fn adt_variance(&self, krate: CrateId, adt_id: chalk_db::AdtId) -> chalk_db::Variances; |
117 | 123 | ||
118 | #[salsa::invoke(crate::traits::chalk::associated_ty_value_query)] | 124 | #[salsa::invoke(chalk_db::associated_ty_value_query)] |
119 | fn associated_ty_value( | 125 | fn associated_ty_value( |
120 | &self, | 126 | &self, |
121 | krate: CrateId, | 127 | krate: CrateId, |
122 | id: chalk::AssociatedTyValueId, | 128 | id: chalk_db::AssociatedTyValueId, |
123 | ) -> Arc<chalk::AssociatedTyValue>; | 129 | ) -> Arc<chalk_db::AssociatedTyValue>; |
124 | 130 | ||
125 | #[salsa::invoke(crate::traits::trait_solve_query)] | 131 | #[salsa::invoke(crate::traits::trait_solve_query)] |
126 | fn trait_solve( | 132 | fn trait_solve( |
@@ -129,12 +135,12 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> { | |||
129 | goal: crate::Canonical<crate::InEnvironment<crate::DomainGoal>>, | 135 | goal: crate::Canonical<crate::InEnvironment<crate::DomainGoal>>, |
130 | ) -> Option<crate::Solution>; | 136 | ) -> Option<crate::Solution>; |
131 | 137 | ||
132 | #[salsa::invoke(crate::traits::chalk::program_clauses_for_chalk_env_query)] | 138 | #[salsa::invoke(chalk_db::program_clauses_for_chalk_env_query)] |
133 | fn program_clauses_for_chalk_env( | 139 | fn program_clauses_for_chalk_env( |
134 | &self, | 140 | &self, |
135 | krate: CrateId, | 141 | krate: CrateId, |
136 | env: chalk_ir::Environment<chalk::Interner>, | 142 | env: chalk_ir::Environment<Interner>, |
137 | ) -> chalk_ir::ProgramClauses<chalk::Interner>; | 143 | ) -> chalk_ir::ProgramClauses<Interner>; |
138 | } | 144 | } |
139 | 145 | ||
140 | fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> { | 146 | fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> { |
diff --git a/crates/hir_ty/src/diagnostics.rs b/crates/hir_ty/src/diagnostics.rs index 86f937e1d..84fc8ce14 100644 --- a/crates/hir_ty/src/diagnostics.rs +++ b/crates/hir_ty/src/diagnostics.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! Type inference-based diagnostics. |
2 | mod expr; | 2 | mod expr; |
3 | mod match_check; | 3 | mod match_check; |
4 | mod unsafe_check; | 4 | mod unsafe_check; |
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index d7a3977e5..e7c9dabc2 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs | |||
@@ -1,4 +1,6 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! The `HirDisplay` trait, which serves two purposes: Turning various bits from |
2 | //! HIR back into source code, and just displaying them for debugging/testing | ||
3 | //! purposes. | ||
2 | 4 | ||
3 | use std::{ | 5 | use std::{ |
4 | array, | 6 | array, |
@@ -20,11 +22,11 @@ use hir_expand::name::Name; | |||
20 | 22 | ||
21 | use crate::{ | 23 | use crate::{ |
22 | const_from_placeholder_idx, db::HirDatabase, from_assoc_type_id, from_foreign_def_id, | 24 | const_from_placeholder_idx, db::HirDatabase, from_assoc_type_id, from_foreign_def_id, |
23 | from_placeholder_idx, lt_from_placeholder_idx, primitive, subst_prefix, to_assoc_type_id, | 25 | from_placeholder_idx, lt_from_placeholder_idx, mapping::from_chalk, primitive, subst_prefix, |
24 | traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, | 26 | to_assoc_type_id, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, Const, |
25 | CallableSig, Const, ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, | 27 | ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData, |
26 | LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, | 28 | LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, QuantifiedWhereClause, |
27 | QuantifiedWhereClause, Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause, | 29 | Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause, |
28 | }; | 30 | }; |
29 | 31 | ||
30 | pub struct HirFormatter<'a> { | 32 | pub struct HirFormatter<'a> { |
@@ -265,7 +267,7 @@ impl HirDisplay for ProjectionTy { | |||
265 | write!(f, " as {}", trait_.name)?; | 267 | write!(f, " as {}", trait_.name)?; |
266 | if self.substitution.len(&Interner) > 1 { | 268 | if self.substitution.len(&Interner) > 1 { |
267 | write!(f, "<")?; | 269 | write!(f, "<")?; |
268 | f.write_joined(&self.substitution.interned()[1..], ", ")?; | 270 | f.write_joined(&self.substitution.as_slice(&Interner)[1..], ", ")?; |
269 | write!(f, ">")?; | 271 | write!(f, ">")?; |
270 | } | 272 | } |
271 | write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?; | 273 | write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?; |
@@ -416,7 +418,7 @@ impl HirDisplay for Ty { | |||
416 | write!(f, ",)")?; | 418 | write!(f, ",)")?; |
417 | } else { | 419 | } else { |
418 | write!(f, "(")?; | 420 | write!(f, "(")?; |
419 | f.write_joined(&*substs.interned(), ", ")?; | 421 | f.write_joined(&*substs.as_slice(&Interner), ", ")?; |
420 | write!(f, ")")?; | 422 | write!(f, ")")?; |
421 | } | 423 | } |
422 | } | 424 | } |
@@ -444,7 +446,7 @@ impl HirDisplay for Ty { | |||
444 | // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self? | 446 | // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self? |
445 | if total_len > 0 { | 447 | if total_len > 0 { |
446 | write!(f, "<")?; | 448 | write!(f, "<")?; |
447 | f.write_joined(¶meters.interned()[..total_len], ", ")?; | 449 | f.write_joined(¶meters.as_slice(&Interner)[..total_len], ", ")?; |
448 | write!(f, ">")?; | 450 | write!(f, ">")?; |
449 | } | 451 | } |
450 | } | 452 | } |
@@ -491,7 +493,7 @@ impl HirDisplay for Ty { | |||
491 | .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) | 493 | .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) |
492 | .filter(|defaults| !defaults.is_empty()) | 494 | .filter(|defaults| !defaults.is_empty()) |
493 | { | 495 | { |
494 | None => parameters.interned().as_ref(), | 496 | None => parameters.as_slice(&Interner), |
495 | Some(default_parameters) => { | 497 | Some(default_parameters) => { |
496 | let mut default_from = 0; | 498 | let mut default_from = 0; |
497 | for (i, parameter) in parameters.iter(&Interner).enumerate() { | 499 | for (i, parameter) in parameters.iter(&Interner).enumerate() { |
@@ -515,11 +517,11 @@ impl HirDisplay for Ty { | |||
515 | } | 517 | } |
516 | } | 518 | } |
517 | } | 519 | } |
518 | ¶meters.interned()[0..default_from] | 520 | ¶meters.as_slice(&Interner)[0..default_from] |
519 | } | 521 | } |
520 | } | 522 | } |
521 | } else { | 523 | } else { |
522 | parameters.interned().as_ref() | 524 | parameters.as_slice(&Interner) |
523 | }; | 525 | }; |
524 | if !parameters_to_write.is_empty() { | 526 | if !parameters_to_write.is_empty() { |
525 | write!(f, "<")?; | 527 | write!(f, "<")?; |
@@ -542,7 +544,7 @@ impl HirDisplay for Ty { | |||
542 | write!(f, "{}::{}", trait_.name, type_alias_data.name)?; | 544 | write!(f, "{}::{}", trait_.name, type_alias_data.name)?; |
543 | if parameters.len(&Interner) > 0 { | 545 | if parameters.len(&Interner) > 0 { |
544 | write!(f, "<")?; | 546 | write!(f, "<")?; |
545 | f.write_joined(&*parameters.interned(), ", ")?; | 547 | f.write_joined(&*parameters.as_slice(&Interner), ", ")?; |
546 | write!(f, ">")?; | 548 | write!(f, ">")?; |
547 | } | 549 | } |
548 | } else { | 550 | } else { |
@@ -749,13 +751,13 @@ fn write_bounds_like_dyn_trait( | |||
749 | // existential) here, which is the only thing that's | 751 | // existential) here, which is the only thing that's |
750 | // possible in actual Rust, and hence don't print it | 752 | // possible in actual Rust, and hence don't print it |
751 | write!(f, "{}", f.db.trait_data(trait_).name)?; | 753 | write!(f, "{}", f.db.trait_data(trait_).name)?; |
752 | if let [_, params @ ..] = &*trait_ref.substitution.interned().as_slice() { | 754 | if let [_, params @ ..] = &*trait_ref.substitution.as_slice(&Interner) { |
753 | if is_fn_trait { | 755 | if is_fn_trait { |
754 | if let Some(args) = | 756 | if let Some(args) = |
755 | params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple()) | 757 | params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple()) |
756 | { | 758 | { |
757 | write!(f, "(")?; | 759 | write!(f, "(")?; |
758 | f.write_joined(&*args.interned(), ", ")?; | 760 | f.write_joined(args.as_slice(&Interner), ", ")?; |
759 | write!(f, ")")?; | 761 | write!(f, ")")?; |
760 | } | 762 | } |
761 | } else if !params.is_empty() { | 763 | } else if !params.is_empty() { |
@@ -814,7 +816,7 @@ fn fmt_trait_ref(tr: &TraitRef, f: &mut HirFormatter, use_as: bool) -> Result<() | |||
814 | write!(f, "{}", f.db.trait_data(tr.hir_trait_id()).name)?; | 816 | write!(f, "{}", f.db.trait_data(tr.hir_trait_id()).name)?; |
815 | if tr.substitution.len(&Interner) > 1 { | 817 | if tr.substitution.len(&Interner) > 1 { |
816 | write!(f, "<")?; | 818 | write!(f, "<")?; |
817 | f.write_joined(&tr.substitution.interned()[1..], ", ")?; | 819 | f.write_joined(&tr.substitution.as_slice(&Interner)[1..], ", ")?; |
818 | write!(f, ">")?; | 820 | write!(f, ">")?; |
819 | } | 821 | } |
820 | Ok(()) | 822 | Ok(()) |
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index cbbfa8b5c..50497eecb 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs | |||
@@ -17,10 +17,11 @@ use syntax::ast::RangeOp; | |||
17 | use crate::{ | 17 | use crate::{ |
18 | autoderef, dummy_usize_const, | 18 | autoderef, dummy_usize_const, |
19 | lower::lower_to_chalk_mutability, | 19 | lower::lower_to_chalk_mutability, |
20 | mapping::from_chalk, | ||
20 | method_resolution, op, | 21 | method_resolution, op, |
21 | primitive::{self, UintTy}, | 22 | primitive::{self, UintTy}, |
22 | static_lifetime, to_chalk_trait_id, | 23 | static_lifetime, to_chalk_trait_id, |
23 | traits::{chalk::from_chalk, FnTrait}, | 24 | traits::FnTrait, |
24 | utils::{generics, Generics}, | 25 | utils::{generics, Generics}, |
25 | AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner, | 26 | AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner, |
26 | ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyExt, TyKind, | 27 | ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyExt, TyKind, |
@@ -462,7 +463,11 @@ impl<'a> InferenceContext<'a> { | |||
462 | }; | 463 | }; |
463 | match canonicalized.decanonicalize_ty(derefed_ty.value).kind(&Interner) { | 464 | match canonicalized.decanonicalize_ty(derefed_ty.value).kind(&Interner) { |
464 | TyKind::Tuple(_, substs) => name.as_tuple_index().and_then(|idx| { | 465 | TyKind::Tuple(_, substs) => name.as_tuple_index().and_then(|idx| { |
465 | substs.interned().get(idx).map(|a| a.assert_ty_ref(&Interner)).cloned() | 466 | substs |
467 | .as_slice(&Interner) | ||
468 | .get(idx) | ||
469 | .map(|a| a.assert_ty_ref(&Interner)) | ||
470 | .cloned() | ||
466 | }), | 471 | }), |
467 | TyKind::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => { | 472 | TyKind::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => { |
468 | let local_id = self.db.struct_data(*s).variant_data.field(name)?; | 473 | let local_id = self.db.struct_data(*s).variant_data.field(name)?; |
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index a41e8e116..aea354cde 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs | |||
@@ -122,7 +122,7 @@ impl<'a> InferenceContext<'a> { | |||
122 | let ty = match &body[pat] { | 122 | let ty = match &body[pat] { |
123 | &Pat::Tuple { ref args, ellipsis } => { | 123 | &Pat::Tuple { ref args, ellipsis } => { |
124 | let expectations = match expected.as_tuple() { | 124 | let expectations = match expected.as_tuple() { |
125 | Some(parameters) => &*parameters.interned().as_slice(), | 125 | Some(parameters) => &*parameters.as_slice(&Interner), |
126 | _ => &[], | 126 | _ => &[], |
127 | }; | 127 | }; |
128 | 128 | ||
@@ -242,7 +242,7 @@ impl<'a> InferenceContext<'a> { | |||
242 | let (inner_ty, alloc_ty) = match expected.as_adt() { | 242 | let (inner_ty, alloc_ty) = match expected.as_adt() { |
243 | Some((adt, subst)) if adt == box_adt => ( | 243 | Some((adt, subst)) if adt == box_adt => ( |
244 | subst.at(&Interner, 0).assert_ty_ref(&Interner).clone(), | 244 | subst.at(&Interner, 0).assert_ty_ref(&Interner).clone(), |
245 | subst.interned().get(1).and_then(|a| a.ty(&Interner).cloned()), | 245 | subst.as_slice(&Interner).get(1).and_then(|a| a.ty(&Interner).cloned()), |
246 | ), | 246 | ), |
247 | _ => (self.result.standard_types.unknown.clone(), None), | 247 | _ => (self.result.standard_types.unknown.clone(), None), |
248 | }; | 248 | }; |
diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index f8955aa32..495282eba 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs | |||
@@ -101,7 +101,7 @@ impl<'a> InferenceContext<'a> { | |||
101 | let substs = ctx.substs_from_path(path, typable, true); | 101 | let substs = ctx.substs_from_path(path, typable, true); |
102 | let ty = TyBuilder::value_ty(self.db, typable) | 102 | let ty = TyBuilder::value_ty(self.db, typable) |
103 | .use_parent_substs(&parent_substs) | 103 | .use_parent_substs(&parent_substs) |
104 | .fill(substs.interned()[parent_substs.len(&Interner)..].iter().cloned()) | 104 | .fill(substs.as_slice(&Interner)[parent_substs.len(&Interner)..].iter().cloned()) |
105 | .build(); | 105 | .build(); |
106 | Some(ty) | 106 | Some(ty) |
107 | } | 107 | } |
diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/interner.rs index bd9395b7e..a1656115d 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/interner.rs | |||
@@ -1,61 +1,83 @@ | |||
1 | //! Implementation of the Chalk `Interner` trait, which allows customizing the | 1 | //! Implementation of the Chalk `Interner` trait, which allows customizing the |
2 | //! representation of the various objects Chalk deals with (types, goals etc.). | 2 | //! representation of the various objects Chalk deals with (types, goals etc.). |
3 | 3 | ||
4 | use super::tls; | 4 | use crate::{chalk_db, tls, GenericArg}; |
5 | use base_db::salsa::InternId; | 5 | use base_db::salsa::InternId; |
6 | use chalk_ir::{GenericArg, Goal, GoalData}; | 6 | use chalk_ir::{Goal, GoalData}; |
7 | use hir_def::TypeAliasId; | 7 | use hir_def::{ |
8 | intern::{impl_internable, InternStorage, Internable, Interned}, | ||
9 | TypeAliasId, | ||
10 | }; | ||
8 | use smallvec::SmallVec; | 11 | use smallvec::SmallVec; |
9 | use std::{fmt, sync::Arc}; | 12 | use std::{fmt, sync::Arc}; |
10 | 13 | ||
11 | #[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] | 14 | #[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] |
12 | pub struct Interner; | 15 | pub struct Interner; |
13 | 16 | ||
14 | pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>; | 17 | #[derive(PartialEq, Eq, Hash, Debug)] |
15 | pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>; | 18 | pub struct InternedWrapper<T>(T); |
16 | pub(crate) type TraitId = chalk_ir::TraitId<Interner>; | 19 | |
17 | pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>; | 20 | impl<T> std::ops::Deref for InternedWrapper<T> { |
18 | pub(crate) type AdtId = chalk_ir::AdtId<Interner>; | 21 | type Target = T; |
19 | pub(crate) type StructDatum = chalk_solve::rust_ir::AdtDatum<Interner>; | 22 | |
20 | pub(crate) type ImplId = chalk_ir::ImplId<Interner>; | 23 | fn deref(&self) -> &Self::Target { |
21 | pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>; | 24 | &self.0 |
22 | pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>; | 25 | } |
23 | pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>; | 26 | } |
24 | pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>; | 27 | |
25 | pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>; | 28 | impl_internable!( |
26 | pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>; | 29 | InternedWrapper<Vec<chalk_ir::VariableKind<Interner>>>, |
27 | pub(crate) type Variances = chalk_ir::Variances<Interner>; | 30 | InternedWrapper<SmallVec<[GenericArg; 2]>>, |
31 | InternedWrapper<chalk_ir::TyData<Interner>>, | ||
32 | InternedWrapper<chalk_ir::LifetimeData<Interner>>, | ||
33 | InternedWrapper<chalk_ir::ConstData<Interner>>, | ||
34 | InternedWrapper<Vec<chalk_ir::CanonicalVarKind<Interner>>>, | ||
35 | InternedWrapper<Vec<chalk_ir::ProgramClause<Interner>>>, | ||
36 | InternedWrapper<Vec<chalk_ir::QuantifiedWhereClause<Interner>>>, | ||
37 | InternedWrapper<Vec<chalk_ir::Variance>>, | ||
38 | ); | ||
28 | 39 | ||
29 | impl chalk_ir::interner::Interner for Interner { | 40 | impl chalk_ir::interner::Interner for Interner { |
30 | type InternedType = Arc<chalk_ir::TyData<Self>>; | 41 | type InternedType = Interned<InternedWrapper<chalk_ir::TyData<Interner>>>; |
31 | type InternedLifetime = chalk_ir::LifetimeData<Self>; | 42 | type InternedLifetime = Interned<InternedWrapper<chalk_ir::LifetimeData<Self>>>; |
32 | type InternedConst = Arc<chalk_ir::ConstData<Self>>; | 43 | type InternedConst = Interned<InternedWrapper<chalk_ir::ConstData<Self>>>; |
33 | type InternedConcreteConst = (); | 44 | type InternedConcreteConst = (); |
34 | type InternedGenericArg = chalk_ir::GenericArgData<Self>; | 45 | type InternedGenericArg = chalk_ir::GenericArgData<Self>; |
35 | type InternedGoal = Arc<GoalData<Self>>; | 46 | type InternedGoal = Arc<GoalData<Self>>; |
36 | type InternedGoals = Vec<Goal<Self>>; | 47 | type InternedGoals = Vec<Goal<Self>>; |
37 | type InternedSubstitution = SmallVec<[GenericArg<Self>; 2]>; | 48 | type InternedSubstitution = Interned<InternedWrapper<SmallVec<[GenericArg; 2]>>>; |
38 | type InternedProgramClause = Arc<chalk_ir::ProgramClauseData<Self>>; | 49 | type InternedProgramClause = chalk_ir::ProgramClauseData<Self>; |
39 | type InternedProgramClauses = Arc<[chalk_ir::ProgramClause<Self>]>; | 50 | type InternedProgramClauses = Interned<InternedWrapper<Vec<chalk_ir::ProgramClause<Self>>>>; |
40 | type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>; | 51 | type InternedQuantifiedWhereClauses = |
41 | type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>; | 52 | Interned<InternedWrapper<Vec<chalk_ir::QuantifiedWhereClause<Self>>>>; |
42 | type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>; | 53 | type InternedVariableKinds = Interned<InternedWrapper<Vec<chalk_ir::VariableKind<Interner>>>>; |
54 | type InternedCanonicalVarKinds = | ||
55 | Interned<InternedWrapper<Vec<chalk_ir::CanonicalVarKind<Self>>>>; | ||
43 | type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>; | 56 | type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>; |
44 | type InternedVariances = Arc<[chalk_ir::Variance]>; | 57 | type InternedVariances = Interned<InternedWrapper<Vec<chalk_ir::Variance>>>; |
45 | type DefId = InternId; | 58 | type DefId = InternId; |
46 | type InternedAdtId = hir_def::AdtId; | 59 | type InternedAdtId = hir_def::AdtId; |
47 | type Identifier = TypeAliasId; | 60 | type Identifier = TypeAliasId; |
48 | type FnAbi = (); | 61 | type FnAbi = (); |
49 | 62 | ||
50 | fn debug_adt_id(type_kind_id: AdtId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | 63 | fn debug_adt_id( |
64 | type_kind_id: chalk_db::AdtId, | ||
65 | fmt: &mut fmt::Formatter<'_>, | ||
66 | ) -> Option<fmt::Result> { | ||
51 | tls::with_current_program(|prog| Some(prog?.debug_struct_id(type_kind_id, fmt))) | 67 | tls::with_current_program(|prog| Some(prog?.debug_struct_id(type_kind_id, fmt))) |
52 | } | 68 | } |
53 | 69 | ||
54 | fn debug_trait_id(type_kind_id: TraitId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | 70 | fn debug_trait_id( |
71 | type_kind_id: chalk_db::TraitId, | ||
72 | fmt: &mut fmt::Formatter<'_>, | ||
73 | ) -> Option<fmt::Result> { | ||
55 | tls::with_current_program(|prog| Some(prog?.debug_trait_id(type_kind_id, fmt))) | 74 | tls::with_current_program(|prog| Some(prog?.debug_trait_id(type_kind_id, fmt))) |
56 | } | 75 | } |
57 | 76 | ||
58 | fn debug_assoc_type_id(id: AssocTypeId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | 77 | fn debug_assoc_type_id( |
78 | id: chalk_db::AssocTypeId, | ||
79 | fmt: &mut fmt::Formatter<'_>, | ||
80 | ) -> Option<fmt::Result> { | ||
59 | tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt))) | 81 | tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt))) |
60 | } | 82 | } |
61 | 83 | ||
@@ -99,7 +121,7 @@ impl chalk_ir::interner::Interner for Interner { | |||
99 | } | 121 | } |
100 | 122 | ||
101 | fn debug_generic_arg( | 123 | fn debug_generic_arg( |
102 | parameter: &GenericArg<Interner>, | 124 | parameter: &GenericArg, |
103 | fmt: &mut fmt::Formatter<'_>, | 125 | fmt: &mut fmt::Formatter<'_>, |
104 | ) -> Option<fmt::Result> { | 126 | ) -> Option<fmt::Result> { |
105 | tls::with_current_program(|prog| Some(prog?.debug_generic_arg(parameter, fmt))) | 127 | tls::with_current_program(|prog| Some(prog?.debug_generic_arg(parameter, fmt))) |
@@ -194,30 +216,30 @@ impl chalk_ir::interner::Interner for Interner { | |||
194 | 216 | ||
195 | fn intern_ty(&self, kind: chalk_ir::TyKind<Self>) -> Self::InternedType { | 217 | fn intern_ty(&self, kind: chalk_ir::TyKind<Self>) -> Self::InternedType { |
196 | let flags = kind.compute_flags(self); | 218 | let flags = kind.compute_flags(self); |
197 | Arc::new(chalk_ir::TyData { kind, flags }) | 219 | Interned::new(InternedWrapper(chalk_ir::TyData { kind, flags })) |
198 | } | 220 | } |
199 | 221 | ||
200 | fn ty_data<'a>(&self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData<Self> { | 222 | fn ty_data<'a>(&self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData<Self> { |
201 | ty | 223 | &ty.0 |
202 | } | 224 | } |
203 | 225 | ||
204 | fn intern_lifetime(&self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime { | 226 | fn intern_lifetime(&self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime { |
205 | lifetime | 227 | Interned::new(InternedWrapper(lifetime)) |
206 | } | 228 | } |
207 | 229 | ||
208 | fn lifetime_data<'a>( | 230 | fn lifetime_data<'a>( |
209 | &self, | 231 | &self, |
210 | lifetime: &'a Self::InternedLifetime, | 232 | lifetime: &'a Self::InternedLifetime, |
211 | ) -> &'a chalk_ir::LifetimeData<Self> { | 233 | ) -> &'a chalk_ir::LifetimeData<Self> { |
212 | lifetime | 234 | &lifetime.0 |
213 | } | 235 | } |
214 | 236 | ||
215 | fn intern_const(&self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst { | 237 | fn intern_const(&self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst { |
216 | Arc::new(constant) | 238 | Interned::new(InternedWrapper(constant)) |
217 | } | 239 | } |
218 | 240 | ||
219 | fn const_data<'a>(&self, constant: &'a Self::InternedConst) -> &'a chalk_ir::ConstData<Self> { | 241 | fn const_data<'a>(&self, constant: &'a Self::InternedConst) -> &'a chalk_ir::ConstData<Self> { |
220 | constant | 242 | &constant.0 |
221 | } | 243 | } |
222 | 244 | ||
223 | fn const_eq( | 245 | fn const_eq( |
@@ -264,23 +286,23 @@ impl chalk_ir::interner::Interner for Interner { | |||
264 | 286 | ||
265 | fn intern_substitution<E>( | 287 | fn intern_substitution<E>( |
266 | &self, | 288 | &self, |
267 | data: impl IntoIterator<Item = Result<GenericArg<Self>, E>>, | 289 | data: impl IntoIterator<Item = Result<GenericArg, E>>, |
268 | ) -> Result<Self::InternedSubstitution, E> { | 290 | ) -> Result<Self::InternedSubstitution, E> { |
269 | data.into_iter().collect() | 291 | Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) |
270 | } | 292 | } |
271 | 293 | ||
272 | fn substitution_data<'a>( | 294 | fn substitution_data<'a>( |
273 | &self, | 295 | &self, |
274 | substitution: &'a Self::InternedSubstitution, | 296 | substitution: &'a Self::InternedSubstitution, |
275 | ) -> &'a [GenericArg<Self>] { | 297 | ) -> &'a [GenericArg] { |
276 | substitution | 298 | &substitution.as_ref().0 |
277 | } | 299 | } |
278 | 300 | ||
279 | fn intern_program_clause( | 301 | fn intern_program_clause( |
280 | &self, | 302 | &self, |
281 | data: chalk_ir::ProgramClauseData<Self>, | 303 | data: chalk_ir::ProgramClauseData<Self>, |
282 | ) -> Self::InternedProgramClause { | 304 | ) -> Self::InternedProgramClause { |
283 | Arc::new(data) | 305 | data |
284 | } | 306 | } |
285 | 307 | ||
286 | fn program_clause_data<'a>( | 308 | fn program_clause_data<'a>( |
@@ -294,7 +316,7 @@ impl chalk_ir::interner::Interner for Interner { | |||
294 | &self, | 316 | &self, |
295 | data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>, | 317 | data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>, |
296 | ) -> Result<Self::InternedProgramClauses, E> { | 318 | ) -> Result<Self::InternedProgramClauses, E> { |
297 | data.into_iter().collect() | 319 | Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) |
298 | } | 320 | } |
299 | 321 | ||
300 | fn program_clauses_data<'a>( | 322 | fn program_clauses_data<'a>( |
@@ -308,7 +330,7 @@ impl chalk_ir::interner::Interner for Interner { | |||
308 | &self, | 330 | &self, |
309 | data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>, | 331 | data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>, |
310 | ) -> Result<Self::InternedQuantifiedWhereClauses, E> { | 332 | ) -> Result<Self::InternedQuantifiedWhereClauses, E> { |
311 | data.into_iter().collect() | 333 | Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) |
312 | } | 334 | } |
313 | 335 | ||
314 | fn quantified_where_clauses_data<'a>( | 336 | fn quantified_where_clauses_data<'a>( |
@@ -322,21 +344,21 @@ impl chalk_ir::interner::Interner for Interner { | |||
322 | &self, | 344 | &self, |
323 | data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>, | 345 | data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>, |
324 | ) -> Result<Self::InternedVariableKinds, E> { | 346 | ) -> Result<Self::InternedVariableKinds, E> { |
325 | data.into_iter().collect() | 347 | Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) |
326 | } | 348 | } |
327 | 349 | ||
328 | fn variable_kinds_data<'a>( | 350 | fn variable_kinds_data<'a>( |
329 | &self, | 351 | &self, |
330 | parameter_kinds: &'a Self::InternedVariableKinds, | 352 | parameter_kinds: &'a Self::InternedVariableKinds, |
331 | ) -> &'a [chalk_ir::VariableKind<Self>] { | 353 | ) -> &'a [chalk_ir::VariableKind<Self>] { |
332 | ¶meter_kinds | 354 | ¶meter_kinds.as_ref().0 |
333 | } | 355 | } |
334 | 356 | ||
335 | fn intern_canonical_var_kinds<E>( | 357 | fn intern_canonical_var_kinds<E>( |
336 | &self, | 358 | &self, |
337 | data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>, | 359 | data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>, |
338 | ) -> Result<Self::InternedCanonicalVarKinds, E> { | 360 | ) -> Result<Self::InternedCanonicalVarKinds, E> { |
339 | data.into_iter().collect() | 361 | Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) |
340 | } | 362 | } |
341 | 363 | ||
342 | fn canonical_var_kinds_data<'a>( | 364 | fn canonical_var_kinds_data<'a>( |
@@ -376,7 +398,7 @@ impl chalk_ir::interner::Interner for Interner { | |||
376 | &self, | 398 | &self, |
377 | data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>, | 399 | data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>, |
378 | ) -> Result<Self::InternedVariances, E> { | 400 | ) -> Result<Self::InternedVariances, E> { |
379 | data.into_iter().collect() | 401 | Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) |
380 | } | 402 | } |
381 | 403 | ||
382 | fn variances_data<'a>( | 404 | fn variances_data<'a>( |
@@ -390,3 +412,12 @@ impl chalk_ir::interner::Interner for Interner { | |||
390 | impl chalk_ir::interner::HasInterner for Interner { | 412 | impl chalk_ir::interner::HasInterner for Interner { |
391 | type Interner = Self; | 413 | type Interner = Self; |
392 | } | 414 | } |
415 | |||
416 | #[macro_export] | ||
417 | macro_rules! has_interner { | ||
418 | ($t:ty) => { | ||
419 | impl HasInterner for $t { | ||
420 | type Interner = crate::Interner; | ||
421 | } | ||
422 | }; | ||
423 | } | ||
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 874c95411..113234fa4 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -7,21 +7,23 @@ macro_rules! eprintln { | |||
7 | } | 7 | } |
8 | 8 | ||
9 | mod autoderef; | 9 | mod autoderef; |
10 | pub mod primitive; | ||
11 | pub mod traits; | ||
12 | pub mod method_resolution; | ||
13 | mod op; | ||
14 | mod lower; | ||
15 | pub(crate) mod infer; | ||
16 | pub(crate) mod utils; | ||
17 | mod chalk_cast; | ||
18 | mod chalk_ext; | ||
19 | mod builder; | 10 | mod builder; |
11 | mod chalk_db; | ||
12 | mod chalk_ext; | ||
13 | mod infer; | ||
14 | mod interner; | ||
15 | mod lower; | ||
16 | mod mapping; | ||
17 | mod op; | ||
18 | mod tls; | ||
19 | mod utils; | ||
20 | mod walk; | 20 | mod walk; |
21 | |||
22 | pub mod display; | ||
23 | pub mod db; | 21 | pub mod db; |
24 | pub mod diagnostics; | 22 | pub mod diagnostics; |
23 | pub mod display; | ||
24 | pub mod method_resolution; | ||
25 | pub mod primitive; | ||
26 | pub mod traits; | ||
25 | 27 | ||
26 | #[cfg(test)] | 28 | #[cfg(test)] |
27 | mod tests; | 29 | mod tests; |
@@ -30,16 +32,12 @@ mod test_db; | |||
30 | 32 | ||
31 | use std::sync::Arc; | 33 | use std::sync::Arc; |
32 | 34 | ||
33 | use base_db::salsa; | ||
34 | use chalk_ir::{ | 35 | use chalk_ir::{ |
35 | fold::{Fold, Shift}, | 36 | fold::{Fold, Shift}, |
36 | interner::HasInterner, | 37 | interner::HasInterner, |
37 | UintTy, | 38 | UintTy, |
38 | }; | 39 | }; |
39 | use hir_def::{ | 40 | use hir_def::{expr::ExprId, type_ref::Rawness, TypeParamId}; |
40 | expr::ExprId, type_ref::Rawness, ConstParamId, LifetimeParamId, TraitId, TypeAliasId, | ||
41 | TypeParamId, | ||
42 | }; | ||
43 | 41 | ||
44 | use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; | 42 | use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; |
45 | 43 | ||
@@ -47,11 +45,17 @@ pub use autoderef::autoderef; | |||
47 | pub use builder::TyBuilder; | 45 | pub use builder::TyBuilder; |
48 | pub use chalk_ext::*; | 46 | pub use chalk_ext::*; |
49 | pub use infer::{could_unify, InferenceResult}; | 47 | pub use infer::{could_unify, InferenceResult}; |
48 | pub use interner::Interner; | ||
50 | pub use lower::{ | 49 | pub use lower::{ |
51 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, | 50 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, |
52 | TyDefId, TyLoweringContext, ValueTyDefId, | 51 | TyDefId, TyLoweringContext, ValueTyDefId, |
53 | }; | 52 | }; |
54 | pub use traits::{chalk::Interner, TraitEnvironment}; | 53 | pub use mapping::{ |
54 | const_from_placeholder_idx, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, | ||
55 | from_placeholder_idx, lt_from_placeholder_idx, to_assoc_type_id, to_chalk_trait_id, | ||
56 | to_foreign_def_id, to_placeholder_idx, | ||
57 | }; | ||
58 | pub use traits::TraitEnvironment; | ||
55 | pub use walk::TypeWalk; | 59 | pub use walk::TypeWalk; |
56 | 60 | ||
57 | pub use chalk_ir::{ | 61 | pub use chalk_ir::{ |
@@ -94,6 +98,10 @@ pub type ConstValue = chalk_ir::ConstValue<Interner>; | |||
94 | pub type ConcreteConst = chalk_ir::ConcreteConst<Interner>; | 98 | pub type ConcreteConst = chalk_ir::ConcreteConst<Interner>; |
95 | 99 | ||
96 | pub type ChalkTraitId = chalk_ir::TraitId<Interner>; | 100 | pub type ChalkTraitId = chalk_ir::TraitId<Interner>; |
101 | pub type TraitRef = chalk_ir::TraitRef<Interner>; | ||
102 | pub type QuantifiedWhereClause = Binders<WhereClause>; | ||
103 | pub type QuantifiedWhereClauses = chalk_ir::QuantifiedWhereClauses<Interner>; | ||
104 | pub type Canonical<T> = chalk_ir::Canonical<T>; | ||
97 | 105 | ||
98 | pub type FnSig = chalk_ir::FnSig<Interner>; | 106 | pub type FnSig = chalk_ir::FnSig<Interner>; |
99 | 107 | ||
@@ -109,7 +117,7 @@ pub type WhereClause = chalk_ir::WhereClause<Interner>; | |||
109 | pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution { | 117 | pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution { |
110 | Substitution::from_iter( | 118 | Substitution::from_iter( |
111 | &Interner, | 119 | &Interner, |
112 | s.interned()[..std::cmp::min(s.len(&Interner), n)].iter().cloned(), | 120 | s.as_slice(&Interner)[..std::cmp::min(s.len(&Interner), n)].iter().cloned(), |
113 | ) | 121 | ) |
114 | } | 122 | } |
115 | 123 | ||
@@ -118,14 +126,14 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> { | |||
118 | generics(db.upcast(), id.parent).param_idx(id) | 126 | generics(db.upcast(), id.parent).param_idx(id) |
119 | } | 127 | } |
120 | 128 | ||
121 | pub fn wrap_empty_binders<T>(value: T) -> Binders<T> | 129 | pub(crate) fn wrap_empty_binders<T>(value: T) -> Binders<T> |
122 | where | 130 | where |
123 | T: Fold<Interner, Result = T> + HasInterner<Interner = Interner>, | 131 | T: Fold<Interner, Result = T> + HasInterner<Interner = Interner>, |
124 | { | 132 | { |
125 | Binders::empty(&Interner, value.shifted_in_from(&Interner, DebruijnIndex::ONE)) | 133 | Binders::empty(&Interner, value.shifted_in_from(&Interner, DebruijnIndex::ONE)) |
126 | } | 134 | } |
127 | 135 | ||
128 | pub fn make_only_type_binders<T: HasInterner<Interner = Interner>>( | 136 | pub(crate) fn make_only_type_binders<T: HasInterner<Interner = Interner>>( |
129 | num_vars: usize, | 137 | num_vars: usize, |
130 | value: T, | 138 | value: T, |
131 | ) -> Binders<T> { | 139 | ) -> Binders<T> { |
@@ -153,14 +161,6 @@ pub fn make_canonical<T: HasInterner<Interner = Interner>>( | |||
153 | Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } | 161 | Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } |
154 | } | 162 | } |
155 | 163 | ||
156 | pub type TraitRef = chalk_ir::TraitRef<Interner>; | ||
157 | |||
158 | pub type QuantifiedWhereClause = Binders<WhereClause>; | ||
159 | |||
160 | pub type QuantifiedWhereClauses = chalk_ir::QuantifiedWhereClauses<Interner>; | ||
161 | |||
162 | pub type Canonical<T> = chalk_ir::Canonical<T>; | ||
163 | |||
164 | /// A function signature as seen by type inference: Several parameter types and | 164 | /// A function signature as seen by type inference: Several parameter types and |
165 | /// one return type. | 165 | /// one return type. |
166 | #[derive(Clone, PartialEq, Eq, Debug)] | 166 | #[derive(Clone, PartialEq, Eq, Debug)] |
@@ -169,6 +169,8 @@ pub struct CallableSig { | |||
169 | is_varargs: bool, | 169 | is_varargs: bool, |
170 | } | 170 | } |
171 | 171 | ||
172 | has_interner!(CallableSig); | ||
173 | |||
172 | /// A polymorphic function signature. | 174 | /// A polymorphic function signature. |
173 | pub type PolyFnSig = Binders<CallableSig>; | 175 | pub type PolyFnSig = Binders<CallableSig>; |
174 | 176 | ||
@@ -187,7 +189,7 @@ impl CallableSig { | |||
187 | .shifted_out_to(&Interner, DebruijnIndex::ONE) | 189 | .shifted_out_to(&Interner, DebruijnIndex::ONE) |
188 | .expect("unexpected lifetime vars in fn ptr") | 190 | .expect("unexpected lifetime vars in fn ptr") |
189 | .0 | 191 | .0 |
190 | .interned() | 192 | .as_slice(&Interner) |
191 | .iter() | 193 | .iter() |
192 | .map(|arg| arg.assert_ty_ref(&Interner).clone()) | 194 | .map(|arg| arg.assert_ty_ref(&Interner).clone()) |
193 | .collect(), | 195 | .collect(), |
@@ -232,61 +234,13 @@ pub struct ReturnTypeImplTraits { | |||
232 | pub(crate) impl_traits: Vec<ReturnTypeImplTrait>, | 234 | pub(crate) impl_traits: Vec<ReturnTypeImplTrait>, |
233 | } | 235 | } |
234 | 236 | ||
237 | has_interner!(ReturnTypeImplTraits); | ||
238 | |||
235 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 239 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
236 | pub(crate) struct ReturnTypeImplTrait { | 240 | pub(crate) struct ReturnTypeImplTrait { |
237 | pub(crate) bounds: Binders<Vec<QuantifiedWhereClause>>, | 241 | pub(crate) bounds: Binders<Vec<QuantifiedWhereClause>>, |
238 | } | 242 | } |
239 | 243 | ||
240 | pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId { | ||
241 | chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id)) | ||
242 | } | ||
243 | |||
244 | pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId { | ||
245 | salsa::InternKey::from_intern_id(id.0) | ||
246 | } | ||
247 | |||
248 | pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId { | ||
249 | chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id)) | ||
250 | } | ||
251 | |||
252 | pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId { | ||
253 | salsa::InternKey::from_intern_id(id.0) | ||
254 | } | ||
255 | |||
256 | pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeParamId { | ||
257 | assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); | ||
258 | let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); | ||
259 | db.lookup_intern_type_param_id(interned_id) | ||
260 | } | ||
261 | |||
262 | pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex { | ||
263 | let interned_id = db.intern_type_param_id(id); | ||
264 | PlaceholderIndex { | ||
265 | ui: chalk_ir::UniverseIndex::ROOT, | ||
266 | idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(), | ||
267 | } | ||
268 | } | ||
269 | |||
270 | pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId { | ||
271 | assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); | ||
272 | let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); | ||
273 | db.lookup_intern_lifetime_param_id(interned_id) | ||
274 | } | ||
275 | |||
276 | pub fn const_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> ConstParamId { | ||
277 | assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); | ||
278 | let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); | ||
279 | db.lookup_intern_const_param_id(interned_id) | ||
280 | } | ||
281 | |||
282 | pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId { | ||
283 | chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id)) | ||
284 | } | ||
285 | |||
286 | pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId { | ||
287 | salsa::InternKey::from_intern_id(id.0) | ||
288 | } | ||
289 | |||
290 | pub fn static_lifetime() -> Lifetime { | 244 | pub fn static_lifetime() -> Lifetime { |
291 | LifetimeData::Static.intern(&Interner) | 245 | LifetimeData::Static.intern(&Interner) |
292 | } | 246 | } |
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 8a22d9ea3..a035686bc 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs | |||
@@ -27,13 +27,14 @@ use stdx::impl_from; | |||
27 | 27 | ||
28 | use crate::{ | 28 | use crate::{ |
29 | db::HirDatabase, | 29 | db::HirDatabase, |
30 | dummy_usize_const, static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx, | 30 | dummy_usize_const, |
31 | traits::chalk::{Interner, ToChalk}, | 31 | mapping::ToChalk, |
32 | static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx, | ||
32 | utils::{ | 33 | utils::{ |
33 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, Generics, | 34 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, Generics, |
34 | }, | 35 | }, |
35 | AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, | 36 | AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, |
36 | FnSubst, ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, | 37 | FnSubst, ImplTraitId, Interner, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, |
37 | QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, | 38 | QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, |
38 | TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause, | 39 | TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause, |
39 | }; | 40 | }; |
diff --git a/crates/hir_ty/src/mapping.rs b/crates/hir_ty/src/mapping.rs new file mode 100644 index 000000000..5e86fafe5 --- /dev/null +++ b/crates/hir_ty/src/mapping.rs | |||
@@ -0,0 +1,154 @@ | |||
1 | //! This module contains the implementations of the `ToChalk` trait, which | ||
2 | //! handles conversion between our data types and their corresponding types in | ||
3 | //! Chalk (in both directions); plus some helper functions for more specialized | ||
4 | //! conversions. | ||
5 | |||
6 | use chalk_solve::rust_ir; | ||
7 | |||
8 | use base_db::salsa::{self, InternKey}; | ||
9 | use hir_def::{ConstParamId, LifetimeParamId, TraitId, TypeAliasId, TypeParamId}; | ||
10 | |||
11 | use crate::{ | ||
12 | chalk_db, db::HirDatabase, AssocTypeId, CallableDefId, ChalkTraitId, FnDefId, ForeignDefId, | ||
13 | Interner, OpaqueTyId, PlaceholderIndex, | ||
14 | }; | ||
15 | |||
16 | pub(crate) trait ToChalk { | ||
17 | type Chalk; | ||
18 | fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk; | ||
19 | fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self; | ||
20 | } | ||
21 | |||
22 | pub(crate) fn from_chalk<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T | ||
23 | where | ||
24 | T: ToChalk<Chalk = ChalkT>, | ||
25 | { | ||
26 | T::from_chalk(db, chalk) | ||
27 | } | ||
28 | |||
29 | impl ToChalk for hir_def::ImplId { | ||
30 | type Chalk = chalk_db::ImplId; | ||
31 | |||
32 | fn to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::ImplId { | ||
33 | chalk_ir::ImplId(self.as_intern_id()) | ||
34 | } | ||
35 | |||
36 | fn from_chalk(_db: &dyn HirDatabase, impl_id: chalk_db::ImplId) -> hir_def::ImplId { | ||
37 | InternKey::from_intern_id(impl_id.0) | ||
38 | } | ||
39 | } | ||
40 | |||
41 | impl ToChalk for CallableDefId { | ||
42 | type Chalk = FnDefId; | ||
43 | |||
44 | fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId { | ||
45 | db.intern_callable_def(self).into() | ||
46 | } | ||
47 | |||
48 | fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId { | ||
49 | db.lookup_intern_callable_def(fn_def_id.into()) | ||
50 | } | ||
51 | } | ||
52 | |||
53 | pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); | ||
54 | |||
55 | impl ToChalk for TypeAliasAsValue { | ||
56 | type Chalk = chalk_db::AssociatedTyValueId; | ||
57 | |||
58 | fn to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::AssociatedTyValueId { | ||
59 | rust_ir::AssociatedTyValueId(self.0.as_intern_id()) | ||
60 | } | ||
61 | |||
62 | fn from_chalk( | ||
63 | _db: &dyn HirDatabase, | ||
64 | assoc_ty_value_id: chalk_db::AssociatedTyValueId, | ||
65 | ) -> TypeAliasAsValue { | ||
66 | TypeAliasAsValue(TypeAliasId::from_intern_id(assoc_ty_value_id.0)) | ||
67 | } | ||
68 | } | ||
69 | |||
70 | impl From<FnDefId> for crate::db::InternedCallableDefId { | ||
71 | fn from(fn_def_id: FnDefId) -> Self { | ||
72 | InternKey::from_intern_id(fn_def_id.0) | ||
73 | } | ||
74 | } | ||
75 | |||
76 | impl From<crate::db::InternedCallableDefId> for FnDefId { | ||
77 | fn from(callable_def_id: crate::db::InternedCallableDefId) -> Self { | ||
78 | chalk_ir::FnDefId(callable_def_id.as_intern_id()) | ||
79 | } | ||
80 | } | ||
81 | |||
82 | impl From<OpaqueTyId> for crate::db::InternedOpaqueTyId { | ||
83 | fn from(id: OpaqueTyId) -> Self { | ||
84 | InternKey::from_intern_id(id.0) | ||
85 | } | ||
86 | } | ||
87 | |||
88 | impl From<crate::db::InternedOpaqueTyId> for OpaqueTyId { | ||
89 | fn from(id: crate::db::InternedOpaqueTyId) -> Self { | ||
90 | chalk_ir::OpaqueTyId(id.as_intern_id()) | ||
91 | } | ||
92 | } | ||
93 | |||
94 | impl From<chalk_ir::ClosureId<Interner>> for crate::db::InternedClosureId { | ||
95 | fn from(id: chalk_ir::ClosureId<Interner>) -> Self { | ||
96 | Self::from_intern_id(id.0) | ||
97 | } | ||
98 | } | ||
99 | |||
100 | impl From<crate::db::InternedClosureId> for chalk_ir::ClosureId<Interner> { | ||
101 | fn from(id: crate::db::InternedClosureId) -> Self { | ||
102 | chalk_ir::ClosureId(id.as_intern_id()) | ||
103 | } | ||
104 | } | ||
105 | |||
106 | pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId { | ||
107 | chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id)) | ||
108 | } | ||
109 | |||
110 | pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId { | ||
111 | salsa::InternKey::from_intern_id(id.0) | ||
112 | } | ||
113 | |||
114 | pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId { | ||
115 | chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id)) | ||
116 | } | ||
117 | |||
118 | pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId { | ||
119 | salsa::InternKey::from_intern_id(id.0) | ||
120 | } | ||
121 | |||
122 | pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeParamId { | ||
123 | assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); | ||
124 | let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); | ||
125 | db.lookup_intern_type_param_id(interned_id) | ||
126 | } | ||
127 | |||
128 | pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex { | ||
129 | let interned_id = db.intern_type_param_id(id); | ||
130 | PlaceholderIndex { | ||
131 | ui: chalk_ir::UniverseIndex::ROOT, | ||
132 | idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(), | ||
133 | } | ||
134 | } | ||
135 | |||
136 | pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId { | ||
137 | assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); | ||
138 | let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); | ||
139 | db.lookup_intern_lifetime_param_id(interned_id) | ||
140 | } | ||
141 | |||
142 | pub fn const_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> ConstParamId { | ||
143 | assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); | ||
144 | let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); | ||
145 | db.lookup_intern_const_param_id(interned_id) | ||
146 | } | ||
147 | |||
148 | pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId { | ||
149 | chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id)) | ||
150 | } | ||
151 | |||
152 | pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId { | ||
153 | salsa::InternKey::from_intern_id(id.0) | ||
154 | } | ||
diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index be3e4f09a..3693e3284 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs | |||
@@ -21,32 +21,36 @@ use crate::{ | |||
21 | primitive::{self, FloatTy, IntTy, UintTy}, | 21 | primitive::{self, FloatTy, IntTy, UintTy}, |
22 | static_lifetime, | 22 | static_lifetime, |
23 | utils::all_super_traits, | 23 | utils::all_super_traits, |
24 | AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId, | 24 | AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, ForeignDefId, InEnvironment, Interner, |
25 | InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder, | 25 | Scalar, Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, |
26 | TyExt, TyKind, | ||
27 | }; | 26 | }; |
28 | 27 | ||
29 | /// This is used as a key for indexing impls. | 28 | /// This is used as a key for indexing impls. |
30 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | 29 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
31 | pub enum TyFingerprint { | 30 | pub enum TyFingerprint { |
31 | // These are lang item impls: | ||
32 | Str, | 32 | Str, |
33 | Slice, | 33 | Slice, |
34 | Array, | 34 | Array, |
35 | Never, | 35 | Never, |
36 | RawPtr(Mutability), | 36 | RawPtr(Mutability), |
37 | Scalar(Scalar), | 37 | Scalar(Scalar), |
38 | // These can have user-defined impls: | ||
38 | Adt(hir_def::AdtId), | 39 | Adt(hir_def::AdtId), |
39 | Dyn(TraitId), | 40 | Dyn(TraitId), |
40 | Tuple(usize), | ||
41 | ForeignType(ForeignDefId), | 41 | ForeignType(ForeignDefId), |
42 | FnPtr(usize, FnSig), | 42 | // These only exist for trait impls |
43 | Unit, | ||
44 | Unnameable, | ||
45 | Function(u32), | ||
43 | } | 46 | } |
44 | 47 | ||
45 | impl TyFingerprint { | 48 | impl TyFingerprint { |
46 | /// Creates a TyFingerprint for looking up an impl. Only certain types can | 49 | /// Creates a TyFingerprint for looking up an inherent impl. Only certain |
47 | /// have impls: if we have some `struct S`, we can have an `impl S`, but not | 50 | /// types can have inherent impls: if we have some `struct S`, we can have |
48 | /// `impl &S`. Hence, this will return `None` for reference types and such. | 51 | /// an `impl S`, but not `impl &S`. Hence, this will return `None` for |
49 | pub fn for_impl(ty: &Ty) -> Option<TyFingerprint> { | 52 | /// reference types and such. |
53 | pub fn for_inherent_impl(ty: &Ty) -> Option<TyFingerprint> { | ||
50 | let fp = match ty.kind(&Interner) { | 54 | let fp = match ty.kind(&Interner) { |
51 | TyKind::Str => TyFingerprint::Str, | 55 | TyKind::Str => TyFingerprint::Str, |
52 | TyKind::Never => TyFingerprint::Never, | 56 | TyKind::Never => TyFingerprint::Never, |
@@ -54,17 +58,52 @@ impl TyFingerprint { | |||
54 | TyKind::Array(..) => TyFingerprint::Array, | 58 | TyKind::Array(..) => TyFingerprint::Array, |
55 | TyKind::Scalar(scalar) => TyFingerprint::Scalar(*scalar), | 59 | TyKind::Scalar(scalar) => TyFingerprint::Scalar(*scalar), |
56 | TyKind::Adt(AdtId(adt), _) => TyFingerprint::Adt(*adt), | 60 | TyKind::Adt(AdtId(adt), _) => TyFingerprint::Adt(*adt), |
57 | TyKind::Tuple(cardinality, _) => TyFingerprint::Tuple(*cardinality), | ||
58 | TyKind::Raw(mutability, ..) => TyFingerprint::RawPtr(*mutability), | 61 | TyKind::Raw(mutability, ..) => TyFingerprint::RawPtr(*mutability), |
59 | TyKind::Foreign(alias_id, ..) => TyFingerprint::ForeignType(*alias_id), | 62 | TyKind::Foreign(alias_id, ..) => TyFingerprint::ForeignType(*alias_id), |
60 | TyKind::Function(FnPointer { sig, substitution: substs, .. }) => { | ||
61 | TyFingerprint::FnPtr(substs.0.len(&Interner) - 1, *sig) | ||
62 | } | ||
63 | TyKind::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_))?, | 63 | TyKind::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_))?, |
64 | _ => return None, | 64 | _ => return None, |
65 | }; | 65 | }; |
66 | Some(fp) | 66 | Some(fp) |
67 | } | 67 | } |
68 | |||
69 | /// Creates a TyFingerprint for looking up a trait impl. | ||
70 | pub fn for_trait_impl(ty: &Ty) -> Option<TyFingerprint> { | ||
71 | let fp = match ty.kind(&Interner) { | ||
72 | TyKind::Str => TyFingerprint::Str, | ||
73 | TyKind::Never => TyFingerprint::Never, | ||
74 | TyKind::Slice(..) => TyFingerprint::Slice, | ||
75 | TyKind::Array(..) => TyFingerprint::Array, | ||
76 | TyKind::Scalar(scalar) => TyFingerprint::Scalar(*scalar), | ||
77 | TyKind::Adt(AdtId(adt), _) => TyFingerprint::Adt(*adt), | ||
78 | TyKind::Raw(mutability, ..) => TyFingerprint::RawPtr(*mutability), | ||
79 | TyKind::Foreign(alias_id, ..) => TyFingerprint::ForeignType(*alias_id), | ||
80 | TyKind::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_))?, | ||
81 | TyKind::Ref(_, _, ty) => return TyFingerprint::for_trait_impl(ty), | ||
82 | TyKind::Tuple(_, subst) => { | ||
83 | let first_ty = subst.interned().get(0).map(|arg| arg.assert_ty_ref(&Interner)); | ||
84 | if let Some(ty) = first_ty { | ||
85 | return TyFingerprint::for_trait_impl(ty); | ||
86 | } else { | ||
87 | TyFingerprint::Unit | ||
88 | } | ||
89 | } | ||
90 | TyKind::AssociatedType(_, _) | ||
91 | | TyKind::OpaqueType(_, _) | ||
92 | | TyKind::FnDef(_, _) | ||
93 | | TyKind::Closure(_, _) | ||
94 | | TyKind::Generator(..) | ||
95 | | TyKind::GeneratorWitness(..) => TyFingerprint::Unnameable, | ||
96 | TyKind::Function(fn_ptr) => { | ||
97 | TyFingerprint::Function(fn_ptr.substitution.0.len(&Interner) as u32) | ||
98 | } | ||
99 | TyKind::Alias(_) | ||
100 | | TyKind::Placeholder(_) | ||
101 | | TyKind::BoundVar(_) | ||
102 | | TyKind::InferenceVar(_, _) | ||
103 | | TyKind::Error => return None, | ||
104 | }; | ||
105 | Some(fp) | ||
106 | } | ||
68 | } | 107 | } |
69 | 108 | ||
70 | pub(crate) const ALL_INT_FPS: [TyFingerprint; 12] = [ | 109 | pub(crate) const ALL_INT_FPS: [TyFingerprint; 12] = [ |
@@ -112,7 +151,7 @@ impl TraitImpls { | |||
112 | None => continue, | 151 | None => continue, |
113 | }; | 152 | }; |
114 | let self_ty = db.impl_self_ty(impl_id); | 153 | let self_ty = db.impl_self_ty(impl_id); |
115 | let self_ty_fp = TyFingerprint::for_impl(self_ty.skip_binders()); | 154 | let self_ty_fp = TyFingerprint::for_trait_impl(self_ty.skip_binders()); |
116 | impls | 155 | impls |
117 | .map | 156 | .map |
118 | .entry(target_trait) | 157 | .entry(target_trait) |
@@ -157,10 +196,13 @@ impl TraitImpls { | |||
157 | } | 196 | } |
158 | 197 | ||
159 | /// Queries all trait impls for the given type. | 198 | /// Queries all trait impls for the given type. |
160 | pub fn for_self_ty(&self, fp: TyFingerprint) -> impl Iterator<Item = ImplId> + '_ { | 199 | pub fn for_self_ty_without_blanket_impls( |
200 | &self, | ||
201 | fp: TyFingerprint, | ||
202 | ) -> impl Iterator<Item = ImplId> + '_ { | ||
161 | self.map | 203 | self.map |
162 | .values() | 204 | .values() |
163 | .flat_map(move |impls| impls.get(&None).into_iter().chain(impls.get(&Some(fp)))) | 205 | .flat_map(move |impls| impls.get(&Some(fp)).into_iter()) |
164 | .flat_map(|it| it.iter().copied()) | 206 | .flat_map(|it| it.iter().copied()) |
165 | } | 207 | } |
166 | 208 | ||
@@ -215,9 +257,11 @@ impl InherentImpls { | |||
215 | } | 257 | } |
216 | 258 | ||
217 | let self_ty = db.impl_self_ty(impl_id); | 259 | let self_ty = db.impl_self_ty(impl_id); |
218 | if let Some(fp) = TyFingerprint::for_impl(self_ty.skip_binders()) { | 260 | let fp = TyFingerprint::for_inherent_impl(self_ty.skip_binders()); |
261 | if let Some(fp) = fp { | ||
219 | map.entry(fp).or_default().push(impl_id); | 262 | map.entry(fp).or_default().push(impl_id); |
220 | } | 263 | } |
264 | // `fp` should only be `None` in error cases (either erroneous code or incomplete name resolution) | ||
221 | } | 265 | } |
222 | } | 266 | } |
223 | 267 | ||
@@ -228,7 +272,7 @@ impl InherentImpls { | |||
228 | } | 272 | } |
229 | 273 | ||
230 | pub fn for_self_ty(&self, self_ty: &Ty) -> &[ImplId] { | 274 | pub fn for_self_ty(&self, self_ty: &Ty) -> &[ImplId] { |
231 | match TyFingerprint::for_impl(self_ty) { | 275 | match TyFingerprint::for_inherent_impl(self_ty) { |
232 | Some(fp) => self.map.get(&fp).map(|vec| vec.as_ref()).unwrap_or(&[]), | 276 | Some(fp) => self.map.get(&fp).map(|vec| vec.as_ref()).unwrap_or(&[]), |
233 | None => &[], | 277 | None => &[], |
234 | } | 278 | } |
@@ -609,6 +653,7 @@ fn iterate_trait_method_candidates( | |||
609 | } | 653 | } |
610 | } | 654 | } |
611 | known_implemented = true; | 655 | known_implemented = true; |
656 | // FIXME: we shouldn't be ignoring the binders here | ||
612 | if callback(&self_ty.value, *item) { | 657 | if callback(&self_ty.value, *item) { |
613 | return true; | 658 | return true; |
614 | } | 659 | } |
diff --git a/crates/hir_ty/src/op.rs b/crates/hir_ty/src/op.rs index 0491c5cb4..0222de2bc 100644 --- a/crates/hir_ty/src/op.rs +++ b/crates/hir_ty/src/op.rs | |||
@@ -9,21 +9,55 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { | |||
9 | BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => TyKind::Scalar(Scalar::Bool).intern(&Interner), | 9 | BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => TyKind::Scalar(Scalar::Bool).intern(&Interner), |
10 | BinaryOp::Assignment { .. } => TyBuilder::unit(), | 10 | BinaryOp::Assignment { .. } => TyBuilder::unit(), |
11 | BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => { | 11 | BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => { |
12 | match lhs_ty.kind(&Interner) { | 12 | // all integer combinations are valid here |
13 | if matches!( | ||
14 | lhs_ty.kind(&Interner), | ||
13 | TyKind::Scalar(Scalar::Int(_)) | 15 | TyKind::Scalar(Scalar::Int(_)) |
14 | | TyKind::Scalar(Scalar::Uint(_)) | 16 | | TyKind::Scalar(Scalar::Uint(_)) |
15 | | TyKind::Scalar(Scalar::Float(_)) => lhs_ty, | 17 | | TyKind::InferenceVar(_, TyVariableKind::Integer) |
16 | TyKind::InferenceVar(_, TyVariableKind::Integer) | 18 | ) && matches!( |
17 | | TyKind::InferenceVar(_, TyVariableKind::Float) => lhs_ty, | 19 | rhs_ty.kind(&Interner), |
18 | _ => TyKind::Error.intern(&Interner), | 20 | TyKind::Scalar(Scalar::Int(_)) |
21 | | TyKind::Scalar(Scalar::Uint(_)) | ||
22 | | TyKind::InferenceVar(_, TyVariableKind::Integer) | ||
23 | ) { | ||
24 | lhs_ty | ||
25 | } else { | ||
26 | TyKind::Error.intern(&Interner) | ||
19 | } | 27 | } |
20 | } | 28 | } |
21 | BinaryOp::ArithOp(_) => match rhs_ty.kind(&Interner) { | 29 | BinaryOp::ArithOp(_) => match (lhs_ty.kind(&Interner), rhs_ty.kind(&Interner)) { |
22 | TyKind::Scalar(Scalar::Int(_)) | 30 | // (int, int) | (uint, uint) | (float, float) |
23 | | TyKind::Scalar(Scalar::Uint(_)) | 31 | (TyKind::Scalar(Scalar::Int(_)), TyKind::Scalar(Scalar::Int(_))) |
24 | | TyKind::Scalar(Scalar::Float(_)) => rhs_ty, | 32 | | (TyKind::Scalar(Scalar::Uint(_)), TyKind::Scalar(Scalar::Uint(_))) |
25 | TyKind::InferenceVar(_, TyVariableKind::Integer) | 33 | | (TyKind::Scalar(Scalar::Float(_)), TyKind::Scalar(Scalar::Float(_))) => rhs_ty, |
26 | | TyKind::InferenceVar(_, TyVariableKind::Float) => rhs_ty, | 34 | // ({int}, int) | ({int}, uint) |
35 | (TyKind::InferenceVar(_, TyVariableKind::Integer), TyKind::Scalar(Scalar::Int(_))) | ||
36 | | (TyKind::InferenceVar(_, TyVariableKind::Integer), TyKind::Scalar(Scalar::Uint(_))) => { | ||
37 | rhs_ty | ||
38 | } | ||
39 | // (int, {int}) | (uint, {int}) | ||
40 | (TyKind::Scalar(Scalar::Int(_)), TyKind::InferenceVar(_, TyVariableKind::Integer)) | ||
41 | | (TyKind::Scalar(Scalar::Uint(_)), TyKind::InferenceVar(_, TyVariableKind::Integer)) => { | ||
42 | lhs_ty | ||
43 | } | ||
44 | // ({float} | float) | ||
45 | (TyKind::InferenceVar(_, TyVariableKind::Float), TyKind::Scalar(Scalar::Float(_))) => { | ||
46 | rhs_ty | ||
47 | } | ||
48 | // (float, {float}) | ||
49 | (TyKind::Scalar(Scalar::Float(_)), TyKind::InferenceVar(_, TyVariableKind::Float)) => { | ||
50 | lhs_ty | ||
51 | } | ||
52 | // ({int}, {int}) | ({float}, {float}) | ||
53 | ( | ||
54 | TyKind::InferenceVar(_, TyVariableKind::Integer), | ||
55 | TyKind::InferenceVar(_, TyVariableKind::Integer), | ||
56 | ) | ||
57 | | ( | ||
58 | TyKind::InferenceVar(_, TyVariableKind::Float), | ||
59 | TyKind::InferenceVar(_, TyVariableKind::Float), | ||
60 | ) => rhs_ty, | ||
27 | _ => TyKind::Error.intern(&Interner), | 61 | _ => TyKind::Error.intern(&Interner), |
28 | }, | 62 | }, |
29 | } | 63 | } |
diff --git a/crates/hir_ty/src/primitive.rs b/crates/hir_ty/src/primitive.rs index 2449addfb..d7f48c69a 100644 --- a/crates/hir_ty/src/primitive.rs +++ b/crates/hir_ty/src/primitive.rs | |||
@@ -1,7 +1,4 @@ | |||
1 | //! Defines primitive types, which have a couple of peculiarities: | 1 | //! A few helper functions for dealing with primitives. |
2 | //! | ||
3 | //! * during type inference, they can be uncertain (ie, `let x = 92;`) | ||
4 | //! * they don't belong to any particular crate. | ||
5 | 2 | ||
6 | pub use chalk_ir::{FloatTy, IntTy, UintTy}; | 3 | pub use chalk_ir::{FloatTy, IntTy, UintTy}; |
7 | pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}; | 4 | pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}; |
diff --git a/crates/hir_ty/src/tests/macros.rs b/crates/hir_ty/src/tests/macros.rs index 86e3d8b86..b8e373ed8 100644 --- a/crates/hir_ty/src/tests/macros.rs +++ b/crates/hir_ty/src/tests/macros.rs | |||
@@ -1065,11 +1065,11 @@ fn macro_in_arm() { | |||
1065 | } | 1065 | } |
1066 | "#, | 1066 | "#, |
1067 | expect![[r#" | 1067 | expect![[r#" |
1068 | !0..2 '()': () | ||
1068 | 51..110 '{ ... }; }': () | 1069 | 51..110 '{ ... }; }': () |
1069 | 61..62 'x': u32 | 1070 | 61..62 'x': u32 |
1070 | 65..107 'match ... }': u32 | 1071 | 65..107 'match ... }': u32 |
1071 | 71..73 '()': () | 1072 | 71..73 '()': () |
1072 | 84..91 'unit!()': () | ||
1073 | 95..100 '92u32': u32 | 1073 | 95..100 '92u32': u32 |
1074 | "#]], | 1074 | "#]], |
1075 | ); | 1075 | ); |
diff --git a/crates/hir_ty/src/tests/patterns.rs b/crates/hir_ty/src/tests/patterns.rs index 85a28e76b..f514b3efe 100644 --- a/crates/hir_ty/src/tests/patterns.rs +++ b/crates/hir_ty/src/tests/patterns.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use expect_test::expect; | 1 | use expect_test::expect; |
2 | 2 | ||
3 | use super::{check_infer, check_infer_with_mismatches}; | 3 | use super::{check_infer, check_infer_with_mismatches, check_types}; |
4 | 4 | ||
5 | #[test] | 5 | #[test] |
6 | fn infer_pattern() { | 6 | fn infer_pattern() { |
@@ -825,3 +825,29 @@ fn foo(foo: Foo) { | |||
825 | "#]], | 825 | "#]], |
826 | ); | 826 | ); |
827 | } | 827 | } |
828 | |||
829 | #[test] | ||
830 | fn macro_pat() { | ||
831 | check_types( | ||
832 | r#" | ||
833 | macro_rules! pat { | ||
834 | ($name:ident) => { Enum::Variant1($name) } | ||
835 | } | ||
836 | |||
837 | enum Enum { | ||
838 | Variant1(u8), | ||
839 | Variant2, | ||
840 | } | ||
841 | |||
842 | fn f(e: Enum) { | ||
843 | match e { | ||
844 | pat!(bind) => { | ||
845 | bind; | ||
846 | //^^^^ u8 | ||
847 | } | ||
848 | Enum::Variant2 => {} | ||
849 | } | ||
850 | } | ||
851 | "#, | ||
852 | ) | ||
853 | } | ||
diff --git a/crates/hir_ty/src/tests/regression.rs b/crates/hir_ty/src/tests/regression.rs index b69f86050..9cd9f473d 100644 --- a/crates/hir_ty/src/tests/regression.rs +++ b/crates/hir_ty/src/tests/regression.rs | |||
@@ -974,3 +974,41 @@ fn param_overrides_fn() { | |||
974 | "#, | 974 | "#, |
975 | ) | 975 | ) |
976 | } | 976 | } |
977 | |||
978 | #[test] | ||
979 | fn lifetime_from_chalk_during_deref() { | ||
980 | check_types( | ||
981 | r#" | ||
982 | #[lang = "deref"] | ||
983 | pub trait Deref { | ||
984 | type Target; | ||
985 | } | ||
986 | |||
987 | struct Box<T: ?Sized> {} | ||
988 | impl<T> Deref for Box<T> { | ||
989 | type Target = T; | ||
990 | |||
991 | fn deref(&self) -> &Self::Target { | ||
992 | loop {} | ||
993 | } | ||
994 | } | ||
995 | |||
996 | trait Iterator { | ||
997 | type Item; | ||
998 | } | ||
999 | |||
1000 | pub struct Iter<'a, T: 'a> { | ||
1001 | inner: Box<dyn IterTrait<'a, T, Item = &'a T> + 'a>, | ||
1002 | } | ||
1003 | |||
1004 | trait IterTrait<'a, T: 'a>: Iterator<Item = &'a T> { | ||
1005 | fn clone_box(&self); | ||
1006 | } | ||
1007 | |||
1008 | fn clone_iter<T>(s: Iter<T>) { | ||
1009 | s.inner.clone_box(); | ||
1010 | //^^^^^^^^^^^^^^^^^^^ () | ||
1011 | } | ||
1012 | "#, | ||
1013 | ) | ||
1014 | } | ||
diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 65b71fdfa..ffc7c8ef4 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs | |||
@@ -263,15 +263,14 @@ mod ops { | |||
263 | fn infer_from_bound_1() { | 263 | fn infer_from_bound_1() { |
264 | check_infer( | 264 | check_infer( |
265 | r#" | 265 | r#" |
266 | trait Trait<T> {} | 266 | trait Trait<T> {} |
267 | struct S<T>(T); | 267 | struct S<T>(T); |
268 | impl<U> Trait<U> for S<U> {} | 268 | impl<U> Trait<U> for S<U> {} |
269 | fn foo<T: Trait<u32>>(t: T) {} | 269 | fn foo<T: Trait<u32>>(t: T) {} |
270 | fn test() { | 270 | fn test() { |
271 | let s = S(unknown); | 271 | let s = S(unknown); |
272 | foo(s); | 272 | foo(s); |
273 | } | 273 | }"#, |
274 | "#, | ||
275 | expect![[r#" | 274 | expect![[r#" |
276 | 85..86 't': T | 275 | 85..86 't': T |
277 | 91..93 '{}': () | 276 | 91..93 '{}': () |
@@ -291,15 +290,14 @@ fn infer_from_bound_1() { | |||
291 | fn infer_from_bound_2() { | 290 | fn infer_from_bound_2() { |
292 | check_infer( | 291 | check_infer( |
293 | r#" | 292 | r#" |
294 | trait Trait<T> {} | 293 | trait Trait<T> {} |
295 | struct S<T>(T); | 294 | struct S<T>(T); |
296 | impl<U> Trait<U> for S<U> {} | 295 | impl<U> Trait<U> for S<U> {} |
297 | fn foo<U, T: Trait<U>>(t: T) -> U {} | 296 | fn foo<U, T: Trait<U>>(t: T) -> U {} |
298 | fn test() { | 297 | fn test() { |
299 | let s = S(unknown); | 298 | let s = S(unknown); |
300 | let x: u32 = foo(s); | 299 | let x: u32 = foo(s); |
301 | } | 300 | }"#, |
302 | "#, | ||
303 | expect![[r#" | 301 | expect![[r#" |
304 | 86..87 't': T | 302 | 86..87 't': T |
305 | 97..99 '{}': () | 303 | 97..99 '{}': () |
@@ -321,13 +319,12 @@ fn trait_default_method_self_bound_implements_trait() { | |||
321 | cov_mark::check!(trait_self_implements_self); | 319 | cov_mark::check!(trait_self_implements_self); |
322 | check_infer( | 320 | check_infer( |
323 | r#" | 321 | r#" |
324 | trait Trait { | 322 | trait Trait { |
325 | fn foo(&self) -> i64; | 323 | fn foo(&self) -> i64; |
326 | fn bar(&self) -> { | 324 | fn bar(&self) -> { |
327 | let x = self.foo(); | 325 | let x = self.foo(); |
328 | } | 326 | } |
329 | } | 327 | }"#, |
330 | "#, | ||
331 | expect![[r#" | 328 | expect![[r#" |
332 | 26..30 'self': &Self | 329 | 26..30 'self': &Self |
333 | 52..56 'self': &Self | 330 | 52..56 'self': &Self |
@@ -343,15 +340,14 @@ fn trait_default_method_self_bound_implements_trait() { | |||
343 | fn trait_default_method_self_bound_implements_super_trait() { | 340 | fn trait_default_method_self_bound_implements_super_trait() { |
344 | check_infer( | 341 | check_infer( |
345 | r#" | 342 | r#" |
346 | trait SuperTrait { | 343 | trait SuperTrait { |
347 | fn foo(&self) -> i64; | 344 | fn foo(&self) -> i64; |
348 | } | 345 | } |
349 | trait Trait: SuperTrait { | 346 | trait Trait: SuperTrait { |
350 | fn bar(&self) -> { | 347 | fn bar(&self) -> { |
351 | let x = self.foo(); | 348 | let x = self.foo(); |
352 | } | 349 | } |
353 | } | 350 | }"#, |
354 | "#, | ||
355 | expect![[r#" | 351 | expect![[r#" |
356 | 31..35 'self': &Self | 352 | 31..35 'self': &Self |
357 | 85..89 'self': &Self | 353 | 85..89 'self': &Self |
@@ -367,18 +363,17 @@ fn trait_default_method_self_bound_implements_super_trait() { | |||
367 | fn infer_project_associated_type() { | 363 | fn infer_project_associated_type() { |
368 | check_infer( | 364 | check_infer( |
369 | r#" | 365 | r#" |
370 | trait Iterable { | 366 | trait Iterable { |
371 | type Item; | 367 | type Item; |
372 | } | 368 | } |
373 | struct S; | 369 | struct S; |
374 | impl Iterable for S { type Item = u32; } | 370 | impl Iterable for S { type Item = u32; } |
375 | fn test<T: Iterable>() { | 371 | fn test<T: Iterable>() { |
376 | let x: <S as Iterable>::Item = 1; | 372 | let x: <S as Iterable>::Item = 1; |
377 | let y: <T as Iterable>::Item = no_matter; | 373 | let y: <T as Iterable>::Item = no_matter; |
378 | let z: T::Item = no_matter; | 374 | let z: T::Item = no_matter; |
379 | let a: <T>::Item = no_matter; | 375 | let a: <T>::Item = no_matter; |
380 | } | 376 | }"#, |
381 | "#, | ||
382 | expect![[r#" | 377 | expect![[r#" |
383 | 108..261 '{ ...ter; }': () | 378 | 108..261 '{ ...ter; }': () |
384 | 118..119 'x': u32 | 379 | 118..119 'x': u32 |
@@ -397,20 +392,19 @@ fn infer_project_associated_type() { | |||
397 | fn infer_return_associated_type() { | 392 | fn infer_return_associated_type() { |
398 | check_infer( | 393 | check_infer( |
399 | r#" | 394 | r#" |
400 | trait Iterable { | 395 | trait Iterable { |
401 | type Item; | 396 | type Item; |
402 | } | 397 | } |
403 | struct S; | 398 | struct S; |
404 | impl Iterable for S { type Item = u32; } | 399 | impl Iterable for S { type Item = u32; } |
405 | fn foo1<T: Iterable>(t: T) -> T::Item {} | 400 | fn foo1<T: Iterable>(t: T) -> T::Item {} |
406 | fn foo2<T: Iterable>(t: T) -> <T as Iterable>::Item {} | 401 | fn foo2<T: Iterable>(t: T) -> <T as Iterable>::Item {} |
407 | fn foo3<T: Iterable>(t: T) -> <T>::Item {} | 402 | fn foo3<T: Iterable>(t: T) -> <T>::Item {} |
408 | fn test() { | 403 | fn test() { |
409 | let x = foo1(S); | 404 | let x = foo1(S); |
410 | let y = foo2(S); | 405 | let y = foo2(S); |
411 | let z = foo3(S); | 406 | let z = foo3(S); |
412 | } | 407 | }"#, |
413 | "#, | ||
414 | expect![[r#" | 408 | expect![[r#" |
415 | 106..107 't': T | 409 | 106..107 't': T |
416 | 123..125 '{}': () | 410 | 123..125 '{}': () |
@@ -439,13 +433,12 @@ fn infer_return_associated_type() { | |||
439 | fn infer_associated_type_bound() { | 433 | fn infer_associated_type_bound() { |
440 | check_infer( | 434 | check_infer( |
441 | r#" | 435 | r#" |
442 | trait Iterable { | 436 | trait Iterable { |
443 | type Item; | 437 | type Item; |
444 | } | 438 | } |
445 | fn test<T: Iterable<Item=u32>>() { | 439 | fn test<T: Iterable<Item=u32>>() { |
446 | let y: T::Item = unknown; | 440 | let y: T::Item = unknown; |
447 | } | 441 | }"#, |
448 | "#, | ||
449 | expect![[r#" | 442 | expect![[r#" |
450 | 67..100 '{ ...own; }': () | 443 | 67..100 '{ ...own; }': () |
451 | 77..78 'y': u32 | 444 | 77..78 'y': u32 |
@@ -458,9 +451,8 @@ fn infer_associated_type_bound() { | |||
458 | fn infer_const_body() { | 451 | fn infer_const_body() { |
459 | check_infer( | 452 | check_infer( |
460 | r#" | 453 | r#" |
461 | const A: u32 = 1 + 1; | 454 | const A: u32 = 1 + 1; |
462 | static B: u64 = { let x = 1; x }; | 455 | static B: u64 = { let x = 1; x };"#, |
463 | "#, | ||
464 | expect![[r#" | 456 | expect![[r#" |
465 | 15..16 '1': u32 | 457 | 15..16 '1': u32 |
466 | 15..20 '1 + 1': u32 | 458 | 15..20 '1 + 1': u32 |
@@ -477,13 +469,12 @@ fn infer_const_body() { | |||
477 | fn tuple_struct_fields() { | 469 | fn tuple_struct_fields() { |
478 | check_infer( | 470 | check_infer( |
479 | r#" | 471 | r#" |
480 | struct S(i32, u64); | 472 | struct S(i32, u64); |
481 | fn test() -> u64 { | 473 | fn test() -> u64 { |
482 | let a = S(4, 6); | 474 | let a = S(4, 6); |
483 | let b = a.0; | 475 | let b = a.0; |
484 | a.1 | 476 | a.1 |
485 | } | 477 | }"#, |
486 | "#, | ||
487 | expect![[r#" | 478 | expect![[r#" |
488 | 37..86 '{ ... a.1 }': u64 | 479 | 37..86 '{ ... a.1 }': u64 |
489 | 47..48 'a': S | 480 | 47..48 'a': S |
@@ -504,13 +495,12 @@ fn tuple_struct_fields() { | |||
504 | fn tuple_struct_with_fn() { | 495 | fn tuple_struct_with_fn() { |
505 | check_infer( | 496 | check_infer( |
506 | r#" | 497 | r#" |
507 | struct S(fn(u32) -> u64); | 498 | struct S(fn(u32) -> u64); |
508 | fn test() -> u64 { | 499 | fn test() -> u64 { |
509 | let a = S(|i| 2*i); | 500 | let a = S(|i| 2*i); |
510 | let b = a.0(4); | 501 | let b = a.0(4); |
511 | a.0(2) | 502 | a.0(2) |
512 | } | 503 | }"#, |
513 | "#, | ||
514 | expect![[r#" | 504 | expect![[r#" |
515 | 43..101 '{ ...0(2) }': u64 | 505 | 43..101 '{ ...0(2) }': u64 |
516 | 53..54 'a': S | 506 | 53..54 'a': S |
@@ -949,27 +939,26 @@ fn test<T: ApplyL>(t: T) { | |||
949 | fn argument_impl_trait() { | 939 | fn argument_impl_trait() { |
950 | check_infer_with_mismatches( | 940 | check_infer_with_mismatches( |
951 | r#" | 941 | r#" |
952 | trait Trait<T> { | 942 | trait Trait<T> { |
953 | fn foo(&self) -> T; | 943 | fn foo(&self) -> T; |
954 | fn foo2(&self) -> i64; | 944 | fn foo2(&self) -> i64; |
955 | } | 945 | } |
956 | fn bar(x: impl Trait<u16>) {} | 946 | fn bar(x: impl Trait<u16>) {} |
957 | struct S<T>(T); | 947 | struct S<T>(T); |
958 | impl<T> Trait<T> for S<T> {} | 948 | impl<T> Trait<T> for S<T> {} |
959 | 949 | ||
960 | fn test(x: impl Trait<u64>, y: &impl Trait<u32>) { | 950 | fn test(x: impl Trait<u64>, y: &impl Trait<u32>) { |
961 | x; | 951 | x; |
962 | y; | 952 | y; |
963 | let z = S(1); | 953 | let z = S(1); |
964 | bar(z); | 954 | bar(z); |
965 | x.foo(); | 955 | x.foo(); |
966 | y.foo(); | 956 | y.foo(); |
967 | z.foo(); | 957 | z.foo(); |
968 | x.foo2(); | 958 | x.foo2(); |
969 | y.foo2(); | 959 | y.foo2(); |
970 | z.foo2(); | 960 | z.foo2(); |
971 | } | 961 | }"#, |
972 | "#, | ||
973 | expect![[r#" | 962 | expect![[r#" |
974 | 29..33 'self': &Self | 963 | 29..33 'self': &Self |
975 | 54..58 'self': &Self | 964 | 54..58 'self': &Self |
@@ -1007,30 +996,29 @@ fn argument_impl_trait() { | |||
1007 | fn argument_impl_trait_type_args_1() { | 996 | fn argument_impl_trait_type_args_1() { |
1008 | check_infer_with_mismatches( | 997 | check_infer_with_mismatches( |
1009 | r#" | 998 | r#" |
1010 | trait Trait {} | 999 | trait Trait {} |
1011 | trait Foo { | 1000 | trait Foo { |
1012 | // this function has an implicit Self param, an explicit type param, | 1001 | // this function has an implicit Self param, an explicit type param, |
1013 | // and an implicit impl Trait param! | 1002 | // and an implicit impl Trait param! |
1014 | fn bar<T>(x: impl Trait) -> T { loop {} } | 1003 | fn bar<T>(x: impl Trait) -> T { loop {} } |
1015 | } | 1004 | } |
1016 | fn foo<T>(x: impl Trait) -> T { loop {} } | 1005 | fn foo<T>(x: impl Trait) -> T { loop {} } |
1017 | struct S; | 1006 | struct S; |
1018 | impl Trait for S {} | 1007 | impl Trait for S {} |
1019 | struct F; | 1008 | struct F; |
1020 | impl Foo for F {} | 1009 | impl Foo for F {} |
1021 | 1010 | ||
1022 | fn test() { | 1011 | fn test() { |
1023 | Foo::bar(S); | 1012 | Foo::bar(S); |
1024 | <F as Foo>::bar(S); | 1013 | <F as Foo>::bar(S); |
1025 | F::bar(S); | 1014 | F::bar(S); |
1026 | Foo::bar::<u32>(S); | 1015 | Foo::bar::<u32>(S); |
1027 | <F as Foo>::bar::<u32>(S); | 1016 | <F as Foo>::bar::<u32>(S); |
1028 | 1017 | ||
1029 | foo(S); | 1018 | foo(S); |
1030 | foo::<u32>(S); | 1019 | foo::<u32>(S); |
1031 | foo::<u32, i32>(S); // we should ignore the extraneous i32 | 1020 | foo::<u32, i32>(S); // we should ignore the extraneous i32 |
1032 | } | 1021 | }"#, |
1033 | "#, | ||
1034 | expect![[r#" | 1022 | expect![[r#" |
1035 | 155..156 'x': impl Trait | 1023 | 155..156 'x': impl Trait |
1036 | 175..186 '{ loop {} }': T | 1024 | 175..186 '{ loop {} }': T |
@@ -1073,21 +1061,20 @@ fn argument_impl_trait_type_args_1() { | |||
1073 | fn argument_impl_trait_type_args_2() { | 1061 | fn argument_impl_trait_type_args_2() { |
1074 | check_infer_with_mismatches( | 1062 | check_infer_with_mismatches( |
1075 | r#" | 1063 | r#" |
1076 | trait Trait {} | 1064 | trait Trait {} |
1077 | struct S; | 1065 | struct S; |
1078 | impl Trait for S {} | 1066 | impl Trait for S {} |
1079 | struct F<T>; | 1067 | struct F<T>; |
1080 | impl<T> F<T> { | 1068 | impl<T> F<T> { |
1081 | fn foo<U>(self, x: impl Trait) -> (T, U) { loop {} } | 1069 | fn foo<U>(self, x: impl Trait) -> (T, U) { loop {} } |
1082 | } | 1070 | } |
1083 | 1071 | ||
1084 | fn test() { | 1072 | fn test() { |
1085 | F.foo(S); | 1073 | F.foo(S); |
1086 | F::<u32>.foo(S); | 1074 | F::<u32>.foo(S); |
1087 | F::<u32>.foo::<i32>(S); | 1075 | F::<u32>.foo::<i32>(S); |
1088 | F::<u32>.foo::<i32, u32>(S); // extraneous argument should be ignored | 1076 | F::<u32>.foo::<i32, u32>(S); // extraneous argument should be ignored |
1089 | } | 1077 | }"#, |
1090 | "#, | ||
1091 | expect![[r#" | 1078 | expect![[r#" |
1092 | 87..91 'self': F<T> | 1079 | 87..91 'self': F<T> |
1093 | 93..94 'x': impl Trait | 1080 | 93..94 'x': impl Trait |
@@ -1115,15 +1102,14 @@ fn argument_impl_trait_type_args_2() { | |||
1115 | fn argument_impl_trait_to_fn_pointer() { | 1102 | fn argument_impl_trait_to_fn_pointer() { |
1116 | check_infer_with_mismatches( | 1103 | check_infer_with_mismatches( |
1117 | r#" | 1104 | r#" |
1118 | trait Trait {} | 1105 | trait Trait {} |
1119 | fn foo(x: impl Trait) { loop {} } | 1106 | fn foo(x: impl Trait) { loop {} } |
1120 | struct S; | 1107 | struct S; |
1121 | impl Trait for S {} | 1108 | impl Trait for S {} |
1122 | 1109 | ||
1123 | fn test() { | 1110 | fn test() { |
1124 | let f: fn(S) -> () = foo; | 1111 | let f: fn(S) -> () = foo; |
1125 | } | 1112 | }"#, |
1126 | "#, | ||
1127 | expect![[r#" | 1113 | expect![[r#" |
1128 | 22..23 'x': impl Trait | 1114 | 22..23 'x': impl Trait |
1129 | 37..48 '{ loop {} }': () | 1115 | 37..48 '{ loop {} }': () |
@@ -1140,24 +1126,23 @@ fn argument_impl_trait_to_fn_pointer() { | |||
1140 | fn impl_trait() { | 1126 | fn impl_trait() { |
1141 | check_infer( | 1127 | check_infer( |
1142 | r#" | 1128 | r#" |
1143 | trait Trait<T> { | 1129 | trait Trait<T> { |
1144 | fn foo(&self) -> T; | 1130 | fn foo(&self) -> T; |
1145 | fn foo2(&self) -> i64; | 1131 | fn foo2(&self) -> i64; |
1146 | } | 1132 | } |
1147 | fn bar() -> impl Trait<u64> {} | 1133 | fn bar() -> impl Trait<u64> {} |
1148 | 1134 | ||
1149 | fn test(x: impl Trait<u64>, y: &impl Trait<u64>) { | 1135 | fn test(x: impl Trait<u64>, y: &impl Trait<u64>) { |
1150 | x; | 1136 | x; |
1151 | y; | 1137 | y; |
1152 | let z = bar(); | 1138 | let z = bar(); |
1153 | x.foo(); | 1139 | x.foo(); |
1154 | y.foo(); | 1140 | y.foo(); |
1155 | z.foo(); | 1141 | z.foo(); |
1156 | x.foo2(); | 1142 | x.foo2(); |
1157 | y.foo2(); | 1143 | y.foo2(); |
1158 | z.foo2(); | 1144 | z.foo2(); |
1159 | } | 1145 | }"#, |
1160 | "#, | ||
1161 | expect![[r#" | 1146 | expect![[r#" |
1162 | 29..33 'self': &Self | 1147 | 29..33 'self': &Self |
1163 | 54..58 'self': &Self | 1148 | 54..58 'self': &Self |
@@ -1191,16 +1176,15 @@ fn simple_return_pos_impl_trait() { | |||
1191 | cov_mark::check!(lower_rpit); | 1176 | cov_mark::check!(lower_rpit); |
1192 | check_infer( | 1177 | check_infer( |
1193 | r#" | 1178 | r#" |
1194 | trait Trait<T> { | 1179 | trait Trait<T> { |
1195 | fn foo(&self) -> T; | 1180 | fn foo(&self) -> T; |
1196 | } | 1181 | } |
1197 | fn bar() -> impl Trait<u64> { loop {} } | 1182 | fn bar() -> impl Trait<u64> { loop {} } |
1198 | 1183 | ||
1199 | fn test() { | 1184 | fn test() { |
1200 | let a = bar(); | 1185 | let a = bar(); |
1201 | a.foo(); | 1186 | a.foo(); |
1202 | } | 1187 | }"#, |
1203 | "#, | ||
1204 | expect![[r#" | 1188 | expect![[r#" |
1205 | 29..33 'self': &Self | 1189 | 29..33 'self': &Self |
1206 | 71..82 '{ loop {} }': ! | 1190 | 71..82 '{ loop {} }': ! |
@@ -1220,25 +1204,24 @@ fn simple_return_pos_impl_trait() { | |||
1220 | fn more_return_pos_impl_trait() { | 1204 | fn more_return_pos_impl_trait() { |
1221 | check_infer( | 1205 | check_infer( |
1222 | r#" | 1206 | r#" |
1223 | trait Iterator { | 1207 | trait Iterator { |
1224 | type Item; | 1208 | type Item; |
1225 | fn next(&mut self) -> Self::Item; | 1209 | fn next(&mut self) -> Self::Item; |
1226 | } | 1210 | } |
1227 | trait Trait<T> { | 1211 | trait Trait<T> { |
1228 | fn foo(&self) -> T; | 1212 | fn foo(&self) -> T; |
1229 | } | 1213 | } |
1230 | fn bar() -> (impl Iterator<Item = impl Trait<u32>>, impl Trait<u64>) { loop {} } | 1214 | fn bar() -> (impl Iterator<Item = impl Trait<u32>>, impl Trait<u64>) { loop {} } |
1231 | fn baz<T>(t: T) -> (impl Iterator<Item = impl Trait<T>>, impl Trait<T>) { loop {} } | 1215 | fn baz<T>(t: T) -> (impl Iterator<Item = impl Trait<T>>, impl Trait<T>) { loop {} } |
1232 | 1216 | ||
1233 | fn test() { | 1217 | fn test() { |
1234 | let (a, b) = bar(); | 1218 | let (a, b) = bar(); |
1235 | a.next().foo(); | 1219 | a.next().foo(); |
1236 | b.foo(); | 1220 | b.foo(); |
1237 | let (c, d) = baz(1u128); | 1221 | let (c, d) = baz(1u128); |
1238 | c.next().foo(); | 1222 | c.next().foo(); |
1239 | d.foo(); | 1223 | d.foo(); |
1240 | } | 1224 | }"#, |
1241 | "#, | ||
1242 | expect![[r#" | 1225 | expect![[r#" |
1243 | 49..53 'self': &mut Self | 1226 | 49..53 'self': &mut Self |
1244 | 101..105 'self': &Self | 1227 | 101..105 'self': &Self |
@@ -1279,24 +1262,23 @@ fn more_return_pos_impl_trait() { | |||
1279 | fn dyn_trait() { | 1262 | fn dyn_trait() { |
1280 | check_infer( | 1263 | check_infer( |
1281 | r#" | 1264 | r#" |
1282 | trait Trait<T> { | 1265 | trait Trait<T> { |
1283 | fn foo(&self) -> T; | 1266 | fn foo(&self) -> T; |
1284 | fn foo2(&self) -> i64; | 1267 | fn foo2(&self) -> i64; |
1285 | } | 1268 | } |
1286 | fn bar() -> dyn Trait<u64> {} | 1269 | fn bar() -> dyn Trait<u64> {} |
1287 | 1270 | ||
1288 | fn test(x: dyn Trait<u64>, y: &dyn Trait<u64>) { | 1271 | fn test(x: dyn Trait<u64>, y: &dyn Trait<u64>) { |
1289 | x; | 1272 | x; |
1290 | y; | 1273 | y; |
1291 | let z = bar(); | 1274 | let z = bar(); |
1292 | x.foo(); | 1275 | x.foo(); |
1293 | y.foo(); | 1276 | y.foo(); |
1294 | z.foo(); | 1277 | z.foo(); |
1295 | x.foo2(); | 1278 | x.foo2(); |
1296 | y.foo2(); | 1279 | y.foo2(); |
1297 | z.foo2(); | 1280 | z.foo2(); |
1298 | } | 1281 | }"#, |
1299 | "#, | ||
1300 | expect![[r#" | 1282 | expect![[r#" |
1301 | 29..33 'self': &Self | 1283 | 29..33 'self': &Self |
1302 | 54..58 'self': &Self | 1284 | 54..58 'self': &Self |
@@ -1329,22 +1311,21 @@ fn dyn_trait() { | |||
1329 | fn dyn_trait_in_impl() { | 1311 | fn dyn_trait_in_impl() { |
1330 | check_infer( | 1312 | check_infer( |
1331 | r#" | 1313 | r#" |
1332 | trait Trait<T, U> { | 1314 | trait Trait<T, U> { |
1333 | fn foo(&self) -> (T, U); | 1315 | fn foo(&self) -> (T, U); |
1334 | } | 1316 | } |
1335 | struct S<T, U> {} | 1317 | struct S<T, U> {} |
1336 | impl<T, U> S<T, U> { | 1318 | impl<T, U> S<T, U> { |
1337 | fn bar(&self) -> &dyn Trait<T, U> { loop {} } | 1319 | fn bar(&self) -> &dyn Trait<T, U> { loop {} } |
1338 | } | 1320 | } |
1339 | trait Trait2<T, U> { | 1321 | trait Trait2<T, U> { |
1340 | fn baz(&self) -> (T, U); | 1322 | fn baz(&self) -> (T, U); |
1341 | } | 1323 | } |
1342 | impl<T, U> Trait2<T, U> for dyn Trait<T, U> { } | 1324 | impl<T, U> Trait2<T, U> for dyn Trait<T, U> { } |
1343 | 1325 | ||
1344 | fn test(s: S<u32, i32>) { | 1326 | fn test(s: S<u32, i32>) { |
1345 | s.bar().baz(); | 1327 | s.bar().baz(); |
1346 | } | 1328 | }"#, |
1347 | "#, | ||
1348 | expect![[r#" | 1329 | expect![[r#" |
1349 | 32..36 'self': &Self | 1330 | 32..36 'self': &Self |
1350 | 102..106 'self': &S<T, U> | 1331 | 102..106 'self': &S<T, U> |
@@ -1365,20 +1346,19 @@ fn dyn_trait_in_impl() { | |||
1365 | fn dyn_trait_bare() { | 1346 | fn dyn_trait_bare() { |
1366 | check_infer( | 1347 | check_infer( |
1367 | r#" | 1348 | r#" |
1368 | trait Trait { | 1349 | trait Trait { |
1369 | fn foo(&self) -> u64; | 1350 | fn foo(&self) -> u64; |
1370 | } | 1351 | } |
1371 | fn bar() -> Trait {} | 1352 | fn bar() -> Trait {} |
1372 | 1353 | ||
1373 | fn test(x: Trait, y: &Trait) -> u64 { | 1354 | fn test(x: Trait, y: &Trait) -> u64 { |