Skip to content

Commit 992ceb6

Browse files
committed
Implement .use keyword as an alias of clone
1 parent 6a8eabb commit 992ceb6

File tree

37 files changed

+326
-32
lines changed

37 files changed

+326
-32
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,7 @@ impl Expr {
13501350
// Never need parens
13511351
ExprKind::Array(_)
13521352
| ExprKind::Await(..)
1353+
| ExprKind::Use(..)
13531354
| ExprKind::Block(..)
13541355
| ExprKind::Call(..)
13551356
| ExprKind::ConstBlock(_)
@@ -1530,6 +1531,8 @@ pub enum ExprKind {
15301531
Gen(CaptureBy, P<Block>, GenBlockKind, Span),
15311532
/// An await expression (`my_future.await`). Span is of await keyword.
15321533
Await(P<Expr>, Span),
1534+
/// A use expression (`x.use`). Span is of use keyword.
1535+
Use(P<Expr>, Span),
15331536

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

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,10 @@ pub fn walk_expr<T: MutVisitor>(vis: &mut T, Expr { kind, id, span, attrs, token
17471747
vis.visit_expr(expr);
17481748
vis.visit_span(await_kw_span);
17491749
}
1750+
ExprKind::Use(expr, use_kw_span) => {
1751+
vis.visit_expr(expr);
1752+
vis.visit_span(use_kw_span);
1753+
}
17501754
ExprKind::Assign(el, er, span) => {
17511755
vis.visit_expr(el);
17521756
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
@@ -1210,6 +1210,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
12101210
}
12111211
ExprKind::Gen(_capt, body, _kind, _decl_span) => try_visit!(visitor.visit_block(body)),
12121212
ExprKind::Await(expr, _span) => try_visit!(visitor.visit_expr(expr)),
1213+
ExprKind::Use(expr, _span) => try_visit!(visitor.visit_expr(expr)),
12131214
ExprKind::Assign(lhs, rhs, _span) => {
12141215
try_visit!(visitor.visit_expr(lhs));
12151216
try_visit!(visitor.visit_expr(rhs));

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
208208
},
209209
),
210210
ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
211+
ExprKind::Use(expr, use_kw_span) => self.lower_expr_use(*use_kw_span, expr),
211212
ExprKind::Closure(box Closure {
212213
binder,
213214
capture_clause,
@@ -1064,6 +1065,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
10641065
)
10651066
}
10661067

1068+
fn lower_expr_use(&mut self, use_kw_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
1069+
hir::ExprKind::Use(self.lower_expr(expr), use_kw_span)
1070+
}
1071+
10671072
fn lower_expr_closure(
10681073
&mut self,
10691074
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
@@ -575,6 +575,14 @@ impl<'a> State<'a> {
575575
);
576576
self.word(".await");
577577
}
578+
ast::ExprKind::Use(expr, _) => {
579+
self.print_expr_cond_paren(
580+
expr,
581+
expr.precedence() < ExprPrecedence::Unambiguous,
582+
fixup,
583+
);
584+
self.word(".use");
585+
}
578586
ast::ExprKind::Assign(lhs, rhs, _) => {
579587
self.print_expr_cond_paren(
580588
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
@@ -2071,6 +2071,7 @@ impl Expr<'_> {
20712071
| ExprKind::Tup(_)
20722072
| ExprKind::Type(..)
20732073
| ExprKind::UnsafeBinderCast(..)
2074+
| ExprKind::Use(..)
20742075
| ExprKind::Err(_) => ExprPrecedence::Unambiguous,
20752076

20762077
ExprKind::DropTemps(expr, ..) => expr.precedence(),
@@ -2117,6 +2118,7 @@ impl Expr<'_> {
21172118
ExprKind::Path(QPath::TypeRelative(..))
21182119
| ExprKind::Call(..)
21192120
| ExprKind::MethodCall(..)
2121+
| ExprKind::Use(..)
21202122
| ExprKind::Struct(..)
21212123
| ExprKind::Tup(..)
21222124
| ExprKind::If(..)
@@ -2190,7 +2192,9 @@ impl Expr<'_> {
21902192

21912193
pub fn can_have_side_effects(&self) -> bool {
21922194
match self.peel_drop_temps().kind {
2193-
ExprKind::Path(_) | ExprKind::Lit(_) | ExprKind::OffsetOf(..) => false,
2195+
ExprKind::Path(_) | ExprKind::Lit(_) | ExprKind::OffsetOf(..) | ExprKind::Use(..) => {
2196+
false
2197+
}
21942198
ExprKind::Type(base, _)
21952199
| ExprKind::Unary(_, base)
21962200
| ExprKind::Field(base, _)
@@ -2452,6 +2456,8 @@ pub enum ExprKind<'hir> {
24522456
///
24532457
/// [`type_dependent_def_id`]: ../../rustc_middle/ty/struct.TypeckResults.html#method.type_dependent_def_id
24542458
MethodCall(&'hir PathSegment<'hir>, &'hir Expr<'hir>, &'hir [Expr<'hir>], Span),
2459+
/// An use expression (e.g., `var.use`).
2460+
Use(&'hir Expr<'hir>, Span),
24552461
/// A tuple (e.g., `(a, b, c, d)`).
24562462
Tup(&'hir [Expr<'hir>]),
24572463
/// 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
@@ -817,6 +817,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
817817
try_visit!(visitor.visit_expr(receiver));
818818
walk_list!(visitor, visit_expr, arguments);
819819
}
820+
ExprKind::Use(expr, _) => {
821+
try_visit!(visitor.visit_expr(expr));
822+
}
820823
ExprKind::Binary(_, ref left_expression, ref right_expression) => {
821824
try_visit!(visitor.visit_expr(left_expression));
822825
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
@@ -1485,6 +1485,10 @@ impl<'a> State<'a> {
14851485
hir::ExprKind::MethodCall(segment, receiver, args, _) => {
14861486
self.print_expr_method_call(segment, receiver, args);
14871487
}
1488+
hir::ExprKind::Use(expr, _) => {
1489+
self.print_expr(expr);
1490+
self.word(".use");
1491+
}
14881492
hir::ExprKind::Binary(op, lhs, rhs) => {
14891493
self.print_expr_binary(op, lhs, rhs);
14901494
}

0 commit comments

Comments
 (0)