Skip to content

Commit 295b70f

Browse files
committed
Exhaustively handle expressions in patterns
1 parent 3ce731c commit 295b70f

File tree

38 files changed

+443
-201
lines changed

38 files changed

+443
-201
lines changed

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+28-23
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,

Diff for: compiler/rustc_ast_lowering/src/index.rs

+8
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| {

Diff for: 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)]

Diff for: compiler/rustc_ast_lowering/src/pat.rs

+38-13
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::{
@@ -366,24 +369,46 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
366369
// }
367370
// m!(S);
368371
// ```
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(_)) => {}
372+
fn lower_expr_within_pat(&mut self, expr: &Expr, allow_paths: bool) -> &'hir hir::PatExpr<'hir> {
373+
let err = |guar| hir::PatExprKind::Lit {
374+
lit: self.arena.alloc(respan(self.lower_span(expr.span), LitKind::Err(guar))),
375+
negated: false,
376+
};
377+
let kind = match &expr.kind {
378+
ExprKind::Lit(lit) => {
379+
hir::PatExprKind::Lit { lit: self.lower_lit(lit, expr.span), negated: false }
380+
}
381+
ExprKind::ConstBlock(c) => hir::PatExprKind::ConstBlock(self.lower_const_block(c)),
382+
ExprKind::IncludedBytes(bytes) => hir::PatExprKind::Lit {
383+
lit: self.arena.alloc(respan(
384+
self.lower_span(expr.span),
385+
LitKind::ByteStr(Arc::clone(bytes), StrStyle::Cooked),
386+
)),
387+
negated: false,
388+
},
389+
ExprKind::Err(guar) => err(*guar),
390+
ExprKind::Dummy => span_bug!(expr.span, "lowered ExprKind::Dummy"),
391+
ExprKind::Path(qself, path) if allow_paths => hir::PatExprKind::Path(self.lower_qpath(
392+
expr.id,
393+
qself,
394+
path,
395+
ParamMode::Optional,
396+
AllowReturnTypeNotation::No,
397+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
398+
None,
399+
)),
400+
ExprKind::Unary(UnOp::Neg, inner) if let ExprKind::Lit(lit) = &inner.kind => {
401+
hir::PatExprKind::Lit { lit: self.lower_lit(lit, expr.span), negated: true }
402+
}
378403
_ => {
379404
let pattern_from_macro = expr.is_approximately_pattern();
380405
let guar = self.dcx().emit_err(ArbitraryExpressionInPattern {
381406
span: expr.span,
382407
pattern_from_macro_note: pattern_from_macro,
383408
});
384-
return self.arena.alloc(self.expr_err(expr.span, guar));
409+
err(guar)
385410
}
386-
}
387-
self.lower_expr(expr)
411+
};
412+
self.arena.alloc(hir::PatExpr { hir_id: self.lower_node_id(expr.id), span: expr.span, kind })
388413
}
389414
}

Diff for: compiler/rustc_hir/src/hir.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,26 @@ impl fmt::Debug for DotDotPos {
15191519
}
15201520
}
15211521

1522+
#[derive(Debug, Clone, Copy, HashStable_Generic)]
1523+
pub struct PatExpr<'hir> {
1524+
pub hir_id: HirId,
1525+
pub span: Span,
1526+
pub kind: PatExprKind<'hir>,
1527+
}
1528+
1529+
#[derive(Debug, Clone, Copy, HashStable_Generic)]
1530+
pub enum PatExprKind<'hir> {
1531+
Lit {
1532+
lit: &'hir Lit,
1533+
// FIXME: move this into `Lit` and handle negated literal expressions
1534+
// once instead of matching on unop neg expressions everywhere.
1535+
negated: bool,
1536+
},
1537+
ConstBlock(ConstBlock),
1538+
/// A path pattern for a unit struct/variant or a (maybe-associated) constant.
1539+
Path(QPath<'hir>),
1540+
}
1541+
15221542
#[derive(Debug, Clone, Copy, HashStable_Generic)]
15231543
pub enum PatKind<'hir> {
15241544
/// Represents a wildcard pattern (i.e., `_`).
@@ -1564,10 +1584,10 @@ pub enum PatKind<'hir> {
15641584
Ref(&'hir Pat<'hir>, Mutability),
15651585

15661586
/// A literal.
1567-
Lit(&'hir Expr<'hir>),
1587+
Lit(&'hir PatExpr<'hir>),
15681588

