Skip to content

Commit

Permalink
Rollup merge of #87687 - camsteffen:inline-macros, r=oli-obk
Browse files Browse the repository at this point in the history
Inline some macros

I factored out some macros that are not really necessary.
  • Loading branch information
camsteffen authored Aug 2, 2021
2 parents 1b48f4d + 02cd72e commit 87a99c5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 64 deletions.
16 changes: 4 additions & 12 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,18 +442,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
then: &Block,
else_opt: Option<&Expr>,
) -> hir::ExprKind<'hir> {
macro_rules! make_if {
($opt:expr) => {{
let cond = self.lower_expr(cond);
let then_expr = self.lower_block_expr(then);
hir::ExprKind::If(cond, self.arena.alloc(then_expr), $opt)
}};
}
if let Some(rslt) = else_opt {
make_if!(Some(self.lower_expr(rslt)))
} else {
make_if!(None)
}
let cond = self.lower_expr(cond);
let then = self.arena.alloc(self.lower_block_expr(then));
let els = else_opt.map(|els| self.lower_expr(els));
hir::ExprKind::If(cond, then, els)
}

fn lower_expr_if_let(
Expand Down
100 changes: 49 additions & 51 deletions compiler/rustc_typeck/src/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,6 @@ use rustc_trait_selection::traits::{
StatementAsExpression,
};

macro_rules! create_maybe_get_coercion_reason {
($fn_name:ident, $node:expr) => {
pub(crate) fn $fn_name(&self, hir_id: hir::HirId, sp: Span) -> Option<(Span, String)> {
let node = $node(self.tcx.hir(), hir_id);
if let hir::Node::Block(block) = node {
// check that the body's parent is an fn
let parent = self.tcx.hir().get(
self.tcx.hir().get_parent_node(self.tcx.hir().get_parent_node(block.hir_id)),
);
if let (
Some(expr),
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. }),
) = (&block.expr, parent)
{
// check that the `if` expr without `else` is the fn body's expr
if expr.span == sp {
return self.get_fn_decl(hir_id).and_then(|(fn_decl, _)| {
let span = fn_decl.output.span();
let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok()?;
Some((
span,
format!("expected `{}` because of this return type", snippet),
))
});
}
}
}
if let hir::Node::Local(hir::Local { ty: Some(_), pat, .. }) = node {
return Some((pat.span, "expected because of this assignment".to_string()));
}
None
}
};
}

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn check_match(
&self,
Expand Down Expand Up @@ -154,7 +119,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr.span,
&arms[0].body,
&mut coercion,
|hir_id, span| self.maybe_get_coercion_reason(hir_id, span),
|hir_id, span| self.coercion_reason_match(hir_id, span),
) {
tcx.ty_error()
} else {
Expand Down Expand Up @@ -373,23 +338,56 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
error
}

create_maybe_get_coercion_reason!(
maybe_get_coercion_reason,
|hir: rustc_middle::hir::map::Map<'a>, id| {
let arm_id = hir.get_parent_node(id);
let match_id = hir.get_parent_node(arm_id);
let containing_id = hir.get_parent_node(match_id);
hir.get(containing_id)
}
);
pub(crate) fn coercion_reason_if(
&self,
hir_id: hir::HirId,
span: Span,
) -> Option<(Span, String)> {
self.coercion_reason_inner(hir_id, span, 1)
}

create_maybe_get_coercion_reason!(
maybe_get_coercion_reason_if,
|hir: rustc_middle::hir::map::Map<'a>, id| {
let rslt = hir.get_parent_node(hir.get_parent_node(id));
hir.get(rslt)
pub(crate) fn coercion_reason_match(
&self,
hir_id: hir::HirId,
span: Span,
) -> Option<(Span, String)> {
self.coercion_reason_inner(hir_id, span, 2)
}

fn coercion_reason_inner(
&self,
hir_id: hir::HirId,
span: Span,
parent_index: usize,
) -> Option<(Span, String)> {
let hir = self.tcx.hir();
let mut parent_iter = hir.parent_iter(hir_id);
let (_, node) = parent_iter.nth(parent_index)?;
match node {
hir::Node::Block(block) => {
let expr = block.expr?;
// check that the body's parent is an fn
let (_, parent) = parent_iter.nth(1)?;
if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. }) = parent {
// check that the `if` expr without `else` is the fn body's expr
if expr.span == span {
let (fn_decl, _) = self.get_fn_decl(hir_id)?;
let span = fn_decl.output.span();
let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok()?;
return Some((
span,
format!("expected `{}` because of this return type", snippet),
));
}
}
None
}
hir::Node::Local(hir::Local { ty: Some(_), pat, .. }) => {
Some((pat.span, "expected because of this assignment".to_string()))
}
_ => None,
}
);
}

pub(crate) fn if_cause(
&self,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.diverges.set(cond_diverges | then_diverges & else_diverges);
} else {
self.if_fallback_coercion(sp, then_expr, &mut coerce, |hir_id, span| {
self.maybe_get_coercion_reason_if(hir_id, span)
self.coercion_reason_if(hir_id, span)
});

// If the condition is false we can't diverge.
Expand Down

0 comments on commit 87a99c5

Please sign in to comment.