Skip to content

Commit 801e8e4

Browse files
committed
Auto merge of rust-lang#134248 - oli-obk:patkind-path-removal, r=<try>
Merge `PatKind::Path` into `PatKind::Lit` Follow-up to rust-lang#134228 We always had a duplication where `Path`s could be represented as `PatKind::Path` or `PatKind::Lit(ExprKind::Path)`. We had to handle both everywhere, and still do after rust-lang#134228, so I'm removing it now. subsequently we can also nuke `visit_pattern_type_pattern`
2 parents fb546ee + 2269e1f commit 801e8e4

File tree

78 files changed

+717
-432
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+717
-432
lines changed

compiler/rustc_ast/src/ast.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl Pat {
623623
PatKind::Wild
624624
| PatKind::Rest
625625
| PatKind::Never
626-
| PatKind::Lit(_)
626+
| PatKind::Expr(_)
627627
| PatKind::Range(..)
628628
| PatKind::Ident(..)
629629
| PatKind::Path(..)
@@ -801,8 +801,8 @@ pub enum PatKind {
801801
/// A reference pattern (e.g., `&mut (a, b)`).
802802
Ref(P<Pat>, Mutability),
803803

804-
/// A literal.
805-
Lit(P<Expr>),
804+
/// A literal, const block or path.
805+
Expr(P<Expr>),
806806

807807
/// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
808808
Range(Option<P<Expr>>, Option<P<Expr>>, Spanned<RangeEnd>),

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ pub fn walk_pat<T: MutVisitor>(vis: &mut T, pat: &mut P<Pat>) {
15121512
vis.visit_ident(ident);
15131513
visit_opt(sub, |sub| vis.visit_pat(sub));
15141514
}
1515-
PatKind::Lit(e) => vis.visit_expr(e),
1515+
PatKind::Expr(e) => vis.visit_expr(e),
15161516
PatKind::TupleStruct(qself, path, elems) => {
15171517
vis.visit_qself(qself);
15181518
vis.visit_path(path);

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) -> V::Res
680680
try_visit!(visitor.visit_ident(ident));
681681
visit_opt!(visitor, visit_pat, optional_subpattern);
682682
}
683-
PatKind::Lit(expression) => try_visit!(visitor.visit_expr(expression)),
683+
PatKind::Expr(expression) => try_visit!(visitor.visit_expr(expression)),
684684
PatKind::Range(lower_bound, upper_bound, _end) => {
685685
visit_opt!(visitor, visit_expr, lower_bound);
686686
visit_opt!(visitor, visit_expr, upper_bound);

compiler/rustc_ast_lowering/src/expr.rs

+33-24
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
102102

103103
let kind = match &e.kind {
104104
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
105-
ExprKind::ConstBlock(c) => {
106-
let c = self.with_new_scopes(c.value.span, |this| {
107-
let def_id = this.local_def_id(c.id);
108-
hir::ConstBlock {
109-
def_id,
110-
hir_id: this.lower_node_id(c.id),
111-
body: this.lower_const_body(c.value.span, Some(&c.value)),
112-
}
113-
});
114-
hir::ExprKind::ConstBlock(c)
115-
}
105+
ExprKind::ConstBlock(c) => hir::ExprKind::ConstBlock(self.lower_const_block(c)),
116106
ExprKind::Repeat(expr, count) => {
117107
let expr = self.lower_expr(expr);
118108
let count = self.lower_array_length_to_const_arg(count);
@@ -153,18 +143,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
153143
let ohs = self.lower_expr(ohs);
154144
hir::ExprKind::Unary(op, ohs)
155145
}
156-
ExprKind::Lit(token_lit) => {
157-
let lit_kind = match LitKind::from_token_lit(*token_lit) {
158-
Ok(lit_kind) => lit_kind,
159-
Err(err) => {
160-
let guar =
161-
report_lit_error(&self.tcx.sess.psess, err, *token_lit, e.span);
162-
LitKind::Err(guar)
163-
}
164-
};
165-
let lit = self.arena.alloc(respan(self.lower_span(e.span), lit_kind));
166-
hir::ExprKind::Lit(lit)
167-
}
146+
ExprKind::Lit(token_lit) => hir::ExprKind::Lit(self.lower_lit(token_lit, e.span)),
168147
ExprKind::IncludedBytes(bytes) => {
169148
let lit = self.arena.alloc(respan(
170149
self.lower_span(e.span),
@@ -403,6 +382,32 @@ impl<'hir> LoweringContext<'_, 'hir> {
403382
})
404383
}
405384

385+
pub(crate) fn lower_const_block(&mut self, c: &AnonConst) -> hir::ConstBlock {
386+
self.with_new_scopes(c.value.span, |this| {
387+
let def_id = this.local_def_id(c.id);
388+
hir::ConstBlock {
389+
def_id,
390+
hir_id: this.lower_node_id(c.id),
391+
body: this.lower_const_body(c.value.span, Some(&c.value)),
392+
}
393+
})
394+
}
395+
396+
pub(crate) fn lower_lit(
397+
&mut self,
398+
token_lit: &token::Lit,
399+
span: Span,
400+
) -> &'hir Spanned<LitKind> {
401+
let lit_kind = match LitKind::from_token_lit(*token_lit) {
402+
Ok(lit_kind) => lit_kind,
403+
Err(err) => {
404+
let guar = report_lit_error(&self.tcx.sess.psess, err, *token_lit, span);
405+
LitKind::Err(guar)
406+
}
407+
};
408+
self.arena.alloc(respan(self.lower_span(span), lit_kind))
409+
}
410+
406411
fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
407412
match u {
408413
UnOp::Deref => hir::UnOp::Deref,
@@ -1386,7 +1391,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
13861391
None,
13871392
);
13881393
// Destructure like a unit struct.
1389-
let unit_struct_pat = hir::PatKind::Path(qpath);
1394+
let unit_struct_pat = hir::PatKind::Expr(self.arena.alloc(hir::PatExpr {
1395+
hir_id: self.lower_node_id(lhs.id),
1396+
span: lhs.span,
1397+
kind: hir::PatExprKind::Path(qpath),
1398+
}));
13901399
return self.pat_without_dbm(lhs.span, unit_struct_pat);
13911400
}
13921401
}

