From 14cb96ec0e6be3b99bfe4ea373c058dcbd2a4f79 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 19:54:16 +0200 Subject: Allign RecordPat with RecordExpr --- crates/ra_hir/src/semantics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 6f3b3dc9a..44c71857e 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -216,7 +216,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.resolve_record_field(field) } - pub fn resolve_record_field_pat(&self, field: &ast::RecordFieldPat) -> Option { + pub fn resolve_record_field_pat(&self, field: &ast::RecordPatField) -> Option { self.imp.resolve_record_field_pat(field) } @@ -429,7 +429,7 @@ impl<'db> SemanticsImpl<'db> { self.analyze(field.syntax()).resolve_record_field(self.db, field) } - fn resolve_record_field_pat(&self, field: &ast::RecordFieldPat) -> Option { + fn resolve_record_field_pat(&self, field: &ast::RecordPatField) -> Option { self.analyze(field.syntax()).resolve_record_field_pat(self.db, field) } -- cgit v1.2.3 From 98181087984157e27faba0b969e384f3c62c39d5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 20:09:09 +0200 Subject: Rename BindPat -> IdentPat --- crates/ra_hir/src/semantics.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 44c71857e..307b336f2 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -236,7 +236,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.lower_path(path) } - pub fn resolve_bind_pat_to_const(&self, pat: &ast::BindPat) -> Option { + pub fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option { self.imp.resolve_bind_pat_to_const(pat) } @@ -452,7 +452,7 @@ impl<'db> SemanticsImpl<'db> { Path::from_src(path.clone(), &Hygiene::new(self.db.upcast(), src.file_id.into())) } - fn resolve_bind_pat_to_const(&self, pat: &ast::BindPat) -> Option { + fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option { self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat) } @@ -594,7 +594,7 @@ to_def_impls![ (crate::EnumVariant, ast::Variant, enum_variant_to_def), (crate::TypeParam, ast::TypeParam, type_param_to_def), (crate::MacroDef, ast::MacroCall, macro_call_to_def), // this one is dubious, not all calls are macros - (crate::Local, ast::BindPat, bind_pat_to_def), + (crate::Local, ast::IdentPat, bind_pat_to_def), ]; fn find_root(node: &SyntaxNode) -> SyntaxNode { -- cgit v1.2.3 From 6cde0b1aa0f6b8623c6b81b2396f4a0345891233 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sat, 8 Aug 2020 14:14:18 -0400 Subject: Add support for extern crate This adds syntax highlighting, hover and goto def functionality for extern crate --- crates/ra_hir/src/semantics.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 307b336f2..e392130ab 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -8,7 +8,7 @@ use hir_def::{ resolver::{self, HasResolver, Resolver}, AsMacroCall, FunctionId, TraitId, VariantId, }; -use hir_expand::{diagnostics::AstDiagnostic, hygiene::Hygiene, ExpansionInfo}; +use hir_expand::{diagnostics::AstDiagnostic, hygiene::Hygiene, name::AsName, ExpansionInfo}; use hir_ty::associated_type_shorthand_candidates; use itertools::Itertools; use ra_db::{FileId, FileRange}; @@ -24,8 +24,8 @@ use crate::{ diagnostics::Diagnostic, semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, source_analyzer::{resolve_hir_path, resolve_hir_path_qualifier, SourceAnalyzer}, - AssocItem, Callable, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, Module, - ModuleDef, Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, VariantDef, + AssocItem, Callable, Crate, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, + Module, ModuleDef, Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, VariantDef, }; use resolver::TypeNs; @@ -228,6 +228,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.resolve_path(path) } + pub fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option { + self.imp.resolve_extern_crate(extern_crate) + } + pub fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option { self.imp.resolve_variant(record_lit).map(VariantDef::from) } @@ -443,6 +447,17 @@ impl<'db> SemanticsImpl<'db> { self.analyze(path.syntax()).resolve_path(self.db, path) } + fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option { + 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 + } + }) + } + fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option { self.analyze(record_lit.syntax()).resolve_variant(self.db, record_lit) } @@ -612,6 +627,10 @@ impl<'a> SemanticsScope<'a> { Some(Module { id: self.resolver.module()? }) } + pub fn krate(&self) -> Option { + Some(Crate { id: self.resolver.krate()? }) + } + /// Note: `FxHashSet` 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 { -- cgit v1.2.3 From 08182aa9fad4021e60cdc80ee0a578929507e115 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sun, 19 Jul 2020 11:45:46 -0400 Subject: Move unsafe packed ref logic to Semantics, use `Attrs::by_key` to simplify repr attr lookup --- crates/ra_hir/src/semantics.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index e392130ab..1072b3971 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -279,6 +279,47 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { pub fn assert_contains_node(&self, node: &SyntaxNode) { self.imp.assert_contains_node(node) } + + pub fn is_unsafe_pat(&self, pat: &ast::Pat) -> bool { + let ty = (|| { + let parent = match pat { + ast::Pat::BindPat(bind_pat) => bind_pat.syntax().parent()?, + _ => return None, + }; + + // `BindPat` can live under `RecordPat` directly under `RecordFieldPat` or + // `RecordFieldPatList`. `RecordFieldPat` also lives under `RecordFieldPatList`, + // so this tries to lookup the `BindPat` anywhere along that structure to the + // `RecordPat` so we can get the containing type. + let record_pat = ast::RecordFieldPat::cast(parent.clone()) + .and_then(|record_pat| record_pat.syntax().parent()) + .or_else(|| Some(parent.clone())) + .and_then(|parent| { + ast::RecordFieldPatList::cast(parent)? + .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. + ty.map(|ty| ty.is_packed(self.db)).unwrap_or(false) + } } impl<'db> SemanticsImpl<'db> { -- cgit v1.2.3 From a6af0272f7bf129a3063cdd7096f685fc58438e6 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 23 Jul 2020 10:11:37 -0400 Subject: Move semantic logic into Semantics, fix missing tag for safe amp operator, using functional methods rather than clunky inline closure --- crates/ra_hir/src/semantics.rs | 110 ++++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 35 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 1072b3971..f706a186e 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -25,7 +25,8 @@ use crate::{ semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, source_analyzer::{resolve_hir_path, resolve_hir_path_qualifier, SourceAnalyzer}, AssocItem, Callable, Crate, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, - Module, ModuleDef, Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, VariantDef, + Module, ModuleDef, Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, TypeRef, + VariantDef, }; use resolver::TypeNs; @@ -280,45 +281,84 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.assert_contains_node(node) } - pub fn is_unsafe_pat(&self, pat: &ast::Pat) -> bool { - let ty = (|| { - let parent = match pat { - ast::Pat::BindPat(bind_pat) => bind_pat.syntax().parent()?, - _ => return None, - }; - - // `BindPat` can live under `RecordPat` directly under `RecordFieldPat` or - // `RecordFieldPatList`. `RecordFieldPat` also lives under `RecordFieldPatList`, - // so this tries to lookup the `BindPat` anywhere along that structure to the - // `RecordPat` so we can get the containing type. - let record_pat = ast::RecordFieldPat::cast(parent.clone()) - .and_then(|record_pat| record_pat.syntax().parent()) - .or_else(|| Some(parent.clone())) - .and_then(|parent| { - ast::RecordFieldPatList::cast(parent)? - .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, - }; + pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> Option<()> { + let expr = method_call_expr.expr()?; + let field_expr = + if let ast::Expr::FieldExpr(field_expr) = expr { field_expr } else { return None }; + let ty = self.type_of_expr(&field_expr.expr()?)?; + if !ty.is_packed(self.db) { + return None; + } - self.type_of_expr(&field_expr.expr()?) + let func = self.resolve_method_call(&method_call_expr)?; + if func.has_self_param(self.db) { + let params = func.params(self.db); + if matches!(params.into_iter().next(), Some(TypeRef::Reference(..))) { + Some(()) } else { None } - })(); + } else { + None + } + } - // Binding a reference to a packed type is possibly unsafe. - ty.map(|ty| ty.is_packed(self.db)).unwrap_or(false) + pub fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool { + 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. + } + + pub fn is_unsafe_bind_pat(&self, bind_pat: &ast::BindPat) -> bool { + bind_pat + .syntax() + .parent() + .and_then(|parent| { + // `BindPat` can live under `RecordPat` directly under `RecordFieldPat` or + // `RecordFieldPatList`. `RecordFieldPat` also lives under `RecordFieldPatList`, + // so this tries to lookup the `BindPat` anywhere along that structure to the + // `RecordPat` so we can get the containing type. + let record_pat = ast::RecordFieldPat::cast(parent.clone()) + .and_then(|record_pat| record_pat.syntax().parent()) + .or_else(|| Some(parent.clone())) + .and_then(|parent| { + ast::RecordFieldPatList::cast(parent)? + .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) } } -- cgit v1.2.3 From 39fdd41df4052cef5da4876067ae28615012476b Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 23 Jul 2020 18:31:28 -0400 Subject: Return bool from is_unsafe_method_call and cleanup usages --- crates/ra_hir/src/semantics.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index f706a186e..9697c7082 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -281,26 +281,26 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.assert_contains_node(node) } - pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> Option<()> { - let expr = method_call_expr.expr()?; - let field_expr = - if let ast::Expr::FieldExpr(field_expr) = expr { field_expr } else { return None }; - let ty = self.type_of_expr(&field_expr.expr()?)?; - if !ty.is_packed(self.db) { - return None; - } + pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> bool { + method_call_expr + .expr() + .and_then(|expr| { + let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr { + field_expr + } else { + return None; + }; + 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)?; - if func.has_self_param(self.db) { - let params = func.params(self.db); - if matches!(params.into_iter().next(), Some(TypeRef::Reference(..))) { - Some(()) - } else { - None - } - } else { - None - } + let func = self.resolve_method_call(&method_call_expr)?; + let is_unsafe = func.has_self_param(self.db) + && matches!(func.params(self.db).first(), Some(TypeRef::Reference(..))); + Some(is_unsafe) + }) + .unwrap_or(false) } pub fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool { -- cgit v1.2.3 From 61dff939f909e0c53bcd3be4c3e672c794022cde Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 30 Jul 2020 09:26:40 -0400 Subject: Move unsafe semantics methods into `SemanticsImpl` and reference them in `Semantics` --- crates/ra_hir/src/semantics.rs | 154 ++++++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 71 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 9697c7082..758d00409 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -282,83 +282,15 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { } pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> bool { - method_call_expr - .expr() - .and_then(|expr| { - let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr { - field_expr - } else { - return None; - }; - 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)?; - let is_unsafe = func.has_self_param(self.db) - && matches!(func.params(self.db).first(), Some(TypeRef::Reference(..))); - Some(is_unsafe) - }) - .unwrap_or(false) + self.imp.is_unsafe_method_call(method_call_expr) } pub fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool { - 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. + self.imp.is_unsafe_ref_expr(ref_expr) } pub fn is_unsafe_bind_pat(&self, bind_pat: &ast::BindPat) -> bool { - bind_pat - .syntax() - .parent() - .and_then(|parent| { - // `BindPat` can live under `RecordPat` directly under `RecordFieldPat` or - // `RecordFieldPatList`. `RecordFieldPat` also lives under `RecordFieldPatList`, - // so this tries to lookup the `BindPat` anywhere along that structure to the - // `RecordPat` so we can get the containing type. - let record_pat = ast::RecordFieldPat::cast(parent.clone()) - .and_then(|record_pat| record_pat.syntax().parent()) - .or_else(|| Some(parent.clone())) - .and_then(|parent| { - ast::RecordFieldPatList::cast(parent)? - .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) + self.imp.is_unsafe_bind_pat(bind_pat) } } @@ -655,6 +587,86 @@ impl<'db> SemanticsImpl<'db> { }); InFile::new(file_id, node) } + + pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> bool { + method_call_expr + .expr() + .and_then(|expr| { + let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr { + field_expr + } else { + return None; + }; + 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)?; + let is_unsafe = func.has_self_param(self.db) + && matches!(func.params(self.db).first(), Some(TypeRef::Reference(..))); + Some(is_unsafe) + }) + .unwrap_or(false) + } + + pub fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool { + 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. + } + + pub fn is_unsafe_bind_pat(&self, bind_pat: &ast::BindPat) -> bool { + bind_pat + .syntax() + .parent() + .and_then(|parent| { + // `BindPat` can live under `RecordPat` directly under `RecordFieldPat` or + // `RecordFieldPatList`. `RecordFieldPat` also lives under `RecordFieldPatList`, + // so this tries to lookup the `BindPat` anywhere along that structure to the + // `RecordPat` so we can get the containing type. + let record_pat = ast::RecordFieldPat::cast(parent.clone()) + .and_then(|record_pat| record_pat.syntax().parent()) + .or_else(|| Some(parent.clone())) + .and_then(|parent| { + ast::RecordFieldPatList::cast(parent)? + .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) + } } pub trait ToDef: AstNode + Clone { -- cgit v1.2.3 From 2199d0cda9c745ecb460dd987b8da982d02bc130 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Fri, 7 Aug 2020 10:40:09 -0400 Subject: Fix type names broken by rebase, redo expected test because of rebase --- crates/ra_hir/src/semantics.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 758d00409..872f5fa4c 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -289,8 +289,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.is_unsafe_ref_expr(ref_expr) } - pub fn is_unsafe_bind_pat(&self, bind_pat: &ast::BindPat) -> bool { - self.imp.is_unsafe_bind_pat(bind_pat) + pub fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool { + self.imp.is_unsafe_ident_pat(ident_pat) } } @@ -629,20 +629,24 @@ impl<'db> SemanticsImpl<'db> { // more than it should with the current implementation. } - pub fn is_unsafe_bind_pat(&self, bind_pat: &ast::BindPat) -> bool { - bind_pat + pub fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool { + if !ident_pat.ref_token().is_some() { + return false; + } + + ident_pat .syntax() .parent() .and_then(|parent| { - // `BindPat` can live under `RecordPat` directly under `RecordFieldPat` or - // `RecordFieldPatList`. `RecordFieldPat` also lives under `RecordFieldPatList`, - // so this tries to lookup the `BindPat` anywhere along that structure to the + // `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 // `RecordPat` so we can get the containing type. - let record_pat = ast::RecordFieldPat::cast(parent.clone()) + let record_pat = ast::RecordPatField::cast(parent.clone()) .and_then(|record_pat| record_pat.syntax().parent()) .or_else(|| Some(parent.clone())) .and_then(|parent| { - ast::RecordFieldPatList::cast(parent)? + ast::RecordPatFieldList::cast(parent)? .syntax() .parent() .and_then(ast::RecordPat::cast) -- cgit v1.2.3 From ee1586c1ed058ff0f090b552d52fe6bbe2dd7f7f Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 27 Jul 2020 22:46:25 +0300 Subject: Better naming --- crates/ra_hir/src/semantics.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index e392130ab..1c5dc3d51 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -145,6 +145,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.original_range(node) } + pub fn diagnostics_fix_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { + self.imp.diagnostics_fix_range(diagnostics) + } + pub fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { self.imp.diagnostics_range(diagnostics) } @@ -376,6 +380,13 @@ impl<'db> SemanticsImpl<'db> { original_range(self.db, node.as_ref()) } + fn diagnostics_fix_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { + let src = diagnostics.fix_source(); + let root = self.db.parse_or_expand(src.file_id).unwrap(); + let node = src.value.to_node(&root); + original_range(self.db, src.with_value(&node)) + } + fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { let src = diagnostics.source(); let root = self.db.parse_or_expand(src.file_id).unwrap(); -- cgit v1.2.3 From 9963f43d51071ea02f8f6d490b9c49882034b42c Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 9 Aug 2020 01:59:26 +0300 Subject: Refactor the diagnostics --- crates/ra_hir/src/semantics.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 1c5dc3d51..b4420d378 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -109,11 +109,14 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.parse(file_id) } - pub fn ast(&self, d: &T) -> ::AST { - let file_id = d.source().file_id; + pub fn diagnostic_fix_source( + &self, + d: &T, + ) -> ::AST { + let file_id = d.presentation().file_id; let root = self.db.parse_or_expand(file_id).unwrap(); self.imp.cache(root, file_id); - d.ast(self.db.upcast()) + d.fix_source(self.db.upcast()) } pub fn expand(&self, macro_call: &ast::MacroCall) -> Option { @@ -145,12 +148,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.original_range(node) } - pub fn diagnostics_fix_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { - self.imp.diagnostics_fix_range(diagnostics) - } - - pub fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { - self.imp.diagnostics_range(diagnostics) + pub fn diagnostics_presentation_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { + self.imp.diagnostics_presentation_range(diagnostics) } pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator + '_ { @@ -380,15 +379,8 @@ impl<'db> SemanticsImpl<'db> { original_range(self.db, node.as_ref()) } - fn diagnostics_fix_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { - let src = diagnostics.fix_source(); - let root = self.db.parse_or_expand(src.file_id).unwrap(); - let node = src.value.to_node(&root); - original_range(self.db, src.with_value(&node)) - } - - fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { - let src = diagnostics.source(); + fn diagnostics_presentation_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { + let src = diagnostics.presentation(); let root = self.db.parse_or_expand(src.file_id).unwrap(); let node = src.value.to_node(&root); original_range(self.db, src.with_value(&node)) -- cgit v1.2.3 From 936861993935d5b2c78b953e2f4b719e1992bd73 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 10 Aug 2020 22:53:10 +0300 Subject: Make the fix AST source Optional --- crates/ra_hir/src/semantics.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index b4420d378..c5bc2baff 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -8,7 +8,7 @@ use hir_def::{ resolver::{self, HasResolver, Resolver}, AsMacroCall, FunctionId, TraitId, VariantId, }; -use hir_expand::{diagnostics::AstDiagnostic, hygiene::Hygiene, name::AsName, ExpansionInfo}; +use hir_expand::{diagnostics::DiagnosticWithFix, hygiene::Hygiene, name::AsName, ExpansionInfo}; use hir_ty::associated_type_shorthand_candidates; use itertools::Itertools; use ra_db::{FileId, FileRange}; @@ -109,12 +109,12 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.parse(file_id) } - pub fn diagnostic_fix_source( + pub fn diagnostic_fix_source( &self, d: &T, - ) -> ::AST { + ) -> Option<::AST> { let file_id = d.presentation().file_id; - let root = self.db.parse_or_expand(file_id).unwrap(); + let root = self.db.parse_or_expand(file_id)?; self.imp.cache(root, file_id); d.fix_source(self.db.upcast()) } -- cgit v1.2.3 From 29fbc8e02180aac1f4d7819a9626206aa64028a0 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 11 Aug 2020 00:37:23 +0300 Subject: Move the DiagnosticsWithFix trait on the ide level --- crates/ra_hir/src/semantics.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index c5bc2baff..e9f7a033c 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -8,7 +8,7 @@ use hir_def::{ resolver::{self, HasResolver, Resolver}, AsMacroCall, FunctionId, TraitId, VariantId, }; -use hir_expand::{diagnostics::DiagnosticWithFix, hygiene::Hygiene, name::AsName, ExpansionInfo}; +use hir_expand::{hygiene::Hygiene, name::AsName, ExpansionInfo}; use hir_ty::associated_type_shorthand_candidates; use itertools::Itertools; use ra_db::{FileId, FileRange}; @@ -109,14 +109,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.parse(file_id) } - pub fn diagnostic_fix_source( - &self, - d: &T, - ) -> Option<::AST> { - let file_id = d.presentation().file_id; - let root = self.db.parse_or_expand(file_id)?; - self.imp.cache(root, file_id); - d.fix_source(self.db.upcast()) + pub fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) { + self.imp.cache(root_node, file_id) } pub fn expand(&self, macro_call: &ast::MacroCall) -> Option { -- cgit v1.2.3 From c8cad76d25f7fab856c9646b70122e0f9f7d7218 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 11 Aug 2020 00:55:57 +0300 Subject: Improve the ide diagnostics trait API --- crates/ra_hir/src/semantics.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index e9f7a033c..2dfe69039 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -109,10 +109,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.parse(file_id) } - pub fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) { - self.imp.cache(root_node, file_id) - } - pub fn expand(&self, macro_call: &ast::MacroCall) -> Option { self.imp.expand(macro_call) } @@ -377,6 +373,7 @@ impl<'db> SemanticsImpl<'db> { let src = diagnostics.presentation(); let root = self.db.parse_or_expand(src.file_id).unwrap(); let node = src.value.to_node(&root); + self.cache(root, src.file_id); original_range(self.db, src.with_value(&node)) } -- cgit v1.2.3 From db12ccee96bf37367b39ad99638d06da7123c088 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 11 Aug 2020 17:15:11 +0300 Subject: Better naming and docs --- crates/ra_hir/src/semantics.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 2dfe69039..e3c417b41 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -138,8 +138,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.original_range(node) } - pub fn diagnostics_presentation_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { - self.imp.diagnostics_presentation_range(diagnostics) + pub fn diagnostics_display_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { + self.imp.diagnostics_display_range(diagnostics) } pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator + '_ { @@ -369,8 +369,8 @@ impl<'db> SemanticsImpl<'db> { original_range(self.db, node.as_ref()) } - fn diagnostics_presentation_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { - let src = diagnostics.presentation(); + fn diagnostics_display_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { + let src = diagnostics.display_source(); let root = self.db.parse_or_expand(src.file_id).unwrap(); let node = src.value.to_node(&root); self.cache(root, src.file_id); -- cgit v1.2.3 From 208b7bd7ba687fb570feb1b89219f14c63712ce8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 16:32:36 +0200 Subject: Rename ra_prof -> profile --- crates/ra_hir/src/semantics.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 36b688ccb..7e3ec6315 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -12,7 +12,6 @@ use hir_expand::{hygiene::Hygiene, name::AsName, ExpansionInfo}; use hir_ty::associated_type_shorthand_candidates; use itertools::Itertools; use ra_db::{FileId, FileRange}; -use ra_prof::profile; use ra_syntax::{ algo::{find_node_at_offset, skip_trivia_token}, ast, AstNode, Direction, SyntaxNode, SyntaxToken, TextRange, TextSize, @@ -334,7 +333,7 @@ impl<'db> SemanticsImpl<'db> { } fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { - let _p = profile("descend_into_macros"); + let _p = profile::span("descend_into_macros"); let parent = token.parent(); let parent = self.find_file(parent); let sa = self.analyze2(parent.as_ref(), None); @@ -523,7 +522,7 @@ impl<'db> SemanticsImpl<'db> { } fn analyze2(&self, src: InFile<&SyntaxNode>, offset: Option) -> SourceAnalyzer { - let _p = profile("Semantics::analyze2"); + let _p = profile::span("Semantics::analyze2"); let container = match self.with_ctx(|ctx| ctx.find_container(src)) { Some(it) => it, -- cgit v1.2.3 From a1c187eef3ba08076aedb5154929f7eda8d1b424 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 18:26:51 +0200 Subject: Rename ra_syntax -> syntax --- crates/ra_hir/src/semantics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 7e3ec6315..2e0ef4408 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -12,11 +12,11 @@ use hir_expand::{hygiene::Hygiene, name::AsName, ExpansionInfo}; use hir_ty::associated_type_shorthand_candidates; use itertools::Itertools; use ra_db::{FileId, FileRange}; -use ra_syntax::{ +use rustc_hash::{FxHashMap, FxHashSet}; +use syntax::{ algo::{find_node_at_offset, skip_trivia_token}, ast, AstNode, Direction, SyntaxNode, SyntaxToken, TextRange, TextSize, }; -use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ db::HirDatabase, -- cgit v1.2.3 From ed20a857f485a471369cd99b843af19a4d875ad0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 16:25:38 +0200 Subject: Rename ra_db -> base_db --- crates/ra_hir/src/semantics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 2e0ef4408..1467d825d 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -4,6 +4,7 @@ mod source_to_def; use std::{cell::RefCell, fmt, iter::successors}; +use base_db::{FileId, FileRange}; use hir_def::{ resolver::{self, HasResolver, Resolver}, AsMacroCall, FunctionId, TraitId, VariantId, @@ -11,7 +12,6 @@ use hir_def::{ use hir_expand::{hygiene::Hygiene, name::AsName, ExpansionInfo}; use hir_ty::associated_type_shorthand_candidates; use itertools::Itertools; -use ra_db::{FileId, FileRange}; use rustc_hash::{FxHashMap, FxHashSet}; use syntax::{ algo::{find_node_at_offset, skip_trivia_token}, -- cgit v1.2.3 From ae71a631fd657368e8593feb5e025d23147afe60 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 16:36:55 +0200 Subject: Rename ra_hir -> hir --- crates/ra_hir/src/semantics.rs | 840 ----------------------------------------- 1 file changed, 840 deletions(-) delete mode 100644 crates/ra_hir/src/semantics.rs (limited to 'crates/ra_hir/src/semantics.rs') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs deleted file mode 100644 index 1467d825d..000000000 --- a/crates/ra_hir/src/semantics.rs +++ /dev/null @@ -1,840 +0,0 @@ -//! See `Semantics`. - -mod source_to_def; - -use std::{cell::RefCell, fmt, iter::successors}; - -use base_db::{FileId, FileRange}; -use hir_def::{ - resolver::{self, HasResolver, Resolver}, - AsMacroCall, FunctionId, TraitId, VariantId, -}; -use hir_expand::{hygiene::Hygiene, name::AsName, ExpansionInfo}; -use hir_ty::associated_type_shorthand_candidates; -use itertools::Itertools; -use rustc_hash::{FxHashMap, FxHashSet}; -use syntax::{ - algo::{find_node_at_offset, skip_trivia_token}, - ast, AstNode, Direction, SyntaxNode, SyntaxToken, TextRange, TextSize, -}; - -use crate::{ - db::HirDatabase, - diagnostics::Diagnostic, - semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, - source_analyzer::{resolve_hir_path, resolve_hir_path_qualifier, SourceAnalyzer}, - AssocItem, Callable, Crate, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, - Module, ModuleDef, Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, TypeRef, - VariantDef, -}; -use resolver::TypeNs; - -#[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), - SelfType(ImplDef), - Macro(MacroDef), - AssocItem(AssocItem), -} - -impl PathResolution { - fn in_type_ns(&self) -> Option { - match self { - PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())), - PathResolution::Def(ModuleDef::BuiltinType(builtin)) => { - Some(TypeNs::BuiltinType(*builtin)) - } - PathResolution::Def(ModuleDef::Const(_)) - | PathResolution::Def(ModuleDef::EnumVariant(_)) - | PathResolution::Def(ModuleDef::Function(_)) - | PathResolution::Def(ModuleDef::Module(_)) - | PathResolution::Def(ModuleDef::Static(_)) - | PathResolution::Def(ModuleDef::Trait(_)) => None, - PathResolution::Def(ModuleDef::TypeAlias(alias)) => { - Some(TypeNs::TypeAliasId((*alias).into())) - } - PathResolution::Local(_) | PathResolution::Macro(_) => None, - PathResolution::TypeParam(param) => Some(TypeNs::GenericParam((*param).into())), - PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())), - PathResolution::AssocItem(AssocItem::Const(_)) - | PathResolution::AssocItem(AssocItem::Function(_)) => None, - PathResolution::AssocItem(AssocItem::TypeAlias(alias)) => { - Some(TypeNs::TypeAliasId((*alias).into())) - } - } - } - - /// Returns an iterator over associated types that may be specified after this path (using - /// `Ty::Assoc` syntax). - pub fn assoc_type_shorthand_candidates( - &self, - db: &dyn HirDatabase, - mut cb: impl FnMut(TypeAlias) -> Option, - ) -> Option { - associated_type_shorthand_candidates(db, self.in_type_ns()?, |_, _, id| cb(id.into())) - } -} - -/// Primary API to get semantic information, like types, from syntax trees. -pub struct Semantics<'db, DB> { - pub db: &'db DB, - imp: SemanticsImpl<'db>, -} - -pub struct SemanticsImpl<'db> { - pub db: &'db dyn HirDatabase, - s2d_cache: RefCell, - expansion_info_cache: RefCell>>, - cache: RefCell>, -} - -impl 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 { - let impl_ = SemanticsImpl::new(db); - Semantics { db, imp: impl_ } - } - - pub fn parse(&self, file_id: FileId) -> ast::SourceFile { - self.imp.parse(file_id) - } - - pub fn expand(&self, macro_call: &ast::MacroCall) -> Option { - self.imp.expand(macro_call) - } - - pub fn expand_hypothetical( - &self, - actual_macro_call: &ast::MacroCall, - hypothetical_args: &ast::TokenTree, - token_to_map: SyntaxToken, - ) -> Option<(SyntaxNode, SyntaxToken)> { - self.imp.expand_hypothetical(actual_macro_call, hypothetical_args, token_to_map) - } - - pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { - self.imp.descend_into_macros(token) - } - - pub fn descend_node_at_offset( - &self, - node: &SyntaxNode, - offset: TextSize, - ) -> Option { - self.imp.descend_node_at_offset(node, offset).find_map(N::cast) - } - - pub fn original_range(&self, node: &SyntaxNode) -> FileRange { - self.imp.original_range(node) - } - - pub fn diagnostics_display_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { - self.imp.diagnostics_display_range(diagnostics) - } - - pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator + '_ { - self.imp.ancestors_with_macros(node) - } - - pub fn ancestors_at_offset_with_macros( - &self, - node: &SyntaxNode, - offset: TextSize, - ) -> impl Iterator + '_ { - self.imp.ancestors_at_offset_with_macros(node, offset) - } - - /// 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( - &self, - node: &SyntaxNode, - offset: TextSize, - ) -> Option { - self.imp.ancestors_at_offset_with_macros(node, offset).find_map(N::cast) - } - - /// 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( - &self, - node: &SyntaxNode, - offset: TextSize, - ) -> Option { - 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) - } - - pub fn type_of_expr(&self, expr: &ast::Expr) -> Option { - self.imp.type_of_expr(expr) - } - - pub fn type_of_pat(&self, pat: &ast::Pat) -> Option { - self.imp.type_of_pat(pat) - } - - pub fn type_of_self(&self, param: &ast::SelfParam) -> Option { - self.imp.type_of_self(param) - } - - pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { - self.imp.resolve_method_call(call).map(Function::from) - } - - pub fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option { - self.imp.resolve_method_call_as_callable(call) - } - - pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option { - self.imp.resolve_field(field) - } - - pub fn resolve_record_field( - &self, - field: &ast::RecordExprField, - ) -> Option<(Field, Option)> { - self.imp.resolve_record_field(field) - } - - pub fn resolve_record_field_pat(&self, field: &ast::RecordPatField) -> Option { - self.imp.resolve_record_field_pat(field) - } - - pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option { - self.imp.resolve_macro_call(macro_call) - } - - pub fn resolve_path(&self, path: &ast::Path) -> Option { - self.imp.resolve_path(path) - } - - pub fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option { - self.imp.resolve_extern_crate(extern_crate) - } - - pub fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option { - self.imp.resolve_variant(record_lit).map(VariantDef::from) - } - - pub fn lower_path(&self, path: &ast::Path) -> Option { - self.imp.lower_path(path) - } - - pub fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option { - self.imp.resolve_bind_pat_to_const(pat) - } - - // FIXME: use this instead? - // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option; - - pub fn record_literal_missing_fields(&self, literal: &ast::RecordExpr) -> Vec<(Field, Type)> { - self.imp.record_literal_missing_fields(literal) - } - - pub fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> { - self.imp.record_pattern_missing_fields(pattern) - } - - pub fn to_def(&self, src: &T) -> Option { - let src = self.imp.find_file(src.syntax().clone()).with_value(src).cloned(); - T::to_def(&self.imp, src) - } - - pub fn to_module_def(&self, file: FileId) -> Option { - self.imp.to_module_def(file) - } - - pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> { - self.imp.scope(node) - } - - pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> { - self.imp.scope_at_offset(node, offset) - } - - pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> { - self.imp.scope_for_def(def) - } - - pub fn assert_contains_node(&self, node: &SyntaxNode) { - self.imp.assert_contains_node(node) - } - - pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> bool { - self.imp.is_unsafe_method_call(method_call_expr) - } - - pub fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool { - self.imp.is_unsafe_ref_expr(ref_expr) - } - - pub fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool { - self.imp.is_unsafe_ident_pat(ident_pat) - } -} - -impl<'db> SemanticsImpl<'db> { - fn new(db: &'db dyn HirDatabase) -> Self { - SemanticsImpl { - db, - s2d_cache: Default::default(), - cache: Default::default(), - expansion_info_cache: Default::default(), - } - } - - fn parse(&self, file_id: FileId) -> ast::SourceFile { - let tree = self.db.parse(file_id).tree(); - self.cache(tree.syntax().clone(), file_id.into()); - tree - } - - fn expand(&self, macro_call: &ast::MacroCall) -> Option { - let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call); - let sa = self.analyze2(macro_call.map(|it| it.syntax()), None); - let file_id = sa.expand(self.db, macro_call)?; - let node = self.db.parse_or_expand(file_id)?; - self.cache(node.clone(), file_id); - Some(node) - } - - fn expand_hypothetical( - &self, - actual_macro_call: &ast::MacroCall, - hypothetical_args: &ast::TokenTree, - token_to_map: SyntaxToken, - ) -> Option<(SyntaxNode, SyntaxToken)> { - let macro_call = - self.find_file(actual_macro_call.syntax().clone()).with_value(actual_macro_call); - let sa = self.analyze2(macro_call.map(|it| it.syntax()), None); - let krate = sa.resolver.krate()?; - 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, - ) - } - - fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { - let _p = profile::span("descend_into_macros"); - let parent = token.parent(); - let parent = self.find_file(parent); - let sa = self.analyze2(parent.as_ref(), None); - - let token = successors(Some(parent.with_value(token)), |token| { - self.db.check_canceled(); - let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?; - let tt = macro_call.token_tree()?; - if !tt.syntax().text_range().contains_range(token.value.text_range()) { - return None; - } - let file_id = sa.expand(self.db, token.with_value(¯o_call))?; - 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())?; - - self.cache(find_root(&token.value.parent()), token.file_id); - - Some(token) - }) - .last() - .unwrap(); - - token.value - } - - fn descend_node_at_offset( - &self, - node: &SyntaxNode, - offset: TextSize, - ) -> impl Iterator + '_ { - // Handle macro token cases - node.token_at_offset(offset) - .map(|token| self.descend_into_macros(token)) - .map(|it| self.ancestors_with_macros(it.parent())) - .flatten() - } - - fn original_range(&self, node: &SyntaxNode) -> FileRange { - let node = self.find_file(node.clone()); - original_range(self.db, node.as_ref()) - } - - fn diagnostics_display_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { - let src = diagnostics.display_source(); - let root = self.db.parse_or_expand(src.file_id).unwrap(); - let node = src.value.to_node(&root); - self.cache(root, src.file_id); - original_range(self.db, src.with_value(&node)) - } - - fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator + '_ { - let node = self.find_file(node); - node.ancestors_with_macros(self.db.upcast()).map(|it| it.value) - } - - fn ancestors_at_offset_with_macros( - &self, - node: &SyntaxNode, - offset: TextSize, - ) -> impl Iterator + '_ { - node.token_at_offset(offset) - .map(|token| self.ancestors_with_macros(token.parent())) - .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) - } - - fn type_of_expr(&self, expr: &ast::Expr) -> Option { - self.analyze(expr.syntax()).type_of_expr(self.db, &expr) - } - - fn type_of_pat(&self, pat: &ast::Pat) -> Option { - self.analyze(pat.syntax()).type_of_pat(self.db, &pat) - } - - fn type_of_self(&self, param: &ast::SelfParam) -> Option { - self.analyze(param.syntax()).type_of_self(self.db, ¶m) - } - - fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { - self.analyze(call.syntax()).resolve_method_call(self.db, call) - } - - fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option { - // 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) - } - - fn resolve_field(&self, field: &ast::FieldExpr) -> Option { - self.analyze(field.syntax()).resolve_field(self.db, field) - } - - fn resolve_record_field(&self, field: &ast::RecordExprField) -> Option<(Field, Option)> { - self.analyze(field.syntax()).resolve_record_field(self.db, field) - } - - fn resolve_record_field_pat(&self, field: &ast::RecordPatField) -> Option { - self.analyze(field.syntax()).resolve_record_field_pat(self.db, field) - } - - fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option { - 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) - } - - fn resolve_path(&self, path: &ast::Path) -> Option { - self.analyze(path.syntax()).resolve_path(self.db, path) - } - - fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option { - 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 - } - }) - } - - fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option { - self.analyze(record_lit.syntax()).resolve_variant(self.db, record_lit) - } - - fn lower_path(&self, path: &ast::Path) -> Option { - let src = self.find_file(path.syntax().clone()); - Path::from_src(path.clone(), &Hygiene::new(self.db.upcast(), src.file_id.into())) - } - - fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option { - self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat) - } - - fn record_literal_missing_fields(&self, literal: &ast::RecordExpr) -> Vec<(Field, Type)> { - self.analyze(literal.syntax()) - .record_literal_missing_fields(self.db, literal) - .unwrap_or_default() - } - - fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> { - self.analyze(pattern.syntax()) - .record_pattern_missing_fields(self.db, pattern) - .unwrap_or_default() - } - - fn with_ctx T, T>(&self, f: F) -> T { - let mut cache = self.s2d_cache.borrow_mut(); - let mut ctx = SourceToDefCtx { db: self.db, cache: &mut *cache }; - f(&mut ctx) - } - - fn to_module_def(&self, file: FileId) -> Option { - self.with_ctx(|ctx| ctx.file_to_def(file)).map(Module::from) - } - - fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> { - let node = self.find_file(node.clone()); - let resolver = self.analyze2(node.as_ref(), None).resolver; - SemanticsScope { db: self.db, resolver } - } - - fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> { - let node = self.find_file(node.clone()); - let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver; - SemanticsScope { db: self.db, resolver } - } - - fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> { - let resolver = def.id.resolver(self.db.upcast()); - SemanticsScope { db: self.db, resolver } - } - - fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer { - let src = self.find_file(node.clone()); - self.analyze2(src.as_ref(), None) - } - - fn analyze2(&self, src: InFile<&SyntaxNode>, offset: Option) -> SourceAnalyzer { - let _p = profile::span("Semantics::analyze2"); - - let container = match self.with_ctx(|ctx| ctx.find_container(src)) { - Some(it) => it, - None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src), - }; - - let resolver = match container { - ChildContainer::DefWithBodyId(def) => { - return SourceAnalyzer::new_for_body(self.db, def, src, offset) - } - 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()), - ChildContainer::TypeAliasId(it) => it.resolver(self.db.upcast()), - ChildContainer::GenericDefId(it) => it.resolver(self.db.upcast()), - }; - SourceAnalyzer::new_for_resolver(resolver, src) - } - - fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) { - 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)) - } - - fn assert_contains_node(&self, node: &SyntaxNode) { - self.find_file(node.clone()); - } - - fn lookup(&self, root_node: &SyntaxNode) -> Option { - let cache = self.cache.borrow(); - cache.get(root_node).copied() - } - - fn find_file(&self, node: SyntaxNode) -> InFile { - 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::>() - .join(", ") - ) - }); - InFile::new(file_id, node) - } - - pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> bool { - method_call_expr - .expr() - .and_then(|expr| { - let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr { - field_expr - } else { - return None; - }; - 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)?; - let is_unsafe = func.has_self_param(self.db) - && matches!(func.params(self.db).first(), Some(TypeRef::Reference(..))); - Some(is_unsafe) - }) - .unwrap_or(false) - } - - pub fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool { - 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. - } - - pub fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool { - if !ident_pat.ref_token().is_some() { - return false; - } - - ident_pat - .syntax() - .parent() - .and_then(|parent| { - // `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 - // `RecordPat` so we can get the containing type. - let record_pat = ast::RecordPatField::cast(parent.clone()) - .and_then(|record_pat| record_pat.syntax().parent()) - .or_else(|| Some(parent.clone())) - .and_then(|parent| { - ast::RecordPatFieldList::cast(parent)? - .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) - } -} - -pub trait ToDef: AstNode + Clone { - type Def; - - fn to_def(sema: &SemanticsImpl, src: InFile) -> Option; -} - -macro_rules! to_def_impls { - ($(($def:path, $ast:path, $meth:ident)),* ,) => {$( - impl ToDef for $ast { - type Def = $def; - fn to_def(sema: &SemanticsImpl, src: InFile) -> Option { - sema.with_ctx(|ctx| ctx.$meth(src)).map(<$def>::from) - } - } - )*} -} - -to_def_impls![ - (crate::Module, ast::Module, module_to_def), - (crate::Struct, ast::Struct, struct_to_def), - (crate::Enum, ast::Enum, enum_to_def), - (crate::Union, ast::Union, union_to_def), - (crate::Trait, ast::Trait, trait_to_def), - (crate::ImplDef, ast::Impl, impl_to_def), - (crate::TypeAlias, ast::TypeAlias, type_alias_to_def), - (crate::Const, ast::Const, const_to_def), - (crate::Static, ast::Static, static_to_def), - (crate::Function, ast::Fn, fn_to_def), - (crate::Field, ast::RecordField, record_field_to_def), - (crate::Field, ast::TupleField, tuple_field_to_def), - (crate::EnumVariant, ast::Variant, enum_variant_to_def), - (crate::TypeParam, ast::TypeParam, type_param_to_def), - (crate::MacroDef, ast::MacroCall, macro_call_to_def), // this one is dubious, not all calls are macros - (crate::Local, ast::IdentPat, bind_pat_to_def), -]; - -fn find_root(node: &SyntaxNode) -> SyntaxNode { - node.ancestors().last().unwrap() -} - -#[derive(Debug)] -pub struct SemanticsScope<'a> { - pub db: &'a dyn HirDatabase, - resolver: Resolver, -} - -impl<'a> SemanticsScope<'a> { - pub fn module(&self) -> Option { - Some(Module { id: self.resolver.module()? }) - } - - pub fn krate(&self) -> Option { - Some(Crate { id: self.resolver.krate()? }) - } - - /// Note: `FxHashSet` 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 { - let resolver = &self.resolver; - resolver.traits_in_scope(self.db.upcast()) - } - - pub fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) { - let resolver = &self.resolver; - - resolver.process_all_names(self.db.upcast(), &mut |name, def| { - let def = match def { - resolver::ScopeDef::PerNs(it) => { - let items = ScopeDef::all_items(it); - for item in items { - f(name.clone(), item); - } - return; - } - resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()), - resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()), - resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(TypeParam { id }), - resolver::ScopeDef::Local(pat_id) => { - let parent = resolver.body_owner().unwrap().into(); - ScopeDef::Local(Local { parent, pat_id }) - } - }; - f(name, def) - }) - } - - pub fn resolve_hir_path(&self, path: &Path) -> Option { - resolve_hir_path(self.db, &self.resolver, path) - } - - /// Resolves a path where we know it is a qualifier of another path. - /// - /// For example, if we have: - /// ``` - /// mod my { - /// pub mod foo { - /// struct Bar; - /// } - /// - /// pub fn foo() {} - /// } - /// ``` - /// then we know that `foo` in `my::foo::Bar` refers to the module, not the function. - pub fn resolve_hir_path_qualifier(&self, path: &Path) -> Option { - resolve_hir_path_qualifier(self.db, &self.resolver, path) - } -} - -// FIXME: Change `HasSource` trait to work with `Semantics` and remove this? -pub fn original_range(db: &dyn HirDatabase, node: InFile<&SyntaxNode>) -> FileRange { - if let Some(range) = original_range_opt(db, node) { - let original_file = range.file_id.original_file(db.upcast()); - if range.file_id == original_file.into() { - return FileRange { file_id: original_file, range: range.value }; - } - - log::error!("Fail to mapping up more for {:?}", range); - return FileRange { file_id: range.file_id.original_file(db.upcast()), range: range.value }; - } - - // Fall back to whole macro call - if let Some(expansion) = node.file_id.expansion_info(db.upcast()) { - if let Some(call_node) = expansion.call_node() { - return FileRange { - file_id: call_node.file_id.original_file(db.upcast()), - range: call_node.value.text_range(), - }; - } - } - - FileRange { file_id: node.file_id.original_file(db.upcast()), range: node.value.text_range() } -} - -fn original_range_opt( - db: &dyn HirDatabase, - node: InFile<&SyntaxNode>, -) -> Option> { - let expansion = node.file_id.expansion_info(db.upcast())?; - - // the input node has only one token ? - let single = skip_trivia_token(node.value.first_token()?, Direction::Next)? - == skip_trivia_token(node.value.last_token()?, Direction::Prev)?; - - Some(node.value.descendants().find_map(|it| { - let first = skip_trivia_token(it.first_token()?, Direction::Next)?; - let first = ascend_call_token(db, &expansion, node.with_value(first))?; - - let last = skip_trivia_token(it.last_token()?, Direction::Prev)?; - let last = ascend_call_token(db, &expansion, node.with_value(last))?; - - if (!single && first == last) || (first.file_id != last.file_id) { - return None; - } - - Some(first.with_value(first.value.text_range().cover(last.value.text_range()))) - })?) -} - -fn ascend_call_token( - db: &dyn HirDatabase, - expansion: &ExpansionInfo, - token: InFile, -) -> Option> { - let (mapped, origin) = expansion.map_token_up(token.as_ref())?; - if origin != Origin::Call { - return None; - } - if let Some(info) = mapped.file_id.expansion_info(db.upcast()) { - return ascend_call_token(db, &info, mapped); - } - Some(mapped) -} -- cgit v1.2.3