Skip to content

Commit 737a969

Browse files
Add helper methods to retrieve Future::Output and Iterator::Item
1 parent 2c6a521 commit 737a969

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/tools/rust-analyzer/crates/hir-def/src/lang_item.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ impl LangItemTarget {
7474
_ => None,
7575
}
7676
}
77+
78+
pub fn as_type_alias(self) -> Option<TypeAliasId> {
79+
match self {
80+
LangItemTarget::TypeAlias(id) => Some(id),
81+
_ => None,
82+
}
83+
}
7784
}
7885

7986
#[derive(Default, Debug, Clone, PartialEq, Eq)]

src/tools/rust-analyzer/crates/hir/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use hir_ty::{
7878
use itertools::Itertools;
7979
use nameres::diagnostics::DefDiagnosticKind;
8080
use rustc_hash::FxHashSet;
81-
use span::{Edition, EditionedFileId, FileId, MacroCallId};
81+
use span::{Edition, EditionedFileId, FileId, MacroCallId, SyntaxContextId};
8282
use stdx::{impl_from, never};
8383
use syntax::{
8484
ast::{self, HasAttrs as _, HasGenericParams, HasName},
@@ -4379,6 +4379,22 @@ impl Type {
43794379
method_resolution::implements_trait(&canonical_ty, db, &self.env, trait_)
43804380
}
43814381

4382+
/// This does **not** resolve `IntoFuture`, only `Future`.
4383+
pub fn future_output(self, db: &dyn HirDatabase) -> Option<Type> {
4384+
let future_output =
4385+
db.lang_item(self.env.krate, LangItem::FutureOutput)?.as_type_alias()?;
4386+
self.normalize_trait_assoc_type(db, &[], future_output.into())
4387+
}
4388+
4389+
/// This does **not** resolve `IntoIterator`, only `Iterator`.
4390+
pub fn iterator_item(self, db: &dyn HirDatabase) -> Option<Type> {
4391+
let iterator_trait = db.lang_item(self.env.krate, LangItem::Iterator)?.as_trait()?;
4392+
let iterator_item = db
4393+
.trait_data(iterator_trait)
4394+
.associated_type_by_name(&Name::new_symbol(sym::Item.clone(), SyntaxContextId::ROOT))?;
4395+
self.normalize_trait_assoc_type(db, &[], iterator_item.into())
4396+
}
4397+
43824398
/// Checks that particular type `ty` implements `std::ops::FnOnce`.
43834399
///
43844400
/// This function can be used to check if a particular type is callable, since FnOnce is a

0 commit comments

Comments
 (0)