Skip to content

Commit

Permalink
Remove useless references/dereferences
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut committed Mar 9, 2021
1 parent c2c4322 commit cd049ef
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 168 deletions.
39 changes: 16 additions & 23 deletions compiler/rustc_mir_build/src/build/block.rs
Expand Up @@ -23,29 +23,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr,
targeted_by_break,
safety_mode,
} = ast_block;
} = *ast_block;
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
this.in_scope((*region_scope, source_info), LintLevel::Inherited, move |this| {
if *targeted_by_break {
this.in_breakable_scope(None, destination, *span, |this| {
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
if targeted_by_break {
this.in_breakable_scope(None, destination, span, |this| {
Some(this.ast_block_stmts(
destination,
block,
*span,
&stmts,
expr.as_deref(),
*safety_mode,
span,
stmts,
expr,
safety_mode,
))
})
} else {
this.ast_block_stmts(
destination,
block,
*span,
&stmts,
expr.as_deref(),
*safety_mode,
)
this.ast_block_stmts(destination, block, span, stmts, expr, safety_mode)
}
})
})
Expand Down Expand Up @@ -87,15 +80,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let source_info = this.source_info(span);
for Stmt { kind, opt_destruction_scope } in stmts {
match kind {
StmtKind::Expr { scope, expr } => {
&StmtKind::Expr { scope, expr } => {
this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
unpack!(
block = this.in_opt_scope(
opt_destruction_scope.map(|de| (de, source_info)),
|this| {
let si = (*scope, source_info);
let si = (scope, source_info);
this.in_scope(si, LintLevel::Inherited, |this| {
this.stmt_expr(block, &expr, Some(*scope))
this.stmt_expr(block, expr, Some(scope))
})
}
)
Expand All @@ -110,7 +103,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let_scope_stack.push(remainder_scope);

// Declare the bindings, which may create a source scope.
let remainder_span = remainder_scope.span(this.tcx, &this.region_scope_tree);
let remainder_span = remainder_scope.span(this.tcx, this.region_scope_tree);

let visibility_scope =
Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
Expand All @@ -128,11 +121,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.declare_bindings(
visibility_scope,
remainder_span,
&pattern,
pattern,
ArmHasGuard(false),
Some((None, initializer_span)),
);
this.expr_into_pattern(block, pattern.clone(), &init)
this.expr_into_pattern(block, pattern.clone(), init)
})
}
)
Expand All @@ -143,7 +136,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.declare_bindings(
visibility_scope,
remainder_span,
&pattern,
pattern,
ArmHasGuard(false),
None,
);
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_mir_build/src/build/expr/as_constant.rs
Expand Up @@ -10,25 +10,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
/// `expr` is a valid compile-time constant!
crate fn as_constant(&mut self, expr: &Expr<'_, 'tcx>) -> Constant<'tcx> {
let this = self;
let Expr { ty, temp_lifetime: _, span, kind } = expr;
let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
match kind {
ExprKind::Scope { region_scope: _, lint_level: _, value } => this.as_constant(&value),
ExprKind::Scope { region_scope: _, lint_level: _, value } => this.as_constant(value),
ExprKind::Literal { literal, user_ty, const_id: _ } => {
let user_ty = user_ty.map(|user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: *span,
span,
user_ty,
inferred_ty: ty,
})
});
assert_eq!(literal.ty, *ty);
Constant { span: *span, user_ty, literal }
assert_eq!(literal.ty, ty);
Constant { span, user_ty, literal }
}
ExprKind::StaticRef { literal, .. } => Constant { span: *span, user_ty: None, literal },
ExprKind::StaticRef { literal, .. } => Constant { span, user_ty: None, literal },
ExprKind::ConstBlock { value } => {
Constant { span: *span, user_ty: None, literal: value }
Constant { span: span, user_ty: None, literal: value }
}
_ => span_bug!(*span, "expression is not a valid constant {:?}", kind),
_ => span_bug!(span, "expression is not a valid constant {:?}", kind),
}
}
}
16 changes: 8 additions & 8 deletions compiler/rustc_mir_build/src/build/expr/as_operand.rs
Expand Up @@ -98,11 +98,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
debug!("as_operand(block={:?}, expr={:?})", block, expr);
let this = self;

if let ExprKind::Scope { region_scope, lint_level, value } = &expr.kind {
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
let source_info = this.source_info(expr.span);
let region_scope = (*region_scope, source_info);
let region_scope = (region_scope, source_info);
return this
.in_scope(region_scope, *lint_level, |this| this.as_operand(block, scope, &value));
.in_scope(region_scope, lint_level, |this| this.as_operand(block, scope, value));
}

