Skip to content

Commit 5c9b227

Browse files
authored
Rollup merge of rust-lang#134140 - compiler-errors:unsafe-binders-ast, r=oli-obk
Add AST support for unsafe binders I'm splitting up rust-lang#130514 into pieces. It's impossible for me to keep up with a huge PR like that. I'll land type system support for this next, probably w/o MIR lowering, which will come later. r? `@oli-obk` cc `@BoxyUwU` and `@lcnr` who also may want to look at this, though this PR doesn't do too much yet
2 parents 9f6b07e + b8c5a0f commit 5c9b227

File tree

53 files changed

+616
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+616
-18
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@ impl Expr {
13821382
| ExprKind::Tup(_)
13831383
| ExprKind::Type(..)
13841384
| ExprKind::Underscore
1385+
| ExprKind::UnsafeBinderCast(..)
13851386
| ExprKind::While(..)
13861387
| ExprKind::Err(_)
13871388
| ExprKind::Dummy => ExprPrecedence::Unambiguous,
@@ -1509,7 +1510,13 @@ pub enum ExprKind {
15091510
/// `'label: for await? pat in iter { block }`
15101511
///
15111512
/// This is desugared to a combination of `loop` and `match` expressions.
1512-
ForLoop { pat: P<Pat>, iter: P<Expr>, body: P<Block>, label: Option<Label>, kind: ForLoopKind },
1513+
ForLoop {
1514+
pat: P<Pat>,
1515+
iter: P<Expr>,
1516+
body: P<Block>,
1517+
label: Option<Label>,
1518+
kind: ForLoopKind,
1519+
},
15131520
/// Conditionless loop (can be exited with `break`, `continue`, or `return`).
15141521
///
15151522
/// `'label: loop { block }`
@@ -1614,6 +1621,8 @@ pub enum ExprKind {
16141621
/// A `format_args!()` expression.
16151622
FormatArgs(P<FormatArgs>),
16161623

1624+
UnsafeBinderCast(UnsafeBinderCastKind, P<Expr>, Option<P<Ty>>),
1625+
16171626
/// Placeholder for an expression that wasn't syntactically well formed in some way.
16181627
Err(ErrorGuaranteed),
16191628

@@ -1652,6 +1661,16 @@ impl GenBlockKind {
16521661
}
16531662
}
16541663

1664+
/// Whether we're unwrapping or wrapping an unsafe binder
1665+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1666+
#[derive(Encodable, Decodable, HashStable_Generic)]
1667+
pub enum UnsafeBinderCastKind {
1668+
// e.g. `&i32` -> `unsafe<'a> &'a i32`
1669+
Wrap,
1670+
// e.g. `unsafe<'a> &'a i32` -> `&i32`
1671+
Unwrap,
1672+
}
1673+
16551674
/// The explicit `Self` type in a "qualified path". The actual
16561675
/// path, including the trait and the associated item, is stored
16571676
/// separately. `position` represents the index of the associated
@@ -2223,6 +2242,12 @@ pub struct BareFnTy {
22232242
pub decl_span: Span,
22242243
}
22252244

2245+
#[derive(Clone, Encodable, Decodable, Debug)]
2246+
pub struct UnsafeBinderTy {
2247+
pub generic_params: ThinVec<GenericParam>,
2248+
pub inner_ty: P<Ty>,
2249+
}
2250+
22262251
/// The various kinds of type recognized by the compiler.
22272252
//
22282253
// Adding a new variant? Please update `test_ty` in `tests/ui/macros/stringify.rs`.
@@ -2242,6 +2267,8 @@ pub enum TyKind {
22422267
PinnedRef(Option<Lifetime>, MutTy),
22432268
/// A bare function (e.g., `fn(usize) -> bool`).
22442269
BareFn(P<BareFnTy>),
2270+
/// An unsafe existential lifetime binder (e.g., `unsafe<'a> &'a ()`).
2271+
UnsafeBinder(P<UnsafeBinderTy>),
22452272
/// The never type (`!`).
22462273
Never,
22472274
/// A tuple (`(A, B, C, D,...)`).

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 11 additions & 0 deletions
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) => {
@@ -1780,6 +1785,12 @@ pub fn walk_expr<T: MutVisitor>(vis: &mut T, Expr { kind, id, span, attrs, token
17801785
ExprKind::TryBlock(body) => vis.visit_block(body),
17811786
ExprKind::Lit(_token) => {}
17821787
ExprKind::IncludedBytes(_bytes) => {}
1788+
ExprKind::UnsafeBinderCast(_kind, expr, ty) => {
1789+
vis.visit_expr(expr);
1790+
if let Some(ty) = ty {
1791+
vis.visit_ty(ty);
1792+
}
1793+
}
17831794
ExprKind::Err(_guar) => {}
17841795
ExprKind::Dummy => {}
17851796
}

compiler/rustc_ast/src/util/classify.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ pub fn leading_labeled_expr(mut expr: &ast::Expr) -> bool {
152152
| Underscore
153153
| Yeet(..)
154154
| Yield(..)
155+
| UnsafeBinderCast(..)
155156
| Err(..)
156157
| Dummy => return false,
157158
}
@@ -232,6 +233,7 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
232233
| Paren(_)
233234
| Try(_)
234235
| Yeet(None)
236+
| UnsafeBinderCast(..)
235237
| Err(_)
236238
| Dummy => break None,
237239
}
@@ -253,6 +255,10 @@ fn type_trailing_braced_mac_call(mut ty: &ast::Ty) -> Option<&ast::MacCall> {
253255
ty = &mut_ty.ty;
254256
}
255257

258+
ast::TyKind::UnsafeBinder(binder) => {
259+
ty = &binder.inner_ty;
260+
}
261+
256262
ast::TyKind::BareFn(fn_ty) => match &fn_ty.decl.output {
257263
ast::FnRetTy::Default(_) => break None,
258264
ast::FnRetTy::Ty(ret) => ty = ret,

compiler/rustc_ast/src/visit.rs

Lines changed: 8 additions & 0 deletions
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));
@@ -1226,6 +1230,10 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
12261230
ExprKind::TryBlock(body) => try_visit!(visitor.visit_block(body)),
12271231
ExprKind::Lit(_token) => {}
12281232
ExprKind::IncludedBytes(_bytes) => {}
1233+
ExprKind::UnsafeBinderCast(_kind, expr, ty) => {
1234+
try_visit!(visitor.visit_expr(expr));
1235+
visit_opt!(visitor, visit_ty, ty);
1236+
}
12291237
ExprKind::Err(_guar) => {}
12301238
ExprKind::Dummy => {}
12311239
}

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
379379
ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
380380
ExprKind::Err(guar) => hir::ExprKind::Err(*guar),
381381

382+
ExprKind::UnsafeBinderCast(kind, expr, ty) => hir::ExprKind::UnsafeBinderCast(
383+
*kind,
384+
self.lower_expr(expr),
385+
ty.as_ref().map(|ty| {
386+
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast))
387+
}),
388+
),
389+
382390
ExprKind::Dummy => {
383391
span_bug!(e.span, "lowered ExprKind::Dummy")
384392
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12281228
param_names: self.lower_fn_params_to_names(&f.decl),
12291229
}))
12301230
}
1231+
TyKind::UnsafeBinder(f) => {
1232+
let generic_params = self.lower_lifetime_binder(t.id, &f.generic_params);
1233+
hir::TyKind::UnsafeBinder(self.arena.alloc(hir::UnsafeBinderTy {
1234+
generic_params,
1235+
inner_ty: self.lower_ty(&f.inner_ty, itctx),
1236+
}))
1237+
}
12311238
TyKind::Never => hir::TyKind::Never,
12321239
TyKind::Tup(tys) => hir::TyKind::Tup(
12331240
self.arena.alloc_from_iter(tys.iter().map(|ty| self.lower_ty_direct(ty, itctx))),

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
560560
gate_all!(return_type_notation, "return type notation is experimental");
561561
gate_all!(pin_ergonomics, "pinned reference syntax is experimental");
562562
gate_all!(unsafe_fields, "`unsafe` fields are experimental");
563+
gate_all!(unsafe_binders, "unsafe binder types are experimental");
563564

564565
if !visitor.features.never_patterns() {
565566
if let Some(spans) = spans.get(&sym::never_patterns) {

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 8 additions & 0 deletions
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_ast_pretty/src/pprust/state/expr.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,25 @@ impl<'a> State<'a> {
772772
self.word_nbsp("try");
773773
self.print_block_with_attrs(blk, attrs)
774774
}
775+
ast::ExprKind::UnsafeBinderCast(kind, expr, ty) => {
776+
self.word("builtin # ");
777+
match kind {
778+
ast::UnsafeBinderCastKind::Wrap => self.word("wrap_binder"),
779+
ast::UnsafeBinderCastKind::Unwrap => self.word("unwrap_binder"),
780+
}
781+
self.popen();
782+
self.ibox(0);
783+
self.print_expr(expr, FixupContext::default());
784+
785+
if let Some(ty) = ty {
786+
self.word(",");
787+
self.space();
788+
self.print_type(ty);
789+
}
790+
791+
self.end();
792+
self.pclose();
793+
}
775794
ast::ExprKind::Err(_) => {
776795
self.popen();
777796
self.word("/*ERROR*/");

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ impl<'cx, 'a> Context<'cx, 'a> {
323323
| ExprKind::While(_, _, _)
324324
| ExprKind::Yeet(_)
325325
| ExprKind::Become(_)
326-
| ExprKind::Yield(_) => {}
326+
| ExprKind::Yield(_)
327+
| ExprKind::UnsafeBinderCast(..) => {}
327328
}
328329
}
329330

0 commit comments

Comments
 (0)