Skip to content

Commit e3d0a14

Browse files
committed
Add not-null pointer patterns to pattern types
1 parent 48251c5 commit e3d0a14

File tree

30 files changed

+295
-25
lines changed

30 files changed

+295
-25
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,9 @@ pub enum TyPatKind {
24842484
/// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
24852485
Range(Option<P<AnonConst>>, Option<P<AnonConst>>, Spanned<RangeEnd>),
24862486

2487+
/// A `!null` pattern for raw pointers.
2488+
NotNull,
2489+
24872490
Or(ThinVec<P<TyPat>>),
24882491

24892492
/// Placeholder for a pattern that wasn't syntactically well formed in some way.

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ pub fn walk_ty_pat<T: MutVisitor>(vis: &mut T, ty: &mut TyPat) {
560560
visit_opt(end, |c| vis.visit_anon_const(c));
561561
}
562562
TyPatKind::Or(variants) => visit_thin_vec(variants, |p| vis.visit_ty_pat(p)),
563-
TyPatKind::Err(_) => {}
563+
TyPatKind::NotNull | TyPatKind::Err(_) => {}
564564
}
565565
vis.visit_span(span);
566566
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ pub fn walk_ty_pat<'a, V: Visitor<'a>>(visitor: &mut V, tp: &'a TyPat) -> V::Res
906906
visit_opt!(visitor, visit_anon_const, end);
907907
}
908908
TyPatKind::Or(variants) => walk_list!(visitor, visit_ty_pat, variants),
909-
TyPatKind::Err(_) => {}
909+
TyPatKind::NotNull | TyPatKind::Err(_) => {}
910910
}
911911
V::Result::output()
912912
}

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
143143
}
144144
// return inner to be processed in next loop
145145
PatKind::Paren(inner) => pattern = inner,
146-
PatKind::MacCall(_) => panic!("{:?} shouldn't exist here", pattern.span),
146+
PatKind::MacCall(_) => {
147+
panic!("{pattern:#?} shouldn't exist here")
148+
}
147149
PatKind::Err(guar) => break hir::PatKind::Err(*guar),
148150
}
149151
};
@@ -464,6 +466,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
464466
)
465467
}),
466468
),
469+
TyPatKind::NotNull => hir::TyPatKind::NotNull,
467470
TyPatKind::Or(variants) => {
468471
hir::TyPatKind::Or(self.arena.alloc_from_iter(
469472
variants.iter().map(|pat| self.lower_ty_pat_mut(pat, base_type)),

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ impl<'a> State<'a> {
12181218
self.print_expr_anon_const(end, &[]);
12191219
}
12201220
}
1221+
rustc_ast::TyPatKind::NotNull => self.word("!null"),
12211222
rustc_ast::TyPatKind::Or(variants) => {
12221223
let mut first = true;
12231224
for pat in variants {

compiler/rustc_builtin_macros/src/pattern_type.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,23 @@ fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P
2828
let ty = parser.parse_ty()?;
2929
parser.expect_keyword(exp!(Is))?;
3030

31-
let pat = pat_to_ty_pat(
32-
cx,
33-
parser
34-
.parse_pat_no_top_guard(
35-
None,
36-
RecoverComma::No,
37-
RecoverColon::No,
38-
CommaRecoveryMode::EitherTupleOrPipe,
39-
)?
40-
.into_inner(),
41-
);
31+
let start = parser.token.span;
32+
let pat = if parser.eat(exp!(Bang)) {
33+
parser.expect_keyword(exp!(Null))?;
34+
ty_pat(TyPatKind::NotNull, start.to(parser.token.span))
35+
} else {
36+
pat_to_ty_pat(
37+
cx,
38+
parser
39+
.parse_pat_no_top_guard(
40+
None,
41+
RecoverComma::No,
42+
RecoverColon::No,
43+
CommaRecoveryMode::EitherTupleOrPipe,
44+
)?
45+
.into_inner(),
46+
)
47+
};
4248

4349
if parser.token != token::Eof {
4450
parser.unexpected()?;

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt,
12481248
// Range patterns are precisely reflected into `valid_range` and thus
12491249
// handled fully by `visit_scalar` (called below).
12501250
ty::PatternKind::Range { .. } => {},
1251+
ty::PatternKind::NotNull => {},
12511252

12521253
// FIXME(pattern_types): check that the value is covered by one of the variants.
12531254
// For now, we rely on layout computation setting the scalar's `valid_range` to

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,9 @@ pub enum TyPatKind<'hir> {
18241824
/// A range pattern (e.g., `1..=2` or `1..2`).
18251825
Range(&'hir ConstArg<'hir>, &'hir ConstArg<'hir>),
18261826

1827+
/// A pattern that excludes null pointers
1828+
NotNull,
1829+
18271830
/// A list of patterns where only one needs to be satisfied
18281831
Or(&'hir [TyPat<'hir>]),
18291832

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ pub fn walk_ty_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v TyPat<'v>)
711711
try_visit!(visitor.visit_const_arg_unambig(upper_bound));
712712
}
713713
TyPatKind::Or(patterns) => walk_list!(visitor, visit_pattern_type_pattern, patterns),
714-
TyPatKind::Err(_) => (),
714+
TyPatKind::NotNull | TyPatKind::Err(_) => (),
715715
}
716716
V::Result::output()
717717
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26512651
.span_delayed_bug(ty_span, "invalid base type for range pattern")),
26522652
}
26532653
}
2654+
hir::TyPatKind::NotNull => Ok(ty::PatternKind::NotNull),
26542655
hir::TyPatKind::Or(patterns) => {
26552656
self.tcx()
26562657
.mk_patterns_from_iter(patterns.iter().map(|pat| {

0 commit comments

Comments
 (0)