let category = Category::of(&expr.kind).unwrap();
Expand All @@ -128,11 +128,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
debug!("as_call_operand(block={:?}, expr={:?})", block, expr);
let this = self;

if let ExprKind::Scope { region_scope, lint_level, value } = &expr.kind {
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
let source_info = this.source_info(expr.span);
let region_scope = (*region_scope, source_info);
return this.in_scope(region_scope, *lint_level, |this| {
this.as_call_operand(block, scope, &value)
let region_scope = (region_scope, source_info);
return this.in_scope(region_scope, lint_level, |this| {
this.as_call_operand(block, scope, value)
});
}

Expand All @@ -149,7 +149,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

// As described above, detect the case where we are passing a value of unsized
// type, and that value is coming from the deref of a box.
if let ExprKind::Deref { ref arg } = expr.kind {
if let ExprKind::Deref { arg } = expr.kind {
// Generate let tmp0 = arg0
let operand = unpack!(block = this.as_temp(block, scope, arg, Mutability::Mut));

Expand Down
38 changes: 18 additions & 20 deletions compiler/rustc_mir_build/src/build/expr/as_place.rs
Expand Up @@ -406,59 +406,57 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let this = self;
let expr_span = expr.span;
let source_info = this.source_info(expr_span);
match &expr.kind {
match expr.kind {
ExprKind::Scope { region_scope, lint_level, value } => {
this.in_scope((*region_scope, source_info), *lint_level, |this| {
this.expr_as_place(block, &value, mutability, fake_borrow_temps)
this.in_scope((region_scope, source_info), lint_level, |this| {
this.expr_as_place(block, value, mutability, fake_borrow_temps)
})
}
ExprKind::Field { lhs, name } => {
let place_builder = unpack!(
block = this.expr_as_place(block, &lhs, mutability, fake_borrow_temps,)
);
block.and(place_builder.field(*name, expr.ty))
let place_builder =
unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
block.and(place_builder.field(name, expr.ty))
}
ExprKind::Deref { arg } => {
let place_builder = unpack!(
block = this.expr_as_place(block, &arg, mutability, fake_borrow_temps,)
);
let place_builder =
unpack!(block = this.expr_as_place(block, arg, mutability, fake_borrow_temps,));
block.and(place_builder.deref())
}
ExprKind::Index { lhs, index } => this.lower_index_expression(
block,
&lhs,
&index,
lhs,
index,
mutability,
fake_borrow_temps,
expr.temp_lifetime,
expr_span,
source_info,
),
ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
let upvar_id = ty::UpvarId::new(*var_hir_id, closure_def_id.expect_local());
let upvar_id = ty::UpvarId::new(var_hir_id, closure_def_id.expect_local());
this.lower_captured_upvar(block, upvar_id)
}

ExprKind::VarRef { id } => {
let place_builder = if this.is_bound_var_in_guard(*id) {
let index = this.var_local_id(*id, RefWithinGuard);
let place_builder = if this.is_bound_var_in_guard(id) {
let index = this.var_local_id(id, RefWithinGuard);
PlaceBuilder::from(index).deref()
} else {
let index = this.var_local_id(*id, OutsideGuard);
let index = this.var_local_id(id, OutsideGuard);
PlaceBuilder::from(index)
};
block.and(place_builder)
}

ExprKind::PlaceTypeAscription { source, user_ty } => {
let place_builder = unpack!(
block = this.expr_as_place(block, &source, mutability, fake_borrow_temps,)
block = this.expr_as_place(block, source, mutability, fake_borrow_temps,)
);
if let Some(user_ty) = user_ty {
let annotation_index =
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span,
user_ty: *user_ty,
user_ty,
inferred_ty: expr.ty,
});

Expand All @@ -481,12 +479,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
ExprKind::ValueTypeAscription { source, user_ty } => {
let temp =
unpack!(block = this.as_temp(block, source.temp_lifetime, &source, mutability));
unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
if let Some(user_ty) = user_ty {
let annotation_index =
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span,
user_ty: *user_ty,
user_ty,
inferred_ty: expr.ty,
});
this.cfg.push(
Expand Down
48 changes: 23 additions & 25 deletions compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
Expand Up @@ -41,27 +41,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let expr_span = expr.span;
let source_info = this.source_info(expr_span);

match &expr.kind {
ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(*did)),
match expr.kind {
ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(did)),
ExprKind::Scope { region_scope, lint_level, value } => {
let region_scope = (*region_scope, source_info);
this.in_scope(region_scope, *lint_level, |this| {
this.as_rvalue(block, scope, &value)
})
let region_scope = (region_scope, source_info);
this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
}
ExprKind::Repeat { value, count } => {
let value_operand = unpack!(block = this.as_operand(block, scope, &value));
let value_operand = unpack!(block = this.as_operand(block, scope, value));
block.and(Rvalue::Repeat(value_operand, count))
}
ExprKind::Binary { op, lhs, rhs } => {
let lhs = unpack!(block = this.as_operand(block, scope, &lhs));
let rhs = unpack!(block = this.as_operand(block, scope, &rhs));
this.build_binary_op(block, *op, expr_span, expr.ty, lhs, rhs)
let lhs = unpack!(block = this.as_operand(block, scope, lhs));
let rhs = unpack!(block = this.as_operand(block, scope, rhs));
this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs)
}
ExprKind::Unary { op, arg } => {
let arg = unpack!(block = this.as_operand(block, scope, &arg));
let arg = unpack!(block = this.as_operand(block, scope, arg));
// Check for -MIN on signed integers
if this.check_overflow && *op == UnOp::Neg && expr.ty.is_signed() {
if this.check_overflow && op == UnOp::Neg && expr.ty.is_signed() {
let bool_ty = this.tcx.types.bool;

let minval = this.minval_literal(expr_span, expr.ty);
Expand All @@ -82,7 +80,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr_span,
);
}
block.and(Rvalue::UnaryOp(*op, arg))
block.and(Rvalue::UnaryOp(op, arg))
}
ExprKind::Box { value } => {
// The `Box<T>` temporary created here is not a part of the HIR,
Expand All @@ -107,18 +105,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block = this.expr_into_dest(
this.tcx.mk_place_deref(Place::from(result)),
block,
&value
value
)
);
block.and(Rvalue::Use(Operand::Move(Place::from(result))))
}
ExprKind::Cast { source } => {
let source = unpack!(block = this.as_operand(block, scope, &source));
let source = unpack!(block = this.as_operand(block, scope, source));
block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
}
ExprKind::Pointer { cast, source } => {
let source = unpack!(block = this.as_operand(block, scope, &source));
block.and(Rvalue::Cast(CastKind::Pointer(*cast), source, expr.ty))
let source = unpack!(block = this.as_operand(block, scope, source));
block.and(Rvalue::Cast(CastKind::Pointer(cast), source, expr.ty))
}
ExprKind::Array { fields } => {
// (*) We would (maybe) be closer to codegen if we
Expand Down Expand Up @@ -151,7 +149,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let el_ty = expr.ty.sequence_element_type(this.tcx);
let fields: Vec<_> = fields
.into_iter()
.map(|f| unpack!(block = this.as_operand(block, scope, &f)))
.map(|f| unpack!(block = this.as_operand(block, scope, f)))
.collect();

block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
Expand All @@ -161,7 +159,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// first process the set of fields
let fields: Vec<_> = fields
.into_iter()
.map(|f| unpack!(block = this.as_operand(block, scope, &f)))
.map(|f| unpack!(block = this.as_operand(block, scope, f)))
.collect();

block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
Expand All @@ -181,25 +179,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// This occurs when capturing by copy/move, while
// by reference captures use as_operand
Some(Category::Place) => {
let place = unpack!(block = this.as_place(block, &upvar));
let place = unpack!(block = this.as_place(block, upvar));
this.consume_by_copy_or_move(place)
}
_ => {
// Turn mutable borrow captures into unique
// borrow captures when capturing an immutable
// variable. This is sound because the mutation
// that caused the capture will cause an error.
match &upvar.kind {
match upvar.kind {
ExprKind::Borrow {
borrow_kind:
BorrowKind::Mut { allow_two_phase_borrow: false },
arg,
} => unpack!(
block = this.limit_capture_mutability(
upvar.span, upvar.ty, scope, block, &arg,
upvar.span, upvar.ty, scope, block, arg,
)
),
_ => unpack!(block = this.as_operand(block, scope, &upvar)),
_ => unpack!(block = this.as_operand(block, scope, upvar)),
}
}
}
Expand All @@ -210,9 +208,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// We implicitly set the discriminant to 0. See
// librustc_mir/transform/deaggregator.rs for details.
let movability = movability.unwrap();
box AggregateKind::Generator(*closure_id, substs, movability)
box AggregateKind::Generator(closure_id, substs, movability)
}
UpvarSubsts::Closure(substs) => box AggregateKind::Closure(*closure_id, substs),
UpvarSubsts::Closure(substs) => box AggregateKind::Closure(closure_id, substs),
};
block.and(Rvalue::Aggregate(result, operands))
}
Expand Down

0 comments on commit cd049ef

Please sign in to comment.