Skip to content

Commit 20ae41c

Browse files
Reuse database in LowerCtx
1 parent c4f9cb9 commit 20ae41c

File tree

7 files changed

+15
-20
lines changed

7 files changed

+15
-20
lines changed

crates/hir/src/semantics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ impl<'a> SemanticsScope<'a> {
855855
/// necessary a heuristic, as it doesn't take hygiene into account.
856856
pub fn speculative_resolve(&self, path: &ast::Path) -> Option<PathResolution> {
857857
let ctx = body::LowerCtx::new(self.db.upcast(), self.file_id);
858-
let path = Path::from_src(self.db.upcast(), path.clone(), &ctx)?;
858+
let path = Path::from_src(path.clone(), &ctx)?;
859859
resolve_hir_path(self.db, &self.resolver, &path)
860860
}
861861
}

crates/hir/src/source_analyzer.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ impl SourceAnalyzer {
204204
macro_call: InFile<&ast::MacroCall>,
205205
) -> Option<MacroDef> {
206206
let ctx = body::LowerCtx::new(db.upcast(), macro_call.file_id);
207-
let path =
208-
macro_call.value.path().and_then(|ast| Path::from_src(db.upcast(), ast, &ctx))?;
207+
let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &ctx))?;
209208
self.resolver.resolve_path_as_macro(db.upcast(), path.mod_path()).map(|it| it.into())
210209
}
211210

@@ -285,7 +284,7 @@ impl SourceAnalyzer {
285284
// This must be a normal source file rather than macro file.
286285
let hygiene = Hygiene::new(db.upcast(), self.file_id);
287286
let ctx = body::LowerCtx::with_hygiene(db.upcast(), &hygiene);
288-
let hir_path = Path::from_src(db.upcast(), path.clone(), &ctx)?;
287+
let hir_path = Path::from_src(path.clone(), &ctx)?;
289288

290289
// Case where path is a qualifier of another path, e.g. foo::bar::Baz where we
291290
// trying to resolve foo::bar.

crates/hir_def/src/body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl Expander {
194194

195195
fn parse_path(&mut self, db: &dyn DefDatabase, path: ast::Path) -> Option<Path> {
196196
let ctx = LowerCtx::with_hygiene(db, &self.cfg_expander.hygiene);
197-
Path::from_src(db, path, &ctx)
197+
Path::from_src(path, &ctx)
198198
}
199199

200200
fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> {

crates/hir_def/src/body/lower.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{
4141
use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};
4242

4343
pub struct LowerCtx<'a> {
44-
db: &'a dyn DefDatabase,
44+
pub db: &'a dyn DefDatabase,
4545
hygiene: Hygiene,
4646
file_id: Option<HirFileId>,
4747
source_ast_id_map: Option<Arc<AstIdMap>>,
@@ -70,7 +70,7 @@ impl<'a> LowerCtx<'a> {
7070
}
7171

7272
pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
73-
Path::from_src(self.db, ast, self)
73+
Path::from_src(ast, self)
7474
}
7575

7676
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> Option<FileAstId<N>> {

crates/hir_def/src/path.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub enum ImportAlias {
4949
impl ModPath {
5050
pub fn from_src(db: &dyn DefDatabase, path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
5151
let ctx = LowerCtx::with_hygiene(db, hygiene);
52-
lower::lower_path(db, path, &ctx).map(|it| (*it.mod_path).clone())
52+
lower::lower_path(path, &ctx).map(|it| (*it.mod_path).clone())
5353
}
5454

5555
pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath {
@@ -169,8 +169,8 @@ pub enum GenericArg {
169169
impl Path {
170170
/// Converts an `ast::Path` to `Path`. Works with use trees.
171171
/// It correctly handles `$crate` based path from macro call.
172-
pub fn from_src(db: &dyn DefDatabase, path: ast::Path, ctx: &LowerCtx) -> Option<Path> {
173-
lower::lower_path(db, path, ctx)
172+
pub fn from_src(path: ast::Path, ctx: &LowerCtx) -> Option<Path> {
173+
lower::lower_path(path, ctx)
174174
}
175175

176176
/// Converts a known mod path to `Path`.

crates/hir_def/src/path/lower.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
mod lower_use;
44

5-
use crate::{db::DefDatabase, intern::Interned};
5+
use crate::intern::Interned;
66
use std::sync::Arc;
77

88
use either::Either;
@@ -20,11 +20,7 @@ pub(super) use lower_use::lower_use_tree;
2020

2121
/// Converts an `ast::Path` to `Path`. Works with use trees.
2222
/// It correctly handles `$crate` based path from macro call.
23-
pub(super) fn lower_path(
24-
db: &dyn DefDatabase,
25-
mut path: ast::Path,
26-
ctx: &LowerCtx,
27-
) -> Option<Path> {
23+
pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx) -> Option<Path> {
2824
let mut kind = PathKind::Plain;
2925
let mut type_anchor = None;
3026
let mut segments = Vec::new();
@@ -40,7 +36,7 @@ pub(super) fn lower_path(
4036
match segment.kind()? {
4137
ast::PathSegmentKind::Name(name_ref) => {
4238
// FIXME: this should just return name
43-
match hygiene.name_ref_to_name(db.upcast(), name_ref) {
39+
match hygiene.name_ref_to_name(ctx.db.upcast(), name_ref) {
4440
Either::Left(name) => {
4541
let args = segment
4642
.generic_arg_list()
@@ -75,7 +71,7 @@ pub(super) fn lower_path(
7571
}
7672
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
7773
Some(trait_ref) => {
78-
let path = Path::from_src(db, trait_ref.path()?, ctx)?;
74+
let path = Path::from_src(trait_ref.path()?, ctx)?;
7975
let mod_path = (*path.mod_path).clone();
8076
let num_segments = path.mod_path.segments.len();
8177
kind = mod_path.kind;
@@ -137,7 +133,7 @@ pub(super) fn lower_path(
137133
// We follow what it did anyway :)
138134
if segments.len() == 1 && kind == PathKind::Plain {
139135
if let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) {
140-
if let Some(crate_id) = hygiene.local_inner_macros(db.upcast(), path) {
136+
if let Some(crate_id) = hygiene.local_inner_macros(ctx.db.upcast(), path) {
141137
kind = PathKind::DollarCrate(crate_id);
142138
}
143139
}

crates/hir_ty/src/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ impl HirDisplay for TypeRef {
10021002
let macro_call = macro_call.to_node(f.db.upcast());
10031003
let ctx = body::LowerCtx::with_hygiene(f.db.upcast(), &Hygiene::new_unhygienic());
10041004
match macro_call.path() {
1005-
Some(path) => match Path::from_src(f.db.upcast(), path, &ctx) {
1005+
Some(path) => match Path::from_src(path, &ctx) {
10061006
Some(path) => path.hir_fmt(f)?,
10071007
None => write!(f, "{{macro}}")?,
10081008
},

0 commit comments

Comments
 (0)