15691589
/// A range pattern (e.g., `1..=2` or `1..2`).
1570-
Range(Option<&'hir Expr<'hir>>, Option<&'hir Expr<'hir>>, RangeEnd),
1590+
Range(Option<&'hir PatExpr<'hir>>, Option<&'hir PatExpr<'hir>>, RangeEnd),
15711591

15721592
/// A slice pattern, `[before_0, ..., before_n, (slice, after_0, ..., after_n)?]`.
15731593
///
@@ -4141,6 +4161,10 @@ pub enum Node<'hir> {
41414161
OpaqueTy(&'hir OpaqueTy<'hir>),
41424162
Pat(&'hir Pat<'hir>),
41434163
PatField(&'hir PatField<'hir>),
4164+
/// Needed as its own node with its own HirId for tracking
4165+
/// the unadjusted type of literals within patterns
4166+
/// (e.g. byte str literals not being of slice type).
4167+
PatExpr(&'hir PatExpr<'hir>),
41444168
Arm(&'hir Arm<'hir>),
41454169
Block(&'hir Block<'hir>),
41464170
LetStmt(&'hir LetStmt<'hir>),
@@ -4197,6 +4221,7 @@ impl<'hir> Node<'hir> {
41974221
| Node::Block(..)
41984222
| Node::Ctor(..)
41994223
| Node::Pat(..)
4224+
| Node::PatExpr(..)
42004225
| Node::Arm(..)
42014226
| Node::LetStmt(..)
42024227
| Node::Crate(..)

Diff for: compiler/rustc_hir/src/intravisit.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ pub trait Visitor<'v>: Sized {
342342
fn visit_pat_field(&mut self, f: &'v PatField<'v>) -> Self::Result {
343343
walk_pat_field(self, f)
344344
}
345+
fn visit_pat_expr(&mut self, lit: &'v PatExpr<'v>) -> Self::Result {
346+
walk_pat_expr(self, lit)
347+
}
345348
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
346349
walk_anon_const(self, c)
347350
}
@@ -685,10 +688,10 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) -> V:
685688
try_visit!(visitor.visit_ident(ident));
686689
visit_opt!(visitor, visit_pat, optional_subpattern);
687690
}
688-
PatKind::Lit(ref expression) => try_visit!(visitor.visit_expr(expression)),
691+
PatKind::Lit(lit) => try_visit!(visitor.visit_pat_expr(lit)),
689692
PatKind::Range(ref lower_bound, ref upper_bound, _) => {
690-
visit_opt!(visitor, visit_expr, lower_bound);
691-
visit_opt!(visitor, visit_expr, upper_bound);
693+
visit_opt!(visitor, visit_pat_expr, lower_bound);
694+
visit_opt!(visitor, visit_pat_expr, upper_bound);
692695
}
693696
PatKind::Never | PatKind::Wild | PatKind::Err(_) => (),
694697
PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
@@ -706,6 +709,15 @@ pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'
706709
visitor.visit_pat(field.pat)
707710
}
708711

