Skip to content

Commit 9fd5694

Browse files
committed
Implement .use keyword as an alias of clone
1 parent 91fcfa1 commit 9fd5694

File tree

38 files changed

+331
-33
lines changed

38 files changed

+331
-33
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,7 @@ impl Expr {
13481348
// Never need parens
13491349
ExprKind::Array(_)
13501350
| ExprKind::Await(..)
1351+
| ExprKind::Use(..)
13511352
| ExprKind::Block(..)
13521353
| ExprKind::Call(..)
13531354
| ExprKind::ConstBlock(_)
@@ -1528,6 +1529,8 @@ pub enum ExprKind {
15281529
Gen(CaptureBy, P<Block>, GenBlockKind, Span),
15291530
/// An await expression (`my_future.await`). Span is of await keyword.
15301531
Await(P<Expr>, Span),
1532+
/// A use expression (`x.use`). Span is of use keyword.
1533+
Use(P<Expr>, Span),
15311534

15321535
/// A try block (`try { ... }`).
15331536
TryBlock(P<Block>),

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,10 @@ pub fn walk_expr<T: MutVisitor>(vis: &mut T, Expr { kind, id, span, attrs, token
17131713
vis.visit_expr(expr);
17141714
vis.visit_span(await_kw_span);
17151715
}
1716+
ExprKind::Use(expr, use_kw_span) => {
1717+
vis.visit_expr(expr);
1718+
vis.visit_span(use_kw_span);
1719+
}
17161720
ExprKind::Assign(el, er, span) => {
17171721
vis.visit_expr(el);
17181722
vis.visit_expr(er);

compiler/rustc_ast/src/util/classify.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub fn leading_labeled_expr(mut expr: &ast::Expr) -> bool {
108108
Assign(e, _, _)
109109
| AssignOp(_, e, _)
110110
| Await(e, _)
111+
| Use(e, _)
111112
| Binary(_, e, _)
112113
| Call(e, _)
113114
| Cast(e, _)
@@ -224,6 +225,7 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
224225
| Lit(_)
225226
| Type(_, _)
226227
| Await(_, _)
228+
| Use(_, _)
227229
| Field(_, _)
228230
| Index(_, _, _)
229231
| Underscore

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
11751175
}
11761176
ExprKind::Gen(_capt, body, _kind, _decl_span) => try_visit!(visitor.visit_block(body)),
11771177
ExprKind::Await(expr, _span) => try_visit!(visitor.visit_expr(expr)),
1178+
ExprKind::Use(expr, _span) => try_visit!(visitor.visit_expr(expr)),
11781179
ExprKind::Assign(lhs, rhs, _span) => {
11791180
try_visit!(visitor.visit_expr(lhs));
11801181
try_visit!(visitor.visit_expr(rhs));

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
229229
},
230230
),
231231
ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
232+
ExprKind::Use(expr, use_kw_span) => self.lower_expr_use(*use_kw_span, expr),
232233
ExprKind::Closure(box Closure {
233234
binder,
234235
capture_clause,
@@ -1033,6 +1034,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
10331034
)
10341035
}
10351036

1037+
/// Desugar `<expr>.use` into:
1038+
/// ```ignore (pseudo-rust)
1039+
/// <expr>.clone()
1040+
/// ```
1041+
fn lower_expr_use(&mut self, use_kw_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
1042+
let expr = self.lower_expr(expr);
1043+
let span = self.mark_span_with_reason(DesugaringKind::Use, use_kw_span, None);
1044+
1045+
hir::ExprKind::Use(expr, span)
1046+
}
1047+
10361048
fn lower_expr_closure(
10371049
&mut self,
10381050
binder: &ClosureBinder,

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,14 @@ impl<'a> State<'a> {
571571
);
572572
self.word(".await");
573573
}
574+
ast::ExprKind::Use(expr, _) => {
575+
self.print_expr_cond_paren(
576+
expr,
577+
expr.precedence() < ExprPrecedence::Unambiguous,
578+
fixup,
579+
);
580+
self.word(".use");
581+
}
574582
ast::ExprKind::Assign(lhs, rhs, _) => {
575583
self.print_expr_cond_paren(
576584
lhs,

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
297297
| ExprKind::AssignOp(_, _, _)
298298
| ExprKind::Gen(_, _, _, _)
299299
| ExprKind::Await(_, _)
300+
| ExprKind::Use(_, _)
300301
| ExprKind::Block(_, _)
301302
| ExprKind::Break(_, _)
302303
| ExprKind::Closure(_)

compiler/rustc_hir/src/hir.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ impl Expr<'_> {
19931993
| ExprKind::Tup(_)
19941994
| ExprKind::Type(..)
19951995
| ExprKind::UnsafeBinderCast(..)
1996+
| ExprKind::Use(..)
19961997
| ExprKind::Err(_) => ExprPrecedence::Unambiguous,
19971998

19981999
ExprKind::DropTemps(expr, ..) => expr.precedence(),
@@ -2039,6 +2040,7 @@ impl Expr<'_> {
20392040
ExprKind::Path(QPath::TypeRelative(..))
20402041
| ExprKind::Call(..)
20412042
| ExprKind::MethodCall(..)
2043+
| ExprKind::Use(..)
20422044
| ExprKind::Struct(..)
20432045
| ExprKind::Tup(..)
20442046
| ExprKind::If(..)
@@ -2100,7 +2102,9 @@ impl Expr<'_> {
21002102

21012103
pub fn can_have_side_effects(&self) -> bool {
21022104
match self.peel_drop_temps().kind {
2103-
ExprKind::Path(_) | ExprKind::Lit(_) | ExprKind::OffsetOf(..) => false,
2105+
ExprKind::Path(_) | ExprKind::Lit(_) | ExprKind::OffsetOf(..) | ExprKind::Use(..) => {
2106+
false
2107+
}
21042108
ExprKind::Type(base, _)
21052109
| ExprKind::Unary(_, base)
21062110
| ExprKind::Field(base, _)
@@ -2323,6 +2327,8 @@ pub enum ExprKind<'hir> {
23232327
///
23242328
/// [`type_dependent_def_id`]: ../../rustc_middle/ty/struct.TypeckResults.html#method.type_dependent_def_id
23252329
MethodCall(&'hir PathSegment<'hir>, &'hir Expr<'hir>, &'hir [Expr<'hir>], Span),
2330+
/// An use expression (e.g., `var.use`).
2331+
Use(&'hir Expr<'hir>, Span),
23262332
/// A tuple (e.g., `(a, b, c, d)`).
23272333
Tup(&'hir [Expr<'hir>]),
23282334
/// A binary operation (e.g., `a + b`, `a * b`).

compiler/rustc_hir/src/intravisit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
764764
try_visit!(visitor.visit_expr(receiver));
765765
walk_list!(visitor, visit_expr, arguments);
766766
}
767+
ExprKind::Use(expr, _) => {
768+
try_visit!(visitor.visit_expr(expr));
769+
}
767770
ExprKind::Binary(_, ref left_expression, ref right_expression) => {
768771
try_visit!(visitor.visit_expr(left_expression));
769772
try_visit!(visitor.visit_expr(right_expression));

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,10 @@ impl<'a> State<'a> {
14631463
hir::ExprKind::MethodCall(segment, receiver, args, _) => {
14641464
self.print_expr_method_call(segment, receiver, args);
14651465
}
1466+
hir::ExprKind::Use(expr, _) => {
1467+
self.print_expr(expr);
1468+
self.word(".use");
1469+
}
14661470
hir::ExprKind::Binary(op, lhs, rhs) => {
14671471
self.print_expr_binary(op, lhs, rhs);
14681472
}

0 commit comments

Comments
 (0)