Skip to content

Commit 0314b62

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

File tree

4 files changed

+27
-91
lines changed

4 files changed

+27
-91
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: 20 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,17 +1165,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11651165
///
11661166
/// This is really an internal helper. From outside the coercion
11671167
/// module, you should instantiate a `CoerceMany` instance.
1168-
fn try_find_coercion_lub<E>(
1168+
fn try_find_coercion_lub(
11691169
&self,
11701170
cause: &ObligationCause<'tcx>,
1171-
exprs: &[E],
1171+
exprs: &[&'tcx hir::Expr<'tcx>],
11721172
prev_ty: Ty<'tcx>,
11731173
new: &hir::Expr<'_>,
11741174
new_ty: Ty<'tcx>,
1175-
) -> RelateResult<'tcx, Ty<'tcx>>
1176-
where
1177-
E: AsCoercionSite,
1178-
{
1175+
) -> RelateResult<'tcx, Ty<'tcx>> {
11791176
let prev_ty = self.try_structurally_resolve_type(cause.span, prev_ty);
11801177
let new_ty = self.try_structurally_resolve_type(new.span, new_ty);
11811178
debug!(
@@ -1269,7 +1266,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12691266
ty::FnDef(..) => Adjust::Pointer(PointerCoercion::ReifyFnPointer(sig.safety())),
12701267
_ => span_bug!(new.span, "should not try to coerce a {new_ty} to a fn pointer"),
12711268
};
1272-
for expr in exprs.iter().map(|e| e.as_coercion_site()) {
1269+
for expr in exprs.iter() {
12731270
self.apply_adjustments(
12741271
expr,
12751272
vec![Adjustment { kind: prev_adjustment.clone(), target: fn_ptr }],
@@ -1316,7 +1313,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13161313

13171314
let (adjustments, target) = self.register_infer_ok_obligations(ok);
13181315
for expr in exprs {
1319-
let expr = expr.as_coercion_site();
13201316
self.apply_adjustments(expr, adjustments.clone());
13211317
}
13221318
debug!(
@@ -1382,40 +1378,35 @@ pub fn can_coerce<'tcx>(
13821378
/// }
13831379
/// let final_ty = coerce.complete(fcx);
13841380
/// ```
1385-
pub(crate) struct CoerceMany<'tcx, 'exprs, E: AsCoercionSite> {
1381+
pub(crate) struct CoerceMany<'tcx> {
13861382
expected_ty: Ty<'tcx>,
13871383
final_ty: Option<Ty<'tcx>>,
1388-
expressions: Expressions<'tcx, 'exprs, E>,
1384+
expressions: Vec<&'tcx hir::Expr<'tcx>>,
13891385
pushed: usize,
13901386
}
13911387

13921388
/// The type of a `CoerceMany` that is storing up the expressions into
13931389
/// 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>>;
1395-
1396-
enum Expressions<'tcx, 'exprs, E: AsCoercionSite> {
1397-
Dynamic(Vec<&'tcx hir::Expr<'tcx>>),
1398-
UpFront(&'exprs [E]),
1399-
}
1390+
pub(crate) type DynamicCoerceMany<'tcx> = CoerceMany<'tcx>;
14001391

1401-
impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1392+
impl<'tcx> CoerceMany<'tcx> {
14021393
/// The usual case; collect the set of expressions dynamically.
14031394
/// If the full set of coercion sites is known before hand,
14041395
/// consider `with_coercion_sites()` instead to avoid allocation.
14051396
pub(crate) fn new(expected_ty: Ty<'tcx>) -> Self {
1406-
Self::make(expected_ty, Expressions::Dynamic(vec![]))
1397+
Self::make(expected_ty, Vec::with_capacity(1))
14071398
}
14081399

14091400
/// As an optimization, you can create a `CoerceMany` with a
14101401
/// preexisting slice of expressions. In this case, you are
14111402
/// expected to pass each element in the slice to `coerce(...)` in
14121403
/// order. This is used with arrays in particular to avoid
14131404
/// 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))
1405+
pub(crate) fn with_coercion_sites(expected_ty: Ty<'tcx>, coercion_sites: usize) -> Self {
1406+
Self::make(expected_ty, Vec::with_capacity(coercion_sites))
14161407
}
14171408

1418-
fn make(expected_ty: Ty<'tcx>, expressions: Expressions<'tcx, 'exprs, E>) -> Self {
1409+
fn make(expected_ty: Ty<'tcx>, expressions: Vec<&'tcx hir::Expr<'tcx>>) -> Self {
14191410
CoerceMany { expected_ty, final_ty: None, expressions, pushed: 0 }
14201411
}
14211412

@@ -1541,22 +1532,13 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15411532
Some(cause.clone()),
15421533
)
15431534
} 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-
}
1535+
fcx.try_find_coercion_lub(
1536+
cause,
1537+
&self.expressions,
1538+
self.merged_ty(),
1539+
expression,
1540+
expression_ty,
1541+
)
15601542
}
15611543
} else {
15621544
// this is a hack for cases where we default to `()` because
@@ -1591,17 +1573,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15911573
Ok(v) => {
15921574
self.final_ty = Some(v);
15931575
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-
}
1576+
self.expressions.push(e);
16051577
self.pushed += 1;
16061578
}
16071579
}
@@ -1961,39 +1933,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
19611933
}
19621934
}
19631935

1964-
/// Something that can be converted into an expression to which we can
1965-
/// apply a coercion.
1966-
pub(crate) trait AsCoercionSite {
1967-
fn as_coercion_site(&self) -> &hir::Expr<'_>;
1968-
}
1969-
1970-
impl AsCoercionSite for hir::Expr<'_> {
1971-
fn as_coercion_site(&self) -> &hir::Expr<'_> {
1972-
self
1973-
}
1974-
}
1975-
1976-
impl<'a, T> AsCoercionSite for &'a T
1977-
where
1978-
T: AsCoercionSite,
1979-
{
1980-
fn as_coercion_site(&self) -> &hir::Expr<'_> {
1981-
(**self).as_coercion_site()
1982-
}
1983-
}
1984-
1985-
impl AsCoercionSite for ! {
1986-
fn as_coercion_site(&self) -> &hir::Expr<'_> {
1987-
*self
1988-
}
1989-
}
1990-
1991-
impl AsCoercionSite for hir::Arm<'_> {
1992-
fn as_coercion_site(&self) -> &hir::Expr<'_> {
1993-
self.body
1994-
}
1995-
}
1996-
19971936
/// Recursively visit goals to decide whether an unsizing is possible.
19981937
/// `Break`s when it isn't, and an error should be raised.
19991938
/// `Continue`s when an unsizing ok based on an implementation of the `Unsize` trait / lang item.

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)