Skip to content

Commit bdcf6f5

Browse files
committed
Introduce LowerCtx for path lowering
1 parent 1635d22 commit bdcf6f5

File tree

14 files changed

+172
-80
lines changed

14 files changed

+172
-80
lines changed

crates/ra_assists/src/assist_ctx.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! This module defines `AssistCtx` -- the API surface that is exposed to assists.
22
use hir::Semantics;
3-
use ra_db::FileRange;
3+
use ra_db::{FileRange, Upcast};
44
use ra_fmt::{leading_indent, reindent};
55
use ra_ide_db::RootDatabase;
66
use ra_syntax::{
77
algo::{self, find_covering_element, find_node_at_offset},
8-
AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize,
9-
TokenAtOffset,
8+
ast, AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange,
9+
TextSize, TokenAtOffset,
1010
};
1111
use ra_text_edit::TextEditBuilder;
1212

@@ -136,6 +136,9 @@ impl<'a> AssistCtx<'a> {
136136
pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement {
137137
find_covering_element(self.source_file.syntax(), range)
138138
}
139+
pub(crate) fn lower_path(&self, path: ast::Path) -> Option<hir::Path> {
140+
hir::Path::from_src(path, &hir::Hygiene::new(self.db.upcast(), self.frange.file_id.into()))
141+
}
139142
}
140143

141144
pub(crate) struct AssistGroup<'a> {

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.lower_path(path.clone())?;
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/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,

