Skip to content

Commit 3232fd5

Browse files
bors[bot]edwin0chenglnicola
authored
Merge #4220 #4240
4220: Introduce LowerCtx r=matklad a=edwin0cheng This PR introduces `LowerCtx` for path lowering. After this PR, there are only 2 places remains for using deprecated `Path::from_ast`, which is related to `AstTransform` I am not familiar. I would like to change these in another PR by others ;) related disscusiion: https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Path.3A.3Afrom_src And also fixed part of #4176 (comment) 4240: Bump deps r=matklad a=lnicola Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com> Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
3 parents d79a699 + 44f5e20 + 1e20467 commit 3232fd5

File tree

24 files changed

+293
-127
lines changed

24 files changed

+293
-127
lines changed

Cargo.lock

Lines changed: 44 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_assists/src/ast_transform.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl<'a> SubstituteTypeParams<'a> {
8585
ast::TypeRef::PathType(path_type) => path_type.path()?,
8686
_ => return None,
8787
};
88+
// FIXME: use `hir::Path::from_src` instead.
8889
let path = hir::Path::from_ast(path)?;
8990
let resolution = self.source_scope.resolve_hir_path(&path)?;
9091
match resolution {
@@ -128,6 +129,7 @@ impl<'a> QualifyPaths<'a> {
128129
// don't try to qualify `Fn(Foo) -> Bar` paths, they are in prelude anyway
129130
return None;
130131
}
132+
// FIXME: use `hir::Path::from_src` instead.
131133
let hir_path = hir::Path::from_ast(p.clone());
132134
let resolution = self.source_scope.resolve_hir_path(&hir_path?)?;
133135
match resolution {

crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option<Assist>
2727
return None;
2828
}
2929

30-
let hir_path = hir::Path::from_ast(path.clone())?;
30+
let hir_path = ctx.sema.lower_path(&path)?;
3131
let segments = collect_hir_path_segments(&hir_path)?;
3232
if segments.len() < 2 {
3333
return None;

crates/ra_hir/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub use hir_def::{
7070
type_ref::Mutability,
7171
};
7272
pub use hir_expand::{
73-
name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin,
73+
hygiene::Hygiene, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId,
74+
MacroFile, Origin,
7475
};
7576
pub use hir_ty::{display::HirDisplay, CallableDef};

crates/ra_hir/src/semantics.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir_def::{
88
resolver::{self, HasResolver, Resolver},
99
AsMacroCall, TraitId,
1010
};
11-
use hir_expand::ExpansionInfo;
11+
use hir_expand::{hygiene::Hygiene, ExpansionInfo};
1212
use hir_ty::associated_type_shorthand_candidates;
1313
use itertools::Itertools;
1414
use ra_db::{FileId, FileRange};
@@ -246,6 +246,11 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
246246
self.analyze(path.syntax()).resolve_path(self.db, path)
247247
}
248248

249+
pub fn lower_path(&self, path: &ast::Path) -> Option<Path> {
250+
let src = self.find_file(path.syntax().clone());
251+
Path::from_src(path.clone(), &Hygiene::new(self.db.upcast(), src.file_id.into()))
252+
}
253+
249254
pub fn resolve_bind_pat_to_const(&self, pat: &ast::BindPat) -> Option<ModuleDef> {
250255
self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat)
251256
}

crates/ra_hir/src/source_analyzer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ impl SourceAnalyzer {
224224
}
225225
}
226226
// This must be a normal source file rather than macro file.
227-
let hir_path = crate::Path::from_ast(path.clone())?;
227+
let hir_path =
228+
crate::Path::from_src(path.clone(), &Hygiene::new(db.upcast(), self.file_id))?;
228229
resolve_hir_path(db, &self.resolver, &hir_path)
229230
}
230231

crates/ra_hir_def/src/adt.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ use ra_prof::profile;
1212
use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner};
1313

1414
use crate::{
15-
body::CfgExpander, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace,
16-
type_ref::TypeRef, visibility::RawVisibility, EnumId, HasModule, LocalEnumVariantId,
17-
LocalFieldId, Lookup, ModuleId, StructId, UnionId, VariantId,
15+
body::{CfgExpander, LowerCtx},
16+
db::DefDatabase,
17+
src::HasChildSource,
18+
src::HasSource,
19+
trace::Trace,
20+
type_ref::TypeRef,
21+
visibility::RawVisibility,
22+
EnumId, HasModule, LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StructId, UnionId,
23+
VariantId,
1824
};
1925

2026
/// Note that we use `StructData` for unions as well!
@@ -198,6 +204,8 @@ fn lower_struct(
198204
trace: &mut Trace<FieldData, Either<ast::TupleFieldDef, ast::RecordFieldDef>>,
199205
ast: &InFile<ast::StructKind>,
200206
) -> StructKind {
207+
let ctx = LowerCtx::new(db, ast.file_id);
208+
201209
match &ast.value {
202210
ast::StructKind::Tuple(fl) => {
203211
for (i, fd) in fl.fields().enumerate() {
@@ -210,7 +218,7 @@ fn lower_struct(
210218
|| Either::Left(fd.clone()),
211219
|| FieldData {
212220
name: Name::new_tuple_field(i),
213-
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
221+
type_ref: TypeRef::from_ast_opt(&ctx, fd.type_ref()),
214222
visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),
215223
},
216224
);
@@ -228,7 +236,7 @@ fn lower_struct(
228236
|| Either::Right(fd.clone()),
229237
|| FieldData {
230238
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
231-
type_ref: TypeRef::from_ast_opt(fd.ascribed_type()),
239+
type_ref: TypeRef::from_ast_opt(&ctx, fd.ascribed_type()),
232240
visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),
233241
},
234242
);

crates/ra_hir_def/src/body.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use ra_prof::profile;
1515
use ra_syntax::{ast, AstNode, AstPtr};
1616
use rustc_hash::FxHashMap;
1717

18+
pub(crate) use lower::LowerCtx;
19+
1820
use crate::{
1921
attr::Attrs,
2022
db::DefDatabase,

0 commit comments

Comments
 (0)