Skip to content

Commit a922d1c

Browse files
committed
Auto merge of #114396 - compiler-errors:hir-typeck-nits, r=oli-obk
Miscellaneous HIR typeck nits Remove some check functions that only have one usage Also remove `Expectation::IsLast`, which was both undocumented, and was also made redundant by my cleanup/fix in #103987 😸
2 parents 2e6ac7f + aa41c98 commit a922d1c

File tree

5 files changed

+28
-68
lines changed

5 files changed

+28
-68
lines changed

compiler/rustc_hir_typeck/src/_match.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
136136
&cause,
137137
Some(&arm.body),
138138
arm_ty,
139-
Some(&mut |err| {
140-
self.suggest_removing_semicolon_for_coerce(
141-
err,
142-
expr,
143-
orig_expected,
144-
arm_ty,
145-
prior_arm,
146-
)
147-
}),
139+
|err| self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm),
148140
false,
149141
);
150142

@@ -181,7 +173,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
181173
&self,
182174
diag: &mut Diagnostic,
183175
expr: &hir::Expr<'tcx>,
184-
expectation: Expectation<'tcx>,
185176
arm_ty: Ty<'tcx>,
186177
prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
187178
) {
@@ -195,7 +186,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
195186
let hir::ExprKind::Block(block, _) = body.value.kind else {
196187
return;
197188
};
198-
let Some(hir::Stmt { kind: hir::StmtKind::Semi(last_expr), .. }) =
189+
let Some(hir::Stmt { kind: hir::StmtKind::Semi(last_expr), span: semi_span, .. }) =
199190
block.innermost_block().stmts.last()
200191
else {
201192
return;
@@ -212,9 +203,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
212203
else {
213204
return;
214205
};
215-
let Expectation::IsLast(stmt) = expectation else {
216-
return;
217-
};
218206

219207
let can_coerce_to_return_ty = match self.ret_coercion.as_ref() {
220208
Some(ret_coercion) => {
@@ -231,7 +219,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
231219
return;
232220
}
233221

234-
let semi_span = expr.span.shrink_to_hi().with_hi(stmt.hi());
222+
let semi_span = expr.span.shrink_to_hi().with_hi(semi_span.hi());
235223
let mut ret_span: MultiSpan = semi_span.into();
236224
ret_span.push_span_label(
237225
expr.span,
@@ -279,7 +267,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
279267
coercion.coerce_forced_unit(
280268
self,
281269
&cause,
282-
&mut |err| {
270+
|err| {
283271
if let Some((span, msg)) = &ret_reason {
284272
err.span_label(*span, msg.clone());
285273
} else if let ExprKind::Block(block, _) = &then_expr.kind

compiler/rustc_hir_typeck/src/coercion.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14181418
expression: &'tcx hir::Expr<'tcx>,
14191419
expression_ty: Ty<'tcx>,
14201420
) {
1421-
self.coerce_inner(fcx, cause, Some(expression), expression_ty, None, false)
1421+
self.coerce_inner(fcx, cause, Some(expression), expression_ty, |_| {}, false)
14221422
}
14231423

14241424
/// Indicates that one of the inputs is a "forced unit". This
@@ -1437,15 +1437,15 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14371437
&mut self,
14381438
fcx: &FnCtxt<'a, 'tcx>,
14391439
cause: &ObligationCause<'tcx>,
1440-
augment_error: &mut dyn FnMut(&mut Diagnostic),
1440+
augment_error: impl FnOnce(&mut Diagnostic),
14411441
label_unit_as_expected: bool,
14421442
) {
14431443
self.coerce_inner(
14441444
fcx,
14451445
cause,
14461446
None,
14471447
Ty::new_unit(fcx.tcx),
1448-
Some(augment_error),
1448+
augment_error,
14491449
label_unit_as_expected,
14501450
)
14511451
}
@@ -1460,7 +1460,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14601460
cause: &ObligationCause<'tcx>,
14611461
expression: Option<&'tcx hir::Expr<'tcx>>,
14621462
mut expression_ty: Ty<'tcx>,
1463-
augment_error: Option<&mut dyn FnMut(&mut Diagnostic)>,
1463+
augment_error: impl FnOnce(&mut Diagnostic),
14641464
label_expression_as_expected: bool,
14651465
) {
14661466
// Incorporate whatever type inference information we have
@@ -1639,9 +1639,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16391639
}
16401640
}
16411641

1642-
if let Some(augment_error) = augment_error {
1643-
augment_error(&mut err);
1644-
}
1642+
augment_error(&mut err);
16451643

16461644
let is_insufficiently_polymorphic =
16471645
matches!(coercion_error, TypeError::RegionsInsufficientlyPolymorphic(..));

compiler/rustc_hir_typeck/src/expectation.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ pub enum Expectation<'tcx> {
2121
/// This rvalue expression will be wrapped in `&` or `Box` and coerced
2222
/// to `&Ty` or `Box<Ty>`, respectively. `Ty` is `[A]` or `Trait`.
2323
ExpectRvalueLikeUnsized(Ty<'tcx>),
24-
25-
IsLast(Span),
2624
}
2725

2826
impl<'a, 'tcx> Expectation<'tcx> {
@@ -88,13 +86,12 @@ impl<'a, 'tcx> Expectation<'tcx> {
8886
ExpectCastableToType(t) => ExpectCastableToType(fcx.resolve_vars_if_possible(t)),
8987
ExpectHasType(t) => ExpectHasType(fcx.resolve_vars_if_possible(t)),
9088
ExpectRvalueLikeUnsized(t) => ExpectRvalueLikeUnsized(fcx.resolve_vars_if_possible(t)),
91-
IsLast(sp) => IsLast(sp),
9289
}
9390
}
9491

9592
pub(super) fn to_option(self, fcx: &FnCtxt<'a, 'tcx>) -> Option<Ty<'tcx>> {
9693
match self.resolve(fcx) {
97-
NoExpectation | IsLast(_) => None,
94+
NoExpectation => None,
9895
ExpectCastableToType(ty) | ExpectHasType(ty) | ExpectRvalueLikeUnsized(ty) => Some(ty),
9996
}
10097
}
@@ -106,9 +103,7 @@ impl<'a, 'tcx> Expectation<'tcx> {
106103
pub(super) fn only_has_type(self, fcx: &FnCtxt<'a, 'tcx>) -> Option<Ty<'tcx>> {
107104
match self {
108105
ExpectHasType(ty) => Some(fcx.resolve_vars_if_possible(ty)),
109-
NoExpectation | ExpectCastableToType(_) | ExpectRvalueLikeUnsized(_) | IsLast(_) => {
110-
None
111-
}
106+
NoExpectation | ExpectCastableToType(_) | ExpectRvalueLikeUnsized(_) => None,
112107
}
113108
}
114109

