Skip to content

Commit 56156af

Browse files
committed
wip hir::ExprKind::Use
1 parent e943f94 commit 56156af

File tree

12 files changed

+49
-14
lines changed

12 files changed

+49
-14
lines changed

Diff for: src/librustc/cfg/construct.rs

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
328328
hir::ExprKind::AddrOf(_, ref e) |
329329
hir::ExprKind::Cast(ref e, _) |
330330
hir::ExprKind::Type(ref e, _) |
331+
hir::ExprKind::Use(ref e) |
331332
hir::ExprKind::Unary(_, ref e) |
332333
hir::ExprKind::Field(ref e, _) |
333334
hir::ExprKind::Yield(ref e) |

Diff for: src/librustc/hir/intravisit.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10111011
visitor.visit_expr(subexpression);
10121012
visitor.visit_ty(typ)
10131013
}
1014+
ExprKind::Use(ref subexpression) => {
1015+
visitor.visit_expr(subexpression);
1016+
}
10141017
ExprKind::While(ref subexpression, ref block, ref opt_label) => {
10151018
walk_list!(visitor, visit_label, opt_label);
10161019
visitor.visit_expr(subexpression);

Diff for: src/librustc/hir/lowering.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -4062,10 +4062,10 @@ impl<'a> LoweringContext<'a> {
40624062
// Lower condition:
40634063
let span_block = self.mark_span_with_reason(IfTemporary, cond.span, None);
40644064
let cond = self.lower_expr(cond);
4065-
let bool_ty = Some(P(self.ty_bool(e.span)));
4065+
//let bool_ty = Some(P(self.ty_bool(e.span)));
40664066
// FIXME(centril, oli-obk): This is silly but we must wrap the condition expression
40674067
// in a block `{ let _t = $cond; _t }` to preserve drop semantics.
4068-
let cond = self.expr_temp(span_block, hir::LocalSource::Normal, P(cond), bool_ty);
4068+
let cond = self.expr_temp(span_block, P(cond));//, bool_ty);
40694069

40704070
hir::ExprKind::Match(
40714071
P(cond),
@@ -4667,6 +4667,8 @@ impl<'a> LoweringContext<'a> {
46674667

46684668
// `{ let _result = ...; _result }`
46694669
// Underscore prevents an `unused_variables` lint if the head diverges.
4670+
// https://github.com/rust-lang/rust/commit/b445bf2bd1139236fd815bf93610ddaf17726111
4671+
// FIXME(centril): Consider optimizing with `ExprKind::Use`.
46704672
let result_ident = self.str_to_ident("_result");
46714673
let (let_stmt, let_stmt_binding) =
46724674
self.stmt_let(e.span, false, result_ident, match_expr);
@@ -5041,16 +5043,19 @@ impl<'a> LoweringContext<'a> {
50415043
fn expr_temp(
50425044
&mut self,
50435045
span: Span,
5044-
source: hir::LocalSource,
5046+
//source: hir::LocalSource,
50455047
expr: P<hir::Expr>,
5046-
ty: Option<P<hir::Ty>>
5048+
//ty: Option<P<hir::Ty>>
50475049
) -> hir::Expr {
5050+
/*
50485051
let tident = self.str_to_ident("_tmp");
50495052
let (tpat, tpat_nid) = self.pat_ident(span, tident);
50505053
let tstmt = self.stmt_let_pat_ty(span, ty, Some(expr), tpat, source);
50515054
let access = self.expr_ident(span, tident, tpat_nid);
50525055
let block = self.block_all(span, hir_vec![tstmt], Some(P(access)));
50535056
self.expr_block(P(block), ThinVec::new())
5057+
*/
5058+
self.expr(span, hir::ExprKind::Use(expr), ThinVec::new())
50545059
}
50555060

50565061
fn expr_match(
@@ -5248,6 +5253,7 @@ impl<'a> LoweringContext<'a> {
52485253
path
52495254
}
52505255

5256+
/*
52515257
/// Constructs and returns the type `bool`.
52525258
fn ty_bool(&mut self, span: Span) -> hir::Ty {
52535259
let id = self.next_id();
@@ -5257,6 +5263,7 @@ impl<'a> LoweringContext<'a> {
52575263
def: Def::PrimTy(hir::PrimTy::Bool)
52585264
})))
52595265
}
5266+
*/
52605267

52615268
fn ty_path(&mut self, id: LoweredNodeId, span: Span, qpath: hir::QPath) -> hir::Ty {
52625269
let mut id = id;

Diff for: src/librustc/hir/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,7 @@ impl Expr {
13661366
ExprKind::Unary(..) => ExprPrecedence::Unary,
13671367
ExprKind::Lit(_) => ExprPrecedence::Lit,
13681368
ExprKind::Type(..) | ExprKind::Cast(..) => ExprPrecedence::Cast,
1369+
ExprKind::Use(ref expr, ..) => expr.precedence(),
13691370
ExprKind::While(..) => ExprPrecedence::While,
13701371
ExprKind::Loop(..) => ExprPrecedence::Loop,
13711372
ExprKind::Match(..) => ExprPrecedence::Match,
@@ -1435,6 +1436,7 @@ impl Expr {
14351436
ExprKind::Binary(..) |
14361437
ExprKind::Yield(..) |
14371438
ExprKind::Cast(..) |
1439+
ExprKind::Use(..) |
14381440
ExprKind::Err => {
14391441
false
14401442
}
@@ -1484,6 +1486,10 @@ pub enum ExprKind {
14841486
Cast(P<Expr>, P<Ty>),
14851487
/// A type reference (e.g., `Foo`).
14861488
Type(P<Expr>, P<Ty>),
1489+
/// Equivalent to `{ let _t = expr; _t }`.
1490+
/// Maps directly to `hair::ExprKind::Use`.
1491+
/// Only exists to tweak the drop order in HIR.
1492+
Use(P<Expr>),
14871493
/// A while loop, with an optional label
14881494
///
14891495
/// I.e., `'label: while expr { <block> }`.

Diff for: src/librustc/hir/print.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,9 @@ impl<'a> State<'a> {
13201320
self.word_space(":")?;
13211321
self.print_type(&ty)?;
13221322
}
1323+
hir::ExprKind::Use(ref expr) => {
1324+
self.print_expr(expr)?;
1325+
}
13231326
hir::ExprKind::While(ref test, ref blk, opt_label) => {
13241327
if let Some(label) = opt_label {
13251328
self.print_ident(label.ident)?;

Diff for: src/librustc/middle/expr_use_visitor.rs

+4
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,10 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
513513
self.consume_expr(&base);
514514
}
515515

516+
hir::ExprKind::Use(ref expr) => {
517+
self.consume_expr(&expr);
518+
}
519+
516520
hir::ExprKind::AssignOp(_, ref lhs, ref rhs) => {
517521
if self.mc.tables.is_method_call(expr) {
518522
self.consume_expr(lhs);

Diff for: src/librustc/middle/liveness.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ fn visit_expr<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, expr: &'tcx Expr) {
520520
hir::ExprKind::Binary(..) |
521521
hir::ExprKind::AddrOf(..) |
522522
hir::ExprKind::Cast(..) |
523+
hir::ExprKind::Use(..) |
523524
hir::ExprKind::Unary(..) |
524525
hir::ExprKind::Break(..) |
525526
hir::ExprKind::Continue(_) |
@@ -1198,6 +1199,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11981199
hir::ExprKind::AddrOf(_, ref e) |
11991200
hir::ExprKind::Cast(ref e, _) |
12001201
hir::ExprKind::Type(ref e, _) |
1202+
hir::ExprKind::Use(ref e) |
12011203
hir::ExprKind::Unary(_, ref e) |
12021204
hir::ExprKind::Yield(ref e) |
12031205
hir::ExprKind::Repeat(ref e, _) => {
@@ -1501,9 +1503,9 @@ fn check_expr<'a, 'tcx>(this: &mut Liveness<'a, 'tcx>, expr: &'tcx Expr) {
15011503
hir::ExprKind::Match(..) | hir::ExprKind::While(..) | hir::ExprKind::Loop(..) |
15021504
hir::ExprKind::Index(..) | hir::ExprKind::Field(..) |
15031505
hir::ExprKind::Array(..) | hir::ExprKind::Tup(..) | hir::ExprKind::Binary(..) |
1504-
hir::ExprKind::Cast(..) | hir::ExprKind::Unary(..) | hir::ExprKind::Ret(..) |
1505-
hir::ExprKind::Break(..) | hir::ExprKind::Continue(..) | hir::ExprKind::Lit(_) |
1506-
hir::ExprKind::Block(..) | hir::ExprKind::AddrOf(..) |
1506+
hir::ExprKind::Cast(..) | hir::ExprKind::Use(..) | hir::ExprKind::Unary(..) |
1507+
hir::ExprKind::Ret(..) | hir::ExprKind::Break(..) | hir::ExprKind::Continue(..) |
1508+
hir::ExprKind::Lit(_) | hir::ExprKind::Block(..) | hir::ExprKind::AddrOf(..) |
15071509
hir::ExprKind::Struct(..) | hir::ExprKind::Repeat(..) |
15081510
hir::ExprKind::Closure(..) | hir::ExprKind::Path(_) | hir::ExprKind::Yield(..) |
15091511
hir::ExprKind::Box(..) | hir::ExprKind::Type(..) | hir::ExprKind::Err => {

Diff for: src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
682682
hir::ExprKind::Assign(..) | hir::ExprKind::AssignOp(..) |
683683
hir::ExprKind::Closure(..) | hir::ExprKind::Ret(..) |
684684
hir::ExprKind::Unary(..) | hir::ExprKind::Yield(..) |
685-
hir::ExprKind::MethodCall(..) | hir::ExprKind::Cast(..) |
685+
hir::ExprKind::MethodCall(..) | hir::ExprKind::Cast(..) | hir::ExprKind::Use(..) |
686686
hir::ExprKind::Array(..) | hir::ExprKind::Tup(..) |
687687
hir::ExprKind::Binary(..) | hir::ExprKind::While(..) |
688688
hir::ExprKind::Block(..) | hir::ExprKind::Loop(..) | hir::ExprKind::Match(..) |

Diff for: src/librustc/middle/region.rs

+6
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,12 @@ fn resolve_expr<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, expr:
898898
visitor.cx.var_parent = visitor.cx.parent;
899899
}
900900

901+
hir::ExprKind::Use(ref expr) => {
902+
// `Use(expr)` does not denote a conditional scope.
903+
// Rather, we want to achieve the same behavior as `{ let _t = expr; _t }`.
904+
terminating(expr.hir_id.local_id);
905+
}
906+
901907
hir::ExprKind::AssignOp(..) | hir::ExprKind::Index(..) |
902908
hir::ExprKind::Unary(..) | hir::ExprKind::Call(..) | hir::ExprKind::MethodCall(..) => {
903909
// FIXME(https://github.com/rust-lang/rfcs/issues/811) Nested method calls

Diff for: src/librustc_mir/hair/cx/expr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,9 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
763763
}
764764
}
765765
}
766+
hir::ExprKind::Use(ref source) => {
767+
ExprKind::Use { source: source.to_ref() }
768+
}
766769
hir::ExprKind::Box(ref value) => {
767770
ExprKind::Box {
768771
value: value.to_ref(),

Diff for: src/librustc_passes/rvalue_promotion.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,9 @@ fn check_expr_kind<'a, 'tcx>(
436436
hir::ExprKind::Err => Promotable,
437437

438438
hir::ExprKind::AddrOf(_, ref expr) |
439-
hir::ExprKind::Repeat(ref expr, _) => {
439+
hir::ExprKind::Repeat(ref expr, _) |
440+
hir::ExprKind::Type(ref expr, _) |
441+
hir::ExprKind::Use(ref expr) => {
440442
v.check_expr(&expr)
441443
}
442444

@@ -483,10 +485,6 @@ fn check_expr_kind<'a, 'tcx>(
483485
array_result
484486
}
485487

486-
hir::ExprKind::Type(ref expr, ref _ty) => {
487-
v.check_expr(&expr)
488-
}
489-
490488
hir::ExprKind::Tup(ref hirvec) => {
491489
let mut tup_result = Promotable;
492490
for index in hirvec.iter() {
@@ -495,7 +493,6 @@ fn check_expr_kind<'a, 'tcx>(
495493
tup_result
496494
}
497495

498-
499496
// Conditional control flow (possible to implement).
500497
hir::ExprKind::Match(ref expr, ref hirvec_arm, ref _match_source) => {
501498
// Compute the most demanding borrow from all the arms'

Diff for: src/librustc_typeck/check/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4321,6 +4321,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
43214321
self.check_expr_eq_type(&e, ty);
43224322
ty
43234323
}
4324+
ExprKind::Use(ref e) => {
4325+
self.check_expr_with_expectation(e, expected)
4326+
}
43244327
ExprKind::Array(ref args) => {
43254328
let uty = expected.to_option(self).and_then(|uty| {
43264329
match uty.sty {

0 commit comments

Comments
 (0)