Skip to content

Commit dbf9e89

Browse files
committed
Add not-null pointer patterns to pattern types
1 parent 0d11be5 commit dbf9e89

File tree

29 files changed

+290
-23
lines changed

29 files changed

+290
-23
lines changed

compiler/rustc_ast/src/ast.rs

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

2574+
/// A `!null` pattern for raw pointers.
2575+
NotNull,
2576+
25742577
Or(ThinVec<P<TyPat>>),
25752578

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

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ macro_rules! common_visitor_and_walkers {
12081208
try_visit!(visit_span(vis, span));
12091209
}
12101210
TyPatKind::Or(variants) => walk_list!(vis, visit_ty_pat, variants),
1211-
TyPatKind::Err(_) => {}
1211+
TyPatKind::NotNull | TyPatKind::Err(_) => {}
12121212
}
12131213
visit_span(vis, span)
12141214
}

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
};
@@ -460,6 +462,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
460462
)
461463
}),
462464
),
465+
TyPatKind::NotNull => hir::TyPatKind::NotNull,
463466
TyPatKind::Or(variants) => {
464467
hir::TyPatKind::Or(self.arena.alloc_from_iter(
465468
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
@@ -1226,6 +1226,7 @@ impl<'a> State<'a> {
12261226
self.print_expr_anon_const(end, &[]);
12271227
}
12281228
}
1229+
rustc_ast::TyPatKind::NotNull => self.word("!null"),
12291230
rustc_ast::TyPatKind::Or(variants) => {
12301231
let mut first = true;
12311232
for pat in variants {

compiler/rustc_builtin_macros/src/pattern_type.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@ 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.parse_pat_no_top_guard(
34-
None,
35-
RecoverComma::No,
36-
RecoverColon::No,
37-
CommaRecoveryMode::EitherTupleOrPipe,
38-
)?,
39-
);
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.parse_pat_no_top_guard(
39+
None,
40+
RecoverComma::No,
41+
RecoverColon::No,
42+
CommaRecoveryMode::EitherTupleOrPipe,
43+
)?,
44+
)
45+
};
4046

4147
if parser.token != token::Eof {
4248
parser.unexpected()?;

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,9 +1249,10 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt,
12491249
// When you extend this match, make sure to also add tests to
12501250
// tests/ui/type/pattern_types/validity.rs((
12511251
match **pat {
1252-
// Range patterns are precisely reflected into `valid_range` and thus
1252+
// Range and non-null patterns are precisely reflected into `valid_range` and thus
12531253
// handled fully by `visit_scalar` (called below).
12541254
ty::PatternKind::Range { .. } => {},
1255+
ty::PatternKind::NotNull => {},
12551256

12561257
// FIXME(pattern_types): check that the value is covered by one of the variants.
12571258
// 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
@@ -1823,6 +1823,9 @@ pub enum TyPatKind<'hir> {
18231823
/// A range pattern (e.g., `1..=2` or `1..2`).
18241824
Range(&'hir ConstArg<'hir>, &'hir ConstArg<'hir>),
18251825

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

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ pub fn walk_ty_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v TyPat<'v>)
726726
try_visit!(visitor.visit_const_arg_unambig(upper_bound));
727727
}
728728
TyPatKind::Or(patterns) => walk_list!(visitor, visit_pattern_type_pattern, patterns),
729-
TyPatKind::Err(_) => (),
729+
TyPatKind::NotNull | TyPatKind::Err(_) => (),
730730
}
731731
V::Result::output()
732732
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25792579
.span_delayed_bug(ty_span, "invalid base type for range pattern")),
25802580
}
25812581
}
2582+
hir::TyPatKind::NotNull => Ok(ty::PatternKind::NotNull),
25822583
hir::TyPatKind::Or(patterns) => {
25832584
self.tcx()
25842585
.mk_patterns_from_iter(patterns.iter().map(|pat| {

compiler/rustc_hir_analysis/src/variance/constraints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
340340
self.add_constraints_from_const(current, start, variance);
341341
self.add_constraints_from_const(current, end, variance);
342342
}
343+
ty::PatternKind::NotNull => {}
343344
ty::PatternKind::Or(patterns) => {
344345
for pat in patterns {
345346
self.add_constraints_from_pat(current, variance, pat)

0 commit comments

Comments
 (0)