compiler/rustc_hir_typeck/src/expr.rs

+10-24
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,13 @@ use rustc_trait_selection::traits::ObligationCtxt;
6060
use rustc_trait_selection::traits::{self, ObligationCauseCode};
6161

6262
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
63-
fn check_expr_eq_type(&self, expr: &'tcx hir::Expr<'tcx>, expected: Ty<'tcx>) {
64-
let ty = self.check_expr_with_hint(expr, expected);
65-
self.demand_eqtype(expr.span, expected, ty);
66-
}
67-
6863
pub fn check_expr_has_type_or_error(
6964
&self,
7065
expr: &'tcx hir::Expr<'tcx>,
71-
expected: Ty<'tcx>,
72-
extend_err: impl FnMut(&mut Diagnostic),
73-
) -> Ty<'tcx> {
74-
self.check_expr_meets_expectation_or_error(expr, ExpectHasType(expected), extend_err)
75-
}
76-
77-
fn check_expr_meets_expectation_or_error(
78-
&self,
79-
expr: &'tcx hir::Expr<'tcx>,
80-
expected: Expectation<'tcx>,
81-
mut extend_err: impl FnMut(&mut Diagnostic),
66+
expected_ty: Ty<'tcx>,
67+
extend_err: impl FnOnce(&mut Diagnostic),
8268
) -> Ty<'tcx> {
83-
let expected_ty = expected.to_option(&self).unwrap_or(self.tcx.types.bool);
84-
let mut ty = self.check_expr_with_expectation(expr, expected);
69+
let mut ty = self.check_expr_with_expectation(expr, ExpectHasType(expected_ty));
8570

