migrate ra_hir to the new rowan

This commit is contained in:
Aleksey Kladov 2019-07-19 10:43:01 +03:00
parent 7e02aa0eff
commit e2b28f5bb8
22 changed files with 206 additions and 210 deletions

View file

@ -4,10 +4,7 @@
use std::sync::Arc;
use ra_arena::{impl_arena_id, Arena, RawId};
use ra_syntax::{
ast::{self, NameOwner, StructKind, TypeAscriptionOwner},
TreeArc,
};
use ra_syntax::ast::{self, NameOwner, StructKind, TypeAscriptionOwner};
use crate::{
type_ref::TypeRef, AsName, AstDatabase, Crate, DefDatabase, Enum, EnumVariant, FieldSource,
@ -59,11 +56,11 @@ impl StructData {
struct_: Struct,
) -> Arc<StructData> {
let src = struct_.source(db);
Arc::new(StructData::new(&*src.ast))
Arc::new(StructData::new(&src.ast))
}
}
fn variants(enum_def: &ast::EnumDef) -> impl Iterator<Item = &ast::EnumVariant> {
fn variants(enum_def: &ast::EnumDef) -> impl Iterator<Item = ast::EnumVariant> {
enum_def.variant_list().into_iter().flat_map(|it| it.variants())
}
@ -71,9 +68,9 @@ impl EnumVariant {
pub(crate) fn source_impl(
self,
db: &(impl DefDatabase + AstDatabase),
) -> Source<TreeArc<ast::EnumVariant>> {
) -> Source<ast::EnumVariant> {
let src = self.parent.source(db);
let ast = variants(&*src.ast)
let ast = variants(&src.ast)
.zip(db.enum_data(self.parent).variants.iter())
.find(|(_syntax, (id, _))| *id == self.id)
.unwrap()
@ -96,7 +93,7 @@ impl EnumData {
pub(crate) fn enum_data_query(db: &(impl DefDatabase + AstDatabase), e: Enum) -> Arc<EnumData> {
let src = e.source(db);
let name = src.ast.name().map(|n| n.as_name());
let variants = variants(&*src.ast)
let variants = variants(&src.ast)
.map(|var| EnumVariantData {
name: var.name().map(|it| it.as_name()),
variant_data: Arc::new(VariantData::new(var.kind())),

View file

@ -4,10 +4,7 @@ pub(crate) mod docs;
use std::sync::Arc;
use ra_db::{CrateId, Edition, FileId, SourceRootId};
use ra_syntax::{
ast::{self, NameOwner, TypeAscriptionOwner},
TreeArc,
};
use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
use crate::{
adt::{EnumVariantId, StructFieldId, VariantDef},
@ -155,8 +152,8 @@ impl_froms!(
);
pub enum ModuleSource {
SourceFile(TreeArc<ast::SourceFile>),
Module(TreeArc<ast::Module>),
SourceFile(ast::SourceFile),
Module(ast::Module),
}
impl ModuleSource {
@ -199,7 +196,7 @@ impl Module {
self,
db: &impl HirDatabase,
import: ImportId,
) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> {
) -> Either<ast::UseTree, ast::ExternCrateItem> {
let src = self.definition_source(db);
let (_, source_map) = db.raw_items_with_source_map(src.file_id);
source_map.get(&src.ast, import)
@ -321,8 +318,8 @@ pub struct StructField {
#[derive(Debug)]
pub enum FieldSource {
Named(TreeArc<ast::NamedFieldDef>),
Pos(TreeArc<ast::PosFieldDef>),
Named(ast::NamedFieldDef),
Pos(ast::PosFieldDef),
}
impl StructField {
@ -736,7 +733,7 @@ impl ConstData {
konst: Const,
) -> Arc<ConstData> {
let node = konst.source(db).ast;
const_data_for(&*node)
const_data_for(&node)
}
pub(crate) fn static_data_query(
@ -744,7 +741,7 @@ impl ConstData {
konst: Static,
) -> Arc<ConstData> {
let node = konst.source(db).ast;
const_data_for(&*node)
const_data_for(&node)
}
}

View file

@ -71,21 +71,21 @@ pub(crate) fn documentation_query(
def: DocDef,
) -> Option<Documentation> {
match def {
DocDef::Module(it) => docs_from_ast(&*it.declaration_source(db)?.ast),
DocDef::Module(it) => docs_from_ast(&it.declaration_source(db)?.ast),
DocDef::StructField(it) => match it.source(db).ast {
FieldSource::Named(named) => docs_from_ast(&*named),
FieldSource::Named(named) => docs_from_ast(&named),
FieldSource::Pos(..) => None,
},
DocDef::Struct(it) => docs_from_ast(&*it.source(db).ast),
DocDef::Enum(it) => docs_from_ast(&*it.source(db).ast),
DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).ast),
DocDef::Static(it) => docs_from_ast(&*it.source(db).ast),
DocDef::Const(it) => docs_from_ast(&*it.source(db).ast),
DocDef::Function(it) => docs_from_ast(&*it.source(db).ast),
DocDef::Union(it) => docs_from_ast(&*it.source(db).ast),
DocDef::Trait(it) => docs_from_ast(&*it.source(db).ast),
DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).ast),
DocDef::MacroDef(it) => docs_from_ast(&*it.source(db).ast),
DocDef::Struct(it) => docs_from_ast(&it.source(db).ast),
DocDef::Enum(it) => docs_from_ast(&it.source(db).ast),
DocDef::EnumVariant(it) => docs_from_ast(&it.source(db).ast),
DocDef::Static(it) => docs_from_ast(&it.source(db).ast),
DocDef::Const(it) => docs_from_ast(&it.source(db).ast),
DocDef::Function(it) => docs_from_ast(&it.source(db).ast),
DocDef::Union(it) => docs_from_ast(&it.source(db).ast),
DocDef::Trait(it) => docs_from_ast(&it.source(db).ast),
DocDef::TypeAlias(it) => docs_from_ast(&it.source(db).ast),
DocDef::MacroDef(it) => docs_from_ast(&it.source(db).ast),
}
}

View file

@ -1,4 +1,4 @@
use ra_syntax::{ast, TreeArc};
use ra_syntax::ast;
use crate::{
ids::AstItemDef, AstDatabase, Const, DefDatabase, Enum, EnumVariant, FieldSource, Function,
@ -34,7 +34,7 @@ impl Module {
pub fn declaration_source(
self,
db: &(impl DefDatabase + AstDatabase),
) -> Option<Source<TreeArc<ast::Module>>> {
) -> Option<Source<ast::Module>> {
let def_map = db.crate_def_map(self.krate);
let decl = def_map[self.module_id].declaration?;
let ast = decl.to_node(db);
@ -49,62 +49,62 @@ impl HasSource for StructField {
}
}
impl HasSource for Struct {
type Ast = TreeArc<ast::StructDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
type Ast = ast::StructDef;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StructDef> {
self.id.source(db)
}
}
impl HasSource for Union {
type Ast = TreeArc<ast::StructDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
type Ast = ast::StructDef;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StructDef> {
self.id.source(db)
}
}
impl HasSource for Enum {
type Ast = TreeArc<ast::EnumDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumDef>> {
type Ast = ast::EnumDef;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::EnumDef> {
self.id.source(db)
}
}
impl HasSource for EnumVariant {
type Ast = TreeArc<ast::EnumVariant>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumVariant>> {
type Ast = ast::EnumVariant;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::EnumVariant> {
self.source_impl(db)
}
}
impl HasSource for Function {
type Ast = TreeArc<ast::FnDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::FnDef>> {
type Ast = ast::FnDef;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::FnDef> {
self.id.source(db)
}
}
impl HasSource for Const {
type Ast = TreeArc<ast::ConstDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ConstDef>> {
type Ast = ast::ConstDef;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::ConstDef> {
self.id.source(db)
}
}
impl HasSource for Static {
type Ast = TreeArc<ast::StaticDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StaticDef>> {
type Ast = ast::StaticDef;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StaticDef> {
self.id.source(db)
}
}
impl HasSource for Trait {
type Ast = TreeArc<ast::TraitDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TraitDef>> {
type Ast = ast::TraitDef;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::TraitDef> {
self.id.source(db)
}
}
impl HasSource for TypeAlias {
type Ast = TreeArc<ast::TypeAliasDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TypeAliasDef>> {
type Ast = ast::TypeAliasDef;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::TypeAliasDef> {
self.id.source(db)
}
}
impl HasSource for MacroDef {
type Ast = TreeArc<ast::MacroCall>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::MacroCall>> {
type Ast = ast::MacroCall;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::MacroCall> {
Source { file_id: self.id.0.file_id(), ast: self.id.0.to_node(db) }
}
}

View file

@ -2,7 +2,7 @@ use std::sync::Arc;
use parking_lot::Mutex;
use ra_db::{salsa, SourceDatabase};
use ra_syntax::{ast, Parse, SmolStr, SyntaxNode, TreeArc};
use ra_syntax::{ast, Parse, SmolStr, SyntaxNode};
use crate::{
adt::{EnumData, StructData},
@ -62,11 +62,11 @@ pub trait AstDatabase: InternDatabase {
#[salsa::transparent]
#[salsa::invoke(crate::source_id::AstIdMap::file_item_query)]
fn ast_id_to_node(&self, file_id: HirFileId, ast_id: ErasedFileAstId) -> TreeArc<SyntaxNode>;
fn ast_id_to_node(&self, file_id: HirFileId, ast_id: ErasedFileAstId) -> SyntaxNode;
#[salsa::transparent]
#[salsa::invoke(crate::ids::HirFileId::parse_or_expand_query)]
fn parse_or_expand(&self, file_id: HirFileId) -> Option<TreeArc<SyntaxNode>>;
fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
#[salsa::invoke(crate::ids::HirFileId::parse_macro_query)]
fn parse_macro(&self, macro_file: ids::MacroFile) -> Option<Parse<SyntaxNode>>;

View file

@ -1,6 +1,6 @@
use std::{any::Any, fmt};
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TreeArc};
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange};
use relative_path::RelativePathBuf;
use crate::{HirDatabase, HirFileId, Name};
@ -33,9 +33,9 @@ pub trait AstDiagnostic {
}
impl dyn Diagnostic {
pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> {
pub fn syntax_node(&self, db: &impl HirDatabase) -> SyntaxNode {
let node = db.parse_or_expand(self.file()).unwrap();
self.syntax_node_ptr().to_node(&*node).to_owned()
self.syntax_node_ptr().to_node(&node)
}
pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
@ -143,11 +143,11 @@ impl Diagnostic for MissingFields {
}
impl AstDiagnostic for MissingFields {
type AST = TreeArc<ast::NamedFieldList>;
type AST = ast::NamedFieldList;
fn ast(&self, db: &impl HirDatabase) -> Self::AST {
let root = db.parse_or_expand(self.file()).unwrap();
let node = self.syntax_node_ptr().to_node(&*root);
ast::NamedFieldList::cast(&node).unwrap().to_owned()
let node = self.syntax_node_ptr().to_node(&root);
ast::NamedFieldList::cast(node).unwrap()
}
}

View file

@ -550,7 +550,7 @@ where
self.exprs.alloc(block)
}
fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId {
fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
let syntax_ptr = SyntaxNodePtr::new(expr.syntax());
match expr.kind() {
ast::ExprKind::IfExpr(e) => {
@ -565,7 +565,8 @@ where
.map(|b| match b {
ast::ElseBranch::Block(it) => self.collect_block(it),
ast::ElseBranch::IfExpr(elif) => {
let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap();
let expr: ast::Expr =
ast::Expr::cast(elif.syntax().clone()).unwrap();
self.collect_expr(expr)
}
})
@ -582,7 +583,7 @@ where
let else_branch = e.else_branch().map(|b| match b {
ast::ElseBranch::Block(it) => self.collect_block(it),
ast::ElseBranch::IfExpr(elif) => {
let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap();
let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap();
self.collect_expr(expr)
}
});
@ -689,7 +690,7 @@ where
let struct_lit = if let Some(nfl) = e.named_field_list() {
let fields = nfl
.fields()
.inspect(|field| field_ptrs.push(AstPtr::new(*field)))
.inspect(|field| field_ptrs.push(AstPtr::new(field)))
.map(|field| StructLitField {
name: field
.name_ref()
@ -699,7 +700,7 @@ where
self.collect_expr(e)
} else if let Some(nr) = field.name_ref() {
// field shorthand
let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(nr)));
let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(&nr)));
self.source_map
.expr_map
.insert(SyntaxNodePtr::new(nr.syntax()), id);
@ -837,7 +838,7 @@ where
let ast_id = self
.db
.ast_id_map(self.current_file_id)
.ast_id(e)
.ast_id(&e)
.with_file_id(self.current_file_id);
if let Some(path) = e.path().and_then(Path::from_ast) {
@ -845,11 +846,11 @@ where
let call_id = MacroCallLoc { def: def.id, ast_id }.id(self.db);
let file_id = call_id.as_file(MacroFileKind::Expr);
if let Some(node) = self.db.parse_or_expand(file_id) {
if let Some(expr) = ast::Expr::cast(&*node) {
if let Some(expr) = ast::Expr::cast(node) {
log::debug!("macro expansion {}", expr.syntax().debug_dump());
let old_file_id =
std::mem::replace(&mut self.current_file_id, file_id);
let id = self.collect_expr(&expr);
let id = self.collect_expr(expr);
self.current_file_id = old_file_id;
return id;
}
@ -863,7 +864,7 @@ where
}
}
fn collect_expr_opt(&mut self, expr: Option<&ast::Expr>) -> ExprId {
fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
if let Some(expr) = expr {
self.collect_expr(expr)
} else {
@ -871,7 +872,7 @@ where
}
}
fn collect_block(&mut self, block: &ast::Block) -> ExprId {
fn collect_block(&mut self, block: ast::Block) -> ExprId {
let statements = block
.statements()
.map(|s| match s.kind() {
@ -890,7 +891,7 @@ where
self.alloc_expr(Expr::Block { statements, tail }, SyntaxNodePtr::new(block.syntax()))
}
fn collect_block_opt(&mut self, block: Option<&ast::Block>) -> ExprId {
fn collect_block_opt(&mut self, block: Option<ast::Block>) -> ExprId {
if let Some(block) = block {
self.collect_block(block)
} else {
@ -898,7 +899,7 @@ where
}
}
fn collect_pat(&mut self, pat: &ast::Pat) -> PatId {
fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
let pattern = match pat.kind() {
ast::PatKind::BindPat(bp) => {
let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
@ -932,7 +933,8 @@ where
let mut fields: Vec<_> = field_pat_list
.bind_pats()
.filter_map(|bind_pat| {
let ast_pat = ast::Pat::cast(bind_pat.syntax()).expect("bind pat is a pat");
let ast_pat =
ast::Pat::cast(bind_pat.syntax().clone()).expect("bind pat is a pat");
let pat = self.collect_pat(ast_pat);
let name = bind_pat.name()?.as_name();
Some(FieldPat { name, pat })
@ -953,11 +955,11 @@ where
ast::PatKind::LiteralPat(_) => Pat::Missing,
ast::PatKind::SlicePat(_) | ast::PatKind::RangePat(_) => Pat::Missing,
};
let ptr = AstPtr::new(pat);
let ptr = AstPtr::new(&pat);
self.alloc_pat(pattern, Either::A(ptr))
}
fn collect_pat_opt(&mut self, pat: Option<&ast::Pat>) -> PatId {
fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
if let Some(pat) = pat {
self.collect_pat(pat)
} else {
@ -965,20 +967,20 @@ where
}
}
fn collect_const_body(&mut self, node: &ast::ConstDef) {
fn collect_const_body(&mut self, node: ast::ConstDef) {
let body = self.collect_expr_opt(node.body());
self.body_expr = Some(body);
}
fn collect_static_body(&mut self, node: &ast::StaticDef) {
fn collect_static_body(&mut self, node: ast::StaticDef) {
let body = self.collect_expr_opt(node.body());
self.body_expr = Some(body);
}
fn collect_fn_body(&mut self, node: &ast::FnDef) {
fn collect_fn_body(&mut self, node: ast::FnDef) {
if let Some(param_list) = node.param_list() {
if let Some(self_param) = param_list.self_param() {
let ptr = AstPtr::new(self_param);
let ptr = AstPtr::new(&self_param);
let param_pat = self.alloc_pat(
Pat::Bind {
name: SELF_PARAM,
@ -1027,17 +1029,17 @@ pub(crate) fn body_with_source_map_query(
DefWithBody::Const(ref c) => {
let src = c.source(db);
collector = ExprCollector::new(def, src.file_id, def.resolver(db), db);
collector.collect_const_body(&src.ast)
collector.collect_const_body(src.ast)
}
DefWithBody::Function(ref f) => {
let src = f.source(db);
collector = ExprCollector::new(def, src.file_id, def.resolver(db), db);
collector.collect_fn_body(&src.ast)
collector.collect_fn_body(src.ast)
}
DefWithBody::Static(ref s) => {
let src = s.source(db);
collector = ExprCollector::new(def, src.file_id, def.resolver(db), db);
collector.collect_static_body(&src.ast)
collector.collect_static_body(src.ast)
}
}

View file

@ -190,7 +190,7 @@ mod tests {
let (db, _source_root, file_id) = MockDatabase::with_single_file(&code);
let file = db.parse(file_id).ok().unwrap();
let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap();
let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap();
let analyzer = SourceAnalyzer::new(&db, file_id, marker.syntax(), None);
let scopes = analyzer.scopes();
@ -290,10 +290,10 @@ mod tests {
let file = db.parse(file_id).ok().unwrap();
let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into())
.expect("failed to find a name at the target offset");
let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap();
let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap();
let analyzer = SourceAnalyzer::new(&db, file_id, name_ref.syntax(), None);
let local_name_entry = analyzer.resolve_local_name(name_ref).unwrap();
let local_name_entry = analyzer.resolve_local_name(&name_ref).unwrap();
let local_name =
local_name_entry.ptr().either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr());
assert_eq!(local_name.range(), expected_name.syntax().range());

View file

@ -79,7 +79,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
.and_then(StructLit::cast)
.and_then(|lit| lit.named_field_list())
{
let field_list_ptr = AstPtr::new(field_list_node);
let field_list_ptr = AstPtr::new(&field_list_node);
self.sink.push(MissingFields {
file: file_id,
field_list: field_list_ptr,

View file

@ -76,17 +76,17 @@ impl GenericParams {
generics.parent_params = parent.map(|p| db.generic_params(p));
let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32;
match def {
GenericDef::Function(it) => generics.fill(&*it.source(db).ast, start),
GenericDef::Struct(it) => generics.fill(&*it.source(db).ast, start),
GenericDef::Union(it) => generics.fill(&*it.source(db).ast, start),
GenericDef::Enum(it) => generics.fill(&*it.source(db).ast, start),
GenericDef::Function(it) => generics.fill(&it.source(db).ast, start),
GenericDef::Struct(it) => generics.fill(&it.source(db).ast, start),
GenericDef::Union(it) => generics.fill(&it.source(db).ast, start),
GenericDef::Enum(it) => generics.fill(&it.source(db).ast, start),
GenericDef::Trait(it) => {
// traits get the Self type as an implicit first type parameter
generics.params.push(GenericParam { idx: start, name: SELF_TYPE, default: None });
generics.fill(&*it.source(db).ast, start + 1);
generics.fill(&it.source(db).ast, start + 1);
}
GenericDef::TypeAlias(it) => generics.fill(&*it.source(db).ast, start),
GenericDef::ImplBlock(it) => generics.fill(&*it.source(db).ast, start),
GenericDef::TypeAlias(it) => generics.fill(&it.source(db).ast, start),
GenericDef::ImplBlock(it) => generics.fill(&it.source(db).ast, start),
GenericDef::EnumVariant(_) => {}
}
@ -102,9 +102,9 @@ impl GenericParams {
}
}
fn fill_params(&mut self, params: &ast::TypeParamList, start: u32) {
fn fill_params(&mut self, params: ast::TypeParamList, start: u32) {
for (idx, type_param) in params.type_params().enumerate() {
let name = type_param.name().map(AsName::as_name).unwrap_or_else(Name::missing);
let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
let default = type_param.default_type().and_then(|t| t.path()).and_then(Path::from_ast);
let param = GenericParam { idx: idx as u32 + start, name: name.clone(), default };
@ -121,7 +121,7 @@ impl GenericParams {
}
}
fn fill_where_predicates(&mut self, where_clause: &ast::WhereClause) {
fn fill_where_predicates(&mut self, where_clause: ast::WhereClause) {
for pred in where_clause.predicates() {
let type_ref = match pred.type_ref() {
Some(type_ref) => type_ref,
@ -134,7 +134,7 @@ impl GenericParams {
}
}
fn add_where_predicate_from_bound(&mut self, bound: &ast::TypeBound, type_ref: TypeRef) {
fn add_where_predicate_from_bound(&mut self, bound: ast::TypeBound, type_ref: TypeRef) {
let path = bound
.type_ref()
.and_then(|tr| match tr.kind() {

View file

@ -6,7 +6,7 @@ use std::{
use mbe::MacroRules;
use ra_db::{salsa, FileId};
use ra_prof::profile;
use ra_syntax::{ast, AstNode, Parse, SyntaxNode, TreeArc};
use ra_syntax::{ast, AstNode, Parse, SyntaxNode};
use crate::{AstDatabase, AstId, DefDatabase, FileAstId, InternDatabase, Module, Source};
@ -58,11 +58,11 @@ impl HirFileId {
pub(crate) fn parse_or_expand_query(
db: &impl AstDatabase,
file_id: HirFileId,
) -> Option<TreeArc<SyntaxNode>> {
) -> Option<SyntaxNode> {
match file_id.0 {
HirFileIdRepr::File(file_id) => Some(db.parse(file_id).tree().syntax().to_owned()),
HirFileIdRepr::File(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
HirFileIdRepr::Macro(macro_file) => {
db.parse_macro(macro_file).map(|it| it.tree().to_owned())
db.parse_macro(macro_file).map(|it| it.syntax_node())
}
}
}
@ -123,7 +123,7 @@ pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>);
pub(crate) fn macro_def_query(db: &impl AstDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> {
let macro_call = id.0.to_node(db);
let arg = macro_call.token_tree()?;
let (tt, _) = mbe::ast_to_token_tree(arg).or_else(|| {
let (tt, _) = mbe::ast_to_token_tree(&arg).or_else(|| {
log::warn!("fail on macro_def to token tree: {:#?}", arg);
None
})?;
@ -138,7 +138,7 @@ pub(crate) fn macro_arg_query(db: &impl AstDatabase, id: MacroCallId) -> Option<
let loc = id.loc(db);
let macro_call = loc.ast_id.to_node(db);
let arg = macro_call.token_tree()?;
let (tt, _) = mbe::ast_to_token_tree(arg)?;
let (tt, _) = mbe::ast_to_token_tree(&arg)?;
Some(Arc::new(tt))
}
@ -262,7 +262,7 @@ pub(crate) trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) };
Self::intern(ctx.db, loc)
}
fn source(self, db: &(impl AstDatabase + DefDatabase)) -> Source<TreeArc<N>> {
fn source(self, db: &(impl AstDatabase + DefDatabase)) -> Source<N> {
let loc = self.lookup_intern(db);
let ast = loc.ast_id.to_node(db);
Source { file_id: loc.ast_id.file_id(), ast }

View file

@ -4,7 +4,7 @@ use std::sync::Arc;
use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId};
use ra_syntax::{
ast::{self, AstNode},
AstPtr, SourceFile, TreeArc,
AstPtr, SourceFile,
};
use crate::{
@ -28,9 +28,9 @@ impl ImplSourceMap {
self.map.insert(impl_id, AstPtr::new(impl_block))
}
pub fn get(&self, source: &ModuleSource, impl_id: ImplId) -> TreeArc<ast::ImplBlock> {
pub fn get(&self, source: &ModuleSource, impl_id: ImplId) -> ast::ImplBlock {
let file = match source {
ModuleSource::SourceFile(file) => &*file,
ModuleSource::SourceFile(file) => file.clone(),
ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
};
@ -45,8 +45,8 @@ pub struct ImplBlock {
}
impl HasSource for ImplBlock {
type Ast = TreeArc<ast::ImplBlock>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> {
type Ast = ast::ImplBlock;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::ImplBlock> {
let source_map = db.impls_in_module_with_source_map(self.module).1;
let src = self.module.definition_source(db);
Source { file_id: src.file_id, ast: source_map.get(&src.ast, self.impl_id) }
@ -132,9 +132,9 @@ impl ImplData {
item_list
.impl_items()
.map(|item_node| match item_node.kind() {
ast::ImplItemKind::FnDef(it) => Function { id: ctx.to_def(it) }.into(),
ast::ImplItemKind::ConstDef(it) => Const { id: ctx.to_def(it) }.into(),
ast::ImplItemKind::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(it) }.into(),
ast::ImplItemKind::FnDef(it) => Function { id: ctx.to_def(&it) }.into(),
ast::ImplItemKind::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(),
ast::ImplItemKind::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(),
})
.collect()
} else {
@ -202,20 +202,20 @@ impl ModuleImplBlocks {
let src = m.module.definition_source(db);
let node = match &src.ast {
ModuleSource::SourceFile(node) => node.syntax(),
ModuleSource::SourceFile(node) => node.syntax().clone(),
ModuleSource::Module(node) => {
node.item_list().expect("inline module should have item list").syntax()
node.item_list().expect("inline module should have item list").syntax().clone()
}
};
for impl_block_ast in node.children().filter_map(ast::ImplBlock::cast) {
let impl_block = ImplData::from_ast(db, src.file_id, m.module, impl_block_ast);
let impl_block = ImplData::from_ast(db, src.file_id, m.module, &impl_block_ast);
let id = m.impls.alloc(impl_block);
for &impl_item in &m.impls[id].items {
m.impls_by_def.insert(impl_item, id);
}
source_map.insert(id, impl_block_ast);
source_map.insert(id, &impl_block_ast);
}
m

View file

@ -1,7 +1,7 @@
use rustc_hash::FxHashMap;
use std::sync::Arc;
use ra_syntax::{ast::AttrsOwner, SmolStr, TreeArc};
use ra_syntax::{ast::AttrsOwner, SmolStr};
use crate::{
AstDatabase, Crate, DefDatabase, Enum, Function, HasSource, HirDatabase, ImplBlock, Module,
@ -95,7 +95,7 @@ impl LangItems {
// Look for impl targets
for impl_block in module.impl_blocks(db) {
let src = impl_block.source(db);
if let Some(lang_item_name) = lang_item_name(&*src.ast) {
if let Some(lang_item_name) = lang_item_name(&src.ast) {
self.items
.entry(lang_item_name)
.or_insert_with(|| LangItemTarget::ImplBlock(impl_block));
@ -137,11 +137,11 @@ impl LangItems {
item: T,
constructor: fn(T) -> LangItemTarget,
) where
T: Copy + HasSource<Ast = TreeArc<N>>,
T: Copy + HasSource<Ast = N>,
N: AttrsOwner,
{
let node = item.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
if let Some(lang_item_name) = lang_item_name(&node) {
self.items.entry(lang_item_name).or_insert_with(|| constructor(item));
}
}

View file

@ -75,7 +75,7 @@ impl AsName for ast::Name {
}
}
impl<'a> AsName for ast::FieldKind<'a> {
impl AsName for ast::FieldKind {
fn as_name(&self) -> Name {
match self {
ast::FieldKind::Name(nr) => nr.as_name(),

View file

@ -3,7 +3,7 @@ use std::{ops::Index, sync::Arc};
use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId};
use ra_syntax::{
ast::{self, AttrsOwner, NameOwner},
AstNode, AstPtr, SmolStr, SourceFile, TreeArc,
AstNode, AstPtr, SmolStr, SourceFile,
};
use test_utils::tested_by;
@ -32,7 +32,7 @@ pub struct ImportSourceMap {
}
type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>;
type ImportSource = Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>>;
type ImportSource = Either<ast::UseTree, ast::ExternCrateItem>;
impl ImportSourcePtr {
fn to_node(self, file: &SourceFile) -> ImportSource {
@ -50,11 +50,11 @@ impl ImportSourceMap {
pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> ImportSource {
let file = match source {
ModuleSource::SourceFile(file) => &*file,
ModuleSource::SourceFile(file) => file.clone(),
ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
};
self.map[import].to_node(file)
self.map[import].to_node(&file)
}
}
@ -76,8 +76,8 @@ impl RawItems {
source_map: ImportSourceMap::default(),
};
if let Some(node) = db.parse_or_expand(file_id) {
if let Some(source_file) = ast::SourceFile::cast(&node) {
collector.process_module(None, &*source_file);
if let Some(source_file) = ast::SourceFile::cast(node) {
collector.process_module(None, source_file);
}
}
(Arc::new(collector.raw_items), Arc::new(collector.source_map))
@ -188,7 +188,7 @@ struct RawItemsCollector {
}
impl RawItemsCollector {
fn process_module(&mut self, current_module: Option<Module>, body: &impl ast::ModuleItemOwner) {
fn process_module(&mut self, current_module: Option<Module>, body: impl ast::ModuleItemOwner) {
for item_or_macro in body.items_with_macros() {
match item_or_macro {
ast::ItemOrMacro::Macro(m) => self.add_macro(current_module, m),
@ -197,7 +197,7 @@ impl RawItemsCollector {
}
}
fn add_item(&mut self, current_module: Option<Module>, item: &ast::ModuleItem) {
fn add_item(&mut self, current_module: Option<Module>, item: ast::ModuleItem) {
let (kind, name) = match item.kind() {
ast::ModuleItemKind::Module(module) => {
self.add_module(current_module, module);
@ -216,7 +216,7 @@ impl RawItemsCollector {
return;
}
ast::ModuleItemKind::StructDef(it) => {
let id = self.source_ast_id_map.ast_id(it);
let id = self.source_ast_id_map.ast_id(&it);
let name = it.name();
if it.is_union() {
(DefKind::Union(id), name)
@ -225,22 +225,22 @@ impl RawItemsCollector {
}
}
ast::ModuleItemKind::EnumDef(it) => {
(DefKind::Enum(self.source_ast_id_map.ast_id(it)), it.name())
(DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItemKind::FnDef(it) => {
(DefKind::Function(self.source_ast_id_map.ast_id(it)), it.name())
(DefKind::Function(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItemKind::TraitDef(it) => {
(DefKind::Trait(self.source_ast_id_map.ast_id(it)), it.name())
(DefKind::Trait(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItemKind::TypeAliasDef(it) => {
(DefKind::TypeAlias(self.source_ast_id_map.ast_id(it)), it.name())
(DefKind::TypeAlias(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItemKind::ConstDef(it) => {
(DefKind::Const(self.source_ast_id_map.ast_id(it)), it.name())
(DefKind::Const(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItemKind::StaticDef(it) => {
(DefKind::Static(self.source_ast_id_map.ast_id(it)), it.name())
(DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name())
}
};
if let Some(name) = name {
@ -250,14 +250,14 @@ impl RawItemsCollector {
}
}
fn add_module(&mut self, current_module: Option<Module>, module: &ast::Module) {
fn add_module(&mut self, current_module: Option<Module>, module: ast::Module) {
let name = match module.name() {
Some(it) => it.as_name(),
None => return,
};
let attr_path = extract_mod_path_attribute(module);
let ast_id = self.source_ast_id_map.ast_id(module);
let attr_path = extract_mod_path_attribute(&module);
let ast_id = self.source_ast_id_map.ast_id(&module);
if module.has_semi() {
let item =
self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id, attr_path });
@ -278,10 +278,10 @@ impl RawItemsCollector {
tested_by!(name_res_works_for_broken_modules);
}
fn add_use_item(&mut self, current_module: Option<Module>, use_item: &ast::UseItem) {
fn add_use_item(&mut self, current_module: Option<Module>, use_item: ast::UseItem) {
let is_prelude = use_item.has_atom_attr("prelude_import");
Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| {
Path::expand_use_item(&use_item, |path, use_tree, is_glob, alias| {
let import_data =
ImportData { path, alias, is_glob, is_prelude, is_extern_crate: false };
self.push_import(current_module, import_data, Either::A(AstPtr::new(use_tree)));
@ -291,11 +291,11 @@ impl RawItemsCollector {
fn add_extern_crate_item(
&mut self,
current_module: Option<Module>,
extern_crate: &ast::ExternCrateItem,
extern_crate: ast::ExternCrateItem,
) {
if let Some(name_ref) = extern_crate.name_ref() {
let path = Path::from_name_ref(name_ref);
let alias = extern_crate.alias().and_then(|a| a.name()).map(AsName::as_name);
let path = Path::from_name_ref(&name_ref);
let alias = extern_crate.alias().and_then(|a| a.name()).map(|it| it.as_name());
let import_data = ImportData {
path,
alias,
@ -303,18 +303,18 @@ impl RawItemsCollector {
is_prelude: false,
is_extern_crate: true,
};
self.push_import(current_module, import_data, Either::B(AstPtr::new(extern_crate)));
self.push_import(current_module, import_data, Either::B(AstPtr::new(&extern_crate)));
}
}
fn add_macro(&mut self, current_module: Option<Module>, m: &ast::MacroCall) {
fn add_macro(&mut self, current_module: Option<Module>, m: ast::MacroCall) {
let path = match m.path().and_then(Path::from_ast) {
Some(it) => it,
_ => return,
};
let name = m.name().map(|it| it.as_name());
let ast_id = self.source_ast_id_map.ast_id(m);
let ast_id = self.source_ast_id_map.ast_id(&m);
let export = m.has_atom_attr("macro_export");
let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export });
self.push_item(current_module, RawItem::Macro(m));

View file

@ -47,9 +47,9 @@ pub enum PathKind {
impl Path {
/// Calls `cb` with all paths, represented by this use item.
pub fn expand_use_item<'a>(
item: &'a ast::UseItem,
mut cb: impl FnMut(Path, &'a ast::UseTree, bool, Option<Name>),
pub fn expand_use_item(
item: &ast::UseItem,
mut cb: impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
) {
if let Some(tree) = item.use_tree() {
expand_use_tree(None, tree, &mut cb);
@ -57,7 +57,7 @@ impl Path {
}
/// Converts an `ast::Path` to `Path`. Works with use trees.
pub fn from_ast(mut path: &ast::Path) -> Option<Path> {
pub fn from_ast(mut path: ast::Path) -> Option<Path> {
let mut kind = PathKind::Plain;
let mut segments = Vec::new();
loop {
@ -87,7 +87,7 @@ impl Path {
break;
}
}
path = match qualifier(path) {
path = match qualifier(&path) {
Some(it) => it,
None => break,
};
@ -95,7 +95,7 @@ impl Path {
segments.reverse();
return Some(Path { kind, segments });
fn qualifier(path: &ast::Path) -> Option<&ast::Path> {
fn qualifier(path: &ast::Path) -> Option<ast::Path> {
if let Some(q) = path.qualifier() {
return Some(q);
}
@ -136,7 +136,7 @@ impl Path {
}
impl GenericArgs {
pub(crate) fn from_ast(node: &ast::TypeArgList) -> Option<GenericArgs> {
pub(crate) fn from_ast(node: ast::TypeArgList) -> Option<GenericArgs> {
let mut args = Vec::new();
for type_arg in node.type_args() {
let type_ref = TypeRef::from_ast_opt(type_arg.type_ref());
@ -160,10 +160,10 @@ impl From<Name> for Path {
}
}
fn expand_use_tree<'a>(
fn expand_use_tree(
prefix: Option<Path>,
tree: &'a ast::UseTree,
cb: &mut impl FnMut(Path, &'a ast::UseTree, bool, Option<Name>),
tree: ast::UseTree,
cb: &mut impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
) {
if let Some(use_tree_list) = tree.use_tree_list() {
let prefix = match tree.path() {
@ -188,7 +188,7 @@ fn expand_use_tree<'a>(
if let Some(segment) = ast_path.segment() {
if segment.kind() == Some(ast::PathSegmentKind::SelfKw) {
if let Some(prefix) = prefix {
cb(prefix, tree, false, alias);
cb(prefix, &tree, false, alias);
return;
}
}
@ -196,7 +196,7 @@ fn expand_use_tree<'a>(
}
if let Some(path) = convert_path(prefix, ast_path) {
let is_glob = tree.has_star();
cb(path, tree, is_glob, alias)
cb(path, &tree, is_glob, alias)
}
// FIXME: report errors somewhere
// We get here if we do
@ -204,7 +204,7 @@ fn expand_use_tree<'a>(
}
}
fn convert_path(prefix: Option<Path>, path: &ast::Path) -> Option<Path> {
fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
let prefix =
if let Some(qual) = path.qualifier() { Some(convert_path(prefix, qual)?) } else { prefix };
let segment = path.segment()?;

View file

@ -37,7 +37,7 @@ pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Option<Mod
pub fn module_from_declaration(
db: &impl HirDatabase,
file_id: FileId,
decl: &ast::Module,
decl: ast::Module,
) -> Option<Module> {
let parent_module = module_from_file_id(db, file_id);
let child_name = decl.name();
@ -50,8 +50,8 @@ pub fn module_from_declaration(
/// Locates the module by position in the source code.
pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Module> {
let parse = db.parse(position.file_id);
match find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) {
Some(m) if !m.has_semi() => module_from_inline(db, position.file_id, m),
match &find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) {
Some(m) if !m.has_semi() => module_from_inline(db, position.file_id, m.clone()),
_ => module_from_file_id(db, position.file_id),
}
}
@ -59,12 +59,12 @@ pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Op
fn module_from_inline(
db: &impl HirDatabase,
file_id: FileId,
module: &ast::Module,
module: ast::Module,
) -> Option<Module> {
assert!(!module.has_semi());
let file_id = file_id.into();
let ast_id_map = db.ast_id_map(file_id);
let item_id = ast_id_map.ast_id(module).with_file_id(file_id);
let item_id = ast_id_map.ast_id(&module).with_file_id(file_id);
module_from_source(db, file_id, Some(item_id))
}
@ -127,16 +127,16 @@ fn try_get_resolver_for_node(
file_id: FileId,
node: &SyntaxNode,
) -> Option<Resolver> {
if let Some(module) = ast::Module::cast(node) {
if let Some(module) = ast::Module::cast(node.clone()) {
Some(module_from_declaration(db, file_id, module)?.resolver(db))
} else if let Some(_) = ast::SourceFile::cast(node) {
} else if let Some(_) = ast::SourceFile::cast(node.clone()) {
Some(module_from_source(db, file_id.into(), None)?.resolver(db))
} else if let Some(s) = ast::StructDef::cast(node) {
} else if let Some(s) = ast::StructDef::cast(node.clone()) {
let module = module_from_child_node(db, file_id, s.syntax())?;
Some(struct_from_module(db, module, s).resolver(db))
} else if let Some(e) = ast::EnumDef::cast(node) {
Some(struct_from_module(db, module, &s).resolver(db))
} else if let Some(e) = ast::EnumDef::cast(node.clone()) {
let module = module_from_child_node(db, file_id, e.syntax())?;
Some(enum_from_module(db, module, e).resolver(db))
Some(enum_from_module(db, module, &e).resolver(db))
} else if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF {
Some(def_with_body_from_child_node(db, file_id, node)?.resolver(db))
} else {
@ -153,14 +153,14 @@ fn def_with_body_from_child_node(
let module = module_from_child_node(db, file_id, node)?;
let ctx = LocationCtx::new(db, module, file_id.into());
node.ancestors().find_map(|node| {
if let Some(def) = ast::FnDef::cast(node) {
return Some(Function { id: ctx.to_def(def) }.into());
if let Some(def) = ast::FnDef::cast(node.clone()) {
return Some(Function { id: ctx.to_def(&def) }.into());
}
if let Some(def) = ast::ConstDef::cast(node) {
return Some(Const { id: ctx.to_def(def) }.into());
if let Some(def) = ast::ConstDef::cast(node.clone()) {
return Some(Const { id: ctx.to_def(&def) }.into());
}
if let Some(def) = ast::StaticDef::cast(node) {
return Some(Static { id: ctx.to_def(def) }.into());
if let Some(def) = ast::StaticDef::cast(node.clone()) {
return Some(Static { id: ctx.to_def(&def) }.into());
}
None
})
@ -237,7 +237,7 @@ impl SourceAnalyzer {
SourceAnalyzer {
resolver: node
.ancestors()
.find_map(|node| try_get_resolver_for_node(db, file_id, node))
.find_map(|node| try_get_resolver_for_node(db, file_id, &node))
.unwrap_or_default(),
body_source_map: None,
infer: None,
@ -257,17 +257,17 @@ impl SourceAnalyzer {
}
pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
let expr_id = self.body_source_map.as_ref()?.node_expr(call.into())?;
let expr_id = self.body_source_map.as_ref()?.node_expr(&call.clone().into())?;
self.infer.as_ref()?.method_resolution(expr_id)
}
pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<crate::StructField> {
let expr_id = self.body_source_map.as_ref()?.node_expr(field.into())?;
let expr_id = self.body_source_map.as_ref()?.node_expr(&field.clone().into())?;
self.infer.as_ref()?.field_resolution(expr_id)
}
pub fn resolve_variant(&self, struct_lit: &ast::StructLit) -> Option<crate::VariantDef> {
let expr_id = self.body_source_map.as_ref()?.node_expr(struct_lit.into())?;
let expr_id = self.body_source_map.as_ref()?.node_expr(&struct_lit.clone().into())?;
self.infer.as_ref()?.variant_resolution(expr_id)
}
@ -290,18 +290,18 @@ impl SourceAnalyzer {
pub fn resolve_path(&self, db: &impl HirDatabase, path: &ast::Path) -> Option<PathResolution> {
if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) {
let expr_id = self.body_source_map.as_ref()?.node_expr(path_expr.into())?;
let expr_id = self.body_source_map.as_ref()?.node_expr(&path_expr.into())?;
if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) {
return Some(PathResolution::AssocItem(assoc));
}
}
if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) {
let pat_id = self.body_source_map.as_ref()?.node_pat(path_pat.into())?;
let pat_id = self.body_source_map.as_ref()?.node_pat(&path_pat.into())?;
if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) {
return Some(PathResolution::AssocItem(assoc));
}
}
let hir_path = crate::Path::from_ast(path)?;
let hir_path = crate::Path::from_ast(path.clone())?;
let res = self.resolver.resolve_path_without_assoc_items(db, &hir_path);
let res = res.clone().take_types().or_else(|| res.take_values())?;
let res = match res {
@ -343,12 +343,12 @@ impl SourceAnalyzer {
// FIXME: at least, this should work with any DefWithBody, but ideally
// this should be hir-based altogether
let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
let ptr = Either::A(AstPtr::new(pat.into()));
let ptr = Either::A(AstPtr::new(&ast::Pat::from(pat.clone())));
fn_def
.syntax()
.descendants()
.filter_map(ast::NameRef::cast)
.filter(|name_ref| match self.resolve_local_name(*name_ref) {
.filter(|name_ref| match self.resolve_local_name(&name_ref) {
None => false,
Some(entry) => entry.ptr() == ptr,
})
@ -411,7 +411,7 @@ fn scope_for(
node: &SyntaxNode,
) -> Option<ScopeId> {
node.ancestors()
.map(SyntaxNodePtr::new)
.map(|it| SyntaxNodePtr::new(&it))
.filter_map(|ptr| source_map.syntax_expr(ptr))
.find_map(|it| scopes.scope_for(it))
}

View file

@ -5,7 +5,7 @@ use std::{
};
use ra_arena::{impl_arena_id, Arena, RawId};
use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxNodePtr, TreeArc};
use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxNodePtr};
use crate::{AstDatabase, HirFileId};
@ -42,9 +42,9 @@ impl<N: AstNode> AstId<N> {
self.file_id
}
pub(crate) fn to_node(&self, db: &impl AstDatabase) -> TreeArc<N> {
pub(crate) fn to_node(&self, db: &impl AstDatabase) -> N {
let syntax_node = db.ast_id_to_node(self.file_id, self.file_ast_id.raw);
N::cast(&syntax_node).unwrap().to_owned()
N::cast(syntax_node).unwrap()
}
}
@ -93,7 +93,7 @@ pub struct AstIdMap {
impl AstIdMap {
pub(crate) fn ast_id_map_query(db: &impl AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
let map = if let Some(node) = db.parse_or_expand(file_id) {
AstIdMap::from_source(&*node)
AstIdMap::from_source(&node)
} else {
AstIdMap::default()
};
@ -104,9 +104,9 @@ impl AstIdMap {
db: &impl AstDatabase,
file_id: HirFileId,
ast_id: ErasedFileAstId,
) -> TreeArc<SyntaxNode> {
) -> SyntaxNode {
let node = db.parse_or_expand(file_id).unwrap();
db.ast_id_map(file_id).arena[ast_id].to_node(&*node).to_owned()
db.ast_id_map(file_id).arena[ast_id].to_node(&node)
}
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
@ -131,7 +131,7 @@ impl AstIdMap {
// change parent's id. This means that, say, adding a new function to a
// trait does not change ids of top-level items, which helps caching.
bfs(node, |it| {
if let Some(module_item) = ast::ModuleItem::cast(it) {
if let Some(module_item) = ast::ModuleItem::cast(it.clone()) {
res.alloc(module_item.syntax());
} else if let Some(macro_call) = ast::MacroCall::cast(it) {
res.alloc(macro_call.syntax());
@ -146,8 +146,8 @@ impl AstIdMap {
}
/// Walks the subtree in bfs order, calling `f` for each node.
fn bfs(node: &SyntaxNode, mut f: impl FnMut(&SyntaxNode)) {
let mut curr_layer = vec![node];
fn bfs(node: &SyntaxNode, mut f: impl FnMut(SyntaxNode)) {
let mut curr_layer = vec![node.clone()];
let mut next_layer = vec![];
while !curr_layer.is_empty() {
curr_layer.drain(..).for_each(|node| {

View file

@ -31,9 +31,9 @@ impl TraitData {
item_list
.impl_items()
.map(|item_node| match item_node.kind() {
ast::ImplItemKind::FnDef(it) => Function { id: ctx.to_def(it) }.into(),
ast::ImplItemKind::ConstDef(it) => Const { id: ctx.to_def(it) }.into(),
ast::ImplItemKind::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(it) }.into(),
ast::ImplItemKind::FnDef(it) => Function { id: ctx.to_def(&it) }.into(),
ast::ImplItemKind::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(),
ast::ImplItemKind::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(),
})
.collect()
} else {

View file

@ -3086,7 +3086,7 @@ fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String {
let file = db.parse(pos.file_id).ok().unwrap();
let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap();
let analyzer = SourceAnalyzer::new(db, pos.file_id, expr.syntax(), Some(pos.offset));
let ty = analyzer.type_of(db, expr).unwrap();
let ty = analyzer.type_of(db, &expr).unwrap();
ty.display(db).to_string()
}
@ -3126,7 +3126,7 @@ fn infer(content: &str) -> String {
types.sort_by_key(|(ptr, _)| (ptr.range().start(), ptr.range().end()));
for (syntax_ptr, ty) in &types {
let node = syntax_ptr.to_node(source_file.syntax());
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node) {
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.clone()) {
(self_param.self_kw_token().range(), "self".to_string())
} else {
(syntax_ptr.range(), node.text().to_string().replace("\n", " "))
@ -3137,7 +3137,7 @@ fn infer(content: &str) -> String {
for node in source_file.syntax().descendants() {
if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF {
let analyzer = SourceAnalyzer::new(&db, file_id, node, None);
let analyzer = SourceAnalyzer::new(&db, file_id, &node, None);
infer_def(analyzer.inference_result(), analyzer.body_source_map());
}
}
@ -3179,7 +3179,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
let node =
algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent();
let events = db.log_executed(|| {
SourceAnalyzer::new(&db, pos.file_id, node, None);
SourceAnalyzer::new(&db, pos.file_id, &node, None);
});
assert!(format!("{:?}", events).contains("infer"))
}
@ -3200,7 +3200,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
let node =
algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent();
let events = db.log_executed(|| {
SourceAnalyzer::new(&db, pos.file_id, node, None);
SourceAnalyzer::new(&db, pos.file_id, &node, None);
});
assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
}

View file

@ -56,7 +56,7 @@ pub enum TypeRef {
impl TypeRef {
/// Converts an `ast::TypeRef` to a `hir::TypeRef`.
pub(crate) fn from_ast(node: &ast::TypeRef) -> Self {
pub(crate) fn from_ast(node: ast::TypeRef) -> Self {
use ra_syntax::ast::TypeRefKind::*;
match node.kind() {
ParenType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
@ -95,7 +95,7 @@ impl TypeRef {
}
}
pub(crate) fn from_ast_opt(node: Option<&ast::TypeRef>) -> Self {
pub(crate) fn from_ast_opt(node: Option<ast::TypeRef>) -> Self {
if let Some(node) = node {
TypeRef::from_ast(node)
} else {

View file

@ -75,7 +75,7 @@ impl<T> Parse<T> {
Parse { green, errors: Arc::new(errors), _ty: PhantomData }
}
fn syntax_node(&self) -> SyntaxNode {
pub fn syntax_node(&self) -> SyntaxNode {
SyntaxNode::new(self.green.clone())
}
}