aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/hir/src/source_analyzer.rs47
-rw-r--r--crates/hir_ty/src/diagnostics/unsafe_check.rs12
-rw-r--r--crates/hir_ty/src/lib.rs19
-rw-r--r--crates/ide/src/syntax_highlighting/test_data/highlighting.html4
-rw-r--r--crates/ide/src/syntax_highlighting/tests.rs4
5 files changed, 59 insertions, 27 deletions
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs
index 30a8e513d..626c3078a 100644
--- a/crates/hir/src/source_analyzer.rs
+++ b/crates/hir/src/source_analyzer.rs
@@ -222,16 +222,19 @@ impl SourceAnalyzer {
222 db: &dyn HirDatabase, 222 db: &dyn HirDatabase,
223 path: &ast::Path, 223 path: &ast::Path,
224 ) -> Option<PathResolution> { 224 ) -> Option<PathResolution> {
225 let mut prefer_value_ns = false;
225 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { 226 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) {
226 let expr_id = self.expr_id(db, &path_expr.into())?; 227 let expr_id = self.expr_id(db, &path_expr.into())?;
227 if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { 228 let infer = self.infer.as_ref()?;
229 if let Some(assoc) = infer.assoc_resolutions_for_expr(expr_id) {
228 return Some(PathResolution::AssocItem(assoc.into())); 230 return Some(PathResolution::AssocItem(assoc.into()));
229 } 231 }
230 if let Some(VariantId::EnumVariantId(variant)) = 232 if let Some(VariantId::EnumVariantId(variant)) =
231 self.infer.as_ref()?.variant_resolution_for_expr(expr_id) 233 infer.variant_resolution_for_expr(expr_id)
232 { 234 {
233 return Some(PathResolution::Def(ModuleDef::Variant(variant.into()))); 235 return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
234 } 236 }
237 prefer_value_ns = true;
235 } 238 }
236 239
237 if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) { 240 if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) {
@@ -277,7 +280,7 @@ impl SourceAnalyzer {
277 } 280 }
278 } 281 }
279 282
280 resolve_hir_path(db, &self.resolver, &hir_path) 283 resolve_hir_path_(db, &self.resolver, &hir_path, prefer_value_ns)
281 } 284 }
282 285
283 pub(crate) fn record_literal_missing_fields( 286 pub(crate) fn record_literal_missing_fields(
@@ -447,12 +450,22 @@ fn adjust(
447 .map(|(_ptr, scope)| *scope) 450 .map(|(_ptr, scope)| *scope)
448} 451}
449 452
453#[inline]
450pub(crate) fn resolve_hir_path( 454pub(crate) fn resolve_hir_path(
451 db: &dyn HirDatabase, 455 db: &dyn HirDatabase,
452 resolver: &Resolver, 456 resolver: &Resolver,
453 path: &Path, 457 path: &Path,
454) -> Option<PathResolution> { 458) -> Option<PathResolution> {
455 let types = 459 resolve_hir_path_(db, resolver, path, false)
460}
461
462fn resolve_hir_path_(
463 db: &dyn HirDatabase,
464 resolver: &Resolver,
465 path: &Path,
466 prefer_value_ns: bool,
467) -> Option<PathResolution> {
468 let types = || {
456 resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty { 469 resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty {
457 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), 470 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
458 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), 471 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
@@ -463,10 +476,11 @@ pub(crate) fn resolve_hir_path(
463 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), 476 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
464 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), 477 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
465 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), 478 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
466 }); 479 })
480 };
467 481
468 let body_owner = resolver.body_owner(); 482 let body_owner = resolver.body_owner();
469 let values = 483 let values = || {
470 resolver.resolve_path_in_value_ns_fully(db.upcast(), path.mod_path()).and_then(|val| { 484 resolver.resolve_path_in_value_ns_fully(db.upcast(), path.mod_path()).and_then(|val| {
471 let res = match val { 485 let res = match val {
472 ValueNs::LocalBinding(pat_id) => { 486 ValueNs::LocalBinding(pat_id) => {
@@ -482,18 +496,25 @@ pub(crate) fn resolve_hir_path(
482 ValueNs::GenericParam(it) => PathResolution::ConstParam(it.into()), 496 ValueNs::GenericParam(it) => PathResolution::ConstParam(it.into()),
483 }; 497 };
484 Some(res) 498 Some(res)
485 }); 499 })
500 };
486 501
487 let items = resolver 502 let items = || {
488 .resolve_module_path_in_items(db.upcast(), path.mod_path()) 503 resolver
489 .take_types() 504 .resolve_module_path_in_items(db.upcast(), path.mod_path())
490 .map(|it| PathResolution::Def(it.into())); 505 .take_types()
506 .map(|it| PathResolution::Def(it.into()))
507 };
491 508
492 types.or(values).or(items).or_else(|| { 509 let macros = || {
493 resolver 510 resolver
494 .resolve_path_as_macro(db.upcast(), path.mod_path()) 511 .resolve_path_as_macro(db.upcast(), path.mod_path())
495 .map(|def| PathResolution::Macro(def.into())) 512 .map(|def| PathResolution::Macro(def.into()))
496 }) 513 };
514
515 if prefer_value_ns { values().or_else(types) } else { types().or_else(values) }
516 .or_else(items)
517 .or_else(macros)
497} 518}
498 519
499/// Resolves a path where we know it is a qualifier of another path. 520/// Resolves a path where we know it is a qualifier of another path.
diff --git a/crates/hir_ty/src/diagnostics/unsafe_check.rs b/crates/hir_ty/src/diagnostics/unsafe_check.rs
index 6dc862826..9c506112d 100644
--- a/crates/hir_ty/src/diagnostics/unsafe_check.rs
+++ b/crates/hir_ty/src/diagnostics/unsafe_check.rs
@@ -12,8 +12,7 @@ use hir_def::{
12use hir_expand::diagnostics::DiagnosticSink; 12use hir_expand::diagnostics::DiagnosticSink;
13 13
14use crate::{ 14use crate::{
15 db::HirDatabase, diagnostics::MissingUnsafe, lower::CallableDefId, ApplicationTy, 15 db::HirDatabase, diagnostics::MissingUnsafe, ApplicationTy, InferenceResult, Ty, TypeCtor,
16 InferenceResult, Ty, TypeCtor,
17}; 16};
18 17
19pub(super) struct UnsafeValidator<'a, 'b: 'a> { 18pub(super) struct UnsafeValidator<'a, 'b: 'a> {
@@ -87,13 +86,8 @@ fn walk_unsafe(
87) { 86) {
88 let expr = &body.exprs[current]; 87 let expr = &body.exprs[current];
89 match expr { 88 match expr {
90 Expr::Call { callee, .. } => { 89 &Expr::Call { callee, .. } => {
91 let ty = &infer[*callee]; 90 if let Some(func) = infer[callee].as_fn_def() {
92 if let &Ty::Apply(ApplicationTy {
93 ctor: TypeCtor::FnDef(CallableDefId::FunctionId(func)),
94 ..
95 }) = ty
96 {
97 if db.function_data(func).is_unsafe { 91 if db.function_data(func).is_unsafe {
98 unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); 92 unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block });
99 } 93 }
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index d47f975d2..6bec389f8 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -29,8 +29,8 @@ use base_db::{salsa, CrateId};
29use hir_def::{ 29use hir_def::{
30 expr::ExprId, 30 expr::ExprId,
31 type_ref::{Mutability, Rawness}, 31 type_ref::{Mutability, Rawness},
32 AdtId, AssocContainerId, DefWithBodyId, GenericDefId, HasModule, LifetimeParamId, Lookup, 32 AdtId, AssocContainerId, DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId,
33 TraitId, TypeAliasId, TypeParamId, 33 Lookup, TraitId, TypeAliasId, TypeParamId,
34}; 34};
35use itertools::Itertools; 35use itertools::Itertools;
36 36
@@ -43,10 +43,9 @@ use crate::{
43 43
44pub use autoderef::autoderef; 44pub use autoderef::autoderef;
45pub use infer::{InferTy, InferenceResult}; 45pub use infer::{InferTy, InferenceResult};
46pub use lower::CallableDefId;
47pub use lower::{ 46pub use lower::{
48 associated_type_shorthand_candidates, callable_item_sig, ImplTraitLoweringMode, TyDefId, 47 associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
49 TyLoweringContext, ValueTyDefId, 48 TyDefId, TyLoweringContext, ValueTyDefId,
50}; 49};
51pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; 50pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
52 51
@@ -824,6 +823,16 @@ impl Ty {
824 } 823 }
825 } 824 }
826 825
826 pub fn as_fn_def(&self) -> Option<FunctionId> {
827 match self {
828 &Ty::Apply(ApplicationTy {
829 ctor: TypeCtor::FnDef(CallableDefId::FunctionId(func)),
830 ..
831 }) => Some(func),
832 _ => None,
833 }
834 }
835
827 pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<FnSig> { 836 pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<FnSig> {
828 match self { 837 match self {
829 Ty::Apply(a_ty) => match a_ty.ctor { 838 Ty::Apply(a_ty) => match a_ty.ctor {
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
index 237149566..2f983c0b8 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
@@ -108,6 +108,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
108 <span class="brace">}</span> 108 <span class="brace">}</span>
109<span class="brace">}</span> 109<span class="brace">}</span>
110 110
111<span class="keyword">fn</span> <span class="function declaration">str</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
112 <span class="function">str</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
113<span class="brace">}</span>
114
111<span class="keyword">static</span> <span class="keyword">mut</span> <span class="static declaration mutable unsafe">STATIC_MUT</span><span class="colon">:</span> <span class="builtin_type">i32</span> <span class="operator">=</span> <span class="numeric_literal">0</span><span class="semicolon">;</span> 115<span class="keyword">static</span> <span class="keyword">mut</span> <span class="static declaration mutable unsafe">STATIC_MUT</span><span class="colon">:</span> <span class="builtin_type">i32</span> <span class="operator">=</span> <span class="numeric_literal">0</span><span class="semicolon">;</span>
112 116
113<span class="keyword">fn</span> <span class="function declaration">foo</span><span class="angle">&lt;</span><span class="lifetime declaration">'a</span><span class="comma">,</span> <span class="type_param declaration">T</span><span class="angle">&gt;</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="type_param">T</span> <span class="brace">{</span> 117<span class="keyword">fn</span> <span class="function declaration">foo</span><span class="angle">&lt;</span><span class="lifetime declaration">'a</span><span class="comma">,</span> <span class="type_param declaration">T</span><span class="angle">&gt;</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="type_param">T</span> <span class="brace">{</span>
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs
index a62704c39..1854da914 100644
--- a/crates/ide/src/syntax_highlighting/tests.rs
+++ b/crates/ide/src/syntax_highlighting/tests.rs
@@ -81,6 +81,10 @@ impl FooCopy {
81 } 81 }
82} 82}
83 83
84fn str() {
85 str();
86}
87
84static mut STATIC_MUT: i32 = 0; 88static mut STATIC_MUT: i32 = 0;
85 89
86fn foo<'a, T>() -> T { 90fn foo<'a, T>() -> T {