2020-02-18 11:35:10 -06:00
|
|
|
//! See `Semantics`.
|
|
|
|
|
2020-02-29 11:32:18 -06:00
|
|
|
mod source_to_def;
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
use std::{cell::RefCell, fmt, iter::successors};
|
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::{FileId, FileRange};
|
2020-02-18 11:35:10 -06:00
|
|
|
use hir_def::{
|
2020-08-15 11:50:41 -05:00
|
|
|
resolver::{self, HasResolver, Resolver, TypeNs},
|
2020-08-19 10:02:50 -05:00
|
|
|
AsMacroCall, FunctionId, TraitId, VariantId,
|
2020-02-18 11:35:10 -06:00
|
|
|
};
|
2020-08-10 16:37:23 -05:00
|
|
|
use hir_expand::{hygiene::Hygiene, name::AsName, ExpansionInfo};
|
2020-04-29 17:03:36 -05:00
|
|
|
use hir_ty::associated_type_shorthand_candidates;
|
2020-04-06 09:58:16 -05:00
|
|
|
use itertools::Itertools;
|
2020-08-12 11:26:51 -05:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2020-12-13 15:13:16 -06:00
|
|
|
use syntax::{
|
|
|
|
algo::find_node_at_offset,
|
2020-12-23 09:34:30 -06:00
|
|
|
ast::{self, GenericParamsOwner, LoopBodyOwner},
|
2021-02-28 05:12:11 -06:00
|
|
|
match_ast, AstNode, 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},
|
2020-08-15 11:50:41 -05:00
|
|
|
source_analyzer::{resolve_hir_path, SourceAnalyzer},
|
2021-03-08 13:08:30 -06:00
|
|
|
Access, AssocItem, Callable, ConstParam, Crate, Field, Function, HirFileId, Impl, InFile,
|
|
|
|
Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, Name, Path, ScopeDef, Trait, Type,
|
2021-01-01 03:06:42 -06:00
|
|
|
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),
|
|
|
|
/// A generic parameter
|
|
|
|
TypeParam(TypeParam),
|
2021-01-01 03:06:42 -06:00
|
|
|
ConstParam(ConstParam),
|
2020-12-17 05:36:15 -06:00
|
|
|
SelfType(Impl),
|
2020-03-05 04:08:31 -06:00
|
|
|
Macro(MacroDef),
|
|
|
|
AssocItem(AssocItem),
|
2020-04-27 17:40:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PathResolution {
|
2020-04-29 17:05:03 -05:00
|
|
|
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
|
|
|
}
|
2020-04-29 17:06:12 -05:00
|
|
|
PathResolution::Def(ModuleDef::Const(_))
|
2020-12-20 01:05:24 -06:00
|
|
|
| PathResolution::Def(ModuleDef::Variant(_))
|
2020-04-29 17:06:12 -05:00
|
|
|
| PathResolution::Def(ModuleDef::Function(_))
|
|
|
|
| PathResolution::Def(ModuleDef::Module(_))
|
|
|
|
| PathResolution::Def(ModuleDef::Static(_))
|
|
|
|
| PathResolution::Def(ModuleDef::Trait(_)) => 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-01-01 03:06:42 -06:00
|
|
|
PathResolution::Local(_) | PathResolution::Macro(_) | 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-29 17:06:12 -05:00
|
|
|
PathResolution::AssocItem(AssocItem::Const(_))
|
|
|
|
| PathResolution::AssocItem(AssocItem::Function(_)) => None,
|
2020-04-27 17:40:32 -05:00
|
|
|
PathResolution::AssocItem(AssocItem::TypeAlias(alias)) => {
|
2020-04-29 17:05:03 -05:00
|
|
|
Some(TypeNs::TypeAliasId((*alias).into()))
|
2020-04-27 17:40:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over associated types that may be specified after this path (using
|
|
|
|
/// `Ty::Assoc` syntax).
|
|
|
|
pub fn assoc_type_shorthand_candidates<R>(
|
|
|
|
&self,
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
mut cb: impl FnMut(TypeAlias) -> Option<R>,
|
|
|
|
) -> Option<R> {
|
2020-04-29 17:09:00 -05:00
|
|
|
associated_type_shorthand_candidates(db, self.in_type_ns()?, |_, _, id| cb(id.into()))
|
2020-04-27 17:40:32 -05:00
|
|
|
}
|
2020-03-05 04:08:31 -06: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>>>,
|
2020-02-18 11:35:10 -06:00
|
|
|
cache: RefCell<FxHashMap<SyntaxNode, HirFileId>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
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,
|
|
|
|
hypothetical_args: &ast::TokenTree,
|
|
|
|
token_to_map: SyntaxToken,
|
|
|
|
) -> Option<(SyntaxNode, SyntaxToken)> {
|
2020-08-14 08:23:27 -05:00
|
|
|
self.imp.speculative_expand(actual_macro_call, hypothetical_args, token_to_map)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.descend_into_macros(token)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn descend_node_at_offset<N: ast::AstNode>(
|
|
|
|
&self,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
offset: TextSize,
|
|
|
|
) -> Option<N> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.descend_node_at_offset(node, offset).find_map(N::cast)
|
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
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// Find a AstNode by offset inside SyntaxNode, if it is inside *Macrofile*,
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
|
|
|
/// Find a AstNode by offset inside SyntaxNode, if it is inside *MacroCall*,
|
|
|
|
/// descend it and find again
|
|
|
|
pub fn find_node_at_offset_with_descend<N: AstNode>(
|
|
|
|
&self,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
offset: TextSize,
|
|
|
|
) -> Option<N> {
|
2020-07-01 06:32:18 -05:00
|
|
|
if let Some(it) = find_node_at_offset(&node, offset) {
|
|
|
|
return Some(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.imp.descend_node_at_offset(node, offset).find_map(N::cast)
|
2020-07-01 04:43:36 -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)
|
|
|
|
}
|
|
|
|
|
2020-07-01 04:43:36 -05:00
|
|
|
pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.type_of_expr(expr)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
|
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)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
) -> Option<(Field, Option<Local>)> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.resolve_record_field(field)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2020-09-04 20:06:05 -05:00
|
|
|
pub fn resolve_record_pat_field(&self, field: &ast::RecordPatField) -> Option<Field> {
|
|
|
|
self.imp.resolve_record_pat_field(field)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroDef> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.resolve_macro_call(macro_call)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: use this instead?
|
|
|
|
// pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>;
|
|
|
|
|
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> {
|
2020-07-01 06:32:18 -05:00
|
|
|
let src = self.imp.find_file(src.syntax().clone()).with_value(src).cloned();
|
|
|
|
T::to_def(&self.imp, 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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> {
|
2020-07-01 06:32:18 -05:00
|
|
|
self.imp.scope(node)
|
2020-07-01 04:43:36 -05:00
|
|
|
}
|
|
|
|
|
2021-01-30 09:19:21 -06:00
|
|
|
pub fn scope_at_offset(&self, token: &SyntaxToken, offset: TextSize) -> SemanticsScope<'db> {
|
|
|
|
self.imp.scope_at_offset(&token.parent().unwrap(), 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
|
|
|
}
|
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(),
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
|
2020-12-11 07:50:47 -06:00
|
|
|
let sa = self.analyze(macro_call.syntax());
|
|
|
|
let file_id = sa.expand(self.db, InFile::new(sa.file_id, macro_call))?;
|
2020-02-18 11:35:10 -06:00
|
|
|
let node = self.db.parse_or_expand(file_id)?;
|
|
|
|
self.cache(node.clone(), file_id);
|
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
|
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,
|
2020-03-08 05:02:14 -05:00
|
|
|
hypothetical_args: &ast::TokenTree,
|
2020-03-07 08:27:03 -06:00
|
|
|
token_to_map: SyntaxToken,
|
|
|
|
) -> Option<(SyntaxNode, SyntaxToken)> {
|
2020-12-11 07:50:47 -06:00
|
|
|
let sa = self.analyze(actual_macro_call.syntax());
|
|
|
|
let macro_call = InFile::new(sa.file_id, actual_macro_call);
|
2020-06-11 05:08:24 -05:00
|
|
|
let krate = sa.resolver.krate()?;
|
2020-07-01 04:43:36 -05:00
|
|
|
let macro_call_id = macro_call.as_call_id(self.db.upcast(), krate, |path| {
|
|
|
|
sa.resolver.resolve_path_as_macro(self.db.upcast(), &path)
|
|
|
|
})?;
|
|
|
|
hir_expand::db::expand_hypothetical(
|
|
|
|
self.db.upcast(),
|
|
|
|
macro_call_id,
|
|
|
|
hypothetical_args,
|
|
|
|
token_to_map,
|
|
|
|
)
|
2020-03-07 08:27:03 -06:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken {
|
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,
|
|
|
|
None => return token,
|
|
|
|
};
|
2020-12-11 07:50:47 -06:00
|
|
|
let sa = self.analyze(&parent);
|
2020-02-18 11:35:10 -06:00
|
|
|
|
2020-12-11 07:50:47 -06:00
|
|
|
let token = successors(Some(InFile::new(sa.file_id, token)), |token| {
|
2020-07-24 06:51:27 -05:00
|
|
|
self.db.check_canceled();
|
2020-02-18 11:35:10 -06:00
|
|
|
let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?;
|
|
|
|
let tt = macro_call.token_tree()?;
|
2020-04-24 16:40:41 -05:00
|
|
|
if !tt.syntax().text_range().contains_range(token.value.text_range()) {
|
2020-02-18 11:35:10 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let file_id = sa.expand(self.db, token.with_value(¯o_call))?;
|
2020-07-24 07:12:13 -05:00
|
|
|
let token = self
|
|
|
|
.expansion_info_cache
|
|
|
|
.borrow_mut()
|
|
|
|
.entry(file_id)
|
|
|
|
.or_insert_with(|| file_id.expansion_info(self.db.upcast()))
|
|
|
|
.as_ref()?
|
|
|
|
.map_token_down(token.as_ref())?;
|
2020-02-18 11:35:10 -06:00
|
|
|
|
2021-01-30 09:19:21 -06:00
|
|
|
if let Some(parent) = token.value.parent() {
|
|
|
|
self.cache(find_root(&parent), token.file_id);
|
|
|
|
}
|
2020-02-18 11:35:10 -06:00
|
|
|
|
|
|
|
Some(token)
|
|
|
|
})
|
|
|
|
.last()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
token.value
|
|
|
|
}
|
|
|
|
|
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,
|
2020-07-01 06:32:18 -05:00
|
|
|
) -> impl Iterator<Item = SyntaxNode> + '_ {
|
2020-03-22 06:52:14 -05:00
|
|
|
// Handle macro token cases
|
|
|
|
node.token_at_offset(offset)
|
|
|
|
.map(|token| self.descend_into_macros(token))
|
2021-01-30 09:19:21 -06:00
|
|
|
.map(|it| self.token_ancestors_with_macros(it))
|
2020-07-01 06:32:18 -05:00
|
|
|
.flatten()
|
2020-03-22 06:52:14 -05:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn original_range(&self, node: &SyntaxNode) -> FileRange {
|
2020-02-18 11:35:10 -06:00
|
|
|
let node = self.find_file(node.clone());
|
2020-12-08 12:01:27 -06:00
|
|
|
node.as_ref().original_file_range(self.db.upcast())
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2021-02-28 05:12:11 -06:00
|
|
|
fn diagnostics_display_range(&self, src: InFile<SyntaxNodePtr>) -> FileRange {
|
2020-04-17 06:06:02 -05:00
|
|
|
let root = self.db.parse_or_expand(src.file_id).unwrap();
|
|
|
|
let node = src.value.to_node(&root);
|
2020-08-10 16:55:57 -05:00
|
|
|
self.cache(root, src.file_id);
|
2020-12-08 12:01:27 -06:00
|
|
|
src.with_value(&node).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,
|
|
|
|
) -> impl Iterator<Item = SyntaxNode> + '_ {
|
|
|
|
token.parent().into_iter().flat_map(move |parent| self.ancestors_with_macros(parent))
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ {
|
2020-02-18 11:35:10 -06:00
|
|
|
let node = self.find_file(node);
|
2020-07-01 04:43:36 -05:00
|
|
|
node.ancestors_with_macros(self.db.upcast()).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| {
|
2020-12-13 15:13:16 -06:00
|
|
|
let gpl = match_ast! {
|
|
|
|
match syn {
|
|
|
|
ast::Fn(it) => it.generic_param_list()?,
|
|
|
|
ast::TypeAlias(it) => it.generic_param_list()?,
|
|
|
|
ast::Struct(it) => it.generic_param_list()?,
|
|
|
|
ast::Enum(it) => it.generic_param_list()?,
|
|
|
|
ast::Union(it) => it.generic_param_list()?,
|
|
|
|
ast::Trait(it) => it.generic_param_list()?,
|
|
|
|
ast::Impl(it) => it.generic_param_list()?,
|
|
|
|
ast::WherePred(it) => it.generic_param_list()?,
|
|
|
|
ast::ForType(it) => it.generic_param_list()?,
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
gpl.lifetime_params()
|
2020-12-15 12:23:51 -06:00
|
|
|
.find(|tp| tp.lifetime().as_ref().map(|lt| lt.text()) == Some(text))
|
2020-12-13 15:13:16 -06:00
|
|
|
})?;
|
|
|
|
let src = self.find_file(lifetime_param.syntax().clone()).with_value(lifetime_param);
|
|
|
|
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(),
|
|
|
|
ast::EffectExpr(it) => it.label(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
label.filter(|l| {
|
|
|
|
l.lifetime()
|
|
|
|
.and_then(|lt| lt.lifetime_ident_token())
|
|
|
|
.map_or(false, |lt| lt.text() == text)
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
let src = self.find_file(label.syntax().clone()).with_value(label);
|
|
|
|
ToDef::to_def(self, src)
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
|
2020-12-13 15:13:16 -06:00
|
|
|
self.analyze(expr.syntax()).type_of_expr(self.db, expr)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
|
2020-12-13 15:13:16 -06:00
|
|
|
self.analyze(pat.syntax()).type_of_pat(self.db, pat)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type> {
|
2020-12-13 15:13:16 -06:00
|
|
|
self.analyze(param.syntax()).type_of_self(self.db, param)
|
2020-07-10 07:08:35 -05:00
|
|
|
}
|
|
|
|
|
2020-07-16 06:00:56 -05:00
|
|
|
fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<FunctionId> {
|
2020-03-25 07:53:15 -05:00
|
|
|
self.analyze(call.syntax()).resolve_method_call(self.db, call)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-07-16 06:00:56 -05:00
|
|
|
fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> {
|
|
|
|
// FIXME: this erases Substs
|
|
|
|
let func = self.resolve_method_call(call)?;
|
|
|
|
let ty = self.db.value_ty(func.into());
|
|
|
|
let resolver = self.analyze(call.syntax()).resolver;
|
|
|
|
let ty = Type::new_with_resolver(self.db, &resolver, ty.value)?;
|
|
|
|
let mut res = ty.as_callable(self.db)?;
|
|
|
|
res.is_bound_method = true;
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Field> {
|
2020-03-25 07:53:15 -05:00
|
|
|
self.analyze(field.syntax()).resolve_field(self.db, field)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:21:30 -05:00
|
|
|
fn resolve_record_field(&self, field: &ast::RecordExprField) -> Option<(Field, Option<Local>)> {
|
2020-03-02 12:00:38 -06:00
|
|
|
self.analyze(field.syntax()).resolve_record_field(self.db, field)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-09-04 20:06:05 -05:00
|
|
|
fn resolve_record_pat_field(&self, field: &ast::RecordPatField) -> Option<Field> {
|
|
|
|
self.analyze(field.syntax()).resolve_record_pat_field(self.db, field)
|
2020-04-18 15:05:06 -05:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroDef> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let sa = self.analyze(macro_call.syntax());
|
|
|
|
let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call);
|
|
|
|
sa.resolve_macro_call(self.db, macro_call)
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn resolve_path(&self, path: &ast::Path) -> Option<PathResolution> {
|
2020-02-18 11:35:10 -06:00
|
|
|
self.analyze(path.syntax()).resolve_path(self.db, path)
|
|
|
|
}
|
|
|
|
|
2020-08-08 13:14:18 -05:00
|
|
|
fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option<Crate> {
|
|
|
|
let krate = self.scope(extern_crate.syntax()).krate()?;
|
|
|
|
krate.dependencies(self.db).into_iter().find_map(|dep| {
|
|
|
|
if dep.name == extern_crate.name_ref()?.as_name() {
|
|
|
|
Some(dep.krate)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-30 09:21:30 -05:00
|
|
|
fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantId> {
|
2020-06-09 16:11:16 -05:00
|
|
|
self.analyze(record_lit.syntax()).resolve_variant(self.db, record_lit)
|
|
|
|
}
|
|
|
|
|
2020-07-31 13:09:09 -05:00
|
|
|
fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> {
|
2020-02-28 09:36:14 -06:00
|
|
|
self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat)
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
.record_literal_missing_fields(self.db, literal)
|
|
|
|
.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())
|
|
|
|
.record_pattern_missing_fields(self.db, pattern)
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:05:46 -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();
|
|
|
|
let mut ctx = SourceToDefCtx { db: self.db, cache: &mut *cache };
|
|
|
|
f(&mut ctx)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> {
|
2020-12-11 07:50:47 -06:00
|
|
|
let sa = self.analyze(node);
|
|
|
|
SemanticsScope { db: self.db, file_id: sa.file_id, resolver: sa.resolver }
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> {
|
2020-12-11 07:50:47 -06:00
|
|
|
let sa = self.analyze_with_offset(node, offset);
|
|
|
|
SemanticsScope { db: self.db, file_id: sa.file_id, resolver: sa.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
|
|
|
}
|
|
|
|
|
|
|
|
fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer {
|
2020-12-11 07:50:47 -06:00
|
|
|
self.analyze_impl(node, None)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
2020-12-11 07:50:47 -06:00
|
|
|
fn analyze_with_offset(&self, node: &SyntaxNode, offset: TextSize) -> SourceAnalyzer {
|
|
|
|
self.analyze_impl(node, Some(offset))
|
|
|
|
}
|
|
|
|
fn analyze_impl(&self, node: &SyntaxNode, offset: Option<TextSize>) -> SourceAnalyzer {
|
|
|
|
let _p = profile::span("Semantics::analyze_impl");
|
|
|
|
let node = self.find_file(node.clone());
|
|
|
|
let node = node.as_ref();
|
2020-02-18 11:35:10 -06:00
|
|
|
|
2020-12-11 07:50:47 -06:00
|
|
|
let container = match self.with_ctx(|ctx| ctx.find_container(node)) {
|
2020-02-18 11:35:10 -06:00
|
|
|
Some(it) => it,
|
2020-12-11 07:50:47 -06:00
|
|
|
None => return SourceAnalyzer::new_for_resolver(Resolver::default(), node),
|
2020-02-18 11:35:10 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let resolver = match container {
|
|
|
|
ChildContainer::DefWithBodyId(def) => {
|
2020-12-11 07:50:47 -06:00
|
|
|
return SourceAnalyzer::new_for_body(self.db, def, node, offset)
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
2020-07-01 04:43:36 -05:00
|
|
|
ChildContainer::TraitId(it) => it.resolver(self.db.upcast()),
|
|
|
|
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
|
|
|
};
|
2020-12-11 07:50:47 -06:00
|
|
|
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) {
|
2020-02-18 11:35:10 -06:00
|
|
|
self.find_file(node.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lookup(&self, root_node: &SyntaxNode) -> Option<HirFileId> {
|
|
|
|
let cache = self.cache.borrow();
|
|
|
|
cache.get(root_node).copied()
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:31:50 -05:00
|
|
|
fn find_file(&self, node: SyntaxNode) -> InFile<SyntaxNode> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let root_node = find_root(&node);
|
|
|
|
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()
|
|
|
|
.map(|it| format!("{:?}", it))
|
|
|
|
.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
|
|
|
};
|
|
|
|
let ty = self.type_of_expr(&field_expr.expr()?)?;
|
|
|
|
if !ty.is_packed(self.db) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
.map(|ty| ty.is_packed(self.db))
|
|
|
|
.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 {
|
2020-08-07 09:40:09 -05:00
|
|
|
if !ident_pat.ref_token().is_some() {
|
|
|
|
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.
|
|
|
|
.map(|ty| ty.is_packed(self.db))
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-07-01 04:43:36 -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;
|
2020-07-01 04:43:36 -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),
|
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),
|
2020-12-15 08:37:37 -06:00
|
|
|
(crate::MacroDef, ast::MacroRules, macro_rules_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),
|
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()
|
|
|
|
}
|
|
|
|
|
2020-09-21 05:30:55 -05:00
|
|
|
/// `SemanticScope` encapsulates the notion of a scope (the set of visible
|
|
|
|
/// 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> {
|
2020-02-18 11:35:10 -06:00
|
|
|
pub fn module(&self) -> Option<Module> {
|
|
|
|
Some(Module { id: self.resolver.module()? })
|
|
|
|
}
|
|
|
|
|
2020-08-08 13:14:18 -05:00
|
|
|
pub fn krate(&self) -> Option<Crate> {
|
|
|
|
Some(Crate { id: self.resolver.krate()? })
|
|
|
|
}
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
/// Note: `FxHashSet<TraitId>` should be treated as an opaque type, passed into `Type
|
|
|
|
// FIXME: rename to visible_traits to not repeat scope?
|
|
|
|
pub fn traits_in_scope(&self) -> FxHashSet<TraitId> {
|
|
|
|
let resolver = &self.resolver;
|
2020-07-01 01:34:45 -05:00
|
|
|
resolver.traits_in_scope(self.db.upcast())
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
|
|
|
|
let resolver = &self.resolver;
|
|
|
|
|
2020-07-01 01:34:45 -05:00
|
|
|
resolver.process_all_names(self.db.upcast(), &mut |name, def| {
|
2020-02-18 11:35:10 -06:00
|
|
|
let def = match def {
|
2020-03-10 21:58:17 -05:00
|
|
|
resolver::ScopeDef::PerNs(it) => {
|
|
|
|
let items = ScopeDef::all_items(it);
|
|
|
|
for item in items {
|
|
|
|
f(name.clone(), item);
|
|
|
|
}
|
2020-03-13 06:28:13 -05:00
|
|
|
return;
|
|
|
|
}
|
2020-02-18 11:35:10 -06:00
|
|
|
resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
|
|
|
|
resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
|
2021-01-19 13:01:49 -06:00
|
|
|
resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(id.into()),
|
2020-02-18 11:35:10 -06:00
|
|
|
resolver::ScopeDef::Local(pat_id) => {
|
2021-03-16 19:27:56 -05:00
|
|
|
let parent = resolver.body_owner().unwrap();
|
2020-02-18 11:35:10 -06:00
|
|
|
ScopeDef::Local(Local { parent, pat_id })
|
|
|
|
}
|
2021-03-20 18:59:45 -05:00
|
|
|
resolver::ScopeDef::Label(label_id) => {
|
|
|
|
let parent = resolver.body_owner().unwrap();
|
|
|
|
ScopeDef::Label(Label { parent, label_id })
|
|
|
|
}
|
2020-02-18 11:35:10 -06:00
|
|
|
};
|
|
|
|
f(name, def)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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> {
|
2020-08-13 16:52:14 -05:00
|
|
|
let hygiene = Hygiene::new(self.db.upcast(), self.file_id);
|
|
|
|
let path = Path::from_src(path.clone(), &hygiene)?;
|
2020-08-15 11:50:41 -05:00
|
|
|
resolve_hir_path(self.db, &self.resolver, &path)
|
2020-05-15 16:23:49 -05:00
|
|
|
}
|
2020-02-18 11:35:10 -06:00
|
|
|
}
|