8671
// While we don't allow *arbitrary* coercions here, we *do* allow
8772
// coercions from ! to `expected`.
@@ -341,9 +326,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
341326
}
342327
ExprKind::Cast(e, t) => self.check_expr_cast(e, t, expr),
343328
ExprKind::Type(e, t) => {
344-
let ty = self.to_ty_saving_user_provided_ty(&t);
345-
self.check_expr_eq_type(&e, ty);
346-
ty
329+
let ascribed_ty = self.to_ty_saving_user_provided_ty(&t);
330+
let ty = self.check_expr_with_hint(e, ascribed_ty);
331+
self.demand_eqtype(e.span, ascribed_ty, ty);
332+
ascribed_ty
347333
}
348334
ExprKind::If(cond, then_expr, opt_else_expr) => {
349335
self.check_then_else(cond, then_expr, opt_else_expr, expr.span, expected)
@@ -666,7 +652,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
666652
coerce.coerce_forced_unit(
667653
self,
668654
&cause,
669-
&mut |mut err| {
655+
|mut err| {
670656
self.suggest_mismatched_types_on_tail(
671657
&mut err, expr, ty, e_ty, target_id,
672658
);
@@ -762,7 +748,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
762748
coercion.coerce_forced_unit(
763749
self,
764750
&cause,
765-
&mut |db| {
751+
|db| {
766752
let span = fn_decl.output.span();
767753
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
768754
db.span_label(
@@ -774,7 +760,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
774760
true,
775761
);
776762
} else {
777-
coercion.coerce_forced_unit(self, &cause, &mut |_| (), true);
763+
coercion.coerce_forced_unit(self, &cause, |_| (), true);
778764
}
779765
}
780766
self.tcx.types.never

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14851485
self.check_decl(local.into());
14861486
}
14871487

1488-
pub fn check_stmt(&self, stmt: &'tcx hir::Stmt<'tcx>, is_last: bool) {
1488+
pub fn check_stmt(&self, stmt: &'tcx hir::Stmt<'tcx>) {
14891489
// Don't do all the complex logic below for `DeclItem`.
14901490
match stmt.kind {
14911491
hir::StmtKind::Item(..) => return,
@@ -1512,14 +1512,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15121512
});
15131513
}
15141514
hir::StmtKind::Semi(ref expr) => {
1515-
// All of this is equivalent to calling `check_expr`, but it is inlined out here
1516-
// in order to capture the fact that this `match` is the last statement in its
1517-
// function. This is done for better suggestions to remove the `;`.
1518-
let expectation = match expr.kind {
1519-
hir::ExprKind::Match(..) if is_last => IsLast(stmt.span),
1520-
_ => NoExpectation,
1521-
};
1522-
self.check_expr_with_expectation(expr, expectation);
1515+
self.check_expr(expr);
15231516
}
15241517
}
15251518

@@ -1570,8 +1563,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15701563
let ctxt = BreakableCtxt { coerce: Some(coerce), may_break: false };
15711564

15721565
let (ctxt, ()) = self.with_breakable_ctxt(blk.hir_id, ctxt, || {
1573-
for (pos, s) in blk.stmts.iter().enumerate() {
1574-
self.check_stmt(s, blk.stmts.len() - 1 == pos);
1566+
for s in blk.stmts {
1567+
self.check_stmt(s);
15751568
}
15761569

15771570
// check the tail expression **without** holding the
@@ -1594,9 +1587,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15941587
&cause,
15951588
Some(tail_expr),
15961589
tail_expr_ty,
1597-
Some(&mut |diag: &mut Diagnostic| {
1590+
|diag| {
15981591
self.suggest_block_to_brackets(diag, blk, tail_expr_ty, ty_for_diagnostic);
1599-
}),
1592+
},
16001593
false,
16011594
);
16021595
} else {
@@ -1633,7 +1626,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16331626
coerce.coerce_forced_unit(
16341627
self,
16351628
&self.misc(sp),
1636-
&mut |err| {
1629+
|err| {
16371630
if let Some(expected_ty) = expected.only_has_type(self) {
16381631
if blk.stmts.is_empty() && blk.expr.is_none() {
16391632
self.suggest_boxing_when_appropriate(

0 commit comments

Comments
 (0)