712+
pub fn walk_pat_expr<'v, V: Visitor<'v>>(visitor: &mut V, lit: &'v PatExpr<'v>) -> V::Result {
713+
try_visit!(visitor.visit_id(lit.hir_id));
714+
match &lit.kind {
715+
PatExprKind::Lit { .. } => V::Result::output(),
716+
PatExprKind::ConstBlock(c) => visitor.visit_inline_const(c),
717+
PatExprKind::Path(qpath) => visitor.visit_qpath(qpath, lit.hir_id, lit.span),
718+
}
719+
}
720+
709721
pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) -> V::Result {
710722
try_visit!(visitor.visit_id(constant.hir_id));
711723
visitor.visit_nested_body(constant.body)

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -2449,17 +2449,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24492449
Ty::new_error(tcx, err)
24502450
}
24512451
hir::PatKind::Range(start, end, include_end) => {
2452-
let expr_to_const = |expr: &'tcx hir::Expr<'tcx>| -> ty::Const<'tcx> {
2453-
let (expr, neg) = match expr.kind {
2454-
hir::ExprKind::Unary(hir::UnOp::Neg, negated) => {
2455-
(negated, Some((expr.hir_id, expr.span)))
2456-
}
2457-
_ => (expr, None),
2458-
};
2459-
let (c, c_ty) = match &expr.kind {
2460-
hir::ExprKind::Lit(lit) => {
2452+
let expr_to_const = |expr: &'tcx hir::PatExpr<'tcx>| -> ty::Const<'tcx> {
2453+
let (c, c_ty) = match expr.kind {
2454+
hir::PatExprKind::Lit { lit, negated } => {
24612455
let lit_input =
2462-
LitToConstInput { lit: &lit.node, ty, neg: neg.is_some() };
2456+
LitToConstInput { lit: &lit.node, ty, neg: negated };
24632457
let ct = match tcx.lit_to_const(lit_input) {
24642458
Ok(c) => c,
24652459
Err(LitToConstError::Reported(err)) => {
@@ -2470,23 +2464,30 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24702464
(ct, ty)
24712465
}
24722466

2473-
hir::ExprKind::Path(hir::QPath::Resolved(
2467+
hir::PatExprKind::Path(hir::QPath::Resolved(
24742468
_,
24752469
path @ &hir::Path {
24762470
res: Res::Def(DefKind::ConstParam, def_id),
24772471
..
24782472
},
24792473
)) => {
2480-
let _ = self.prohibit_generic_args(
2474+
match self.prohibit_generic_args(
24812475
path.segments.iter(),
24822476
GenericsArgsErrExtend::Param(def_id),
2483-
);
2484-
let ty = tcx
2485-
.type_of(def_id)
2486-
.no_bound_vars()
2487-
.expect("const parameter types cannot be generic");
2488-
let ct = self.lower_const_param(def_id, expr.hir_id);
2489-
(ct, ty)
2477+
) {
2478+
Ok(()) => {
2479+
let ty = tcx
2480+
.type_of(def_id)
2481+
.no_bound_vars()
2482+
.expect("const parameter types cannot be generic");
2483+
let ct = self.lower_const_param(def_id, expr.hir_id);
2484+
(ct, ty)
2485+
}
2486+
Err(guar) => (
2487+
ty::Const::new_error(tcx, guar),
2488+
Ty::new_error(tcx, guar),
2489+
),
2490+
}
24902491
}
24912492

24922493
_ => {
@@ -2497,9 +2498,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24972498
}
24982499
};
24992500
self.record_ty(expr.hir_id, c_ty, expr.span);
2500-
if let Some((id, span)) = neg {
2501-
self.record_ty(id, c_ty, span);
2502-
}
25032501
c
25042502
};
25052503

Diff for: compiler/rustc_hir_pretty/src/lib.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ impl<'a> State<'a> {
199199
Node::OpaqueTy(o) => self.print_opaque_ty(o),
200200
Node::Pat(a) => self.print_pat(a),
201201
Node::PatField(a) => self.print_patfield(a),
202+
Node::PatExpr(a) => self.print_pat_expr(a),
202203
Node::Arm(a) => self.print_arm(a),
203204
Node::Infer(_) => self.word("_"),
204205
Node::PreciseCapturingNonLifetimeArg(param) => self.print_ident(param.ident),
@@ -1849,6 +1850,19 @@ impl<'a> State<'a> {
18491850
}
18501851
}
18511852

1853+
fn print_pat_expr(&mut self, expr: &hir::PatExpr<'_>) {
1854+
match &expr.kind {
1855+
hir::PatExprKind::Lit { lit, negated } => {
1856+
if *negated {
1857+
self.word("-");
1858+
}
1859+
self.print_literal(lit);
1860+
}
1861+
hir::PatExprKind::ConstBlock(c) => self.print_inline_const(c),
1862+
hir::PatExprKind::Path(qpath) => self.print_qpath(qpath, true),
1863+
}
1864+
}
1865+
18521866
fn print_pat(&mut self, pat: &hir::Pat<'_>) {
18531867
self.maybe_print_comment(pat.span.lo());
18541868
self.ann.pre(self, AnnNode::Pat(pat));
@@ -1966,17 +1980,17 @@ impl<'a> State<'a> {
19661980
self.pclose();
19671981
}
19681982
}
1969-
PatKind::Lit(e) => self.print_expr(e),
1983+
PatKind::Lit(e) => self.print_pat_expr(e),
19701984
PatKind::Range(begin, end, end_kind) => {
19711985
if let Some(expr) = begin {
1972-
self.print_expr(expr);
1986+
self.print_pat_expr(expr);
19731987
}
19741988
match end_kind {
19751989
RangeEnd::Included => self.word("..."),
19761990
RangeEnd::Excluded => self.word(".."),
19771991
}
19781992
if let Some(expr) = end {
1979-
self.print_expr(expr);
1993+
self.print_pat_expr(expr);
19801994
}
19811995
}
19821996
PatKind::Slice(before, slice, after) => {

0 commit comments

Comments
 (0)