Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d7615a9

Browse files
Cleanup TypeRef lowering
By removing interior mutability from it.
1 parent af764db commit d7615a9

File tree

12 files changed

+151
-170
lines changed

12 files changed

+151
-170
lines changed

src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl ExprCollector<'_> {
407407
let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
408408
let generic_args = e
409409
.generic_arg_list()
410-
.and_then(|it| GenericArgs::from_ast(&self.ctx(), it))
410+
.and_then(|it| GenericArgs::from_ast(&mut self.ctx(), it))
411411
.map(Box::new);
412412
self.alloc_expr(
413413
Expr::MethodCall { receiver, method_name, args, generic_args },
@@ -533,7 +533,7 @@ impl ExprCollector<'_> {
533533
ast::Expr::TryExpr(e) => self.collect_try_operator(syntax_ptr, e),
534534
ast::Expr::CastExpr(e) => {
535535
let expr = self.collect_expr_opt(e.expr());
536-
let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.ty());
536+
let type_ref = TypeRef::from_ast_opt(&mut self.ctx(), e.ty());
537537
self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
538538
}
539539
ast::Expr::RefExpr(e) => {
@@ -572,13 +572,15 @@ impl ExprCollector<'_> {
572572
arg_types.reserve_exact(num_params);
573573
for param in pl.params() {
574574
let pat = this.collect_pat_top(param.pat());
575-
let type_ref = param.ty().map(|it| TypeRef::from_ast(&this.ctx(), it));
575+
let type_ref = param.ty().map(|it| TypeRef::from_ast(&mut this.ctx(), it));
576576
args.push(pat);
577577
arg_types.push(type_ref);
578578
}
579579
}
580-
let ret_type =
581-
e.ret_type().and_then(|r| r.ty()).map(|it| TypeRef::from_ast(&this.ctx(), it));
580+
let ret_type = e
581+
.ret_type()
582+
.and_then(|r| r.ty())
583+
.map(|it| TypeRef::from_ast(&mut this.ctx(), it));
582584

583585
let prev_is_lowering_coroutine = mem::take(&mut this.is_lowering_coroutine);
584586
let prev_try_block_label = this.current_try_block_label.take();
@@ -705,7 +707,7 @@ impl ExprCollector<'_> {
705707
ast::Expr::UnderscoreExpr(_) => self.alloc_expr(Expr::Underscore, syntax_ptr),
706708
ast::Expr::AsmExpr(e) => self.lower_inline_asm(e, syntax_ptr),
707709
ast::Expr::OffsetOfExpr(e) => {
708-
let container = TypeRef::from_ast_opt(&self.ctx(), e.ty());
710+
let container = TypeRef::from_ast_opt(&mut self.ctx(), e.ty());
709711
let fields = e.fields().map(|it| it.as_name()).collect();
710712
self.alloc_expr(Expr::OffsetOf(OffsetOf { container, fields }), syntax_ptr)
711713
}
@@ -1317,7 +1319,7 @@ impl ExprCollector<'_> {
13171319
return;
13181320
}
13191321
let pat = self.collect_pat_top(stmt.pat());
1320-
let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
1322+
let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&mut self.ctx(), it));
13211323
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
13221324
let else_branch = stmt
13231325
.let_else()

