Skip to content

Commit 3b1adfa

Browse files
Parsing unsafe binders
1 parent 8e37e15 commit 3b1adfa

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

compiler/rustc_ast/src/ast.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,12 @@ pub struct BareFnTy {
22232223
pub decl_span: Span,
22242224
}
22252225

2226+
#[derive(Clone, Encodable, Decodable, Debug)]
2227+
pub struct UnsafeBinderTy {
2228+
pub generic_params: ThinVec<GenericParam>,
2229+
pub inner_ty: P<Ty>,
2230+
}
2231+
22262232
/// The various kinds of type recognized by the compiler.
22272233
//
22282234
// Adding a new variant? Please update `test_ty` in `tests/ui/macros/stringify.rs`.
@@ -2242,6 +2248,8 @@ pub enum TyKind {
22422248
PinnedRef(Option<Lifetime>, MutTy),
22432249
/// A bare function (e.g., `fn(usize) -> bool`).
22442250
BareFn(P<BareFnTy>),
2251+
/// An unsafe existential lifetime binder (e.g., `unsafe<'a> &'a ()`).
2252+
UnsafeBinder(P<UnsafeBinderTy>),
22452253
/// The never type (`!`).
22462254
Never,
22472255
/// A tuple (`(A, B, C, D,...)`).

compiler/rustc_ast/src/mut_visit.rs

+5
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,11 @@ pub fn walk_ty<T: MutVisitor>(vis: &mut T, ty: &mut P<Ty>) {
558558
vis.visit_fn_decl(decl);
559559
vis.visit_span(decl_span);
560560
}
561+
TyKind::UnsafeBinder(binder) => {
562+
let UnsafeBinderTy { generic_params, inner_ty } = binder.deref_mut();
563+
generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
564+
vis.visit_ty(inner_ty);
565+
}
561566
TyKind::Tup(tys) => visit_thin_vec(tys, |ty| vis.visit_ty(ty)),
562567
TyKind::Paren(ty) => vis.visit_ty(ty),
563568
TyKind::Pat(ty, pat) => {

compiler/rustc_ast/src/util/classify.rs

+4
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ fn type_trailing_braced_mac_call(mut ty: &ast::Ty) -> Option<&ast::MacCall> {
253253
ty = &mut_ty.ty;
254254
}
255255

256+
ast::TyKind::UnsafeBinder(binder) => {
257+
ty = &binder.inner_ty;
258+
}
259+
256260
ast::TyKind::BareFn(fn_ty) => match &fn_ty.decl.output {
257261
ast::FnRetTy::Default(_) => break None,
258262
ast::FnRetTy::Ty(ret) => ty = ret,

compiler/rustc_ast/src/visit.rs

+4
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) -> V::Result {
522522
walk_list!(visitor, visit_generic_param, generic_params);
523523
try_visit!(visitor.visit_fn_decl(decl));
524524
}
525+
TyKind::UnsafeBinder(binder) => {
526+
walk_list!(visitor, visit_generic_param, &binder.generic_params);
527+
try_visit!(visitor.visit_ty(&binder.inner_ty));
528+
}
525529
TyKind::Path(maybe_qself, path) => {
526530
try_visit!(visitor.visit_qself(maybe_qself));
527531
try_visit!(visitor.visit_path(path, *id));

compiler/rustc_ast_pretty/src/pprust/state.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,14 @@ impl<'a> State<'a> {
11981198
ast::TyKind::BareFn(f) => {
11991199
self.print_ty_fn(f.ext, f.safety, &f.decl, None, &f.generic_params);
12001200
}
1201+
ast::TyKind::UnsafeBinder(f) => {
1202+
self.ibox(INDENT_UNIT);
1203+
self.word("unsafe");
1204+
self.print_generic_params(&f.generic_params);
1205+
self.nbsp();
1206+
self.print_type(&f.inner_ty);
1207+
self.end();
1208+
}
12011209
ast::TyKind::Path(None, path) => {
12021210
self.print_path(path, false, 0);
12031211
}

compiler/rustc_parse/src/parser/ty.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::{
55
self as ast, BareFnTy, BoundAsyncness, BoundConstness, BoundPolarity, DUMMY_NODE_ID, FnRetTy,
66
GenericBound, GenericBounds, GenericParam, Generics, Lifetime, MacCall, MutTy, Mutability,
77
Pinnedness, PolyTraitRef, PreciseCapturingArg, TraitBoundModifiers, TraitObjectSyntax, Ty,
8-
TyKind,
8+
TyKind, UnsafeBinderTy,
99
};
1010
use rustc_errors::{Applicability, PResult};
1111
use rustc_span::symbol::{Ident, kw, sym};
@@ -348,6 +348,8 @@ impl<'a> Parser<'a> {
348348
TyKind::Err(guar)
349349
}
350350
}
351+
} else if self.check_keyword(kw::Unsafe) {
352+
self.parse_unsafe_binder_ty()?
351353
} else {
352354
let msg = format!("expected type, found {}", super::token_descr(&self.token));
353355
let mut err = self.dcx().struct_span_err(lo, msg);
@@ -369,6 +371,19 @@ impl<'a> Parser<'a> {
369371
if allow_qpath_recovery { self.maybe_recover_from_bad_qpath(ty) } else { Ok(ty) }
370372
}
371373

374+
fn parse_unsafe_binder_ty(&mut self) -> PResult<'a, TyKind> {
375+
let lo = self.token.span;
376+
assert!(self.eat_keyword(kw::Unsafe));
377+
self.expect_lt()?;
378+
let generic_params = self.parse_generic_params()?;
379+
self.expect_gt()?;
380+
let inner_ty = self.parse_ty()?;
381+
let span = lo.to(self.prev_token.span);
382+
self.psess.gated_spans.gate(sym::unsafe_binders, span);
383+
384+
Ok(TyKind::UnsafeBinder(P(UnsafeBinderTy { generic_params, inner_ty })))
385+
}
386+
372387
/// Parses either:
373388
/// - `(TYPE)`, a parenthesized type.
374389
/// - `(TYPE,)`, a tuple with a single field of type TYPE.

0 commit comments

Comments
 (0)