Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 05c5164

Browse files
committed
Implement .use keyword as an alias of clone
1 parent 0cf8dbc commit 05c5164

File tree

36 files changed

+247
-24
lines changed

36 files changed

+247
-24
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,7 @@ impl Expr {
13991399
// Never need parens
14001400
ExprKind::Array(_)
14011401
| ExprKind::Await(..)
1402+
| ExprKind::Use(..)
14021403
| ExprKind::Block(..)
14031404
| ExprKind::Call(..)
14041405
| ExprKind::ConstBlock(_)
@@ -1588,6 +1589,8 @@ pub enum ExprKind {
15881589
Gen(CaptureBy, P<Block>, GenBlockKind, Span),
15891590
/// An await expression (`my_future.await`). Span is of await keyword.
15901591
Await(P<Expr>, Span),
1592+
/// A use expression (`x.use`). Span is of use keyword.
1593+
Use(P<Expr>, Span),
15911594

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

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,10 @@ pub fn walk_expr<T: MutVisitor>(vis: &mut T, Expr { kind, id, span, attrs, token
17451745
vis.visit_expr(expr);
17461746
vis.visit_span(await_kw_span);
17471747
}
1748+
ExprKind::Use(expr, use_kw_span) => {
1749+
vis.visit_expr(expr);
1750+
vis.visit_span(use_kw_span);
1751+
}
17481752
ExprKind::Assign(el, er, span) => {
17491753
vis.visit_expr(el);
17501754
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
@@ -1211,6 +1211,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
12111211
}
12121212
ExprKind::Gen(_capt, body, _kind, _decl_span) => try_visit!(visitor.visit_block(body)),
12131213
ExprKind::Await(expr, _span) => try_visit!(visitor.visit_expr(expr)),
1214+
ExprKind::Use(expr, _span) => try_visit!(visitor.visit_expr(expr)),
12141215
ExprKind::Assign(lhs, rhs, _span) => {
12151216
try_visit!(visitor.visit_expr(lhs));
12161217
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
@@ -207,6 +207,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
207207
},
208208
),
209209
ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
210+
ExprKind::Use(expr, use_kw_span) => self.lower_expr_use(*use_kw_span, expr),
210211
ExprKind::Closure(box Closure {
211212
binder,
212213
capture_clause,
@@ -1067,6 +1068,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
10671068
)
10681069
}
10691070

1071+
fn lower_expr_use(&mut self, use_kw_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
1072+
hir::ExprKind::Use(self.lower_expr(expr), use_kw_span)
1073+
}
1074+
10701075
fn lower_expr_closure(
10711076
&mut self,
10721077
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
@@ -574,6 +574,14 @@ impl<'a> State<'a> {
574574
);
575575
self.word(".await");
576576
}
577+
ast::ExprKind::Use(expr, _) => {
578+
self.print_expr_cond_paren(
579+
expr,
580+
expr.precedence() < ExprPrecedence::Unambiguous,
581+
fixup,
582+
);
583+
self.word(".use");
584+
}
577585
ast::ExprKind::Assign(lhs, rhs, _) => {
578586
self.print_expr_cond_paren(
579587
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
@@ -2166,6 +2166,7 @@ impl Expr<'_> {
21662166
| ExprKind::Tup(_)
21672167
| ExprKind::Type(..)
21682168
| ExprKind::UnsafeBinderCast(..)
2169+
| ExprKind::Use(..)
21692170
| ExprKind::Err(_) => ExprPrecedence::Unambiguous,
21702171

21712172
ExprKind::DropTemps(expr, ..) => expr.precedence(),
@@ -2212,6 +2213,7 @@ impl Expr<'_> {
22122213
ExprKind::Path(QPath::TypeRelative(..))
22132214
| ExprKind::Call(..)
22142215
| ExprKind::MethodCall(..)
2216+
| ExprKind::Use(..)
22152217
| ExprKind::Struct(..)
22162218
| ExprKind::Tup(..)
22172219
| ExprKind::If(..)
@@ -2285,7 +2287,9 @@ impl Expr<'_> {
22852287

22862288
pub fn can_have_side_effects(&self) -> bool {
22872289
match self.peel_drop_temps().kind {
2288-
ExprKind::Path(_) | ExprKind::Lit(_) | ExprKind::OffsetOf(..) => false,
2290+
ExprKind::Path(_) | ExprKind::Lit(_) | ExprKind::OffsetOf(..) | ExprKind::Use(..) => {
2291+
false
2292+
}
22892293
ExprKind::Type(base, _)
22902294
| ExprKind::Unary(_, base)
22912295
| ExprKind::Field(base, _)
@@ -2547,6 +2551,8 @@ pub enum ExprKind<'hir> {
25472551
///
25482552
/// [`type_dependent_def_id`]: ../../rustc_middle/ty/struct.TypeckResults.html#method.type_dependent_def_id
25492553
MethodCall(&'hir PathSegment<'hir>, &'hir Expr<'hir>, &'hir [Expr<'hir>], Span),
2554+
/// An use expression (e.g., `var.use`).
2555+
Use(&'hir Expr<'hir>, Span),
25502556
/// A tuple (e.g., `(a, b, c, d)`).
25512557
Tup(&'hir [Expr<'hir>]),
25522558
/// 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
@@ -821,6 +821,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
821821
try_visit!(visitor.visit_expr(receiver));
822822
walk_list!(visitor, visit_expr, arguments);
823823
}
824+
ExprKind::Use(expr, _) => {
825+
try_visit!(visitor.visit_expr(expr));
826+
}
824827
ExprKind::Binary(_, ref left_expression, ref right_expression) => {
825828
try_visit!(visitor.visit_expr(left_expression));
826829
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
@@ -1544,6 +1544,10 @@ impl<'a> State<'a> {
15441544
hir::ExprKind::MethodCall(segment, receiver, args, _) => {
15451545
self.print_expr_method_call(segment, receiver, args);
15461546
}
1547+
hir::ExprKind::Use(expr, _) => {
1548+
self.print_expr(expr);
1549+
self.word(".use");
1550+
}
15471551
hir::ExprKind::Binary(op, lhs, rhs) => {
15481552
self.print_expr_binary(op, lhs, rhs);
15491553
}

0 commit comments

Comments
 (0)