src/tools/rust-analyzer/crates/hir-def/src/expander.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ impl Expander {
161161
types_map: &mut TypesMap,
162162
types_source_map: &mut TypesSourceMap,
163163
) -> Option<Path> {
164-
let ctx = LowerCtx::with_span_map_cell(
164+
let mut ctx = LowerCtx::with_span_map_cell(
165165
db,
166166
self.current_file_id,
167167
self.span_map.clone(),
168168
types_map,
169169
types_source_map,
170170
);
171-
Path::from_src(&ctx, path)
171+
Path::from_src(&mut ctx, path)
172172
}
173173

174174
fn within_limit<F, T: ast::AstNode>(

src/tools/rust-analyzer/crates/hir-def/src/generics.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ pub(crate) struct GenericParamsCollector {
451451
impl GenericParamsCollector {
452452
pub(crate) fn fill(
453453
&mut self,
454-
lower_ctx: &LowerCtx<'_>,
454+
lower_ctx: &mut LowerCtx<'_>,
455455
node: &dyn HasGenericParams,
456456
add_param_attrs: impl FnMut(
457457
Either<LocalTypeOrConstParamId, LocalLifetimeParamId>,
@@ -468,7 +468,7 @@ impl GenericParamsCollector {
468468

469469
pub(crate) fn fill_bounds(
470470
&mut self,
471-
lower_ctx: &LowerCtx<'_>,
471+
lower_ctx: &mut LowerCtx<'_>,
472472
type_bounds: Option<ast::TypeBoundList>,
473473
target: Either<TypeRefId, LifetimeRef>,
474474
) {
@@ -479,7 +479,7 @@ impl GenericParamsCollector {
479479

480480
fn fill_params(
481481
&mut self,
482-
lower_ctx: &LowerCtx<'_>,
482+
lower_ctx: &mut LowerCtx<'_>,
483483
params: ast::GenericParamList,
484484
mut add_param_attrs: impl FnMut(
485485
Either<LocalTypeOrConstParamId, LocalLifetimeParamId>,
@@ -535,7 +535,11 @@ impl GenericParamsCollector {
535535
}
536536
}
537537

538-
fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx<'_>, where_clause: ast::WhereClause) {
538+
fn fill_where_predicates(
539+
&mut self,
540+
lower_ctx: &mut LowerCtx<'_>,
541+
where_clause: ast::WhereClause,
542+
) {
539543
for pred in where_clause.predicates() {
540544
let target = if let Some(type_ref) = pred.ty() {
541545
Either::Left(TypeRef::from_ast(lower_ctx, type_ref))
@@ -569,7 +573,7 @@ impl GenericParamsCollector {
569573

570574
fn add_where_predicate_from_bound(
571575
&mut self,
572-
lower_ctx: &LowerCtx<'_>,
576+
lower_ctx: &mut LowerCtx<'_>,
573577
bound: ast::TypeBound,
574578
hrtb_lifetimes: Option<&[Name]>,
575579
target: Either<TypeRefId, LifetimeRef>,
@@ -670,8 +674,9 @@ impl GenericParamsCollector {
670674
{
671675
let (mut macro_types_map, mut macro_types_source_map) =
672676
(TypesMap::default(), TypesSourceMap::default());
673-
let ctx = expander.ctx(db, &mut macro_types_map, &mut macro_types_source_map);
674-
let type_ref = TypeRef::from_ast(&ctx, expanded.tree());
677+
let mut ctx =
678+
expander.ctx(db, &mut macro_types_map, &mut macro_types_source_map);
679+
let type_ref = TypeRef::from_ast(&mut ctx, expanded.tree());
675680
self.fill_implicit_impl_trait_args(
676681
db,
677682
generics_types_map,

src/tools/rust-analyzer/crates/hir-def/src/hir/type_ref.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub struct TraitRef {
9898

9999
impl TraitRef {
100100
/// Converts an `ast::PathType` to a `hir::TraitRef`.
101-
pub(crate) fn from_ast(ctx: &LowerCtx<'_>, node: ast::Type) -> Option<Self> {
101+
pub(crate) fn from_ast(ctx: &mut LowerCtx<'_>, node: ast::Type) -> Option<Self> {
102102
// FIXME: Use `Path::from_src`
103103
match node {
104104
ast::Type::PathType(path) => {
@@ -240,7 +240,7 @@ pub enum TraitBoundModifier {
240240

241241
impl TypeRef {
242242
/// Converts an `ast::TypeRef` to a `hir::TypeRef`.
243-
pub fn from_ast(ctx: &LowerCtx<'_>, node: ast::Type) -> TypeRefId {
243+
pub fn from_ast(ctx: &mut LowerCtx<'_>, node: ast::Type) -> TypeRefId {
244244
let ty = match &node {
245245
ast::Type::ParenType(inner) => return TypeRef::from_ast_opt(ctx, inner.ty()),
246246
ast::Type::TupleType(inner) => TypeRef::Tuple(EmptyOptimizedThinVec::from_iter(
@@ -321,8 +321,9 @@ impl TypeRef {
321321
// Disallow nested impl traits
322322
TypeRef::Error
323323
} else {
324-
let _guard = ctx.outer_impl_trait_scope(true);
325-
TypeRef::ImplTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
324+
ctx.with_outer_impl_trait_scope(true, |ctx| {
325+
TypeRef::ImplTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
326+
})
326327
}
327328
}
328329
ast::Type::DynTraitType(inner) => {
@@ -336,7 +337,7 @@ impl TypeRef {
336337
ctx.alloc_type_ref(ty, AstPtr::new(&node))
337338
}
338339

339-
pub(crate) fn from_ast_opt(ctx: &LowerCtx<'_>, node: Option<ast::Type>) -> TypeRefId {
340+
pub(crate) fn from_ast_opt(ctx: &mut LowerCtx<'_>, node: Option<ast::Type>) -> TypeRefId {
340341
match node {
341342
Some(node) => TypeRef::from_ast(ctx, node),
342343
None => ctx.alloc_error_type(),
@@ -410,7 +411,7 @@ impl TypeRef {
410411
}
411412

412413
pub(crate) fn type_bounds_from_ast(
413-
lower_ctx: &LowerCtx<'_>,
414+
lower_ctx: &mut LowerCtx<'_>,
414415
type_bounds_opt: Option<ast::TypeBoundList>,
415416
) -> ThinVec<TypeBound> {
416417
if let Some(type_bounds) = type_bounds_opt {
@@ -423,8 +424,8 @@ pub(crate) fn type_bounds_from_ast(
423424
}
424425

425426
impl TypeBound {
426-
pub(crate) fn from_ast(ctx: &LowerCtx<'_>, node: ast::TypeBound) -> Self {
427-
let lower_path_type = |path_type: ast::PathType| ctx.lower_path(path_type.path()?);
427+
pub(crate) fn from_ast(ctx: &mut LowerCtx<'_>, node: ast::TypeBound) -> Self {
428+
let mut lower_path_type = |path_type: ast::PathType| ctx.lower_path(path_type.path()?);
428429

429430
match node.kind() {
430431
ast::TypeBoundKind::PathType(path_type) => {

0 commit comments

Comments
 (0)