2020-02-18 11:35:10 -06:00
|
|
|
//! See `Semantics`.
|
|
|
|
|
2020-02-29 11:32:18 -06:00
|
|
|
mod source_to_def;
|
|
|
|
|
2022-12-20 13:33:27 -06:00
|
|
|
use std::{cell::RefCell, fmt, iter, mem, ops};
|
2020-02-18 11:35:10 -06:00
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::{FileId, FileRange};
|
2022-12-21 14:34:49 -06:00
|
|
|
use either::Either;
|
2020-02-18 11:35:10 -06:00
|
|
|
use hir_def::{
|
2022-12-21 14:34:49 -06:00
|
|
|
body,
|
2023-04-06 12:36:25 -05:00
|
|
|
hir::Expr,
|
2022-12-21 14:34:49 -06:00
|
|
|
macro_id_to_def_id,
|
2020-08-15 11:50:41 -05:00
|
|
|
resolver::{self, HasResolver, Resolver, TypeNs},
|
2022-03-20 08:38:16 -05:00
|
|
|
type_ref::Mutability,
|
2023-03-04 13:33:28 -06:00
|
|
|
AsMacroCall, DefWithBodyId, FieldId, FunctionId, MacroId, TraitId, VariantId,
|
2020-02-18 11:35:10 -06:00
|
|
|
};
|
2022-03-05 17:17:40 -06:00
|
|
|
use hir_expand::{
|
2023-03-13 10:33:52 -05:00
|
|
|
db::ExpandDatabase,
|
2022-03-05 17:17:40 -06:00
|
|
|
name::{known, AsName},
|
|
|
|
ExpansionInfo, MacroCallId,
|
|
|
|
};
|
2020-04-06 09:58:16 -05:00
|
|
|
use itertools::Itertools;
|
2020-08-12 11:26:51 -05:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2021-08-28 14:18:56 -05:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2020-12-13 15:13:16 -06:00
|
|
|
use syntax::{
|
2021-09-18 06:19:29 -05:00
|
|
|
algo::skip_trivia_token,
|
2022-01-07 07:19:11 -06:00
|
|
|
ast::{self, HasAttrs as _, HasGenericParams, HasLoopBody},
|
2022-07-22 10:29:03 -05:00
|
|
|
match_ast, AstNode, Direction, SyntaxKind, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextSize,
|
2020-12-13 15:13:16 -06:00
|
|
|
};
|
2020-02-18 11:35:10 -06:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
db::HirDatabase,
|
2020-02-29 11:32:18 -06:00
|
|
|
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
|
2022-01-01 13:31:04 -06:00
|
|
|
source_analyzer::{resolve_hir_path, SourceAnalyzer},
|
2022-12-20 13:33:27 -06:00
|
|
|
Access, Adjust, Adjustment, AutoBorrow, BindingMode, BuiltinAttr, Callable, ConstParam, Crate,
|
2022-11-04 11:11:15 -05:00
|
|
|
DeriveHelper, Field, Function, HasSource, HirFileId, Impl, InFile, Label, LifetimeParam, Local,
|
|
|
|
Macro, Module, ModuleDef, Name, OverloadedDeref, Path, ScopeDef, ToolModule, Trait, Type,
|
|
|
|
TypeAlias, TypeParam, VariantDef,
|
2020-02-18 11:35:10 -06:00
|
|
|
};
|
|
|
|
|
2020-03-05 04:08:31 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum PathResolution {
|
|
|
|
/// An item
|
|
|
|
Def(ModuleDef),
|
|
|
|
/// A local binding (only value namespace)
|
|
|
|
Local(Local),
|
2021-06-08 09:50:10 -05:00
|
|
|
/// A type parameter
|
2020-03-05 04:08:31 -06:00
|
|
|
TypeParam(TypeParam),
|
2021-06-08 09:50:10 -05:00
|
|
|
/// A const parameter
|
2021-01-01 03:06:42 -06:00
|
|
|
ConstParam(ConstParam),
|
2020-12-17 05:36:15 -06:00
|
|
|
SelfType(Impl),
|
2021-12-03 09:32:14 -06:00
|
|
|
BuiltinAttr(BuiltinAttr),
|
2021-12-03 10:15:19 -06:00
|
|
|
ToolModule(ToolModule),
|
2022-07-24 07:05:37 -05:00
|
|
|
DeriveHelper(DeriveHelper),
|
2020-04-27 17:40:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PathResolution {
|
2022-02-03 05:43:15 -06:00
|
|
|
pub(crate) fn in_type_ns(&self) -> Option<TypeNs> {
|
2020-04-27 17:40:32 -05:00
|
|
|
match self {
|
2020-04-29 17:05:03 -05:00
|
|
|
PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())),
|
2020-04-27 17:40:32 -05:00
|
|
|
PathResolution::Def(ModuleDef::BuiltinType(builtin)) => {
|
2021-02-11 12:52:33 -06:00
|
|
|
Some(TypeNs::BuiltinType((*builtin).into()))
|
2020-04-27 17:40:32 -05:00
|
|
|
}
|
2021-06-17 10:37:14 -05:00
|
|
|
PathResolution::Def(
|
|
|
|
ModuleDef::Const(_)
|
|
|
|
| ModuleDef::Variant(_)
|
2022-03-08 16:51:48 -06:00
|
|
|
| ModuleDef::Macro(_)
|
2021-06-17 10:37:14 -05:00
|
|
|
| ModuleDef::Function(_)
|
|
|
|
| ModuleDef::Module(_)
|
|
|
|
| ModuleDef::Static(_)
|
2023-03-03 09:24:07 -06:00
|
|
|
| ModuleDef::Trait(_)
|
|
|
|
| ModuleDef::TraitAlias(_),
|
2021-06-17 10:37:14 -05:00
|
|
|
) => None,
|
2020-04-27 17:40:32 -05:00
|
|
|
PathResolution::Def(ModuleDef::TypeAlias(alias)) => {
|
2020-04-29 17:05:03 -05:00
|
|
|
Some(TypeNs::TypeAliasId((*alias).into()))
|
2020-04-27 17:40:32 -05:00
|
|
|
}
|
2021-12-03 09:32:14 -06:00
|
|
|
PathResolution::BuiltinAttr(_)
|
2021-12-03 10:15:19 -06:00
|
|
|
| PathResolution::ToolModule(_)
|
2021-12-03 09:32:14 -06:00
|
|
|
| PathResolution::Local(_)
|
2022-07-24 07:05:37 -05:00
|
|
|
| PathResolution::DeriveHelper(_)
|
2021-12-03 09:32:14 -06:00
|
|
|
| PathResolution::ConstParam(_) => None,
|
2020-04-29 17:05:03 -05:00
|
|
|
PathResolution::TypeParam(param) => Some(TypeNs::GenericParam((*param).into())),
|
|
|
|
PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())),
|
2020-04-27 17:40:32 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-05 04:08:31 -06:00
|
|
|
}
|
|
|
|
|
2021-08-02 13:42:25 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TypeInfo {
|
|
|
|
/// The original type of the expression or pattern.
|
2021-08-03 10:28:51 -05:00
|
|
|
pub original: Type,
|
|
|
|
/// The adjusted type, if an adjustment happened.
|
|
|
|
pub adjusted: Option<Type>,
|
2021-08-02 13:42:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeInfo {
|
2021-08-03 10:28:51 -05:00
|
|
|
pub fn original(self) -> Type {
|
|
|
|
self.original
|
2021-08-02 13:42:25 -05:00
|
|
|
}
|
|
|
|
|
2021-08-03 10:28:51 -05:00
|
|
|
pub fn has_adjustment(&self) -> bool {
|
|
|
|
self.adjusted.is_some()
|
2021-08-02 13:42:25 -05:00
|
|
|
}
|
|
|
|
|
2021-08-03 10:28:51 -05:00
|
|
|
/// The adjusted type, or the original in case no adjustments occurred.
|
|
|
|
pub fn adjusted(self) -> Type {
|
|
|
|
self.adjusted.unwrap_or(self.original)
|
2021-08-02 13:42:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
/// Primary API to get semantic information, like types, from syntax trees.
|
|
|
|
pub struct Semantics<'db, DB> {
|
|
|
|
pub db: &'db DB,
|
2020-07-01 06:32:18 -05:00
|
|
|
imp: SemanticsImpl<'db>,
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SemanticsImpl<'db> {
|
|
|
|
pub db: &'db dyn HirDatabase,
|
2020-02-29 11:32:18 -06:00
|
|
|
s2d_cache: RefCell<SourceToDefCache>,
|
2020-07-24 07:12:13 -05:00
|
|
|
expansion_info_cache: RefCell<FxHashMap<HirFileId, Option<ExpansionInfo>>>,
|
2021-11-05 08:25:47 -05:00
|
|
|
// Rootnode to HirFileId cache
|
2020-02-18 11:35:10 -06:00
|
|
|
cache: RefCell<FxHashMap<SyntaxNode, HirFileId>>,
|
2021-11-05 08:25:47 -05:00
|
|
|
// MacroCall to its expansion's HirFileId cache
|
|
|
|
macro_call_cache: RefCell<FxHashMap<InFile<ast::MacroCall>, HirFileId>>,
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<DB> fmt::Debug for Semantics<'_, DB> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "Semantics {{ ... }}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'db, DB: HirDatabase> Semantics<'db, DB> {
|
2022-07-20 08:02:08 -05:00
|
|
|
pub fn new(db: &DB) -> Semantics<'_, DB> {
|
2020-07-01 04:43:36 -05:00
|
|
|
let impl_ = SemanticsImpl::new(db);
|
2020-07-01 06:32:18 -05:00
|
|
|
Semantics { db, imp: impl_ }
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse(&self, file_id: FileId) -> ast::SourceFile {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.parse(file_id)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2021-11-27 05:49:51 -06:00
|
|
|
pub fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode> {
|
|
|
|
self.imp.parse_or_expand(file_id)
|
|
|
|
}
|
|
|
|
|
2020-07-01 04:43:36 -05:00
|
|
|
pub fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.expand(macro_call)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
2021-06-07 09:05:36 -05:00
|
|
|
|
|
|
|
/// If `item` has an attribute macro attached to it, expands it.
|
|
|
|
pub fn expand_attr_macro(&self, item: &ast::Item) -> Option<SyntaxNode> {
|
|
|
|
self.imp.expand_attr_macro(item)
|
|
|
|
}
|
|
|
|
|
2022-03-10 13:53:50 -06:00
|
|
|
pub fn expand_derive_as_pseudo_attr_macro(&self, attr: &ast::Attr) -> Option<SyntaxNode> {
|
|
|
|
self.imp.expand_derive_as_pseudo_attr_macro(attr)
|
|
|
|
}
|
|
|
|
|
2022-03-08 16:51:48 -06:00
|
|
|
pub fn resolve_derive_macro(&self, derive: &ast::Attr) -> Option<Vec<Option<Macro>>> {
|
2021-12-28 07:47:21 -06:00
|
|
|
self.imp.resolve_derive_macro(derive)
|
|
|
|
}
|
|
|
|
|
2021-08-25 20:32:34 -05:00
|
|
|
pub fn expand_derive_macro(&self, derive: &ast::Attr) -> Option<Vec<SyntaxNode>> {
|
2021-08-24 09:33:52 -05:00
|
|
|
self.imp.expand_derive_macro(derive)
|
|
|
|
}
|
|
|
|
|
2021-06-07 12:32:28 -05:00
|
|
|
pub fn is_attr_macro_call(&self, item: &ast::Item) -> bool {
|
|
|
|
self.imp.is_attr_macro_call(item)
|
|
|
|
}
|
|
|
|
|
2022-02-21 06:21:25 -06:00
|
|
|
pub fn is_derive_annotated(&self, item: &ast::Adt) -> bool {
|
|
|
|
self.imp.is_derive_annotated(item)
|
|
|
|
}
|
|
|
|
|
2020-08-14 08:23:27 -05:00
|
|
|
pub fn speculative_expand(
|
2020-07-01 04:43:36 -05:00
|
|
|
&self,
|
|
|
|
actual_macro_call: &ast::MacroCall,
|
2021-05-24 14:21:25 -05:00
|
|
|
speculative_args: &ast::TokenTree,
|
2020-07-01 04:43:36 -05:00
|
|
|
token_to_map: SyntaxToken,
|
|
|
|
) -> Option<(SyntaxNode, SyntaxToken)> {
|
2021-05-24 14:21:25 -05:00
|
|
|
self.imp.speculative_expand(actual_macro_call, speculative_args, token_to_map)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2021-09-02 11:54:09 -05:00
|
|
|
pub fn speculative_expand_attr_macro(
|
|
|
|
&self,
|
|
|
|
actual_macro_call: &ast::Item,
|
|
|
|
speculative_args: &ast::Item,
|
|
|
|
token_to_map: SyntaxToken,
|
|
|
|
) -> Option<(SyntaxNode, SyntaxToken)> {
|
|
|
|
self.imp.speculative_expand_attr(actual_macro_call, speculative_args, token_to_map)
|
|
|
|
}
|
|
|
|
|
2022-03-10 13:53:50 -06:00
|
|
|
pub fn speculative_expand_derive_as_pseudo_attr_macro(
|
|
|
|
&self,
|
|
|
|
actual_macro_call: &ast::Attr,
|
|
|
|
speculative_args: &ast::Attr,
|
|
|
|
token_to_map: SyntaxToken,
|
|
|
|
) -> Option<(SyntaxNode, SyntaxToken)> {
|
|
|
|
self.imp.speculative_expand_derive_as_pseudo_attr_macro(
|
|
|
|
actual_macro_call,
|
|
|
|
speculative_args,
|
|
|
|
token_to_map,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-05 10:02:56 -05:00
|
|
|
/// Descend the token into macrocalls to its first mapped counterpart.
|
2021-10-12 10:52:31 -05:00
|
|
|
pub fn descend_into_macros_single(&self, token: SyntaxToken) -> SyntaxToken {
|
2021-11-05 10:02:56 -05:00
|
|
|
self.imp.descend_into_macros_single(token)
|
2021-08-28 14:18:56 -05:00
|
|
|
}
|
|
|
|
|
2021-11-05 10:02:56 -05:00
|
|
|
/// Descend the token into macrocalls to all its mapped counterparts.
|
2021-10-12 10:52:31 -05:00
|
|
|
pub fn descend_into_macros(&self, token: SyntaxToken) -> SmallVec<[SyntaxToken; 1]> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.descend_into_macros(token)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2022-05-23 09:09:56 -05:00
|
|
|
/// Descend the token into macrocalls to all its mapped counterparts that have the same text as the input token.
|
2022-04-15 12:42:48 -05:00
|
|
|
///
|
2022-05-23 09:09:56 -05:00
|
|
|
/// Returns the original non descended token if none of the mapped counterparts have the same text.
|
|
|
|
pub fn descend_into_macros_with_same_text(
|
2022-04-15 12:42:48 -05:00
|
|
|
&self,
|
|
|
|
token: SyntaxToken,
|
|
|
|
) -> SmallVec<[SyntaxToken; 1]> {
|
2022-05-23 09:09:56 -05:00
|
|
|
self.imp.descend_into_macros_with_same_text(token)
|
2022-04-15 12:42:48 -05:00
|
|
|
}
|
|
|
|
|
2022-07-22 10:29:03 -05:00
|
|
|
pub fn descend_into_macros_with_kind_preference(&self, token: SyntaxToken) -> SyntaxToken {
|
|
|
|
self.imp.descend_into_macros_with_kind_preference(token)
|
|
|
|
}
|
|
|
|
|
2021-09-18 06:19:29 -05:00
|
|
|
/// Maps a node down by mapping its first and last token down.
|
|
|
|
pub fn descend_node_into_attributes<N: AstNode>(&self, node: N) -> SmallVec<[N; 1]> {
|
|
|
|
self.imp.descend_node_into_attributes(node)
|
|
|
|
}
|
|
|
|
|
2021-10-07 07:46:49 -05:00
|
|
|
/// Search for a definition's source and cache its syntax tree
|
|
|
|
pub fn source<Def: HasSource>(&self, def: Def) -> Option<InFile<Def::Ast>>
|
|
|
|
where
|
|
|
|
Def::Ast: AstNode,
|
|
|
|
{
|
|
|
|
self.imp.source(def)
|
|
|
|
}
|
|
|
|
|
2021-08-20 06:49:28 -05:00
|
|
|
pub fn hir_file_for(&self, syntax_node: &SyntaxNode) -> HirFileId {
|
2021-12-21 06:38:58 -06:00
|
|
|
self.imp.find_file(syntax_node).file_id
|
2021-08-20 06:49:28 -05:00
|
|
|
}
|
|
|
|
|
2022-01-31 06:56:14 -06:00
|
|
|
/// Attempts to map the node out of macro expanded files returning the original file range.
|
|
|
|
/// If upmapping is not possible, this will fall back to the range of the macro call of the
|
|
|
|
/// macro file the node resides in.
|
2020-07-01 04:43:36 -05:00
|
|
|
pub fn original_range(&self, node: &SyntaxNode) -> FileRange {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.original_range(node)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2022-01-31 06:56:14 -06:00
|
|
|
/// Attempts to map the node out of macro expanded files returning the original file range.
|
2021-09-18 06:19:29 -05:00
|
|
|
pub fn original_range_opt(&self, node: &SyntaxNode) -> Option<FileRange> {
|
|
|
|
self.imp.original_range_opt(node)
|
|
|
|
}
|
|
|
|
|
2022-01-31 06:56:14 -06:00
|
|
|
/// Attempts to map the node out of macro expanded files.
|
|
|
|
/// This only work for attribute expansions, as other ones do not have nodes as input.
|
2021-11-03 15:12:36 -05:00
|
|
|
pub fn original_ast_node<N: AstNode>(&self, node: N) -> Option<N> {
|
|
|
|
self.imp.original_ast_node(node)
|
|
|
|
}
|
2022-10-01 08:34:45 -05:00
|
|
|
/// Attempts to map the node out of macro expanded files.
|
|
|
|
/// This only work for attribute expansions, as other ones do not have nodes as input.
|
|
|
|
pub fn original_syntax_node(&self, node: &SyntaxNode) -> Option<SyntaxNode> {
|
|
|
|
self.imp.original_syntax_node(node)
|
|
|
|
}
|
2021-11-03 15:12:36 -05:00
|
|
|
|
2021-02-28 05:12:11 -06:00
|
|
|
pub fn diagnostics_display_range(&self, diagnostics: InFile<SyntaxNodePtr>) -> FileRange {
|
2020-08-11 09:15:11 -05:00
|
|
|
self.imp.diagnostics_display_range(diagnostics)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2021-01-30 09:19:21 -06:00
|
|
|
pub fn token_ancestors_with_macros(
|
|
|
|
&self,
|
|
|
|
token: SyntaxToken,
|
|
|
|
) -> impl Iterator<Item = SyntaxNode> + '_ {
|
|
|
|
token.parent().into_iter().flat_map(move |it| self.ancestors_with_macros(it))
|
|
|
|
}
|
2021-09-13 18:59:45 -05:00
|
|
|
|
2021-11-16 14:32:02 -06:00
|
|
|
/// Iterates the ancestors of the given node, climbing up macro expansions while doing so.
|
2020-07-01 04:43:36 -05:00
|
|
|
pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.ancestors_with_macros(node)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ancestors_at_offset_with_macros(
|
|
|
|
&self,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
offset: TextSize,
|
|
|
|
) -> impl Iterator<Item = SyntaxNode> + '_ {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.ancestors_at_offset_with_macros(node, offset)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2021-08-22 07:31:37 -05:00
|
|
|
/// Find an AstNode by offset inside SyntaxNode, if it is inside *Macrofile*,
|
2020-07-01 04:43:36 -05:00
|
|
|
/// search up until it is of the target AstNode type
|
|
|
|
pub fn find_node_at_offset_with_macros<N: AstNode>(
|
|
|
|
&self,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
offset: TextSize,
|
|
|
|
) -> Option<N> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.ancestors_at_offset_with_macros(node, offset).find_map(N::cast)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2021-08-22 07:31:37 -05:00
|
|
|
/// Find an AstNode by offset inside SyntaxNode, if it is inside *MacroCall*,
|
2020-07-01 04:43:36 -05:00
|
|
|
/// descend it and find again
|
|
|
|
pub fn find_node_at_offset_with_descend<N: AstNode>(
|
|
|
|
&self,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
offset: TextSize,
|
|
|
|
) -> Option<N> {
|
2021-08-28 17:36:26 -05:00
|
|
|
self.imp.descend_node_at_offset(node, offset).flatten().find_map(N::cast)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2021-08-28 14:37:27 -05:00
|
|
|
/// Find an AstNode by offset inside SyntaxNode, if it is inside *MacroCall*,
|
|
|
|
/// descend it and find again
|
|
|
|
pub fn find_nodes_at_offset_with_descend<'slf, N: AstNode + 'slf>(
|
|
|
|
&'slf self,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
offset: TextSize,
|
|
|
|
) -> impl Iterator<Item = N> + 'slf {
|
2021-08-28 17:36:26 -05:00
|
|
|
self.imp.descend_node_at_offset(node, offset).filter_map(|mut it| it.find_map(N::cast))
|
2021-08-28 14:37:27 -05:00
|
|
|
}
|
|
|
|
|
2020-12-15 12:23:51 -06:00
|
|
|
pub fn resolve_lifetime_param(&self, lifetime: &ast::Lifetime) -> Option<LifetimeParam> {
|
|
|
|
self.imp.resolve_lifetime_param(lifetime)
|
2020-12-13 15:13:16 -06:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:34:30 -06:00
|
|
|
pub fn resolve_label(&self, lifetime: &ast::Lifetime) -> Option<Label> {
|
|
|
|
self.imp.resolve_label(lifetime)
|
|
|
|
}
|
|
|
|
|
2021-05-06 10:05:49 -05:00
|
|
|
pub fn resolve_type(&self, ty: &ast::Type) -> Option<Type> {
|
|
|
|
self.imp.resolve_type(ty)
|
|
|
|
}
|
|
|
|
|
2022-07-28 03:05:21 -05:00
|
|
|
pub fn resolve_trait(&self, trait_: &ast::Path) -> Option<Trait> {
|
|
|
|
self.imp.resolve_trait(trait_)
|
|
|
|
}
|
|
|
|
|
2022-12-20 13:33:27 -06:00
|
|
|
pub fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjustment>> {
|
2022-11-04 11:11:15 -05:00
|
|
|
self.imp.expr_adjustments(expr)
|
2022-03-20 08:38:16 -05:00
|
|
|
}
|
|
|
|
|
2021-08-02 13:42:25 -05:00
|
|
|
pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<TypeInfo> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.type_of_expr(expr)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2021-08-02 13:42:25 -05:00
|
|
|
pub fn type_of_pat(&self, pat: &ast::Pat) -> Option<TypeInfo> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.type_of_pat(pat)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2020-07-10 07:08:35 -05:00
|
|
|
pub fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type> {
|
|
|
|
self.imp.type_of_self(param)
|
|
|
|
}
|
|
|
|
|
2022-05-14 07:26:08 -05:00
|
|
|
pub fn pattern_adjustments(&self, pat: &ast::Pat) -> SmallVec<[Type; 1]> {
|
|
|
|
self.imp.pattern_adjustments(pat)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn binding_mode_of_pat(&self, pat: &ast::IdentPat) -> Option<BindingMode> {
|
|
|
|
self.imp.binding_mode_of_pat(pat)
|
|
|
|
}
|
|
|
|
|
2020-07-01 04:43:36 -05:00
|
|
|
pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
|
2020-07-16 06:00:56 -05:00
|
|
|
self.imp.resolve_method_call(call).map(Function::from)
|
|
|
|
}
|
|
|
|
|
2023-03-04 13:33:28 -06:00
|
|
|
/// Attempts to resolve this call expression as a method call falling back to resolving it as a field.
|
|
|
|
pub fn resolve_method_call_field_fallback(
|
|
|
|
&self,
|
|
|
|
call: &ast::MethodCallExpr,
|
|
|
|
) -> Option<Either<Function, Field>> {
|
|
|
|
self.imp
|
|
|
|
.resolve_method_call_fallback(call)
|
|
|
|
.map(|it| it.map_left(Function::from).map_right(Field::from))
|
|
|
|
}
|
|
|
|
|
2022-08-05 07:16:36 -05:00
|
|
|
pub fn resolve_await_to_poll(&self, await_expr: &ast::AwaitExpr) -> Option<Function> {
|
|
|
|
self.imp.resolve_await_to_poll(await_expr).map(Function::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_prefix_expr(&self, prefix_expr: &ast::PrefixExpr) -> Option<Function> {
|
|
|
|
self.imp.resolve_prefix_expr(prefix_expr).map(Function::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_index_expr(&self, index_expr: &ast::IndexExpr) -> Option<Function> {
|
|
|
|
self.imp.resolve_index_expr(index_expr).map(Function::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_bin_expr(&self, bin_expr: &ast::BinExpr) -> Option<Function> {
|
|
|
|
self.imp.resolve_bin_expr(bin_expr).map(Function::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_try_expr(&self, try_expr: &ast::TryExpr) -> Option<Function> {
|
|
|
|
self.imp.resolve_try_expr(try_expr).map(Function::from)
|
|
|
|
}
|
|
|
|
|
2020-07-16 06:00:56 -05:00
|
|
|
pub fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> {
|
|
|
|
self.imp.resolve_method_call_as_callable(call)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Field> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.resolve_field(field)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:21:30 -05:00
|
|
|
pub fn resolve_record_field(
|
|
|
|
&self,
|
|
|
|
field: &ast::RecordExprField,
|
2021-05-23 16:54:35 -05:00
|
|
|
) -> Option<(Field, Option<Local>, Type)> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.resolve_record_field(field)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2023-03-14 15:55:03 -05:00
|
|
|
pub fn resolve_record_pat_field(&self, field: &ast::RecordPatField) -> Option<(Field, Type)> {
|
2020-09-04 20:06:05 -05:00
|
|
|
self.imp.resolve_record_pat_field(field)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:51:48 -06:00
|
|
|
pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<Macro> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.resolve_macro_call(macro_call)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2022-03-20 13:07:44 -05:00
|
|
|
pub fn is_unsafe_macro_call(&self, macro_call: &ast::MacroCall) -> bool {
|
|
|
|
self.imp.is_unsafe_macro_call(macro_call)
|
|
|
|
}
|
|
|
|
|
2022-03-08 16:51:48 -06:00
|
|
|
pub fn resolve_attr_macro_call(&self, item: &ast::Item) -> Option<Macro> {
|
2021-08-21 16:24:12 -05:00
|
|
|
self.imp.resolve_attr_macro_call(item)
|
|
|
|
}
|
|
|
|
|
2020-07-01 04:43:36 -05:00
|
|
|
pub fn resolve_path(&self, path: &ast::Path) -> Option<PathResolution> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.resolve_path(path)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2020-08-08 13:14:18 -05:00
|
|
|
pub fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option<Crate> {
|
|
|
|
self.imp.resolve_extern_crate(extern_crate)
|
|
|
|
}
|
|
|
|
|
2020-07-30 09:21:30 -05:00
|
|
|
pub fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantDef> {
|
2020-07-10 07:11:31 -05:00
|
|
|
self.imp.resolve_variant(record_lit).map(VariantDef::from)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2020-07-31 13:09:09 -05:00
|
|
|
pub fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.resolve_bind_pat_to_const(pat)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:21:30 -05:00
|
|
|
pub fn record_literal_missing_fields(&self, literal: &ast::RecordExpr) -> Vec<(Field, Type)> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.record_literal_missing_fields(literal)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.record_pattern_missing_fields(pattern)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_def<T: ToDef>(&self, src: &T) -> Option<T::Def> {
|
2022-12-21 14:34:49 -06:00
|
|
|
self.imp.to_def(src)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_module_def(&self, file: FileId) -> Option<Module> {
|
2021-03-15 08:51:20 -05:00
|
|
|
self.imp.to_module_def(file).next()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.to_module_def(file)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
pub fn scope(&self, node: &SyntaxNode) -> Option<SemanticsScope<'db>> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.scope(node)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
pub fn scope_at_offset(
|
|
|
|
&self,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
offset: TextSize,
|
|
|
|
) -> Option<SemanticsScope<'db>> {
|
2022-03-12 06:04:13 -06:00
|
|
|
self.imp.scope_at_offset(node, offset)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.scope_for_def(def)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn assert_contains_node(&self, node: &SyntaxNode) {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.assert_contains_node(node)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
2020-07-19 10:45:46 -05:00
|
|
|
|
2020-07-30 10:07:13 -05:00
|
|
|
pub fn is_unsafe_method_call(&self, method_call_expr: &ast::MethodCallExpr) -> bool {
|
2020-07-30 08:26:40 -05:00
|
|
|
self.imp.is_unsafe_method_call(method_call_expr)
|
2020-07-23 09:11:37 -05:00
|
|
|
}
|
2020-07-19 10:45:46 -05:00
|
|
|
|
2020-07-23 09:11:37 -05:00
|
|
|
pub fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool {
|
2020-07-30 08:26:40 -05:00
|
|
|
self.imp.is_unsafe_ref_expr(ref_expr)
|
2020-07-23 09:11:37 -05:00
|
|
|
}
|
|
|
|
|
2020-08-07 09:40:09 -05:00
|
|
|
pub fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool {
|
|
|
|
self.imp.is_unsafe_ident_pat(ident_pat)
|
2020-07-19 10:45:46 -05:00
|
|
|
}
|
2022-12-21 14:34:49 -06:00
|
|
|
|
|
|
|
/// Returns `true` if the `node` is inside an `unsafe` context.
|
2022-12-21 16:52:52 -06:00
|
|
|
pub fn is_inside_unsafe(&self, expr: &ast::Expr) -> bool {
|
|
|
|
self.imp.is_inside_unsafe(expr)
|
2022-12-21 14:34:49 -06:00
|
|
|
}
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'db> SemanticsImpl<'db> {
|
2020-07-11 05:31:50 -05:00
|
|
|
fn new(db: &'db dyn HirDatabase) -> Self {
|
2020-07-24 07:12:13 -05:00
|
|
|
SemanticsImpl {
|
|
|
|
db,
|
|
|
|
s2d_cache: Default::default(),
|
|
|
|
cache: Default::default(),
|
|
|
|
expansion_info_cache: Default::default(),
|
2021-11-05 08:25:47 -05:00
|
|
|
macro_call_cache: Default::default(),
|
2020-07-24 07:12:13 -05:00
|
|
|
}
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn parse(&self, file_id: FileId) -> ast::SourceFile {
|
2020-02-18 11:35:10 -06:00
|
|
|
let tree = self.db.parse(file_id).tree();
|
|
|
|
self.cache(tree.syntax().clone(), file_id.into());
|
|
|
|
tree
|
|
|
|
}
|
|
|
|
|
2021-11-27 05:49:51 -06:00
|
|
|
fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode> {
|
|
|
|
let node = self.db.parse_or_expand(file_id)?;
|
|
|
|
self.cache(node.clone(), file_id);
|
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
|
2022-03-31 04:12:08 -05:00
|
|
|
let sa = self.analyze_no_infer(macro_call.syntax())?;
|
2020-12-11 07:50:47 -06:00
|
|
|
let file_id = sa.expand(self.db, InFile::new(sa.file_id, macro_call))?;
|
2022-01-14 03:59:27 -06:00
|
|
|
let node = self.parse_or_expand(file_id)?;
|
2020-02-18 11:35:10 -06:00
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
|
2021-06-07 09:05:36 -05:00
|
|
|
fn expand_attr_macro(&self, item: &ast::Item) -> Option<SyntaxNode> {
|
2022-01-31 06:56:14 -06:00
|
|
|
let src = self.wrap_node_infile(item.clone());
|
2021-06-07 09:05:36 -05:00
|
|
|
let macro_call_id = self.with_ctx(|ctx| ctx.item_to_macro_call(src))?;
|
2022-03-10 13:53:50 -06:00
|
|
|
self.parse_or_expand(macro_call_id.as_file())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_derive_as_pseudo_attr_macro(&self, attr: &ast::Attr) -> Option<SyntaxNode> {
|
|
|
|
let adt = attr.syntax().parent().and_then(ast::Adt::cast)?;
|
2023-03-08 06:51:14 -06:00
|
|
|
let src = self.wrap_node_infile(attr.clone());
|
2022-03-10 13:53:50 -06:00
|
|
|
let call_id = self.with_ctx(|ctx| {
|
|
|
|
ctx.attr_to_derive_macro_call(src.with_value(&adt), src).map(|(_, it, _)| it)
|
|
|
|
})?;
|
|
|
|
self.parse_or_expand(call_id.as_file())
|
2021-06-07 09:05:36 -05:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:51:48 -06:00
|
|
|
fn resolve_derive_macro(&self, attr: &ast::Attr) -> Option<Vec<Option<Macro>>> {
|
|
|
|
let calls = self.derive_macro_calls(attr)?;
|
|
|
|
self.with_ctx(|ctx| {
|
|
|
|
Some(
|
|
|
|
calls
|
|
|
|
.into_iter()
|
|
|
|
.map(|call| {
|
|
|
|
macro_call_to_macro_id(ctx, self.db.upcast(), call?).map(|id| Macro { id })
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
})
|
2021-12-28 07:47:21 -06:00
|
|
|
}
|
|
|
|
|
2021-08-25 20:32:34 -05:00
|
|
|
fn expand_derive_macro(&self, attr: &ast::Attr) -> Option<Vec<SyntaxNode>> {
|
2021-12-28 07:51:17 -06:00
|
|
|
let res: Vec<_> = self
|
|
|
|
.derive_macro_calls(attr)?
|
2022-01-01 13:31:04 -06:00
|
|
|
.into_iter()
|
|
|
|
.flat_map(|call| {
|
|
|
|
let file_id = call?.as_file();
|
2021-12-28 07:50:13 -06:00
|
|
|
let node = self.db.parse_or_expand(file_id)?;
|
|
|
|
self.cache(node.clone(), file_id);
|
|
|
|
Some(node)
|
|
|
|
})
|
|
|
|
.collect();
|
2021-12-28 07:51:17 -06:00
|
|
|
Some(res)
|
2021-12-28 07:50:13 -06:00
|
|
|
}
|
|
|
|
|
2022-01-01 13:31:04 -06:00
|
|
|
fn derive_macro_calls(&self, attr: &ast::Attr) -> Option<Vec<Option<MacroCallId>>> {
|
2022-01-02 09:58:21 -06:00
|
|
|
let adt = attr.syntax().parent().and_then(ast::Adt::cast)?;
|
|
|
|
let file_id = self.find_file(adt.syntax()).file_id;
|
|
|
|
let adt = InFile::new(file_id, &adt);
|
2021-12-21 06:38:58 -06:00
|
|
|
let src = InFile::new(file_id, attr.clone());
|
2021-08-25 20:32:34 -05:00
|
|
|
self.with_ctx(|ctx| {
|
2022-02-20 19:42:58 -06:00
|
|
|
let (.., res) = ctx.attr_to_derive_macro_call(adt, src)?;
|
2021-12-28 07:50:13 -06:00
|
|
|
Some(res.to_vec())
|
2021-08-25 20:32:34 -05:00
|
|
|
})
|
2021-08-24 09:33:52 -05:00
|
|
|
}
|
|
|
|
|
2022-02-21 06:21:25 -06:00
|
|
|
fn is_derive_annotated(&self, adt: &ast::Adt) -> bool {
|
|
|
|
let file_id = self.find_file(adt.syntax()).file_id;
|
|
|
|
let adt = InFile::new(file_id, adt);
|
|
|
|
self.with_ctx(|ctx| ctx.has_derives(adt))
|
|
|
|
}
|
|
|
|
|
2021-06-07 12:32:28 -05:00
|
|
|
fn is_attr_macro_call(&self, item: &ast::Item) -> bool {
|
2021-12-21 06:38:58 -06:00
|
|
|
let file_id = self.find_file(item.syntax()).file_id;
|
|
|
|
let src = InFile::new(file_id, item.clone());
|
2021-06-07 12:32:28 -05:00
|
|
|
self.with_ctx(|ctx| ctx.item_to_macro_call(src).is_some())
|
|
|
|
}
|
|
|
|
|
2020-08-14 08:23:27 -05:00
|
|
|
fn speculative_expand(
|
2020-03-07 08:27:03 -06:00
|
|
|
&self,
|
|
|
|
actual_macro_call: &ast::MacroCall,
|
2021-05-24 14:21:25 -05:00
|
|
|
speculative_args: &ast::TokenTree,
|
2020-03-07 08:27:03 -06:00
|
|
|
token_to_map: SyntaxToken,
|
|
|
|
) -> Option<(SyntaxNode, SyntaxToken)> {
|
2021-12-21 06:38:58 -06:00
|
|
|
let SourceAnalyzer { file_id, resolver, .. } =
|
2022-03-31 04:12:08 -05:00
|
|
|
self.analyze_no_infer(actual_macro_call.syntax())?;
|
2021-12-21 06:38:58 -06:00
|
|
|
let macro_call = InFile::new(file_id, actual_macro_call);
|
2022-03-31 04:12:08 -05:00
|
|
|
let krate = resolver.krate();
|
2020-07-01 04:43:36 -05:00
|
|
|
let macro_call_id = macro_call.as_call_id(self.db.upcast(), krate, |path| {
|
2022-03-08 16:51:48 -06:00
|
|
|
resolver
|
|
|
|
.resolve_path_as_macro(self.db.upcast(), &path)
|
|
|
|
.map(|it| macro_id_to_def_id(self.db.upcast(), it))
|
2020-07-01 04:43:36 -05:00
|
|
|
})?;
|
2021-05-24 14:21:25 -05:00
|
|
|
hir_expand::db::expand_speculative(
|
2020-07-01 04:43:36 -05:00
|
|
|
self.db.upcast(),
|
|
|
|
macro_call_id,
|
2021-09-02 11:54:09 -05:00
|
|
|
speculative_args.syntax(),
|
|
|
|
token_to_map,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn speculative_expand_attr(
|
|
|
|
&self,
|
|
|
|
actual_macro_call: &ast::Item,
|
|
|
|
speculative_args: &ast::Item,
|
|
|
|
token_to_map: SyntaxToken,
|
|
|
|
) -> Option<(SyntaxNode, SyntaxToken)> {
|
2022-01-31 06:56:14 -06:00
|
|
|
let macro_call = self.wrap_node_infile(actual_macro_call.clone());
|
2021-09-02 11:54:09 -05:00
|
|
|
let macro_call_id = self.with_ctx(|ctx| ctx.item_to_macro_call(macro_call))?;
|
|
|
|
hir_expand::db::expand_speculative(
|
|
|
|
self.db.upcast(),
|
|
|
|
macro_call_id,
|
|
|
|
speculative_args.syntax(),
|
2020-07-01 04:43:36 -05:00
|
|
|
token_to_map,
|
|
|
|
)
|
2020-03-07 08:27:03 -06:00
|
|
|
}
|
|
|
|
|
2022-03-10 13:53:50 -06:00
|
|
|
fn speculative_expand_derive_as_pseudo_attr_macro(
|
|
|
|
&self,
|
|
|
|
actual_macro_call: &ast::Attr,
|
|
|
|
speculative_args: &ast::Attr,
|
|
|
|
token_to_map: SyntaxToken,
|
|
|
|
) -> Option<(SyntaxNode, SyntaxToken)> {
|
|
|
|
let attr = self.wrap_node_infile(actual_macro_call.clone());
|
|
|
|
let adt = actual_macro_call.syntax().parent().and_then(ast::Adt::cast)?;
|
|
|
|
let macro_call_id = self.with_ctx(|ctx| {
|
|
|
|
ctx.attr_to_derive_macro_call(attr.with_value(&adt), attr).map(|(_, it, _)| it)
|
|
|
|
})?;
|
|
|
|
hir_expand::db::expand_speculative(
|
|
|
|
self.db.upcast(),
|
|
|
|
macro_call_id,
|
|
|
|
speculative_args.syntax(),
|
|
|
|
token_to_map,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-21 08:52:11 -05:00
|
|
|
// This might not be the correct way to do this, but it works for now
|
2021-09-18 06:19:29 -05:00
|
|
|
fn descend_node_into_attributes<N: AstNode>(&self, node: N) -> SmallVec<[N; 1]> {
|
|
|
|
let mut res = smallvec![];
|
|
|
|
let tokens = (|| {
|
|
|
|
let first = skip_trivia_token(node.syntax().first_token()?, Direction::Next)?;
|
|
|
|
let last = skip_trivia_token(node.syntax().last_token()?, Direction::Prev)?;
|
|
|
|
Some((first, last))
|
|
|
|
})();
|
|
|
|
let (first, last) = match tokens {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return res,
|
|
|
|
};
|
|
|
|
|
|
|
|
if first == last {
|
2022-04-15 12:42:48 -05:00
|
|
|
self.descend_into_macros_impl(first, &mut |InFile { value, .. }| {
|
2022-06-10 09:30:09 -05:00
|
|
|
if let Some(node) = value.parent_ancestors().find_map(N::cast) {
|
2022-04-15 12:42:48 -05:00
|
|
|
res.push(node)
|
|
|
|
}
|
|
|
|
false
|
|
|
|
});
|
2021-09-18 06:19:29 -05:00
|
|
|
} else {
|
|
|
|
// Descend first and last token, then zip them to look for the node they belong to
|
|
|
|
let mut scratch: SmallVec<[_; 1]> = smallvec![];
|
2022-04-15 12:42:48 -05:00
|
|
|
self.descend_into_macros_impl(first, &mut |token| {
|
|
|
|
scratch.push(token);
|
|
|
|
false
|
|
|
|
});
|
2021-09-18 06:19:29 -05:00
|
|
|
|
|
|
|
let mut scratch = scratch.into_iter();
|
2021-11-05 10:02:56 -05:00
|
|
|
self.descend_into_macros_impl(
|
|
|
|
last,
|
2021-12-20 06:19:48 -06:00
|
|
|
&mut |InFile { value: last, file_id: last_fid }| {
|
2021-11-05 10:02:56 -05:00
|
|
|
if let Some(InFile { value: first, file_id: first_fid }) = scratch.next() {
|
|
|
|
if first_fid == last_fid {
|
|
|
|
if let Some(p) = first.parent() {
|
|
|
|
let range = first.text_range().cover(last.text_range());
|
|
|
|
let node = find_root(&p)
|
|
|
|
.covering_element(range)
|
|
|
|
.ancestors()
|
|
|
|
.take_while(|it| it.text_range() == range)
|
|
|
|
.find_map(N::cast);
|
|
|
|
if let Some(node) = node {
|
|
|
|
res.push(node);
|
|
|
|
}
|
2021-09-18 06:19:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 12:42:48 -05:00
|
|
|
false
|
2021-11-05 10:02:56 -05:00
|
|
|
},
|
|
|
|
);
|
2021-09-18 06:19:29 -05:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2021-08-28 14:18:56 -05:00
|
|
|
fn descend_into_macros(&self, token: SyntaxToken) -> SmallVec<[SyntaxToken; 1]> {
|
2021-09-18 06:19:29 -05:00
|
|
|
let mut res = smallvec![];
|
2022-04-15 12:42:48 -05:00
|
|
|
self.descend_into_macros_impl(token, &mut |InFile { value, .. }| {
|
|
|
|
res.push(value);
|
|
|
|
false
|
|
|
|
});
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2022-05-23 09:09:56 -05:00
|
|
|
fn descend_into_macros_with_same_text(&self, token: SyntaxToken) -> SmallVec<[SyntaxToken; 1]> {
|
|
|
|
let text = token.text();
|
2022-04-15 12:42:48 -05:00
|
|
|
let mut res = smallvec![];
|
|
|
|
self.descend_into_macros_impl(token.clone(), &mut |InFile { value, .. }| {
|
2022-05-23 09:09:56 -05:00
|
|
|
if value.text() == text {
|
2022-04-15 12:42:48 -05:00
|
|
|
res.push(value);
|
|
|
|
}
|
|
|
|
false
|
|
|
|
});
|
|
|
|
if res.is_empty() {
|
|
|
|
res.push(token);
|
|
|
|
}
|
2021-09-18 06:19:29 -05:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2022-07-22 10:29:03 -05:00
|
|
|
fn descend_into_macros_with_kind_preference(&self, token: SyntaxToken) -> SyntaxToken {
|
|
|
|
let fetch_kind = |token: &SyntaxToken| match token.parent() {
|
|
|
|
Some(node) => match node.kind() {
|
|
|
|
kind @ (SyntaxKind::NAME | SyntaxKind::NAME_REF) => {
|
|
|
|
node.parent().map_or(kind, |it| it.kind())
|
|
|
|
}
|
|
|
|
_ => token.kind(),
|
|
|
|
},
|
|
|
|
None => token.kind(),
|
|
|
|
};
|
|
|
|
let preferred_kind = fetch_kind(&token);
|
|
|
|
let mut res = None;
|
|
|
|
self.descend_into_macros_impl(token.clone(), &mut |InFile { value, .. }| {
|
|
|
|
if fetch_kind(&value) == preferred_kind {
|
|
|
|
res = Some(value);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
if let None = res {
|
|
|
|
res = Some(value)
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
res.unwrap_or(token)
|
|
|
|
}
|
|
|
|
|
2021-11-05 10:02:56 -05:00
|
|
|
fn descend_into_macros_single(&self, token: SyntaxToken) -> SyntaxToken {
|
|
|
|
let mut res = token.clone();
|
2022-04-15 12:42:48 -05:00
|
|
|
self.descend_into_macros_impl(token, &mut |InFile { value, .. }| {
|
|
|
|
res = value;
|
|
|
|
true
|
|
|
|
});
|
2021-11-05 10:02:56 -05:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn descend_into_macros_impl(
|
|
|
|
&self,
|
|
|
|
token: SyntaxToken,
|
2022-04-15 12:42:48 -05:00
|
|
|
f: &mut dyn FnMut(InFile<SyntaxToken>) -> bool,
|
2021-11-05 10:02:56 -05:00
|
|
|
) {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("descend_into_macros");
|
2021-01-30 09:19:21 -06:00
|
|
|
let parent = match token.parent() {
|
|
|
|
Some(it) => it,
|
2021-09-18 06:19:29 -05:00
|
|
|
None => return,
|
2021-01-30 09:19:21 -06:00
|
|
|
};
|
2022-03-31 04:12:08 -05:00
|
|
|
let sa = match self.analyze_no_infer(&parent) {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
2022-07-24 05:04:15 -05:00
|
|
|
let def_map = sa.resolver.def_map();
|
|
|
|
|
2021-11-05 09:52:10 -05:00
|
|
|
let mut stack: SmallVec<[_; 4]> = smallvec![InFile::new(sa.file_id, token)];
|
2021-08-28 18:38:10 -05:00
|
|
|
let mut cache = self.expansion_info_cache.borrow_mut();
|
2021-11-05 08:25:47 -05:00
|
|
|
let mut mcache = self.macro_call_cache.borrow_mut();
|
2021-09-27 11:44:40 -05:00
|
|
|
|
|
|
|
let mut process_expansion_for_token =
|
2021-11-05 09:52:10 -05:00
|
|
|
|stack: &mut SmallVec<_>, macro_file, item, token: InFile<&_>| {
|
|
|
|
let expansion_info = cache
|
|
|
|
.entry(macro_file)
|
|
|
|
.or_insert_with(|| macro_file.expansion_info(self.db.upcast()))
|
|
|
|
.as_ref()?;
|
|
|
|
|
|
|
|
{
|
|
|
|
let InFile { file_id, value } = expansion_info.expanded();
|
|
|
|
self.cache(value, file_id);
|
|
|
|
}
|
|
|
|
|
2022-04-15 12:42:48 -05:00
|
|
|
let mapped_tokens = expansion_info.map_token_down(self.db.upcast(), item, token)?;
|
2021-09-27 12:18:26 -05:00
|
|
|
let len = stack.len();
|
2022-04-15 12:42:48 -05:00
|
|
|
|
2021-09-27 11:44:40 -05:00
|
|
|
// requeue the tokens we got from mapping our current token down
|
2022-04-15 12:42:48 -05:00
|
|
|
stack.extend(mapped_tokens);
|
2021-09-27 11:44:40 -05:00
|
|
|
// if the length changed we have found a mapping for the token
|
2022-12-30 02:30:23 -06:00
|
|
|
(stack.len() != len).then_some(())
|
2021-09-27 11:44:40 -05:00
|
|
|
};
|
|
|
|
|
2021-09-02 12:10:40 -05:00
|
|
|
// Remap the next token in the queue into a macro call its in, if it is not being remapped
|
|
|
|
// either due to not being in a macro-call or because its unused push it into the result vec,
|
|
|
|
// otherwise push the remapped tokens back into the queue as they can potentially be remapped again.
|
2021-09-27 12:18:26 -05:00
|
|
|
while let Some(token) = stack.pop() {
|
2021-05-17 12:07:10 -05:00
|
|
|
self.db.unwind_if_cancelled();
|
2021-08-28 18:38:10 -05:00
|
|
|
let was_not_remapped = (|| {
|
2022-07-24 05:04:15 -05:00
|
|
|
// First expand into attribute invocations
|
2021-09-27 11:44:40 -05:00
|
|
|
let containing_attribute_macro_call = self.with_ctx(|ctx| {
|
2022-06-10 09:30:09 -05:00
|
|
|
token.value.parent_ancestors().filter_map(ast::Item::cast).find_map(|item| {
|
2021-11-05 08:50:10 -05:00
|
|
|
if item.attrs().next().is_none() {
|
|
|
|
// Don't force populate the dyn cache for items that don't have an attribute anyways
|
|
|
|
return None;
|
|
|
|
}
|
2021-11-05 08:25:47 -05:00
|
|
|
Some((ctx.item_to_macro_call(token.with_value(item.clone()))?, item))
|
|
|
|
})
|
2021-09-27 11:44:40 -05:00
|
|
|
});
|
|
|
|
if let Some((call_id, item)) = containing_attribute_macro_call {
|
2021-09-13 18:20:43 -05:00
|
|
|
let file_id = call_id.as_file();
|
2021-09-27 11:44:40 -05:00
|
|
|
return process_expansion_for_token(
|
2021-09-27 12:18:26 -05:00
|
|
|
&mut stack,
|
2021-09-27 11:44:40 -05:00
|
|
|
file_id,
|
|
|
|
Some(item),
|
|
|
|
token.as_ref(),
|
|
|
|
);
|
2021-09-13 18:20:43 -05:00
|
|
|
}
|
|
|
|
|
2022-07-24 05:04:15 -05:00
|
|
|
// Then check for token trees, that means we are either in a function-like macro or
|
|
|
|
// secondary attribute inputs
|
|
|
|
let tt = token.value.parent_ancestors().map_while(ast::TokenTree::cast).last()?;
|
|
|
|
let parent = tt.syntax().parent()?;
|
2022-02-20 19:42:58 -06:00
|
|
|
|
2022-07-24 05:04:15 -05:00
|
|
|
if tt.left_delimiter_token().map_or(false, |it| it == token.value) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if tt.right_delimiter_token().map_or(false, |it| it == token.value) {
|
|
|
|
return None;
|
|
|
|
}
|
2021-09-27 11:44:40 -05:00
|
|
|
|
2022-07-24 05:04:15 -05:00
|
|
|
if let Some(macro_call) = ast::MacroCall::cast(parent.clone()) {
|
2021-11-05 08:25:47 -05:00
|
|
|
let mcall = token.with_value(macro_call);
|
|
|
|
let file_id = match mcache.get(&mcall) {
|
|
|
|
Some(&it) => it,
|
|
|
|
None => {
|
|
|
|
let it = sa.expand(self.db, mcall.as_ref())?;
|
|
|
|
mcache.insert(mcall, it);
|
|
|
|
it
|
|
|
|
}
|
|
|
|
};
|
2022-07-24 05:04:15 -05:00
|
|
|
process_expansion_for_token(&mut stack, file_id, None, token.as_ref())
|
2022-12-23 01:08:08 -06:00
|
|
|
} else if let Some(meta) = ast::Meta::cast(parent) {
|
2022-07-24 05:04:15 -05:00
|
|
|
// attribute we failed expansion for earlier, this might be a derive invocation
|
|
|
|
// or derive helper attribute
|
|
|
|
let attr = meta.parent_attr()?;
|
|
|
|
|
|
|
|
let adt = if let Some(adt) = attr.syntax().parent().and_then(ast::Adt::cast) {
|
|
|
|
// this might be a derive, or a derive helper on an ADT
|
|
|
|
let derive_call = self.with_ctx(|ctx| {
|
|
|
|
// so try downmapping the token into the pseudo derive expansion
|
|
|
|
// see [hir_expand::builtin_attr_macro] for how the pseudo derive expansion works
|
|
|
|
ctx.attr_to_derive_macro_call(
|
|
|
|
token.with_value(&adt),
|
|
|
|
token.with_value(attr.clone()),
|
|
|
|
)
|
|
|
|
.map(|(_, call_id, _)| call_id)
|
|
|
|
});
|
|
|
|
|
|
|
|
match derive_call {
|
|
|
|
Some(call_id) => {
|
|
|
|
// resolved to a derive
|
|
|
|
let file_id = call_id.as_file();
|
|
|
|
return process_expansion_for_token(
|
|
|
|
&mut stack,
|
|
|
|
file_id,
|
|
|
|
Some(adt.into()),
|
|
|
|
token.as_ref(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
None => Some(adt),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise this could be a derive helper on a variant or field
|
|
|
|
if let Some(field) = attr.syntax().parent().and_then(ast::RecordField::cast)
|
|
|
|
{
|
|
|
|
field.syntax().ancestors().take(4).find_map(ast::Adt::cast)
|
|
|
|
} else if let Some(field) =
|
|
|
|
attr.syntax().parent().and_then(ast::TupleField::cast)
|
|
|
|
{
|
|
|
|
field.syntax().ancestors().take(4).find_map(ast::Adt::cast)
|
|
|
|
} else if let Some(variant) =
|
|
|
|
attr.syntax().parent().and_then(ast::Variant::cast)
|
|
|
|
{
|
|
|
|
variant.syntax().ancestors().nth(2).and_then(ast::Adt::cast)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}?;
|
2022-07-24 07:05:37 -05:00
|
|
|
if !self.with_ctx(|ctx| ctx.has_derives(InFile::new(token.file_id, &adt))) {
|
|
|
|
return None;
|
|
|
|
}
|
2022-07-24 05:04:15 -05:00
|
|
|
// Not an attribute, nor a derive, so it's either a builtin or a derive helper
|
|
|
|
// Try to resolve to a derive helper and downmap
|
|
|
|
let attr_name = attr.path().and_then(|it| it.as_single_name_ref())?.as_name();
|
|
|
|
let id = self.db.ast_id_map(token.file_id).ast_id(&adt);
|
|
|
|
let helpers =
|
|
|
|
def_map.derive_helpers_in_scope(InFile::new(token.file_id, id))?;
|
|
|
|
let item = Some(adt.into());
|
|
|
|
let mut res = None;
|
2022-07-24 07:05:37 -05:00
|
|
|
for (.., derive) in helpers.iter().filter(|(helper, ..)| *helper == attr_name) {
|
2022-07-24 05:04:15 -05:00
|
|
|
res = res.or(process_expansion_for_token(
|
|
|
|
&mut stack,
|
|
|
|
derive.as_file(),
|
|
|
|
item.clone(),
|
|
|
|
token.as_ref(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
res
|
|
|
|
} else {
|
|
|
|
None
|
2021-06-06 08:51:05 -05:00
|
|
|
}
|
2021-09-02 12:10:40 -05:00
|
|
|
})()
|
|
|
|
.is_none();
|
2021-09-13 18:20:43 -05:00
|
|
|
|
2022-04-15 12:42:48 -05:00
|
|
|
if was_not_remapped && f(token) {
|
|
|
|
break;
|
2021-01-30 09:19:21 -06:00
|
|
|
}
|
2021-08-28 14:18:56 -05:00
|
|
|
}
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2021-08-28 17:45:55 -05:00
|
|
|
// Note this return type is deliberate as [`find_nodes_at_offset_with_descend`] wants to stop
|
|
|
|
// traversing the inner iterator when it finds a node.
|
2021-09-14 07:09:52 -05:00
|
|
|
// The outer iterator is over the tokens descendants
|
|
|
|
// The inner iterator is the ancestors of a descendant
|
2020-07-11 05:31:50 -05:00
|
|
|
fn descend_node_at_offset(
|
2020-03-22 06:52:14 -05:00
|
|
|
&self,
|
|
|
|
node: &SyntaxNode,
|
2020-04-24 16:40:41 -05:00
|
|
|
offset: TextSize,
|
2021-08-28 17:36:26 -05:00
|
|
|
) -> impl Iterator<Item = impl Iterator<Item = SyntaxNode> + '_> + '_ {
|
2020-03-22 06:52:14 -05:00
|
|
|
node.token_at_offset(offset)
|
2021-08-28 17:36:26 -05:00
|
|
|
.map(move |token| self.descend_into_macros(token))
|
2021-09-14 07:09:52 -05:00
|
|
|
.map(|descendants| {
|
|
|
|
descendants.into_iter().map(move |it| self.token_ancestors_with_macros(it))
|
|
|
|
})
|
|
|
|
// re-order the tokens from token_at_offset by returning the ancestors with the smaller first nodes first
|
|
|
|
// See algo::ancestors_at_offset, which uses the same approach
|
|
|
|
.kmerge_by(|left, right| {
|
|
|
|
left.clone()
|
|
|
|
.map(|node| node.text_range().len())
|
|
|
|
.lt(right.clone().map(|node| node.text_range().len()))
|
|
|
|
})
|
2020-03-22 06:52:14 -05:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn original_range(&self, node: &SyntaxNode) -> FileRange {
|
2021-12-21 06:38:58 -06:00
|
|
|
let node = self.find_file(node);
|
|
|
|
node.original_file_range(self.db.upcast())
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2021-09-18 06:19:29 -05:00
|
|
|
fn original_range_opt(&self, node: &SyntaxNode) -> Option<FileRange> {
|
2021-12-21 06:38:58 -06:00
|
|
|
let node = self.find_file(node);
|
|
|
|
node.original_file_range_opt(self.db.upcast())
|
2021-09-18 06:19:29 -05:00
|
|
|
}
|
|
|
|
|
2021-11-03 15:12:36 -05:00
|
|
|
fn original_ast_node<N: AstNode>(&self, node: N) -> Option<N> {
|
2022-07-27 05:45:29 -05:00
|
|
|
self.wrap_node_infile(node).original_ast_node(self.db.upcast()).map(
|
|
|
|
|InFile { file_id, value }| {
|
|
|
|
self.cache(find_root(value.syntax()), file_id);
|
|
|
|
value
|
|
|
|
},
|
|
|
|
)
|
2021-11-03 15:12:36 -05:00
|
|
|
}
|
|
|
|
|
2022-10-01 08:34:45 -05:00
|
|
|
fn original_syntax_node(&self, node: &SyntaxNode) -> Option<SyntaxNode> {
|
|
|
|
let InFile { file_id, .. } = self.find_file(node);
|
|
|
|
InFile::new(file_id, node).original_syntax_node(self.db.upcast()).map(
|
|
|
|
|InFile { file_id, value }| {
|
|
|
|
self.cache(find_root(&value), file_id);
|
|
|
|
value
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-28 05:12:11 -06:00
|
|
|
fn diagnostics_display_range(&self, src: InFile<SyntaxNodePtr>) -> FileRange {
|
2022-01-14 03:59:27 -06:00
|
|
|
let root = self.parse_or_expand(src.file_id).unwrap();
|
2022-01-14 04:07:53 -06:00
|
|
|
let node = src.map(|it| it.to_node(&root));
|
|
|
|
node.as_ref().original_file_range(self.db.upcast())
|
2020-04-17 06:06:02 -05:00
|
|
|
}
|
|
|
|
|
2021-01-30 09:19:21 -06:00
|
|
|
fn token_ancestors_with_macros(
|
|
|
|
&self,
|
|
|
|
token: SyntaxToken,
|
2021-09-14 07:09:52 -05:00
|
|
|
) -> impl Iterator<Item = SyntaxNode> + Clone + '_ {
|
2021-01-30 09:19:21 -06:00
|
|
|
token.parent().into_iter().flat_map(move |parent| self.ancestors_with_macros(parent))
|
|
|
|
}
|
|
|
|
|
2021-09-14 07:09:52 -05:00
|
|
|
fn ancestors_with_macros(
|
|
|
|
&self,
|
|
|
|
node: SyntaxNode,
|
|
|
|
) -> impl Iterator<Item = SyntaxNode> + Clone + '_ {
|
2021-12-21 06:38:58 -06:00
|
|
|
let node = self.find_file(&node);
|
2022-01-14 04:07:53 -06:00
|
|
|
let db = self.db.upcast();
|
|
|
|
iter::successors(Some(node.cloned()), move |&InFile { file_id, ref value }| {
|
|
|
|
match value.parent() {
|
|
|
|
Some(parent) => Some(InFile::new(file_id, parent)),
|
|
|
|
None => {
|
|
|
|
self.cache(value.clone(), file_id);
|
|
|
|
file_id.call_node(db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|it| it.value)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn ancestors_at_offset_with_macros(
|
2020-03-07 08:27:03 -06:00
|
|
|
&self,
|
|
|
|
node: &SyntaxNode,
|
2020-04-24 16:40:41 -05:00
|
|
|
offset: TextSize,
|
2020-03-07 08:27:03 -06:00
|
|
|
) -> impl Iterator<Item = SyntaxNode> + '_ {
|
|
|
|
node.token_at_offset(offset)
|
2021-01-30 09:19:21 -06:00
|
|
|
.map(|token| self.token_ancestors_with_macros(token))
|
2020-03-07 08:27:03 -06:00
|
|
|
.kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len())
|
|
|
|
}
|
|
|
|
|
2020-12-15 12:23:51 -06:00
|
|
|
fn resolve_lifetime_param(&self, lifetime: &ast::Lifetime) -> Option<LifetimeParam> {
|
|
|
|
let text = lifetime.text();
|
|
|
|
let lifetime_param = lifetime.syntax().ancestors().find_map(|syn| {
|
2021-09-27 05:54:24 -05:00
|
|
|
let gpl = ast::AnyHasGenericParams::cast(syn)?.generic_param_list()?;
|
2020-12-13 15:13:16 -06:00
|
|
|
gpl.lifetime_params()
|
2021-03-26 12:30:59 -05:00
|
|
|
.find(|tp| tp.lifetime().as_ref().map(|lt| lt.text()).as_ref() == Some(&text))
|
2020-12-13 15:13:16 -06:00
|
|
|
})?;
|
2022-01-31 06:56:14 -06:00
|
|
|
let src = self.wrap_node_infile(lifetime_param);
|
2020-12-13 15:13:16 -06:00
|
|
|
ToDef::to_def(self, src)
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:34:30 -06:00
|
|
|
fn resolve_label(&self, lifetime: &ast::Lifetime) -> Option<Label> {
|
|
|
|
let text = lifetime.text();
|
|
|
|
let label = lifetime.syntax().ancestors().find_map(|syn| {
|
|
|
|
let label = match_ast! {
|
|
|
|
match syn {
|
|
|
|
ast::ForExpr(it) => it.label(),
|
|
|
|
ast::WhileExpr(it) => it.label(),
|
|
|
|
ast::LoopExpr(it) => it.label(),
|
2021-09-26 04:12:57 -05:00
|
|
|
ast::BlockExpr(it) => it.label(),
|
2020-12-23 09:34:30 -06:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
label.filter(|l| {
|
|
|
|
l.lifetime()
|
|
|
|
.and_then(|lt| lt.lifetime_ident_token())
|
|
|
|
.map_or(false, |lt| lt.text() == text)
|
|
|
|
})
|
|
|
|
})?;
|
2022-01-31 06:56:14 -06:00
|
|
|
let src = self.wrap_node_infile(label);
|
2020-12-23 09:34:30 -06:00
|
|
|
ToDef::to_def(self, src)
|
|
|
|
}
|
|
|
|
|
2021-05-06 10:05:49 -05:00
|
|
|
fn resolve_type(&self, ty: &ast::Type) -> Option<Type> {
|
2022-03-31 04:12:08 -05:00
|
|
|
let analyze = self.analyze(ty.syntax())?;
|
2023-04-06 14:16:11 -05:00
|
|
|
let ctx = body::LowerCtx::with_file_id(self.db.upcast(), analyze.file_id);
|
2022-03-31 04:12:08 -05:00
|
|
|
let ty = hir_ty::TyLoweringContext::new(self.db, &analyze.resolver)
|
2021-05-06 10:05:49 -05:00
|
|
|
.lower_ty(&crate::TypeRef::from_ast(&ctx, ty.clone()));
|
2022-03-31 04:12:08 -05:00
|
|
|
Some(Type::new_with_resolver(self.db, &analyze.resolver, ty))
|
2021-05-06 10:05:49 -05:00
|
|
|
}
|
|
|
|
|
2022-07-28 03:05:21 -05:00
|
|
|
fn resolve_trait(&self, path: &ast::Path) -> Option<Trait> {
|
|
|
|
let analyze = self.analyze(path.syntax())?;
|
|
|
|
let hygiene = hir_expand::hygiene::Hygiene::new(self.db.upcast(), analyze.file_id);
|
|
|
|
let ctx = body::LowerCtx::with_hygiene(self.db.upcast(), &hygiene);
|
|
|
|
let hir_path = Path::from_src(path.clone(), &ctx)?;
|
2023-03-08 11:28:52 -06:00
|
|
|
match analyze.resolver.resolve_path_in_type_ns_fully(self.db.upcast(), &hir_path)? {
|
2022-07-28 03:05:21 -05:00
|
|
|
TypeNs::TraitId(id) => Some(Trait { id }),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 13:33:27 -06:00
|
|
|
fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjustment>> {
|
2022-11-04 11:11:15 -05:00
|
|
|
let mutability = |m| match m {
|
|
|
|
hir_ty::Mutability::Not => Mutability::Shared,
|
|
|
|
hir_ty::Mutability::Mut => Mutability::Mut,
|
|
|
|
};
|
2022-12-20 13:33:27 -06:00
|
|
|
|
|
|
|
let analyzer = self.analyze(expr.syntax())?;
|
|
|
|
|
|
|
|
let (mut source_ty, _) = analyzer.type_of_expr(self.db, expr)?;
|
|
|
|
|
|
|
|
analyzer.expr_adjustments(self.db, expr).map(|it| {
|
2022-11-04 11:11:15 -05:00
|
|
|
it.iter()
|
2022-12-20 13:33:27 -06:00
|
|
|
.map(|adjust| {
|
|
|
|
let target =
|
|
|
|
Type::new_with_resolver(self.db, &analyzer.resolver, adjust.target.clone());
|
|
|
|
let kind = match adjust.kind {
|
|
|
|
hir_ty::Adjust::NeverToAny => Adjust::NeverToAny,
|
|
|
|
hir_ty::Adjust::Deref(Some(hir_ty::OverloadedDeref(m))) => {
|
2023-03-04 14:08:04 -06:00
|
|
|
// FIXME: Should we handle unknown mutability better?
|
|
|
|
Adjust::Deref(Some(OverloadedDeref(
|
|
|
|
m.map(mutability).unwrap_or(Mutability::Shared),
|
|
|
|
)))
|
2022-12-20 13:33:27 -06:00
|
|
|
}
|
|
|
|
hir_ty::Adjust::Deref(None) => Adjust::Deref(None),
|
|
|
|
hir_ty::Adjust::Borrow(hir_ty::AutoBorrow::RawPtr(m)) => {
|
|
|
|
Adjust::Borrow(AutoBorrow::RawPtr(mutability(m)))
|
|
|
|
}
|
|
|
|
hir_ty::Adjust::Borrow(hir_ty::AutoBorrow::Ref(m)) => {
|
|
|
|
Adjust::Borrow(AutoBorrow::Ref(mutability(m)))
|
|
|
|
}
|
|
|
|
hir_ty::Adjust::Pointer(pc) => Adjust::Pointer(pc),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update `source_ty` for the next adjustment
|
|
|
|
let source = mem::replace(&mut source_ty, target.clone());
|
|
|
|
|
|
|
|
let adjustment = Adjustment { source, target, kind };
|
|
|
|
|
|
|
|
adjustment
|
2022-11-04 11:11:15 -05:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
2022-03-20 08:38:16 -05:00
|
|
|
}
|
|
|
|
|
2021-08-02 13:42:25 -05:00
|
|
|
fn type_of_expr(&self, expr: &ast::Expr) -> Option<TypeInfo> {
|
2022-03-31 04:12:08 -05:00
|
|
|
self.analyze(expr.syntax())?
|
2021-08-02 13:42:25 -05:00
|
|
|
.type_of_expr(self.db, expr)
|
2021-08-03 10:28:51 -05:00
|
|
|
.map(|(ty, coerced)| TypeInfo { original: ty, adjusted: coerced })
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2021-08-02 13:42:25 -05:00
|
|
|
fn type_of_pat(&self, pat: &ast::Pat) -> Option<TypeInfo> {
|
2022-03-31 04:12:08 -05:00
|
|
|
self.analyze(pat.syntax())?
|
2021-08-02 13:42:25 -05:00
|
|
|
.type_of_pat(self.db, pat)
|
2021-08-03 10:28:51 -05:00
|
|
|
.map(|(ty, coerced)| TypeInfo { original: ty, adjusted: coerced })
|
2021-07-10 12:03:46 -05:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type> {
|
2022-03-31 04:12:08 -05:00
|
|
|
self.analyze(param.syntax())?.type_of_self(self.db, param)
|
2020-07-10 07:08:35 -05:00
|
|
|
}
|
|
|
|
|
2022-05-14 07:26:08 -05:00
|
|
|
fn pattern_adjustments(&self, pat: &ast::Pat) -> SmallVec<[Type; 1]> {
|
|
|
|
self.analyze(pat.syntax())
|
|
|
|
.and_then(|it| it.pattern_adjustments(self.db, pat))
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn binding_mode_of_pat(&self, pat: &ast::IdentPat) -> Option<BindingMode> {
|
|
|
|
self.analyze(pat.syntax())?.binding_mode_of_pat(self.db, pat)
|
|
|
|
}
|
|
|
|
|
2020-07-16 06:00:56 -05:00
|
|
|
fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<FunctionId> {
|
2022-06-25 04:33:27 -05:00
|
|
|
self.analyze(call.syntax())?.resolve_method_call(self.db, call)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2023-03-04 13:33:28 -06:00
|
|
|
fn resolve_method_call_fallback(
|
|
|
|
&self,
|
|
|
|
call: &ast::MethodCallExpr,
|
|
|
|
) -> Option<Either<FunctionId, FieldId>> {
|
|
|
|
self.analyze(call.syntax())?.resolve_method_call_fallback(self.db, call)
|
|
|
|
}
|
|
|
|
|
2022-08-05 07:28:36 -05:00
|
|
|
fn resolve_await_to_poll(&self, await_expr: &ast::AwaitExpr) -> Option<FunctionId> {
|
2022-08-05 07:16:36 -05:00
|
|
|
self.analyze(await_expr.syntax())?.resolve_await_to_poll(self.db, await_expr)
|
|
|
|
}
|
|
|
|
|
2022-08-05 07:28:36 -05:00
|
|
|
fn resolve_prefix_expr(&self, prefix_expr: &ast::PrefixExpr) -> Option<FunctionId> {
|
2022-08-05 07:16:36 -05:00
|
|
|
self.analyze(prefix_expr.syntax())?.resolve_prefix_expr(self.db, prefix_expr)
|
|
|
|
}
|
|
|
|
|
2022-08-05 07:28:36 -05:00
|
|
|
fn resolve_index_expr(&self, index_expr: &ast::IndexExpr) -> Option<FunctionId> {
|
2022-08-05 07:16:36 -05:00
|
|
|
self.analyze(index_expr.syntax())?.resolve_index_expr(self.db, index_expr)
|
|
|
|
}
|
|
|
|
|
2022-08-05 07:28:36 -05:00
|
|
|
fn resolve_bin_expr(&self, bin_expr: &ast::BinExpr) -> Option<FunctionId> {
|
2022-08-05 07:16:36 -05:00
|
|
|
self.analyze(bin_expr.syntax())?.resolve_bin_expr(self.db, bin_expr)
|
|
|
|
}
|
|
|
|
|
2022-08-05 07:28:36 -05:00
|
|
|
fn resolve_try_expr(&self, try_expr: &ast::TryExpr) -> Option<FunctionId> {
|
2022-08-05 07:16:36 -05:00
|
|
|
self.analyze(try_expr.syntax())?.resolve_try_expr(self.db, try_expr)
|
|
|
|
}
|
|
|
|
|
2020-07-16 06:00:56 -05:00
|
|
|
fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> {
|
2022-06-25 04:33:27 -05:00
|
|
|
self.analyze(call.syntax())?.resolve_method_call_as_callable(self.db, call)
|
2020-07-16 06:00:56 -05:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Field> {
|
2022-03-31 04:12:08 -05:00
|
|
|
self.analyze(field.syntax())?.resolve_field(self.db, field)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2021-05-23 16:54:35 -05:00
|
|
|
fn resolve_record_field(
|
|
|
|
&self,
|
|
|
|
field: &ast::RecordExprField,
|
|
|
|
) -> Option<(Field, Option<Local>, Type)> {
|
2022-03-31 04:12:08 -05:00
|
|
|
self.analyze(field.syntax())?.resolve_record_field(self.db, field)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2023-03-14 15:55:03 -05:00
|
|
|
fn resolve_record_pat_field(&self, field: &ast::RecordPatField) -> Option<(Field, Type)> {
|
2022-03-31 04:12:08 -05:00
|
|
|
self.analyze(field.syntax())?.resolve_record_pat_field(self.db, field)
|
2020-04-18 15:05:06 -05:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:51:48 -06:00
|
|
|
fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<Macro> {
|
2022-03-31 04:12:08 -05:00
|
|
|
let sa = self.analyze(macro_call.syntax())?;
|
2021-12-21 06:38:58 -06:00
|
|
|
let macro_call = self.find_file(macro_call.syntax()).with_value(macro_call);
|
2020-02-18 11:35:10 -06:00
|
|
|
sa.resolve_macro_call(self.db, macro_call)
|
|
|
|
}
|
|
|
|
|
2022-03-20 13:07:44 -05:00
|
|
|
fn is_unsafe_macro_call(&self, macro_call: &ast::MacroCall) -> bool {
|
2022-03-31 04:12:08 -05:00
|
|
|
let sa = match self.analyze(macro_call.syntax()) {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return false,
|
|
|
|
};
|
2022-03-20 13:07:44 -05:00
|
|
|
let macro_call = self.find_file(macro_call.syntax()).with_value(macro_call);
|
|
|
|
sa.is_unsafe_macro_call(self.db, macro_call)
|
|
|
|
}
|
|
|
|
|
2022-03-08 16:51:48 -06:00
|
|
|
fn resolve_attr_macro_call(&self, item: &ast::Item) -> Option<Macro> {
|
2022-01-31 06:56:14 -06:00
|
|
|
let item_in_file = self.wrap_node_infile(item.clone());
|
2022-03-08 16:51:48 -06:00
|
|
|
let id = self.with_ctx(|ctx| {
|
|
|
|
let macro_call_id = ctx.item_to_macro_call(item_in_file)?;
|
|
|
|
macro_call_to_macro_id(ctx, self.db.upcast(), macro_call_id)
|
|
|
|
})?;
|
|
|
|
Some(Macro { id })
|
2021-08-21 16:24:12 -05:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn resolve_path(&self, path: &ast::Path) -> Option<PathResolution> {
|
2022-03-31 04:12:08 -05:00
|
|
|
self.analyze(path.syntax())?.resolve_path(self.db, path)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-08-08 13:14:18 -05:00
|
|
|
fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option<Crate> {
|
2022-03-31 04:12:08 -05:00
|
|
|
let krate = self.scope(extern_crate.syntax())?.krate();
|
2022-03-05 17:17:40 -06:00
|
|
|
let name = extern_crate.name_ref()?.as_name();
|
|
|
|
if name == known::SELF_PARAM {
|
|
|
|
return Some(krate);
|
|
|
|
}
|
|
|
|
krate
|
|
|
|
.dependencies(self.db)
|
|
|
|
.into_iter()
|
2022-12-30 02:30:23 -06:00
|
|
|
.find_map(|dep| (dep.name == name).then_some(dep.krate))
|
2020-08-08 13:14:18 -05:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:21:30 -05:00
|
|
|
fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantId> {
|
2022-03-31 04:12:08 -05:00
|
|
|
self.analyze(record_lit.syntax())?.resolve_variant(self.db, record_lit)
|
2020-06-09 16:11:16 -05:00
|
|
|
}
|
|
|
|
|
2020-07-31 13:09:09 -05:00
|
|
|
fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> {
|
2022-03-31 04:12:08 -05:00
|
|
|
self.analyze(pat.syntax())?.resolve_bind_pat_to_const(self.db, pat)
|
2020-02-28 09:36:14 -06:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:21:30 -05:00
|
|
|
fn record_literal_missing_fields(&self, literal: &ast::RecordExpr) -> Vec<(Field, Type)> {
|
2020-04-07 10:09:02 -05:00
|
|
|
self.analyze(literal.syntax())
|
2022-03-31 04:12:08 -05:00
|
|
|
.and_then(|it| it.record_literal_missing_fields(self.db, literal))
|
2020-04-07 10:09:02 -05:00
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> {
|
2020-04-07 10:09:02 -05:00
|
|
|
self.analyze(pattern.syntax())
|
2022-03-31 04:12:08 -05:00
|
|
|
.and_then(|it| it.record_pattern_missing_fields(self.db, pattern))
|
2020-04-07 10:09:02 -05:00
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
fn with_ctx<F: FnOnce(&mut SourceToDefCtx<'_, '_>) -> T, T>(&self, f: F) -> T {
|
2020-02-29 11:32:18 -06:00
|
|
|
let mut cache = self.s2d_cache.borrow_mut();
|
2022-12-23 01:51:52 -06:00
|
|
|
let mut ctx = SourceToDefCtx { db: self.db, cache: &mut cache };
|
2020-02-29 11:32:18 -06:00
|
|
|
f(&mut ctx)
|
|
|
|
}
|
|
|
|
|
2022-12-21 14:34:49 -06:00
|
|
|
fn to_def<T: ToDef>(&self, src: &T) -> Option<T::Def> {
|
|
|
|
let src = self.find_file(src.syntax()).with_value(src).cloned();
|
2022-12-30 02:05:03 -06:00
|
|
|
T::to_def(self, src)
|
2022-12-21 14:34:49 -06:00
|
|
|
}
|
|
|
|
|
2021-03-15 08:51:20 -05:00
|
|
|
fn to_module_def(&self, file: FileId) -> impl Iterator<Item = Module> {
|
|
|
|
self.with_ctx(|ctx| ctx.file_to_def(file)).into_iter().map(Module::from)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
fn scope(&self, node: &SyntaxNode) -> Option<SemanticsScope<'db>> {
|
|
|
|
self.analyze_no_infer(node).map(|SourceAnalyzer { file_id, resolver, .. }| SemanticsScope {
|
|
|
|
db: self.db,
|
|
|
|
file_id,
|
|
|
|
resolver,
|
|
|
|
})
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> Option<SemanticsScope<'db>> {
|
|
|
|
self.analyze_with_offset_no_infer(node, offset).map(
|
|
|
|
|SourceAnalyzer { file_id, resolver, .. }| SemanticsScope {
|
|
|
|
db: self.db,
|
|
|
|
file_id,
|
|
|
|
resolver,
|
|
|
|
},
|
|
|
|
)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> {
|
2021-03-12 17:34:01 -06:00
|
|
|
let file_id = self.db.lookup_intern_trait(def.id).id.file_id();
|
2020-07-01 04:43:36 -05:00
|
|
|
let resolver = def.id.resolver(self.db.upcast());
|
2020-08-13 16:52:14 -05:00
|
|
|
SemanticsScope { db: self.db, file_id, resolver }
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2021-10-07 07:46:49 -05:00
|
|
|
fn source<Def: HasSource>(&self, def: Def) -> Option<InFile<Def::Ast>>
|
|
|
|
where
|
|
|
|
Def::Ast: AstNode,
|
|
|
|
{
|
|
|
|
let res = def.source(self.db)?;
|
|
|
|
self.cache(find_root(res.value.syntax()), res.file_id);
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
/// Returns none if the file of the node is not part of a crate.
|
|
|
|
fn analyze(&self, node: &SyntaxNode) -> Option<SourceAnalyzer> {
|
2021-12-20 06:19:48 -06:00
|
|
|
self.analyze_impl(node, None, true)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
2021-11-10 10:33:35 -06:00
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
/// Returns none if the file of the node is not part of a crate.
|
|
|
|
fn analyze_no_infer(&self, node: &SyntaxNode) -> Option<SourceAnalyzer> {
|
2021-12-20 06:19:48 -06:00
|
|
|
self.analyze_impl(node, None, false)
|
|
|
|
}
|
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
fn analyze_with_offset_no_infer(
|
|
|
|
&self,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
offset: TextSize,
|
|
|
|
) -> Option<SourceAnalyzer> {
|
2021-12-21 06:38:58 -06:00
|
|
|
self.analyze_impl(node, Some(offset), false)
|
|
|
|
}
|
|
|
|
|
2021-12-20 06:19:48 -06:00
|
|
|
fn analyze_impl(
|
|
|
|
&self,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
offset: Option<TextSize>,
|
|
|
|
infer_body: bool,
|
2022-03-31 04:12:08 -05:00
|
|
|
) -> Option<SourceAnalyzer> {
|
2020-12-11 07:50:47 -06:00
|
|
|
let _p = profile::span("Semantics::analyze_impl");
|
2021-12-21 06:38:58 -06:00
|
|
|
let node = self.find_file(node);
|
2020-02-18 11:35:10 -06:00
|
|
|
|
2023-01-30 04:44:51 -06:00
|
|
|
let container = self.with_ctx(|ctx| ctx.find_container(node))?;
|
2020-02-18 11:35:10 -06:00
|
|
|
|
|
|
|
let resolver = match container {
|
|
|
|
ChildContainer::DefWithBodyId(def) => {
|
2022-03-31 04:12:08 -05:00
|
|
|
return Some(if infer_body {
|
2021-12-20 06:19:48 -06:00
|
|
|
SourceAnalyzer::new_for_body(self.db, def, node, offset)
|
|
|
|
} else {
|
|
|
|
SourceAnalyzer::new_for_body_no_infer(self.db, def, node, offset)
|
2022-03-31 04:12:08 -05:00
|
|
|
})
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
2020-07-01 04:43:36 -05:00
|
|
|
ChildContainer::TraitId(it) => it.resolver(self.db.upcast()),
|
2023-03-03 09:24:07 -06:00
|
|
|
ChildContainer::TraitAliasId(it) => it.resolver(self.db.upcast()),
|
2020-07-01 04:43:36 -05:00
|
|
|
ChildContainer::ImplId(it) => it.resolver(self.db.upcast()),
|
|
|
|
ChildContainer::ModuleId(it) => it.resolver(self.db.upcast()),
|
|
|
|
ChildContainer::EnumId(it) => it.resolver(self.db.upcast()),
|
|
|
|
ChildContainer::VariantId(it) => it.resolver(self.db.upcast()),
|
2020-07-11 05:45:30 -05:00
|
|
|
ChildContainer::TypeAliasId(it) => it.resolver(self.db.upcast()),
|
2020-07-01 04:43:36 -05:00
|
|
|
ChildContainer::GenericDefId(it) => it.resolver(self.db.upcast()),
|
2020-02-18 11:35:10 -06:00
|
|
|
};
|
2022-03-31 04:12:08 -05:00
|
|
|
Some(SourceAnalyzer::new_for_resolver(resolver, node))
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) {
|
2020-02-18 11:35:10 -06:00
|
|
|
assert!(root_node.parent().is_none());
|
|
|
|
let mut cache = self.cache.borrow_mut();
|
|
|
|
let prev = cache.insert(root_node, file_id);
|
|
|
|
assert!(prev == None || prev == Some(file_id))
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn assert_contains_node(&self, node: &SyntaxNode) {
|
2021-12-21 06:38:58 -06:00
|
|
|
self.find_file(node);
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lookup(&self, root_node: &SyntaxNode) -> Option<HirFileId> {
|
|
|
|
let cache = self.cache.borrow();
|
|
|
|
cache.get(root_node).copied()
|
|
|
|
}
|
|
|
|
|
2022-01-31 06:56:14 -06:00
|
|
|
fn wrap_node_infile<N: AstNode>(&self, node: N) -> InFile<N> {
|
|
|
|
let InFile { file_id, .. } = self.find_file(node.syntax());
|
|
|
|
InFile::new(file_id, node)
|
|
|
|
}
|
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
/// Wraps the node in a [`InFile`] with the file id it belongs to.
|
2021-12-21 06:38:58 -06:00
|
|
|
fn find_file<'node>(&self, node: &'node SyntaxNode) -> InFile<&'node SyntaxNode> {
|
|
|
|
let root_node = find_root(node);
|
2020-02-18 11:35:10 -06:00
|
|
|
let file_id = self.lookup(&root_node).unwrap_or_else(|| {
|
|
|
|
panic!(
|
|
|
|
"\n\nFailed to lookup {:?} in this Semantics.\n\
|
|
|
|
Make sure to use only query nodes, derived from this instance of Semantics.\n\
|
|
|
|
root node: {:?}\n\
|
|
|
|
known nodes: {}\n\n",
|
|
|
|
node,
|
|
|
|
root_node,
|
|
|
|
self.cache
|
|
|
|
.borrow()
|
|
|
|
.keys()
|
2022-12-23 12:42:58 -06:00
|
|
|
.map(|it| format!("{it:?}"))
|
2020-02-18 11:35:10 -06:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")
|
|
|
|
)
|
|
|
|
});
|
|
|
|
InFile::new(file_id, node)
|
|
|
|
}
|
2020-07-30 08:26:40 -05:00
|
|
|
|
2020-08-19 06:46:34 -05:00
|
|
|
fn is_unsafe_method_call(&self, method_call_expr: &ast::MethodCallExpr) -> bool {
|
2020-07-30 08:26:40 -05:00
|
|
|
method_call_expr
|
2020-08-21 12:12:38 -05:00
|
|
|
.receiver()
|
2020-07-30 08:26:40 -05:00
|
|
|
.and_then(|expr| {
|
2020-08-19 06:46:34 -05:00
|
|
|
let field_expr = match expr {
|
|
|
|
ast::Expr::FieldExpr(field_expr) => field_expr,
|
|
|
|
_ => return None,
|
2020-07-30 08:26:40 -05:00
|
|
|
};
|
2021-08-03 10:28:51 -05:00
|
|
|
let ty = self.type_of_expr(&field_expr.expr()?)?.original;
|
2020-07-30 08:26:40 -05:00
|
|
|
if !ty.is_packed(self.db) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:54:16 -05:00
|
|
|
let func = self.resolve_method_call(method_call_expr).map(Function::from)?;
|
2020-08-19 08:16:24 -05:00
|
|
|
let res = match func.self_param(self.db)?.access(self.db) {
|
|
|
|
Access::Shared | Access::Exclusive => true,
|
|
|
|
Access::Owned => false,
|
|
|
|
};
|
|
|
|
Some(res)
|
2020-07-30 08:26:40 -05:00
|
|
|
})
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2020-08-19 06:46:34 -05:00
|
|
|
fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool {
|
2020-07-30 08:26:40 -05:00
|
|
|
ref_expr
|
|
|
|
.expr()
|
|
|
|
.and_then(|expr| {
|
|
|
|
let field_expr = match expr {
|
|
|
|
ast::Expr::FieldExpr(field_expr) => field_expr,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
let expr = field_expr.expr()?;
|
|
|
|
self.type_of_expr(&expr)
|
|
|
|
})
|
|
|
|
// Binding a reference to a packed type is possibly unsafe.
|
2021-08-03 10:28:51 -05:00
|
|
|
.map(|ty| ty.original.is_packed(self.db))
|
2020-07-30 08:26:40 -05:00
|
|
|
.unwrap_or(false)
|
|
|
|
|
|
|
|
// FIXME This needs layout computation to be correct. It will highlight
|
|
|
|
// more than it should with the current implementation.
|
|
|
|
}
|
|
|
|
|
2020-08-19 06:46:34 -05:00
|
|
|
fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool {
|
2021-09-03 09:00:50 -05:00
|
|
|
if ident_pat.ref_token().is_none() {
|
2020-08-07 09:40:09 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ident_pat
|
2020-07-30 08:26:40 -05:00
|
|
|
.syntax()
|
|
|
|
.parent()
|
|
|
|
.and_then(|parent| {
|
2020-08-07 09:40:09 -05:00
|
|
|
// `IdentPat` can live under `RecordPat` directly under `RecordPatField` or
|
|
|
|
// `RecordPatFieldList`. `RecordPatField` also lives under `RecordPatFieldList`,
|
|
|
|
// so this tries to lookup the `IdentPat` anywhere along that structure to the
|
2020-07-30 08:26:40 -05:00
|
|
|
// `RecordPat` so we can get the containing type.
|
2020-08-07 09:40:09 -05:00
|
|
|
let record_pat = ast::RecordPatField::cast(parent.clone())
|
2020-07-30 08:26:40 -05:00
|
|
|
.and_then(|record_pat| record_pat.syntax().parent())
|
|
|
|
.or_else(|| Some(parent.clone()))
|
|
|
|
.and_then(|parent| {
|
2020-08-07 09:40:09 -05:00
|
|
|
ast::RecordPatFieldList::cast(parent)?
|
2020-07-30 08:26:40 -05:00
|
|
|
.syntax()
|
|
|
|
.parent()
|
|
|
|
.and_then(ast::RecordPat::cast)
|
|
|
|
});
|
|
|
|
|
|
|
|
// If this doesn't match a `RecordPat`, fallback to a `LetStmt` to see if
|
|
|
|
// this is initialized from a `FieldExpr`.
|
|
|
|
if let Some(record_pat) = record_pat {
|
|
|
|
self.type_of_pat(&ast::Pat::RecordPat(record_pat))
|
|
|
|
} else if let Some(let_stmt) = ast::LetStmt::cast(parent) {
|
|
|
|
let field_expr = match let_stmt.initializer()? {
|
|
|
|
ast::Expr::FieldExpr(field_expr) => field_expr,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.type_of_expr(&field_expr.expr()?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Binding a reference to a packed type is possibly unsafe.
|
2021-08-03 10:28:51 -05:00
|
|
|
.map(|ty| ty.original.is_packed(self.db))
|
2020-07-30 08:26:40 -05:00
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
2022-12-21 14:34:49 -06:00
|
|
|
|
2022-12-21 16:52:52 -06:00
|
|
|
fn is_inside_unsafe(&self, expr: &ast::Expr) -> bool {
|
2023-01-14 06:45:20 -06:00
|
|
|
let Some(enclosing_item) = expr.syntax().ancestors().find_map(Either::<ast::Item, ast::Variant>::cast) else { return false };
|
2022-12-21 14:34:49 -06:00
|
|
|
|
|
|
|
let def = match &enclosing_item {
|
2022-12-21 16:52:52 -06:00
|
|
|
Either::Left(ast::Item::Fn(it)) if it.unsafe_token().is_some() => return true,
|
2022-12-21 14:34:49 -06:00
|
|
|
Either::Left(ast::Item::Fn(it)) => {
|
|
|
|
self.to_def(it).map(<_>::into).map(DefWithBodyId::FunctionId)
|
|
|
|
}
|
|
|
|
Either::Left(ast::Item::Const(it)) => {
|
|
|
|
self.to_def(it).map(<_>::into).map(DefWithBodyId::ConstId)
|
|
|
|
}
|
|
|
|
Either::Left(ast::Item::Static(it)) => {
|
|
|
|
self.to_def(it).map(<_>::into).map(DefWithBodyId::StaticId)
|
|
|
|
}
|
|
|
|
Either::Left(_) => None,
|
|
|
|
Either::Right(it) => self.to_def(it).map(<_>::into).map(DefWithBodyId::VariantId),
|
|
|
|
};
|
|
|
|
let Some(def) = def else { return false };
|
|
|
|
let enclosing_node = enclosing_item.as_ref().either(|i| i.syntax(), |v| v.syntax());
|
|
|
|
|
|
|
|
let (body, source_map) = self.db.body_with_source_map(def);
|
|
|
|
|
2022-12-21 16:52:52 -06:00
|
|
|
let file_id = self.find_file(expr.syntax()).file_id;
|
2022-12-21 14:34:49 -06:00
|
|
|
|
2022-12-21 16:52:52 -06:00
|
|
|
let Some(mut parent) = expr.syntax().parent() else { return false };
|
2022-12-21 14:34:49 -06:00
|
|
|
loop {
|
|
|
|
if &parent == enclosing_node {
|
|
|
|
break false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(parent) = ast::Expr::cast(parent.clone()) {
|
|
|
|
if let Some(expr_id) = source_map.node_expr(InFile { file_id, value: &parent }) {
|
|
|
|
if let Expr::Unsafe { .. } = body[expr_id] {
|
|
|
|
break true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let Some(parent_) = parent.parent() else { break false };
|
|
|
|
parent = parent_;
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:51:48 -06:00
|
|
|
fn macro_call_to_macro_id(
|
2022-07-20 08:02:08 -05:00
|
|
|
ctx: &mut SourceToDefCtx<'_, '_>,
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2022-03-08 16:51:48 -06:00
|
|
|
macro_call_id: MacroCallId,
|
|
|
|
) -> Option<MacroId> {
|
|
|
|
let loc = db.lookup_intern_macro_call(macro_call_id);
|
|
|
|
match loc.def.kind {
|
|
|
|
hir_expand::MacroDefKind::Declarative(it)
|
|
|
|
| hir_expand::MacroDefKind::BuiltIn(_, it)
|
|
|
|
| hir_expand::MacroDefKind::BuiltInAttr(_, it)
|
|
|
|
| hir_expand::MacroDefKind::BuiltInDerive(_, it)
|
|
|
|
| hir_expand::MacroDefKind::BuiltInEager(_, it) => {
|
|
|
|
ctx.macro_to_def(InFile::new(it.file_id, it.to_node(db)))
|
|
|
|
}
|
|
|
|
hir_expand::MacroDefKind::ProcMacro(_, _, it) => {
|
|
|
|
ctx.proc_macro_to_def(InFile::new(it.file_id, it.to_node(db)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 11:32:18 -06:00
|
|
|
pub trait ToDef: AstNode + Clone {
|
2020-02-26 06:22:46 -06:00
|
|
|
type Def;
|
2020-02-29 11:32:18 -06:00
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
fn to_def(sema: &SemanticsImpl<'_>, src: InFile<Self>) -> Option<Self::Def>;
|
2020-02-26 06:22:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! to_def_impls {
|
2020-02-29 11:32:18 -06:00
|
|
|
($(($def:path, $ast:path, $meth:ident)),* ,) => {$(
|
2020-02-26 06:22:46 -06:00
|
|
|
impl ToDef for $ast {
|
|
|
|
type Def = $def;
|
2022-07-20 08:02:08 -05:00
|
|
|
fn to_def(sema: &SemanticsImpl<'_>, src: InFile<Self>) -> Option<Self::Def> {
|
2020-02-29 11:32:18 -06:00
|
|
|
sema.with_ctx(|ctx| ctx.$meth(src)).map(<$def>::from)
|
2020-02-26 06:22:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)*}
|
|
|
|
}
|
|
|
|
|
|
|
|
to_def_impls![
|
2020-02-29 11:32:18 -06:00
|
|
|
(crate::Module, ast::Module, module_to_def),
|
2021-03-16 12:57:47 -05:00
|
|
|
(crate::Module, ast::SourceFile, source_file_to_def),
|
2020-07-30 10:50:40 -05:00
|
|
|
(crate::Struct, ast::Struct, struct_to_def),
|
2020-07-30 10:52:53 -05:00
|
|
|
(crate::Enum, ast::Enum, enum_to_def),
|
2020-07-30 10:36:46 -05:00
|
|
|
(crate::Union, ast::Union, union_to_def),
|
2020-07-30 11:17:28 -05:00
|
|
|
(crate::Trait, ast::Trait, trait_to_def),
|
2023-03-03 09:24:07 -06:00
|
|
|
(crate::TraitAlias, ast::TraitAlias, trait_alias_to_def),
|
2020-12-17 05:36:15 -06:00
|
|
|
(crate::Impl, ast::Impl, impl_to_def),
|
2020-07-30 08:25:46 -05:00
|
|
|
(crate::TypeAlias, ast::TypeAlias, type_alias_to_def),
|
2020-07-30 11:02:20 -05:00
|
|
|
(crate::Const, ast::Const, const_to_def),
|
|
|
|
(crate::Static, ast::Static, static_to_def),
|
2020-07-30 07:51:08 -05:00
|
|
|
(crate::Function, ast::Fn, fn_to_def),
|
2020-07-30 09:49:13 -05:00
|
|
|
(crate::Field, ast::RecordField, record_field_to_def),
|
|
|
|
(crate::Field, ast::TupleField, tuple_field_to_def),
|
2020-12-20 01:05:24 -06:00
|
|
|
(crate::Variant, ast::Variant, enum_variant_to_def),
|
2020-02-29 11:32:18 -06:00
|
|
|
(crate::TypeParam, ast::TypeParam, type_param_to_def),
|
2020-12-13 15:13:16 -06:00
|
|
|
(crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def),
|
2021-01-01 03:06:42 -06:00
|
|
|
(crate::ConstParam, ast::ConstParam, const_param_to_def),
|
2021-12-20 06:47:06 -06:00
|
|
|
(crate::GenericParam, ast::GenericParam, generic_param_to_def),
|
2022-03-08 16:51:48 -06:00
|
|
|
(crate::Macro, ast::Macro, macro_to_def),
|
2020-07-31 13:09:09 -05:00
|
|
|
(crate::Local, ast::IdentPat, bind_pat_to_def),
|
2021-01-15 11:57:32 -06:00
|
|
|
(crate::Local, ast::SelfParam, self_param_to_def),
|
2020-12-23 09:34:30 -06:00
|
|
|
(crate::Label, ast::Label, label_to_def),
|
2021-08-14 10:42:06 -05:00
|
|
|
(crate::Adt, ast::Adt, adt_to_def),
|
2020-02-26 06:22:46 -06:00
|
|
|
];
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
fn find_root(node: &SyntaxNode) -> SyntaxNode {
|
|
|
|
node.ancestors().last().unwrap()
|
|
|
|
}
|
|
|
|
|
2023-01-30 04:44:51 -06:00
|
|
|
/// `SemanticsScope` encapsulates the notion of a scope (the set of visible
|
2020-09-21 05:30:55 -05:00
|
|
|
/// names) at a particular program point.
|
|
|
|
///
|
|
|
|
/// It is a bit tricky, as scopes do not really exist inside the compiler.
|
|
|
|
/// Rather, the compiler directly computes for each reference the definition it
|
|
|
|
/// refers to. It might transiently compute the explicit scope map while doing
|
|
|
|
/// so, but, generally, this is not something left after the analysis.
|
|
|
|
///
|
|
|
|
/// However, we do very much need explicit scopes for IDE purposes --
|
2020-09-21 07:35:42 -05:00
|
|
|
/// completion, at its core, lists the contents of the current scope. The notion
|
|
|
|
/// of scope is also useful to answer questions like "what would be the meaning
|
|
|
|
/// of this piece of code if we inserted it into this position?".
|
2020-09-21 05:30:55 -05:00
|
|
|
///
|
|
|
|
/// So `SemanticsScope` is constructed from a specific program point (a syntax
|
|
|
|
/// node or just a raw offset) and provides access to the set of visible names
|
2020-09-21 07:35:42 -05:00
|
|
|
/// on a somewhat best-effort basis.
|
2020-09-21 05:30:55 -05:00
|
|
|
///
|
2020-09-21 07:35:42 -05:00
|
|
|
/// Note that if you are wondering "what does this specific existing name mean?",
|
2020-09-21 05:30:55 -05:00
|
|
|
/// you'd better use the `resolve_` family of methods.
|
2020-07-10 18:26:24 -05:00
|
|
|
#[derive(Debug)]
|
2020-07-01 01:34:45 -05:00
|
|
|
pub struct SemanticsScope<'a> {
|
|
|
|
pub db: &'a dyn HirDatabase,
|
2020-08-13 16:52:14 -05:00
|
|
|
file_id: HirFileId,
|
2020-02-18 11:35:10 -06:00
|
|
|
resolver: Resolver,
|
|
|
|
}
|
|
|
|
|
2020-07-01 01:34:45 -05:00
|
|
|
impl<'a> SemanticsScope<'a> {
|
2022-03-31 04:12:08 -05:00
|
|
|
pub fn module(&self) -> Module {
|
|
|
|
Module { id: self.resolver.module() }
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
pub fn krate(&self) -> Crate {
|
|
|
|
Crate { id: self.resolver.krate() }
|
2020-08-08 13:14:18 -05:00
|
|
|
}
|
|
|
|
|
Refactor autoderef and method resolution
- don't return the receiver type from method resolution; instead just
return the autorefs/autoderefs that happened and repeat them. This
ensures all the effects like trait obligations and whatever we learned
about type variables from derefing them are actually applied. Also, it
allows us to get rid of `decanonicalize_ty`, which was just wrong in
principle.
- Autoderef itself now directly works with an inference table. Sadly
this has the effect of making it harder to use as an iterator, often
requiring manual `while let` loops. (rustc works around this by using
inner mutability in the inference context, so that things like unifying
types don't require a unique reference.)
- We now record the adjustments (autoref/deref) for method receivers
and index expressions, which we didn't before.
- Removed the redundant crate parameter from method resolution, since
the trait_env contains the crate as well.
- in the HIR API, the methods now take a scope to determine the trait env.
`Type` carries a trait env, but I think that's probably a bad decision
because it's easy to create it with the wrong env, e.g. by using
`Adt::ty`. This mostly didn't matter so far because
`iterate_method_candidates` took a crate parameter and ignored
`self.krate`, but the trait env would still have been wrong in those
cases, which I think would give some wrong results in some edge cases.
Fixes #10058.
2022-02-16 10:44:03 -06:00
|
|
|
pub(crate) fn resolver(&self) -> &Resolver {
|
|
|
|
&self.resolver
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:21:42 -05:00
|
|
|
/// Note: `VisibleTraits` should be treated as an opaque type, passed into `Type
|
|
|
|
pub fn visible_traits(&self) -> VisibleTraits {
|
2020-02-18 11:35:10 -06:00
|
|
|
let resolver = &self.resolver;
|
2022-05-05 15:21:42 -05:00
|
|
|
VisibleTraits(resolver.traits_in_scope(self.db.upcast()))
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2023-03-29 07:08:25 -05:00
|
|
|
/// Calls the passed closure `f` on all names in scope.
|
2020-02-18 11:35:10 -06:00
|
|
|
pub fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
|
2021-08-03 09:36:06 -05:00
|
|
|
let scope = self.resolver.names_in_scope(self.db.upcast());
|
|
|
|
for (name, entries) in scope {
|
|
|
|
for entry in entries {
|
|
|
|
let def = match entry {
|
|
|
|
resolver::ScopeDef::ModuleDef(it) => ScopeDef::ModuleDef(it.into()),
|
|
|
|
resolver::ScopeDef::Unknown => ScopeDef::Unknown,
|
|
|
|
resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
|
|
|
|
resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
|
|
|
|
resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(id.into()),
|
2023-02-18 14:32:55 -06:00
|
|
|
resolver::ScopeDef::Local(binding_id) => match self.resolver.body_owner() {
|
|
|
|
Some(parent) => ScopeDef::Local(Local { parent, binding_id }),
|
2022-03-14 14:36:35 -05:00
|
|
|
None => continue,
|
|
|
|
},
|
|
|
|
resolver::ScopeDef::Label(label_id) => match self.resolver.body_owner() {
|
|
|
|
Some(parent) => ScopeDef::Label(Label { parent, label_id }),
|
|
|
|
None => continue,
|
|
|
|
},
|
2021-08-03 09:36:06 -05:00
|
|
|
};
|
|
|
|
f(name.clone(), def)
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-08-13 16:52:14 -05:00
|
|
|
/// Resolve a path as-if it was written at the given scope. This is
|
|
|
|
/// necessary a heuristic, as it doesn't take hygiene into account.
|
2020-08-14 08:23:27 -05:00
|
|
|
pub fn speculative_resolve(&self, path: &ast::Path) -> Option<PathResolution> {
|
2023-04-06 14:16:11 -05:00
|
|
|
let ctx = body::LowerCtx::with_file_id(self.db.upcast(), self.file_id);
|
2021-04-10 10:49:12 -05:00
|
|
|
let path = Path::from_src(path.clone(), &ctx)?;
|
2020-08-15 11:50:41 -05:00
|
|
|
resolve_hir_path(self.db, &self.resolver, &path)
|
2020-05-15 16:23:49 -05:00
|
|
|
}
|
2022-02-03 05:43:15 -06:00
|
|
|
|
|
|
|
/// Iterates over associated types that may be specified after the given path (using
|
|
|
|
/// `Ty::Assoc` syntax).
|
|
|
|
pub fn assoc_type_shorthand_candidates<R>(
|
|
|
|
&self,
|
|
|
|
resolution: &PathResolution,
|
|
|
|
mut cb: impl FnMut(&Name, TypeAlias) -> Option<R>,
|
|
|
|
) -> Option<R> {
|
|
|
|
let def = self.resolver.generic_def()?;
|
|
|
|
hir_ty::associated_type_shorthand_candidates(
|
|
|
|
self.db,
|
|
|
|
def,
|
|
|
|
resolution.in_type_ns()?,
|
2022-12-07 08:22:37 -06:00
|
|
|
|name, id| cb(name, id.into()),
|
2022-02-03 05:43:15 -06:00
|
|
|
)
|
|
|
|
}
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
2022-05-05 15:21:42 -05:00
|
|
|
|
2023-02-27 08:51:45 -06:00
|
|
|
#[derive(Debug)]
|
2022-05-05 15:21:42 -05:00
|
|
|
pub struct VisibleTraits(pub FxHashSet<TraitId>);
|
2022-06-20 14:55:33 -05:00
|
|
|
|
|
|
|
impl ops::Deref for VisibleTraits {
|
|
|
|
type Target = FxHashSet<TraitId>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|