From 20ae41c1a12963e938cb3bd4c7c84007412d6fa6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 6 May 2021 23:23:50 +0200 Subject: Reuse database in LowerCtx --- crates/hir/src/semantics.rs | 2 +- crates/hir/src/source_analyzer.rs | 5 ++--- crates/hir_def/src/body.rs | 2 +- crates/hir_def/src/body/lower.rs | 4 ++-- crates/hir_def/src/path.rs | 6 +++--- crates/hir_def/src/path/lower.rs | 14 +++++--------- crates/hir_ty/src/display.rs | 2 +- 7 files changed, 15 insertions(+), 20 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index d53d81c07..62500602a 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -855,7 +855,7 @@ impl<'a> SemanticsScope<'a> { /// necessary a heuristic, as it doesn't take hygiene into account. pub fn speculative_resolve(&self, path: &ast::Path) -> Option { let ctx = body::LowerCtx::new(self.db.upcast(), self.file_id); - let path = Path::from_src(self.db.upcast(), path.clone(), &ctx)?; + let path = Path::from_src(path.clone(), &ctx)?; resolve_hir_path(self.db, &self.resolver, &path) } } diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index 186421cbd..b5c65808e 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -204,8 +204,7 @@ impl SourceAnalyzer { macro_call: InFile<&ast::MacroCall>, ) -> Option { let ctx = body::LowerCtx::new(db.upcast(), macro_call.file_id); - let path = - macro_call.value.path().and_then(|ast| Path::from_src(db.upcast(), ast, &ctx))?; + let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &ctx))?; self.resolver.resolve_path_as_macro(db.upcast(), path.mod_path()).map(|it| it.into()) } @@ -285,7 +284,7 @@ impl SourceAnalyzer { // This must be a normal source file rather than macro file. let hygiene = Hygiene::new(db.upcast(), self.file_id); let ctx = body::LowerCtx::with_hygiene(db.upcast(), &hygiene); - let hir_path = Path::from_src(db.upcast(), path.clone(), &ctx)?; + let hir_path = Path::from_src(path.clone(), &ctx)?; // Case where path is a qualifier of another path, e.g. foo::bar::Baz where we // trying to resolve foo::bar. diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs index 9510a8575..8360426f1 100644 --- a/crates/hir_def/src/body.rs +++ b/crates/hir_def/src/body.rs @@ -194,7 +194,7 @@ impl Expander { fn parse_path(&mut self, db: &dyn DefDatabase, path: ast::Path) -> Option { let ctx = LowerCtx::with_hygiene(db, &self.cfg_expander.hygiene); - Path::from_src(db, path, &ctx) + Path::from_src(path, &ctx) } fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option { diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index e4fa7f9c9..75dc19c11 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -41,7 +41,7 @@ use crate::{ use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; pub struct LowerCtx<'a> { - db: &'a dyn DefDatabase, + pub db: &'a dyn DefDatabase, hygiene: Hygiene, file_id: Option, source_ast_id_map: Option>, @@ -70,7 +70,7 @@ impl<'a> LowerCtx<'a> { } pub(crate) fn lower_path(&self, ast: ast::Path) -> Option { - Path::from_src(self.db, ast, self) + Path::from_src(ast, self) } pub(crate) fn ast_id(&self, item: &N) -> Option> { diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index 64bff318e..a43441b1c 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs @@ -49,7 +49,7 @@ pub enum ImportAlias { impl ModPath { pub fn from_src(db: &dyn DefDatabase, path: ast::Path, hygiene: &Hygiene) -> Option { let ctx = LowerCtx::with_hygiene(db, hygiene); - lower::lower_path(db, path, &ctx).map(|it| (*it.mod_path).clone()) + lower::lower_path(path, &ctx).map(|it| (*it.mod_path).clone()) } pub fn from_segments(kind: PathKind, segments: impl IntoIterator) -> ModPath { @@ -169,8 +169,8 @@ pub enum GenericArg { impl Path { /// Converts an `ast::Path` to `Path`. Works with use trees. /// It correctly handles `$crate` based path from macro call. - pub fn from_src(db: &dyn DefDatabase, path: ast::Path, ctx: &LowerCtx) -> Option { - lower::lower_path(db, path, ctx) + pub fn from_src(path: ast::Path, ctx: &LowerCtx) -> Option { + lower::lower_path(path, ctx) } /// Converts a known mod path to `Path`. diff --git a/crates/hir_def/src/path/lower.rs b/crates/hir_def/src/path/lower.rs index 3b3a3738f..a873325b2 100644 --- a/crates/hir_def/src/path/lower.rs +++ b/crates/hir_def/src/path/lower.rs @@ -2,7 +2,7 @@ mod lower_use; -use crate::{db::DefDatabase, intern::Interned}; +use crate::intern::Interned; use std::sync::Arc; use either::Either; @@ -20,11 +20,7 @@ pub(super) use lower_use::lower_use_tree; /// Converts an `ast::Path` to `Path`. Works with use trees. /// It correctly handles `$crate` based path from macro call. -pub(super) fn lower_path( - db: &dyn DefDatabase, - mut path: ast::Path, - ctx: &LowerCtx, -) -> Option { +pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx) -> Option { let mut kind = PathKind::Plain; let mut type_anchor = None; let mut segments = Vec::new(); @@ -40,7 +36,7 @@ pub(super) fn lower_path( match segment.kind()? { ast::PathSegmentKind::Name(name_ref) => { // FIXME: this should just return name - match hygiene.name_ref_to_name(db.upcast(), name_ref) { + match hygiene.name_ref_to_name(ctx.db.upcast(), name_ref) { Either::Left(name) => { let args = segment .generic_arg_list() @@ -75,7 +71,7 @@ pub(super) fn lower_path( } // >::Foo desugars to Trait::Foo Some(trait_ref) => { - let path = Path::from_src(db, trait_ref.path()?, ctx)?; + let path = Path::from_src(trait_ref.path()?, ctx)?; let mod_path = (*path.mod_path).clone(); let num_segments = path.mod_path.segments.len(); kind = mod_path.kind; @@ -137,7 +133,7 @@ pub(super) fn lower_path( // We follow what it did anyway :) if segments.len() == 1 && kind == PathKind::Plain { if let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) { - if let Some(crate_id) = hygiene.local_inner_macros(db.upcast(), path) { + if let Some(crate_id) = hygiene.local_inner_macros(ctx.db.upcast(), path) { kind = PathKind::DollarCrate(crate_id); } } diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index f452edc61..1f6edf7a2 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1002,7 +1002,7 @@ impl HirDisplay for TypeRef { let macro_call = macro_call.to_node(f.db.upcast()); let ctx = body::LowerCtx::with_hygiene(f.db.upcast(), &Hygiene::new_unhygienic()); match macro_call.path() { - Some(path) => match Path::from_src(f.db.upcast(), path, &ctx) { + Some(path) => match Path::from_src(path, &ctx) { Some(path) => path.hir_fmt(f)?, None => write!(f, "{{macro}}")?, }, -- cgit v1.2.3