Skip to content

Commit 6d08c10

Browse files
committed
Add not-null pointer patterns to pattern types
1 parent 1c04750 commit 6d08c10

File tree

32 files changed

+304
-35
lines changed

32 files changed

+304
-35
lines changed

compiler/rustc_ast/src/ast.rs

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

2515+
/// A `!null` pattern for raw pointers.
2516+
NotNull,
2517+
25152518
Or(ThinVec<P<TyPat>>),
25162519

25172520
/// 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
@@ -1030,7 +1030,7 @@ macro_rules! common_visitor_and_walkers {
10301030
visit_opt!(vis, visit_anon_const, end);
10311031
}
10321032
TyPatKind::Or(variants) => walk_list!(vis, visit_ty_pat, variants),
1033-
TyPatKind::Err(_) => {}
1033+
TyPatKind::NotNull | TyPatKind::Err(_) => {}
10341034
}
10351035
visit_span(vis, span)
10361036
}

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: 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
@@ -1245,9 +1245,10 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt,
12451245
// When you extend this match, make sure to also add tests to
12461246
// tests/ui/type/pattern_types/validity.rs((
12471247
match **pat {
1248-
// Range patterns are precisely reflected into `valid_range` and thus
1248+
// Range and non-null 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
@@ -1820,6 +1820,9 @@ pub enum TyPatKind<'hir> {
18201820
/// A range pattern (e.g., `1..=2` or `1..2`).
18211821
Range(&'hir ConstArg<'hir>, &'hir ConstArg<'hir>),
18221822

1823+
/// A pattern that excludes null pointers
1824+
NotNull,
1825+
18231826
/// A list of patterns where only one needs to be satisfied
18241827
Or(&'hir [TyPat<'hir>]),
18251828

compiler/rustc_hir/src/intravisit.rs

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

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

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)