compiler/rustc_ast_lowering/src/index.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
209209
});
210210
}
211211

212+
fn visit_pat_expr(&mut self, expr: &'hir PatExpr<'hir>) {
213+
self.insert(expr.span, expr.hir_id, Node::PatExpr(expr));
214+
215+
self.with_parent(expr.hir_id, |this| {
216+
intravisit::walk_pat_expr(this, expr);
217+
});
218+
}
219+
212220
fn visit_pat_field(&mut self, field: &'hir PatField<'hir>) {
213221
self.insert(field.span, field.hir_id, Node::PatField(field));
214222
self.with_parent(field.hir_id, |this| {
@@ -385,10 +393,6 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
385393
});
386394
}
387395

388-
fn visit_pattern_type_pattern(&mut self, p: &'hir hir::Pat<'hir>) {
389-
self.visit_pat(p)
390-
}
391-
392396
fn visit_precise_capturing_arg(
393397
&mut self,
394398
arg: &'hir PreciseCapturingArg<'hir>,

compiler/rustc_ast_lowering/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#![doc(rust_logo)]
3636
#![feature(assert_matches)]
3737
#![feature(box_patterns)]
38+
#![feature(if_let_guard)]
3839
#![feature(let_chains)]
3940
#![feature(rustdoc_internals)]
4041
#![warn(unreachable_pub)]

compiler/rustc_ast_lowering/src/pat.rs

+62-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use std::sync::Arc;
2+
13
use rustc_ast::ptr::P;
24
use rustc_ast::*;
35
use rustc_data_structures::stack::ensure_sufficient_stack;
46
use rustc_hir as hir;
57
use rustc_hir::def::Res;
6-
use rustc_span::source_map::Spanned;
8+
use rustc_middle::span_bug;
9+
use rustc_span::source_map::{Spanned, respan};
710
use rustc_span::{Ident, Span};
811

912
use super::errors::{
@@ -35,8 +38,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
3538
lower_sub,
3639
);
3740
}
38-
PatKind::Lit(e) => {
39-
break hir::PatKind::Lit(self.lower_expr_within_pat(e, false));
41+
PatKind::Expr(e) => {
42+
break hir::PatKind::Expr(self.lower_expr_within_pat(e, false));
4043
}
4144
PatKind::TupleStruct(qself, path, pats) => {
4245
let qpath = self.lower_qpath(
@@ -66,7 +69,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
6669
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
6770
None,
6871
);
69-
break hir::PatKind::Path(qpath);
72+
let kind = hir::PatExprKind::Path(qpath);
73+
let expr = hir::PatExpr { hir_id: pat_hir_id, span: pattern.span, kind };
74+
let expr = self.arena.alloc(expr);
75+
return hir::Pat {
76+
hir_id: self.next_id(),
77+
kind: hir::PatKind::Expr(expr),
78+
span: pattern.span,
79+
default_binding_modes: true,
80+
};
7081
}
7182
PatKind::Struct(qself, path, fields, etc) => {
7283
let qpath = self.lower_qpath(
@@ -302,14 +313,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
302313
Some(res) => {
303314
let hir_id = self.next_id();
304315
let res = self.lower_res(res);
305-
hir::PatKind::Path(hir::QPath::Resolved(
316+
let kind = hir::PatExprKind::Path(hir::QPath::Resolved(
306317
None,
307318
self.arena.alloc(hir::Path {
308319
span: self.lower_span(ident.span),
309320
res,
310321
segments: arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
311322
}),
312-
))
323+
));
324+
let lit = hir::PatExpr { kind, hir_id: self.next_id(), span: ident.span };
325+
let lit = self.arena.alloc(lit);
326+
hir::PatKind::Expr(lit)
313327
}
314328
}
315329
}
@@ -366,24 +380,54 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
366380
// }
367381
// m!(S);
368382
// ```
369-
fn lower_expr_within_pat(&mut self, expr: &Expr, allow_paths: bool) -> &'hir hir::Expr<'hir> {
370-
match &expr.kind {
371-
ExprKind::Lit(..)
372-
| ExprKind::ConstBlock(..)
373-
| ExprKind::IncludedBytes(..)
374-
| ExprKind::Err(_)
375-
| ExprKind::Dummy => {}
376-
ExprKind::Path(..) if allow_paths => {}
377-
ExprKind::Unary(UnOp::Neg, inner) if matches!(inner.kind, ExprKind::Lit(_)) => {}
383+
fn lower_expr_within_pat(
384+
&mut self,
385+
expr: &Expr,
386+
allow_paths: bool,
387+
) -> &'hir hir::PatExpr<'hir> {
388+
let err = |guar| hir::PatExprKind::Lit {
389+
lit: self.arena.alloc(respan(self.lower_span(expr.span), LitKind::Err(guar))),
390+
negated: false,
391+
};
392+
let kind = match &expr.kind {
393+
ExprKind::Lit(lit) => {
394+
hir::PatExprKind::Lit { lit: self.lower_lit(lit, expr.span), negated: false }
395+
}
396+
ExprKind::ConstBlock(c) => hir::PatExprKind::ConstBlock(self.lower_const_block(c)),
397+
ExprKind::IncludedBytes(bytes) => hir::PatExprKind::Lit {
398+
lit: self.arena.alloc(respan(
399+
self.lower_span(expr.span),
400+
LitKind::ByteStr(Arc::clone(bytes), StrStyle::Cooked),
401+
)),
402+
negated: false,
403+
},
404+
ExprKind::Err(guar) => err(*guar),
405+
ExprKind::Dummy => span_bug!(expr.span, "lowered ExprKind::Dummy"),
406+
ExprKind::Path(qself, path) if allow_paths => hir::PatExprKind::Path(self.lower_qpath(
407+
expr.id,
408+
qself,
409+
path,
410+
ParamMode::Optional,
411+
AllowReturnTypeNotation::No,
412+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
413+
None,
414+
)),
415+
ExprKind::Unary(UnOp::Neg, inner) if let ExprKind::Lit(lit) = &inner.kind => {
416+
hir::PatExprKind::Lit { lit: self.lower_lit(lit, expr.span), negated: true }
417+
}
378418
_ => {
379419
let pattern_from_macro = expr.is_approximately_pattern();
380420
let guar = self.dcx().emit_err(ArbitraryExpressionInPattern {
381421
span: expr.span,
382422
pattern_from_macro_note: pattern_from_macro,
383423
});
384-
return self.arena.alloc(self.expr_err(expr.span, guar));
424+
err(guar)
385425
}
386-
}
387-
self.lower_expr(expr)
426+
};
427+
self.arena.alloc(hir::PatExpr {
428+
hir_id: self.lower_node_id(expr.id),
429+
span: expr.span,
430+
kind,
431+
})
388432
}
389433
}

compiler/rustc_ast_pretty/src/pprust/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ impl<'a> State<'a> {
17011701
self.print_pat(inner);
17021702
}
17031703
}
1704-
PatKind::Lit(e) => self.print_expr(e, FixupContext::default()),
1704+
PatKind::Expr(e) => self.print_expr(e, FixupContext::default()),
17051705
PatKind::Range(begin, end, Spanned { node: end_kind, .. }) => {
17061706
if let Some(e) = begin {
17071707
self.print_expr(e, FixupContext::default());

compiler/rustc_expand/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl MacResult for MacEager {
522522
return Some(P(ast::Pat {
523523
id: ast::DUMMY_NODE_ID,
524524
span: e.span,
525-
kind: PatKind::Lit(e),
525+
kind: PatKind::Expr(e),
526526
tokens: None,
527527
}));
528528
}

compiler/rustc_expand/src/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ impl<'a> ExtCtxt<'a> {
486486
self.pat(span, PatKind::Wild)
487487
}
488488
pub fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
489-
self.pat(span, PatKind::Lit(expr))
489+
self.pat(span, PatKind::Expr(expr))
490490
}
491491
pub fn pat_ident(&self, span: Span, ident: Ident) -> P<ast::Pat> {
492492
self.pat_ident_binding_mode(span, ident, ast::BindingMode::NONE)

0 commit comments

Comments
 (0)