Skip to content

Commit 68a558c

Browse files
authored
Rollup merge of #134140 - compiler-errors:unsafe-binders-ast, r=oli-obk
Add AST support for unsafe binders I'm splitting up #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 6b12b6b + 05c9d68 commit 68a558c

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

src/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ pub(crate) fn format_expr(
413413
ast::ExprKind::FormatArgs(..)
414414
| ast::ExprKind::Type(..)
415415
| ast::ExprKind::IncludedBytes(..)
416-
| ast::ExprKind::OffsetOf(..) => {
416+
| ast::ExprKind::OffsetOf(..)
417+
| ast::ExprKind::UnsafeBinderCast(..) => {
417418
// These don't normally occur in the AST because macros aren't expanded. However,
418419
// rustfmt tries to parse macro arguments when formatting macros, so it's not totally
419420
// impossible for rustfmt to come across one of these nodes when formatting a file.

src/types.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,31 @@ impl Rewrite for ast::Ty {
10161016
let pat = pat.rewrite_result(context, shape)?;
10171017
Ok(format!("{ty} is {pat}"))
10181018
}
1019+
ast::TyKind::UnsafeBinder(ref binder) => {
1020+
let mut result = String::new();
1021+
if let Some(ref lifetime_str) =
1022+
rewrite_bound_params(context, shape, &binder.generic_params)
1023+
{
1024+
result.push_str("unsafe<");
1025+
result.push_str(lifetime_str);
1026+
result.push_str("> ");
1027+
}
1028+
1029+
let inner_ty_shape = if context.use_block_indent() {
1030+
shape
1031+
.offset_left(result.len())
1032+
.max_width_error(shape.width, self.span())?
1033+
} else {
1034+
shape
1035+
.visual_indent(result.len())
1036+
.sub_width(result.len())
1037+
.max_width_error(shape.width, self.span())?
1038+
};
1039+
1040+
let rewrite = binder.inner_ty.rewrite_result(context, inner_ty_shape)?;
1041+
result.push_str(&rewrite);
1042+
Ok(result)
1043+
}
10191044
}
10201045
}
10211046
}

src/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
504504
| ast::ExprKind::IncludedBytes(..)
505505
| ast::ExprKind::InlineAsm(..)
506506
| ast::ExprKind::OffsetOf(..)
507+
| ast::ExprKind::UnsafeBinderCast(..)
507508
| ast::ExprKind::Let(..)
508509
| ast::ExprKind::Path(..)
509510
| ast::ExprKind::Range(..)

tests/source/unsafe-binders.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn foo() -> unsafe<'a>
2+
&'a () {}
3+
4+
struct Foo {
5+
x: unsafe<'a>
6+
&'a (),
7+
}
8+
9+
struct Bar(unsafe<'a> &'a ());
10+
11+
impl Trait for unsafe<'a> &'a () {}

tests/target/unsafe-binders.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn foo() -> unsafe<'a> &'a () {}
2+
3+
struct Foo {
4+
x: unsafe<'a> &'a (),
5+
}
6+
7+
struct Bar(unsafe<'a> &'a ());
8+
9+
impl Trait for unsafe<'a> &'a () {}

0 commit comments

Comments
 (0)