Skip to content

Commit 62ba365

Browse files
committed
Review comments: use newtype instead of bool
1 parent 671d7c4 commit 62ba365

File tree

5 files changed

+68
-43
lines changed

5 files changed

+68
-43
lines changed

Diff for: compiler/rustc_infer/src/infer/error_reporting/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use crate::infer::OriginalQueryValues;
5454
use crate::traits::error_reporting::report_object_safety_error;
5555
use crate::traits::{
5656
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
57+
StatementAsExpression,
5758
};
5859

5960
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -689,7 +690,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
689690
let msg = "`match` arms have incompatible types";
690691
err.span_label(outer_error_span, msg);
691692
if let Some((sp, boxed)) = semi_span {
692-
if boxed {
693+
if matches!(boxed, StatementAsExpression::NeedsBoxing) {
693694
err.span_suggestion_verbose(
694695
sp,
695696
"consider removing this semicolon and boxing the expression",
@@ -727,7 +728,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
727728
err.span_label(sp, "`if` and `else` have incompatible types");
728729
}
729730
if let Some((sp, boxed)) = semicolon {
730-
if boxed {
731+
if matches!(boxed, StatementAsExpression::NeedsBoxing) {
731732
err.span_suggestion_verbose(
732733
sp,
733734
"consider removing this semicolon and boxing the expression",

Diff for: compiler/rustc_middle/src/traits/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,24 @@ impl ObligationCauseCode<'_> {
340340
#[cfg(target_arch = "x86_64")]
341341
static_assert_size!(ObligationCauseCode<'_>, 32);
342342

343+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
344+
pub enum StatementAsExpression {
345+
CorrectType,
346+
NeedsBoxing,
347+
}
348+
349+
impl<'tcx> ty::Lift<'tcx> for StatementAsExpression {
350+
type Lifted = StatementAsExpression;
351+
fn lift_to_tcx(self, _tcx: TyCtxt<'tcx>) -> Option<StatementAsExpression> {
352+
Some(self)
353+
}
354+
}
355+
343356
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
344357
pub struct MatchExpressionArmCause<'tcx> {
345358
pub arm_span: Span,
346359
pub scrut_span: Span,
347-
pub semi_span: Option<(Span, bool)>,
360+
pub semi_span: Option<(Span, StatementAsExpression)>,
348361
pub source: hir::MatchSource,
349362
pub prior_arms: Vec<Span>,
350363
pub last_ty: Ty<'tcx>,
@@ -357,7 +370,7 @@ pub struct IfExpressionCause {
357370
pub then: Span,
358371
pub else_sp: Span,
359372
pub outer: Option<Span>,
360-
pub semicolon: Option<(Span, bool)>,
373+
pub semicolon: Option<(Span, StatementAsExpression)>,
361374
pub opt_suggest_box_span: Option<Span>,
362375
}
363376

Diff for: compiler/rustc_typeck/src/check/_match.rs

+26-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_trait_selection::opaque_types::InferCtxtExt as _;
99
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
1010
use rustc_trait_selection::traits::{
1111
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
12+
StatementAsExpression,
1213
};
1314

1415
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -188,18 +189,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
188189
}
189190
}
190191
} else {
191-
let (arm_span, mut semi_span) = if let hir::ExprKind::Block(blk, _) = &arm.body.kind
192-
{
193-
self.find_block_span(blk, prior_arm_ty)
194-
} else {
195-
(arm.body.span, None)
196-
};
197-
if semi_span.is_none() && i > 0 {
198-
if let hir::ExprKind::Block(blk, _) = &arms[i - 1].body.kind {
199-
let (_, semi_span_prev) = self.find_block_span(blk, Some(arm_ty));
200-
semi_span = semi_span_prev;
201-
}
202-
}
192+
let (arm_span, semi_span) =
193+
self.get_appropriate_arm_semicolon_removal_span(&arms, i, prior_arm_ty, arm_ty);
203194
let (span, code) = match i {
204195
// The reason for the first arm to fail is not that the match arms diverge,
205196
// but rather that there's a prior obligation that doesn't hold.
@@ -249,6 +240,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
249240
coercion.complete(self)
250241
}
251242

243+
fn get_appropriate_arm_semicolon_removal_span(
244+
&self,
245+
arms: &'tcx [hir::Arm<'tcx>],
246+
i: usize,
247+
prior_arm_ty: Option<Ty<'tcx>>,
248+
arm_ty: Ty<'tcx>,
249+
) -> (Span, Option<(Span, StatementAsExpression)>) {
250+
let arm = &arms[i];
251+
let (arm_span, mut semi_span) = if let hir::ExprKind::Block(blk, _) = &arm.body.kind {
252+
self.find_block_span(blk, prior_arm_ty)
253+
} else {
254+
(arm.body.span, None)
255+
};
256+
if semi_span.is_none() && i > 0 {
257+
if let hir::ExprKind::Block(blk, _) = &arms[i - 1].body.kind {
258+
let (_, semi_span_prev) = self.find_block_span(blk, Some(arm_ty));
259+
semi_span = semi_span_prev;
260+
}
261+
}
262+
(arm_span, semi_span)
263+
}
264+
252265
/// When the previously checked expression (the scrutinee) diverges,
253266
/// warn the user about the match arms being unreachable.
254267
fn warn_arms_when_scrutinee_diverges(
@@ -521,7 +534,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
521534
&self,
522535
block: &'tcx hir::Block<'tcx>,
523536
expected_ty: Option<Ty<'tcx>>,
524-
) -> (Span, Option<(Span, bool)>) {
537+
) -> (Span, Option<(Span, StatementAsExpression)>) {
525538
if let Some(expr) = &block.expr {
526539
(expr.span, None)
527540
} else if let Some(stmt) = block.stmts.last() {

Diff for: compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ use rustc_span::{self, BytePos, MultiSpan, Span};
3333
use rustc_trait_selection::infer::InferCtxtExt as _;
3434
use rustc_trait_selection::opaque_types::InferCtxtExt as _;
3535
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
36-
use rustc_trait_selection::traits::{self, ObligationCauseCode, TraitEngine, TraitEngineExt};
36+
use rustc_trait_selection::traits::{
37+
self, ObligationCauseCode, StatementAsExpression, TraitEngine, TraitEngineExt,
38+
};
3739

3840
use std::collections::hash_map::Entry;
3941
use std::slice;
@@ -1061,7 +1063,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10611063
&self,
10621064
blk: &'tcx hir::Block<'tcx>,
10631065
expected_ty: Ty<'tcx>,
1064-
) -> Option<(Span, bool)> {
1066+
) -> Option<(Span, StatementAsExpression)> {
10651067
// Be helpful when the user wrote `{... expr;}` and
10661068
// taking the `;` off is enough to fix the error.
10671069
let last_stmt = blk.stmts.last()?;
@@ -1078,49 +1080,45 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10781080
);
10791081
let last_hir_id = self.tcx.hir().local_def_id_to_hir_id(last_def_id.expect_local());
10801082
let exp_hir_id = self.tcx.hir().local_def_id_to_hir_id(exp_def_id.expect_local());
1081-
if let (
1082-
hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: last_bounds, .. }),
1083-
hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: exp_bounds, .. }),
1084-
) = (
1083+
match (
10851084
&self.tcx.hir().expect_item(last_hir_id).kind,
10861085
&self.tcx.hir().expect_item(exp_hir_id).kind,
10871086
) {
1088-
debug!("{:?} {:?}", last_bounds, exp_bounds);
1089-
last_bounds.iter().zip(exp_bounds.iter()).all(|(left, right)| {
1087+
(
1088+
hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: last_bounds, .. }),
1089+
hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: exp_bounds, .. }),
1090+
) if last_bounds.iter().zip(exp_bounds.iter()).all(|(left, right)| {
10901091
match (left, right) {
10911092
(
10921093
hir::GenericBound::Trait(tl, ml),
10931094
hir::GenericBound::Trait(tr, mr),
1094-
) => {
1095-
tl.trait_ref.trait_def_id() == tr.trait_ref.trait_def_id()
1096-
&& ml == mr
1095+
) if tl.trait_ref.trait_def_id() == tr.trait_ref.trait_def_id()
1096+
&& ml == mr =>
1097+
{
1098+
true
10971099
}
10981100
(
10991101
hir::GenericBound::LangItemTrait(langl, _, _, argsl),
11001102
hir::GenericBound::LangItemTrait(langr, _, _, argsr),
1101-
) => {
1103+
) if langl == langr => {
11021104
// FIXME: consider the bounds!
11031105
debug!("{:?} {:?}", argsl, argsr);
1104-
langl == langr
1106+
true
11051107
}
11061108
_ => false,
11071109
}
1108-
})
1109-
} else {
1110-
false
1110+
}) =>
1111+
{
1112+
StatementAsExpression::NeedsBoxing
1113+
}
1114+
_ => StatementAsExpression::CorrectType,
11111115
}
11121116
}
1113-
_ => false,
1117+
_ => StatementAsExpression::CorrectType,
11141118
};
1115-
debug!(
1116-
"needs_box {:?} {:?} {:?}",
1117-
needs_box,
1118-
last_expr_ty.kind(),
1119-
self.can_sub(self.param_env, last_expr_ty, expected_ty)
1120-
);
11211119
if (matches!(last_expr_ty.kind(), ty::Error(_))
11221120
|| self.can_sub(self.param_env, last_expr_ty, expected_ty).is_err())
1123-
&& !needs_box
1121+
&& matches!(needs_box, StatementAsExpression::CorrectType)
11241122
{
11251123
return None;
11261124
}

Diff for: compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_middle::ty::{self, Ty};
2020
use rustc_session::Session;
2121
use rustc_span::symbol::{sym, Ident};
2222
use rustc_span::{self, MultiSpan, Span};
23-
use rustc_trait_selection::traits::{self, ObligationCauseCode};
23+
use rustc_trait_selection::traits::{self, ObligationCauseCode, StatementAsExpression};
2424

2525
use std::mem::replace;
2626
use std::slice;
@@ -759,7 +759,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
759759
err: &mut DiagnosticBuilder<'_>,
760760
) {
761761
if let Some((span_semi, boxed)) = self.could_remove_semicolon(blk, expected_ty) {
762-
if boxed {
762+
if let StatementAsExpression::NeedsBoxing = boxed {
763763
err.span_suggestion_verbose(
764764
span_semi,
765765
"consider removing this semicolon and boxing the expression",

0 commit comments

Comments
 (0)