Add helper methods to retrieve Future::Output and Iterator::Item

This commit is contained in:
Chayim Refael Friedman 2024-08-22 14:35:39 +03:00
parent 34920dde8d
commit e6d59e65ec
2 changed files with 24 additions and 1 deletions

View file

@ -74,6 +74,13 @@ impl LangItemTarget {
_ => None, _ => None,
} }
} }
pub fn as_type_alias(self) -> Option<TypeAliasId> {
match self {
LangItemTarget::TypeAlias(id) => Some(id),
_ => None,
}
}
} }
#[derive(Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq)]

View file

@ -78,7 +78,7 @@ use hir_ty::{
use itertools::Itertools; use itertools::Itertools;
use nameres::diagnostics::DefDiagnosticKind; use nameres::diagnostics::DefDiagnosticKind;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use span::{Edition, EditionedFileId, FileId, MacroCallId}; use span::{Edition, EditionedFileId, FileId, MacroCallId, SyntaxContextId};
use stdx::{impl_from, never}; use stdx::{impl_from, never};
use syntax::{ use syntax::{
ast::{self, HasAttrs as _, HasGenericParams, HasName}, ast::{self, HasAttrs as _, HasGenericParams, HasName},
@ -4379,6 +4379,22 @@ impl Type {
method_resolution::implements_trait(&canonical_ty, db, &self.env, trait_) method_resolution::implements_trait(&canonical_ty, db, &self.env, trait_)
} }
/// This does **not** resolve `IntoFuture`, only `Future`.
pub fn future_output(self, db: &dyn HirDatabase) -> Option<Type> {
let future_output =
db.lang_item(self.env.krate, LangItem::FutureOutput)?.as_type_alias()?;
self.normalize_trait_assoc_type(db, &[], future_output.into())
}
/// This does **not** resolve `IntoIterator`, only `Iterator`.
pub fn iterator_item(self, db: &dyn HirDatabase) -> Option<Type> {
let iterator_trait = db.lang_item(self.env.krate, LangItem::Iterator)?.as_trait()?;
let iterator_item = db
.trait_data(iterator_trait)
.associated_type_by_name(&Name::new_symbol(sym::Item.clone(), SyntaxContextId::ROOT))?;
self.normalize_trait_assoc_type(db, &[], iterator_item.into())
}
/// Checks that particular type `ty` implements `std::ops::FnOnce`. /// Checks that particular type `ty` implements `std::ops::FnOnce`.
/// ///
/// This function can be used to check if a particular type is callable, since FnOnce is a /// This function can be used to check if a particular type is callable, since FnOnce is a