name: i.ident.name,
attrs: lower_attrs(lctx, &i.attrs),
vis: lower_visibility(lctx, i.vis),
+ defaultness: lower_defaultness(lctx, i.defaultness),
node: match i.node {
ImplItemKind::Const(ref ty, ref expr) => {
hir::ImplItemKind::Const(lower_ty(lctx, ty), lower_expr(lctx, expr))
dialect,
expn_id,
}) => hir::ExprInlineAsm(hir::InlineAsm {
- inputs: inputs.iter()
- .map(|&(ref c, ref input)| (c.clone(), lower_expr(lctx, input)))
- .collect(),
+ inputs: inputs.iter().map(|&(ref c, _)| c.clone()).collect(),
outputs: outputs.iter()
.map(|out| {
hir::InlineAsmOutput {
constraint: out.constraint.clone(),
- expr: lower_expr(lctx, &out.expr),
is_rw: out.is_rw,
is_indirect: out.is_indirect,
}
alignstack: alignstack,
dialect: dialect,
expn_id: expn_id,
- }),
+ }, outputs.iter().map(|out| lower_expr(lctx, &out.expr)).collect(),
+ inputs.iter().map(|&(_, ref input)| lower_expr(lctx, input)).collect()),
ExprKind::Struct(ref path, ref fields, ref maybe_expr) => {
hir::ExprStruct(lower_path(lctx, path),
fields.iter().map(|x| lower_field(lctx, x)).collect(),
});
}
+ // Desugar ExprKind::Try
+ // From: `<expr>?`
+ ExprKind::Try(ref sub_expr) => {
+ // to:
+ //
+ // {
+ // match <expr> {
+ // Ok(val) => val,
+ // Err(err) => {
+ // return Err(From::from(err))
+ // }
+ // }
+ // }
+
+ return cache_ids(lctx, e.id, |lctx| {
+ // expand <expr>
+ let sub_expr = lower_expr(lctx, sub_expr);
+
+ // Ok(val) => val
+ let ok_arm = {
+ let val_ident = lctx.str_to_ident("val");
+ let val_pat = pat_ident(lctx, e.span, val_ident);
+ let val_expr = expr_ident(lctx, e.span, val_ident, None);
+ let ok_pat = pat_ok(lctx, e.span, val_pat);
+
+ arm(hir_vec![ok_pat], val_expr)
+ };
+
+ // Err(err) => return Err(From::from(err))
+ let err_arm = {
+ let err_ident = lctx.str_to_ident("err");
+ let from_expr = {
+ let path = std_path(lctx, &["convert", "From", "from"]);
+ let path = path_global(e.span, path);
+ let from = expr_path(lctx, path, None);
+ let err_expr = expr_ident(lctx, e.span, err_ident, None);
+
+ expr_call(lctx, e.span, from, hir_vec![err_expr], None)
+ };
+ let err_expr = {
+ let path = std_path(lctx, &["result", "Result", "Err"]);
+ let path = path_global(e.span, path);
+ let err_ctor = expr_path(lctx, path, None);
+ expr_call(lctx, e.span, err_ctor, hir_vec![from_expr], None)
+ };
+ let err_pat = pat_err(lctx, e.span, pat_ident(lctx, e.span, err_ident));
+ let ret_expr = expr(lctx, e.span,
+ hir::Expr_::ExprRet(Some(err_expr)), None);
+
+ arm(hir_vec![err_pat], ret_expr)
+ };
+
+ expr_match(lctx, e.span, sub_expr, hir_vec![err_arm, ok_arm],
+ hir::MatchSource::TryDesugar, None)
+ })
+ }
+
ExprKind::Mac(_) => panic!("Shouldn't exist here"),
},
span: e.span,
}
}
+pub fn lower_defaultness(_lctx: &LoweringContext, d: Defaultness) -> hir::Defaultness {
+ match d {
+ Defaultness::Default => hir::Defaultness::Default,
+ Defaultness::Final => hir::Defaultness::Final,
+ }
+}
+
pub fn lower_block_check_mode(lctx: &LoweringContext, b: &BlockCheckMode) -> hir::BlockCheckMode {
match *b {
BlockCheckMode::Default => hir::DefaultBlock,
})
}
+fn pat_ok(lctx: &LoweringContext, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
+ let ok = std_path(lctx, &["result", "Result", "Ok"]);
+ let path = path_global(span, ok);
+ pat_enum(lctx, span, path, hir_vec![pat])
+}
+
+fn pat_err(lctx: &LoweringContext, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
+ let err = std_path(lctx, &["result", "Result", "Err"]);
+ let path = path_global(span, err);
+ pat_enum(lctx, span, path, hir_vec![pat])
+}
+
fn pat_some(lctx: &LoweringContext, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
let some = std_path(lctx, &["option", "Option", "Some"]);
let path = path_global(span, some);