From 75689f2ad8f74bbbff1f44f75330b2e4d43ed44a Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 31 Mar 2022 11:12:08 +0200 Subject: [PATCH] internal: Enforce Resolver to always have a module scope --- crates/hir/src/lib.rs | 26 ++-- crates/hir/src/semantics.rs | 121 +++++++++++------- crates/hir/src/source_analyzer.rs | 33 ++--- crates/hir_def/src/resolver.rs | 45 +++---- crates/hir_ty/src/chalk_db.rs | 5 +- crates/hir_ty/src/infer.rs | 2 +- crates/hir_ty/src/infer/expr.rs | 13 +- crates/hir_ty/src/infer/path.rs | 9 +- crates/hir_ty/src/lower.rs | 21 ++- crates/ide/src/hover.rs | 2 +- crates/ide/src/hover/render.rs | 4 +- crates/ide/src/inlay_hints.rs | 7 +- crates/ide/src/lib.rs | 2 +- crates/ide/src/syntax_highlighting.rs | 8 +- .../ide/src/syntax_highlighting/highlight.rs | 22 ++-- .../src/handlers/add_explicit_type.rs | 2 +- .../src/handlers/add_missing_impl_members.rs | 2 +- .../src/handlers/add_missing_match_arms.rs | 5 +- .../src/handlers/add_return_type.rs | 2 +- .../src/handlers/convert_bool_then.rs | 2 +- .../src/handlers/convert_into_to_from.rs | 4 +- .../handlers/convert_iter_for_each_to_for.rs | 35 ++--- .../src/handlers/expand_glob_import.rs | 4 +- .../src/handlers/extract_function.rs | 4 +- .../extract_struct_from_enum_variant.rs | 2 +- .../src/handlers/fix_visibility.rs | 4 +- .../src/handlers/generate_constant.rs | 4 +- .../generate_default_from_enum_variant.rs | 2 +- .../src/handlers/generate_default_from_new.rs | 2 +- .../src/handlers/generate_delegate_methods.rs | 2 +- .../src/handlers/generate_deref.rs | 2 +- .../generate_documentation_template.rs | 2 +- .../handlers/generate_from_impl_for_enum.rs | 2 +- .../src/handlers/generate_function.rs | 15 +-- .../src/handlers/generate_getter.rs | 37 +++--- .../handlers/generate_is_empty_from_len.rs | 2 +- .../ide_assists/src/handlers/inline_call.rs | 12 +- .../src/handlers/promote_local_to_const.rs | 2 +- .../src/handlers/qualify_method_call.rs | 2 +- .../replace_derive_with_manual_impl.rs | 4 +- .../replace_qualified_name_with_use.rs | 8 +- .../src/handlers/unwrap_result_return_type.rs | 4 +- .../handlers/wrap_return_type_in_result.rs | 4 +- crates/ide_completion/src/completions.rs | 10 +- .../src/completions/attribute.rs | 2 +- .../src/completions/attribute/cfg.rs | 29 ++--- .../src/completions/attribute/derive.rs | 2 +- crates/ide_completion/src/completions/dot.rs | 2 +- .../src/completions/flyimport.rs | 2 +- .../src/completions/fn_param.rs | 22 ++-- crates/ide_completion/src/completions/mod_.rs | 2 +- .../ide_completion/src/completions/pattern.rs | 4 +- .../src/completions/qualified_path.rs | 47 ++++--- .../ide_completion/src/completions/record.rs | 11 +- .../src/completions/trait_impl.rs | 2 +- crates/ide_completion/src/completions/use_.rs | 2 +- crates/ide_completion/src/completions/vis.rs | 21 ++- crates/ide_completion/src/context.rs | 19 +-- crates/ide_completion/src/lib.rs | 2 +- crates/ide_completion/src/render/variant.rs | 2 +- crates/ide_completion/src/snippet.rs | 2 +- crates/ide_db/src/famous_defs.rs | 4 +- crates/ide_db/src/imports/import_assets.rs | 21 +-- crates/ide_db/src/path_transform.rs | 13 +- .../src/handlers/missing_fields.rs | 8 +- .../src/handlers/type_mismatch.rs | 2 +- crates/ide_ssr/src/from_comment.rs | 2 +- crates/ide_ssr/src/lib.rs | 11 +- crates/ide_ssr/src/matching.rs | 6 +- crates/ide_ssr/src/resolving.rs | 8 +- crates/ide_ssr/src/tests.rs | 8 +- 71 files changed, 360 insertions(+), 398 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 171131c99eb8..8b99662685c5 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -159,7 +159,7 @@ pub fn dependencies(self, db: &dyn HirDatabase) -> Vec { .map(|dep| { let krate = Crate { id: dep.crate_id }; let name = dep.as_name(); - CrateDependency { krate, name, } + CrateDependency { krate, name } }) .collect() } @@ -2224,7 +2224,7 @@ pub(crate) fn by_name(db: &dyn HirDatabase, krate: Crate, name: &str) -> Option< Some(BuiltinAttr { krate: Some(krate.id), idx }) } - pub(crate) fn builtin(name: &str) -> Option { + fn builtin(name: &str) -> Option { hir_def::builtin_attr::INERT_ATTRIBUTES .iter() .position(|tool| tool.name == name) @@ -2263,7 +2263,7 @@ pub(crate) fn by_name(db: &dyn HirDatabase, krate: Crate, name: &str) -> Option< Some(ToolModule { krate: Some(krate.id), idx }) } - pub(crate) fn builtin(name: &str) -> Option { + fn builtin(name: &str) -> Option { hir_def::builtin_attr::TOOL_MODULES .iter() .position(|&tool| tool == name) @@ -2613,13 +2613,9 @@ pub struct Type { } impl Type { - pub(crate) fn new_with_resolver( - db: &dyn HirDatabase, - resolver: &Resolver, - ty: Ty, - ) -> Option { - let krate = resolver.krate()?; - Some(Type::new_with_resolver_inner(db, krate, resolver, ty)) + pub(crate) fn new_with_resolver(db: &dyn HirDatabase, resolver: &Resolver, ty: Ty) -> Type { + let krate = resolver.krate(); + Type::new_with_resolver_inner(db, krate, resolver, ty) } pub(crate) fn new_with_resolver_inner( @@ -3038,10 +3034,7 @@ fn iterate_method_candidates_dyn( // There should be no inference vars in types passed here let canonical = hir_ty::replace_errors_with_variables(&self.ty); - let krate = match scope.krate() { - Some(k) => k, - None => return, - }; + let krate = scope.krate(); let environment = scope.resolver().generic_def().map_or_else( || Arc::new(TraitEnvironment::empty(krate.id)), |d| db.trait_environment(d), @@ -3098,10 +3091,7 @@ fn iterate_path_candidates_dyn( ) { let canonical = hir_ty::replace_errors_with_variables(&self.ty); - let krate = match scope.krate() { - Some(k) => k, - None => return, - }; + let krate = scope.krate(); let environment = scope.resolver().generic_def().map_or_else( || Arc::new(TraitEnvironment::empty(krate.id)), |d| db.trait_environment(d), diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 4afdde6494f3..8a700027a3ad 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -403,11 +403,15 @@ pub fn to_module_defs(&self, file: FileId) -> impl Iterator { self.imp.to_module_def(file) } - pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> { + pub fn scope(&self, node: &SyntaxNode) -> Option> { self.imp.scope(node) } - pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> { + pub fn scope_at_offset( + &self, + node: &SyntaxNode, + offset: TextSize, + ) -> Option> { self.imp.scope_at_offset(node, offset) } @@ -456,7 +460,7 @@ fn parse_or_expand(&self, file_id: HirFileId) -> Option { } fn expand(&self, macro_call: &ast::MacroCall) -> Option { - let sa = self.analyze_no_infer(macro_call.syntax()); + let sa = self.analyze_no_infer(macro_call.syntax())?; let file_id = sa.expand(self.db, InFile::new(sa.file_id, macro_call))?; let node = self.parse_or_expand(file_id)?; Some(node) @@ -535,9 +539,9 @@ fn speculative_expand( token_to_map: SyntaxToken, ) -> Option<(SyntaxNode, SyntaxToken)> { let SourceAnalyzer { file_id, resolver, .. } = - self.analyze_no_infer(actual_macro_call.syntax()); + self.analyze_no_infer(actual_macro_call.syntax())?; let macro_call = InFile::new(file_id, actual_macro_call); - let krate = resolver.krate()?; + let krate = resolver.krate(); let macro_call_id = macro_call.as_call_id(self.db.upcast(), krate, |path| { resolver .resolve_path_as_macro(self.db.upcast(), &path) @@ -669,7 +673,10 @@ fn descend_into_macros_impl( Some(it) => it, None => return, }; - let sa = self.analyze_no_infer(&parent); + let sa = match self.analyze_no_infer(&parent) { + Some(it) => it, + None => return, + }; let mut stack: SmallVec<[_; 4]> = smallvec![InFile::new(sa.file_id, token)]; let mut cache = self.expansion_info_cache.borrow_mut(); let mut mcache = self.macro_call_cache.borrow_mut(); @@ -903,70 +910,74 @@ fn resolve_label(&self, lifetime: &ast::Lifetime) -> Option