diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-05-25 14:02:31 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-05-25 14:02:31 +0100 |
commit | ddc2fb392303379316b84da3cdcb281d8413e18a (patch) | |
tree | f4056812324bc4c27a188092e280e49bc1fefc1b /crates/hir_ty | |
parent | 835cf55887527bd1953cb7004259214f7c215095 (diff) | |
parent | 3ee4e6c54c0bb9586c607dd8890d58fb21c95259 (diff) |
Merge #8989
8989: fix: Fix type inference not working for new Try trait r=Veykril a=Veykril
Fixes https://github.com/rust-analyzer/rust-analyzer/issues/8907
bors r+
Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/hir_ty')
-rw-r--r-- | crates/hir_ty/src/infer.rs | 5 | ||||
-rw-r--r-- | crates/hir_ty/src/tests/traits.rs | 37 |
2 files changed, 41 insertions, 1 deletions
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index db3c937ff..edb65622f 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs | |||
@@ -580,7 +580,10 @@ impl<'a> InferenceContext<'a> { | |||
580 | fn resolve_ops_try_ok(&self) -> Option<TypeAliasId> { | 580 | fn resolve_ops_try_ok(&self) -> Option<TypeAliasId> { |
581 | let path = path![core::ops::Try]; | 581 | let path = path![core::ops::Try]; |
582 | let trait_ = self.resolver.resolve_known_trait(self.db.upcast(), &path)?; | 582 | let trait_ = self.resolver.resolve_known_trait(self.db.upcast(), &path)?; |
583 | self.db.trait_data(trait_).associated_type_by_name(&name![Ok]) | 583 | let trait_data = self.db.trait_data(trait_); |
584 | trait_data | ||
585 | .associated_type_by_name(&name![Ok]) | ||
586 | .or_else(|| trait_data.associated_type_by_name(&name![Output])) | ||
584 | } | 587 | } |
585 | 588 | ||
586 | fn resolve_ops_neg_output(&self) -> Option<TypeAliasId> { | 589 | fn resolve_ops_neg_output(&self) -> Option<TypeAliasId> { |
diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index a5a2df54c..71905baeb 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs | |||
@@ -161,6 +161,43 @@ mod result { | |||
161 | } | 161 | } |
162 | 162 | ||
163 | #[test] | 163 | #[test] |
164 | fn infer_tryv2() { | ||
165 | check_types( | ||
166 | r#" | ||
167 | //- /main.rs crate:main deps:core | ||
168 | fn test() { | ||
169 | let r: Result<i32, u64> = Result::Ok(1); | ||
170 | let v = r?; | ||
171 | v; | ||
172 | } //^ i32 | ||
173 | |||
174 | //- /core.rs crate:core | ||
175 | #[prelude_import] use ops::*; | ||
176 | mod ops { | ||
177 | trait Try { | ||
178 | type Output; | ||
179 | type Residual; | ||
180 | } | ||
181 | } | ||
182 | |||
183 | #[prelude_import] use result::*; | ||
184 | mod result { | ||
185 | enum Infallible {} | ||
186 | enum Result<O, E> { | ||
187 | Ok(O), | ||
188 | Err(E) | ||
189 | } | ||
190 | |||
191 | impl<O, E> crate::ops::Try for Result<O, E> { | ||
192 | type Output = O; | ||
193 | type Error = Result<Infallible, E>; | ||
194 | } | ||
195 | } | ||
196 | "#, | ||
197 | ); | ||
198 | } | ||
199 | |||
200 | #[test] | ||
164 | fn infer_for_loop() { | 201 | fn infer_for_loop() { |
165 | check_types( | 202 | check_types( |
166 | r#" | 203 | r#" |