Skip to content

Commit f8c9958

Browse files
committed
Remove Expressions (and just use a Vec)
1 parent ee44706 commit f8c9958

File tree

4 files changed

+23
-50
lines changed

4 files changed

+23
-50
lines changed

compiler/rustc_hir_typeck/src/_match.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_trait_selection::traits::{
1010
};
1111
use tracing::{debug, instrument};
1212

13-
use crate::coercion::{AsCoercionSite, CoerceMany};
13+
use crate::coercion::CoerceMany;
1414
use crate::{Diverges, Expectation, FnCtxt, GatherLocalsVisitor, Needs};
1515

1616
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -73,7 +73,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7373
Expectation::ExpectHasType(ety) if ety != tcx.types.unit => ety,
7474
_ => self.next_ty_var(expr.span),
7575
};
76-
CoerceMany::with_coercion_sites(coerce_first, arms)
76+
CoerceMany::with_coercion_sites(coerce_first, arms.len())
7777
};
7878

7979
let mut prior_non_diverging_arms = vec![]; // Used only for diagnostics.
@@ -269,16 +269,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
269269
/// Handle the fallback arm of a desugared if(-let) like a missing else.
270270
///
271271
/// Returns `true` if there was an error forcing the coercion to the `()` type.
272-
pub(super) fn if_fallback_coercion<T>(
272+
pub(super) fn if_fallback_coercion(
273273
&self,
274274
if_span: Span,
275275
cond_expr: &'tcx hir::Expr<'tcx>,
276276
then_expr: &'tcx hir::Expr<'tcx>,
277-
coercion: &mut CoerceMany<'tcx, '_, T>,
278-
) -> bool
279-
where
280-
T: AsCoercionSite,
281-
{
277+
coercion: &mut CoerceMany<'tcx>,
278+
) -> bool {
282279
// If this `if` expr is the parent's function return expr,
283280
// the cause of the type coercion is the return type, point at it. (#25228)
284281
let hir_id = self.tcx.parent_hir_id(self.tcx.parent_hir_id(then_expr.hir_id));

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,40 +1382,35 @@ pub fn can_coerce<'tcx>(
13821382
/// }
13831383
/// let final_ty = coerce.complete(fcx);
13841384
/// ```
1385-
pub(crate) struct CoerceMany<'tcx, 'exprs, E: AsCoercionSite> {
1385+
pub(crate) struct CoerceMany<'tcx> {
13861386
expected_ty: Ty<'tcx>,
13871387
final_ty: Option<Ty<'tcx>>,
1388-
expressions: Expressions<'tcx, 'exprs, E>,
1388+
expressions: Vec<&'tcx hir::Expr<'tcx>>,
13891389
pushed: usize,
13901390
}
13911391

13921392
/// The type of a `CoerceMany` that is storing up the expressions into
13931393
/// a buffer. We use this in `check/mod.rs` for things like `break`.
1394-
pub(crate) type DynamicCoerceMany<'tcx> = CoerceMany<'tcx, 'tcx, &'tcx hir::Expr<'tcx>>;
1394+
pub(crate) type DynamicCoerceMany<'tcx> = CoerceMany<'tcx>;
13951395

1396-
enum Expressions<'tcx, 'exprs, E: AsCoercionSite> {
1397-
Dynamic(Vec<&'tcx hir::Expr<'tcx>>),
1398-
UpFront(&'exprs [E]),
1399-
}
1400-
1401-
impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1396+
impl<'tcx> CoerceMany<'tcx> {
14021397
/// The usual case; collect the set of expressions dynamically.
14031398
/// If the full set of coercion sites is known before hand,
14041399
/// consider `with_coercion_sites()` instead to avoid allocation.
14051400
pub(crate) fn new(expected_ty: Ty<'tcx>) -> Self {
1406-
Self::make(expected_ty, Expressions::Dynamic(vec![]))
1401+
Self::make(expected_ty, Vec::with_capacity(1))
14071402
}
14081403

14091404
/// As an optimization, you can create a `CoerceMany` with a
14101405
/// preexisting slice of expressions. In this case, you are
14111406
/// expected to pass each element in the slice to `coerce(...)` in
14121407
/// order. This is used with arrays in particular to avoid
14131408
/// needlessly cloning the slice.
1414-
pub(crate) fn with_coercion_sites(expected_ty: Ty<'tcx>, coercion_sites: &'exprs [E]) -> Self {
1415-
Self::make(expected_ty, Expressions::UpFront(coercion_sites))
1409+
pub(crate) fn with_coercion_sites(expected_ty: Ty<'tcx>, coercion_sites: usize) -> Self {
1410+
Self::make(expected_ty, Vec::with_capacity(coercion_sites))
14161411
}
14171412

1418-
fn make(expected_ty: Ty<'tcx>, expressions: Expressions<'tcx, 'exprs, E>) -> Self {
1413+
fn make(expected_ty: Ty<'tcx>, expressions: Vec<&'tcx hir::Expr<'tcx>>) -> Self {
14191414
CoerceMany { expected_ty, final_ty: None, expressions, pushed: 0 }
14201415
}
14211416

@@ -1541,22 +1536,13 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15411536
Some(cause.clone()),
15421537
)
15431538
} else {
1544-
match self.expressions {
1545-
Expressions::Dynamic(ref exprs) => fcx.try_find_coercion_lub(
1546-
cause,
1547-
exprs,
1548-
self.merged_ty(),
1549-
expression,
1550-
expression_ty,
1551-
),
1552-
Expressions::UpFront(coercion_sites) => fcx.try_find_coercion_lub(
1553-
cause,
1554-
&coercion_sites[0..self.pushed],
1555-
self.merged_ty(),
1556-
expression,
1557-
expression_ty,
1558-
),
1559-
}
1539+
fcx.try_find_coercion_lub(
1540+
cause,
1541+
&self.expressions,
1542+
self.merged_ty(),
1543+
expression,
1544+
expression_ty,
1545+
)
15601546
}
15611547
} else {
15621548
// this is a hack for cases where we default to `()` because
@@ -1591,17 +1577,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15911577
Ok(v) => {
15921578
self.final_ty = Some(v);
15931579
if let Some(e) = expression {
1594-
match self.expressions {
1595-
Expressions::Dynamic(ref mut buffer) => buffer.push(e),
1596-
Expressions::UpFront(coercion_sites) => {
1597-
// if the user gave us an array to validate, check that we got
1598-
// the next expression in the list, as expected
1599-
assert_eq!(
1600-
coercion_sites[self.pushed].as_coercion_site().hir_id,
1601-
e.hir_id
1602-
);
1603-
}
1604-
}
1580+
self.expressions.push(e);
16051581
self.pushed += 1;
16061582
}
16071583
}

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16811681
.to_option(self)
16821682
.and_then(|uty| self.try_structurally_resolve_type(expr.span, uty).builtin_index())
16831683
.unwrap_or_else(|| self.next_ty_var(expr.span));
1684-
let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
1684+
let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args.len());
16851685

16861686
for e in args {
16871687
let e_ty = self.check_expr_with_hint(e, coerce_to);

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10361036
let coerce = if blk.targeted_by_break {
10371037
CoerceMany::new(coerce_to_ty)
10381038
} else {
1039-
CoerceMany::with_coercion_sites(coerce_to_ty, blk.expr.as_slice())
1039+
CoerceMany::with_coercion_sites(coerce_to_ty, 1)
10401040
};
10411041

10421042
let prev_diverges = self.diverges.get();

0 commit comments

Comments
 (0)