crates/ra_hir_def/src/body/lower.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
44
use either::Either;
55
use hir_expand::{
6+
hygiene::Hygiene,
67
name::{name, AsName, Name},
7-
MacroDefId, MacroDefKind,
8+
HirFileId, MacroDefId, MacroDefKind,
89
};
910
use ra_arena::Arena;
1011
use ra_syntax::{
@@ -26,7 +27,7 @@ use crate::{
2627
LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
2728
},
2829
item_scope::BuiltinShadowMode,
29-
path::GenericArgs,
30+
path::{GenericArgs, Path},
3031
type_ref::{Mutability, TypeRef},
3132
AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId,
3233
StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
@@ -35,17 +36,37 @@ use crate::{
3536
use super::{ExprSource, PatSource};
3637
use ast::AstChildren;
3738

39+
pub(crate) struct LowerCtx {
40+
hygiene: Hygiene,
41+
}
42+
43+
impl LowerCtx {
44+
pub fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self {
45+
LowerCtx { hygiene: Hygiene::new(db.upcast(), file_id) }
46+
}
47+
pub fn with_hygiene(hygiene: &Hygiene) -> Self {
48+
LowerCtx { hygiene: hygiene.clone() }
49+
}
50+
51+
pub fn lower_path(&self, ast: ast::Path) -> Option<Path> {
52+
Path::from_src(ast, &self.hygiene)
53+
}
54+
}
55+
3856
pub(super) fn lower(
3957
db: &dyn DefDatabase,
4058
def: DefWithBodyId,
4159
expander: Expander,
4260
params: Option<ast::ParamList>,
4361
body: Option<ast::Expr>,
4462
) -> (Body, BodySourceMap) {
63+
let ctx = LowerCtx::new(db, expander.current_file_id.clone());
64+
4565
ExprCollector {
4666
db,
4767
def,
4868
expander,
69+
ctx,
4970
source_map: BodySourceMap::default(),
5071
body: Body {
5172
exprs: Arena::default(),
@@ -62,7 +83,7 @@ struct ExprCollector<'a> {
6283
db: &'a dyn DefDatabase,
6384
def: DefWithBodyId,
6485
expander: Expander,
65-
86+
ctx: LowerCtx,
6687
body: Body,
6788
source_map: BodySourceMap,
6889
}
@@ -237,7 +258,8 @@ impl ExprCollector<'_> {
237258
Vec::new()
238259
};
239260
let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
240-
let generic_args = e.type_arg_list().and_then(GenericArgs::from_ast);
261+
let generic_args =
262+
e.type_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx, it));
241263
self.alloc_expr(
242264
Expr::MethodCall { receiver, method_name, args, generic_args },
243265
syntax_ptr,
@@ -343,7 +365,7 @@ impl ExprCollector<'_> {
343365
}
344366
ast::Expr::CastExpr(e) => {
345367
let expr = self.collect_expr_opt(e.expr());
346-
let type_ref = TypeRef::from_ast_opt(e.type_ref());
368+
let type_ref = TypeRef::from_ast_opt(&self.ctx, e.type_ref());
347369
self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
348370
}
349371
ast::Expr::RefExpr(e) => {
@@ -365,12 +387,16 @@ impl ExprCollector<'_> {
365387
if let Some(pl) = e.param_list() {
366388
for param in pl.params() {
367389
let pat = self.collect_pat_opt(param.pat());
368-
let type_ref = param.ascribed_type().map(TypeRef::from_ast);
390+
let type_ref =
391+
param.ascribed_type().map(|it| TypeRef::from_ast(&self.ctx, it));
369392
args.push(pat);
370393
arg_types.push(type_ref);
371394
}
372395
}
373-
let ret_type = e.ret_type().and_then(|r| r.type_ref()).map(TypeRef::from_ast);
396+
let ret_type = e
397+
.ret_type()
398+
.and_then(|r| r.type_ref())
399+
.map(|it| TypeRef::from_ast(&self.ctx, it));
374400
let body = self.collect_expr_opt(e.body());
375401
self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
376402
}
@@ -476,7 +502,7 @@ impl ExprCollector<'_> {
476502
.map(|s| match s {
477503
ast::Stmt::LetStmt(stmt) => {
478504
let pat = self.collect_pat_opt(stmt.pat());
479-
let type_ref = stmt.ascribed_type().map(TypeRef::from_ast);
505+
let type_ref = stmt.ascribed_type().map(|it| TypeRef::from_ast(&self.ctx, it));
480506
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
481507
Statement::Let { pat, type_ref, initializer }
482508
}

crates/ra_hir_def/src/data.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use ra_syntax::ast::{
1515

1616
use crate::{
1717
attr::Attrs,
18+
body::LowerCtx,
1819
db::DefDatabase,
1920
path::{path, AssociatedTypeBinding, GenericArgs, Path},
2021
src::HasSource,
@@ -40,13 +41,14 @@ impl FunctionData {
4041
pub(crate) fn fn_data_query(db: &impl DefDatabase, func: FunctionId) -> Arc<FunctionData> {
4142
let loc = func.lookup(db);
4243
let src = loc.source(db);
44+
let ctx = LowerCtx::new(db, src.file_id);
4345
let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
4446
let mut params = Vec::new();
4547
let mut has_self_param = false;
4648
if let Some(param_list) = src.value.param_list() {
4749
if let Some(self_param) = param_list.self_param() {
4850
let self_type = if let Some(type_ref) = self_param.ascribed_type() {
49-
TypeRef::from_ast(type_ref)
51+
TypeRef::from_ast(&ctx, type_ref)
5052
} else {
5153
let self_type = TypeRef::Path(name![Self].into());
5254
match self_param.kind() {
@@ -63,14 +65,14 @@ impl FunctionData {
6365
has_self_param = true;
6466
}
6567
for param in param_list.params() {
66-
let type_ref = TypeRef::from_ast_opt(param.ascribed_type());
68+
let type_ref = TypeRef::from_ast_opt(&ctx, param.ascribed_type());
6769
params.push(type_ref);
6870
}
6971
}
7072
let attrs = Attrs::new(&src.value, &Hygiene::new(db.upcast(), src.file_id));
7173

7274
let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) {
73-
TypeRef::from_ast(type_ref)
75+
TypeRef::from_ast(&ctx, type_ref)
7476
} else {
7577
TypeRef::unit()
7678
};
@@ -122,15 +124,16 @@ impl TypeAliasData {
122124
let loc = typ.lookup(db);
123125
let node = loc.source(db);
124126
let name = node.value.name().map_or_else(Name::missing, |n| n.as_name());
125-
let type_ref = node.value.type_ref().map(TypeRef::from_ast);
127+
let lower_ctx = LowerCtx::new(db, node.file_id);
128+
let type_ref = node.value.type_ref().map(|it| TypeRef::from_ast(&lower_ctx, it));
126129
let vis_default = RawVisibility::default_for_container(loc.container);
127130
let visibility = RawVisibility::from_ast_with_default(
128131
db,
129132
vis_default,
130133
node.as_ref().map(|n| n.visibility()),
131134
);
132135
let bounds = if let Some(bound_list) = node.value.type_bound_list() {
133-
bound_list.bounds().map(TypeBound::from_ast).collect()
136+
bound_list.bounds().map(|it| TypeBound::from_ast(&lower_ctx, it)).collect()
134137
} else {
135138
Vec::new()
136139
};
@@ -223,9 +226,10 @@ impl ImplData {
223226
let _p = profile("impl_data_query");
224227
let impl_loc = id.lookup(db);
225228
let src = impl_loc.source(db);
229+
let lower_ctx = LowerCtx::new(db, src.file_id);
226230

227-
let target_trait = src.value.target_trait().map(TypeRef::from_ast);
228-
let target_type = TypeRef::from_ast_opt(src.value.target_type());
231+
let target_trait = src.value.target_trait().map(|it| TypeRef::from_ast(&lower_ctx, it));
232+
let target_type = TypeRef::from_ast_opt(&lower_ctx, src.value.target_type());
229233
let is_negative = src.value.excl_token().is_some();
230234
let module_id = impl_loc.container.module(db);
231235

@@ -279,8 +283,9 @@ impl ConstData {
279283
vis_default: RawVisibility,
280284
node: InFile<N>,
281285
) -> ConstData {
286+
let ctx = LowerCtx::new(db, node.file_id);
282287
let name = node.value.name().map(|n| n.as_name());
283-
let type_ref = TypeRef::from_ast_opt(node.value.ascribed_type());
288+
let type_ref = TypeRef::from_ast_opt(&ctx, node.value.ascribed_type());
284289
let visibility =
285290
RawVisibility::from_ast_with_default(db, vis_default, node.map(|n| n.visibility()));
286291
ConstData { name, type_ref, visibility }

0 commit comments

Comments
 (0)