Skip to content

Commit 33658cb

Browse files
committed
Add not-null pointer patterns to pattern types
1 parent 61392fe commit 33658cb

File tree

31 files changed

+312
-36
lines changed

31 files changed

+312
-36
lines changed

compiler/rustc_ast/src/ast.rs

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

2297+
/// A `!null` pattern for raw pointers.
2298+
NotNull,
2299+
22972300
/// Placeholder for a pattern that wasn't syntactically well formed in some way.
22982301
Err(ErrorGuaranteed),
22992302
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ pub fn walk_ty_pat<T: MutVisitor>(vis: &mut T, ty: &mut P<TyPat>) {
606606
visit_opt(start, |c| vis.visit_anon_const(c));
607607
visit_opt(end, |c| vis.visit_anon_const(c));
608608
}
609-
TyPatKind::Err(_) => {}
609+
TyPatKind::NotNull | TyPatKind::Err(_) => {}
610610
}
611611
visit_lazy_tts(vis, tokens);
612612
vis.visit_span(span);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ pub fn walk_ty_pat<'a, V: Visitor<'a>>(visitor: &mut V, tp: &'a TyPat) -> V::Res
565565
visit_opt!(visitor, visit_anon_const, start);
566566
visit_opt!(visitor, visit_anon_const, end);
567567
}
568-
TyPatKind::Err(_) => {}
568+
TyPatKind::NotNull | TyPatKind::Err(_) => {}
569569
}
570570
V::Result::output()
571571
}

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
142142
}
143143
// return inner to be processed in next loop
144144
PatKind::Paren(inner) => pattern = inner,
145-
PatKind::MacCall(_) => panic!("{:?} shouldn't exist here", pattern.span),
145+
PatKind::MacCall(_) => {
146+
panic!("{pattern:#?} shouldn't exist here")
147+
}
146148
PatKind::Err(guar) => break hir::PatKind::Err(*guar),
147149
}
148150
};
@@ -463,6 +465,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
463465
)
464466
}),
465467
),
468+
TyPatKind::NotNull => hir::TyPatKind::NotNull,
466469
TyPatKind::Err(guar) => hir::TyPatKind::Err(*guar),
467470
};
468471

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,7 @@ impl<'a> State<'a> {
11661166
self.print_expr_anon_const(end, &[]);
11671167
}
11681168
}
1169+
rustc_ast::TyPatKind::NotNull => self.word("!null"),
11691170
rustc_ast::TyPatKind::Err(_) => {
11701171
self.popen();
11711172
self.word("/*ERROR*/");

compiler/rustc_builtin_macros/src/pattern_type.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_ast::ptr::P;
22
use rustc_ast::tokenstream::TokenStream;
3-
use rustc_ast::{AnonConst, DUMMY_NODE_ID, Ty, TyPat, TyPatKind, ast};
3+
use rustc_ast::{AnonConst, DUMMY_NODE_ID, Ty, TyPat, TyPatKind, ast, token};
44
use rustc_errors::PResult;
55
use rustc_expand::base::{self, DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
66
use rustc_parse::exp;
@@ -26,19 +26,31 @@ fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P
2626

2727
let ty = parser.parse_ty()?;
2828
parser.expect_keyword(exp!(Is))?;
29-
let pat = parser.parse_pat_no_top_alt(None, None)?.into_inner();
3029

31-
let kind = match pat.kind {
32-
ast::PatKind::Range(start, end, include_end) => TyPatKind::Range(
33-
start.map(|value| P(AnonConst { id: DUMMY_NODE_ID, value })),
34-
end.map(|value| P(AnonConst { id: DUMMY_NODE_ID, value })),
35-
include_end,
36-
),
37-
ast::PatKind::Err(guar) => TyPatKind::Err(guar),
38-
_ => TyPatKind::Err(cx.dcx().span_err(pat.span, "pattern not supported in pattern types")),
30+
let start = parser.token.span;
31+
let kind = if parser.eat(exp!(Not)) {
32+
parser.expect_keyword(exp!(Null))?;
33+
TyPatKind::NotNull
34+
} else {
35+
let pat = parser.parse_pat_no_top_alt(None, None)?.into_inner();
36+
match pat.kind {
37+
ast::PatKind::Range(start, end, include_end) => TyPatKind::Range(
38+
start.map(|value| P(AnonConst { id: DUMMY_NODE_ID, value })),
39+
end.map(|value| P(AnonConst { id: DUMMY_NODE_ID, value })),
40+
include_end,
41+
),
42+
ast::PatKind::Err(guar) => TyPatKind::Err(guar),
43+
_ => TyPatKind::Err(
44+
cx.dcx().span_err(pat.span, "pattern not supported in pattern types"),
45+
),
46+
}
3947
};
4048

41-
let pat = P(TyPat { id: pat.id, kind, span: pat.span, tokens: pat.tokens });
49+
let span = start.to(parser.token.span);
50+
let pat = P(TyPat { id: DUMMY_NODE_ID, kind, span, tokens: None });
4251

52+
if parser.token != token::Eof {
53+
parser.unexpected()?;
54+
}
4355
Ok((ty, pat))
4456
}

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
6969
}
7070
ty::Pat(_, pat) => match **pat {
7171
ty::PatternKind::Range { .. } => ConstValue::from_target_usize(0u64, &tcx),
72-
// Future pattern kinds may have more variants
72+
ty::PatternKind::NotNull => ConstValue::from_target_usize(0_u64, &tcx),
7373
},
7474
ty::Bound(_, _) => bug!("bound ty during ctfe"),
7575
ty::Bool

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
}
12531254
_ => {

compiler/rustc_hir/src/hir.rs

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

1673+
/// A pattern that excludes null pointers
1674+
NotNull,
1675+
16731676
/// A placeholder for a pattern that wasn't well formed in some way.
16741677
Err(ErrorGuaranteed),
16751678
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ pub fn walk_ty_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v TyPat<'v>)
712712
try_visit!(visitor.visit_const_arg_unambig(lower_bound));
713713
try_visit!(visitor.visit_const_arg_unambig(upper_bound));
714714
}
715-
TyPatKind::Err(_) => (),
715+
TyPatKind::NotNull | TyPatKind::Err(_) => (),
716716
}
717717
V::Result::output()
718718
}

0 commit comments

Comments
 (0)