Skip to content

Commit 3aa766a

Browse files
Fix tools
1 parent 285ab06 commit 3aa766a

File tree

18 files changed

+102
-9
lines changed

18 files changed

+102
-9
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,9 @@ pub(crate) fn codegen_place<'tcx>(
964964
cplace = cplace.place_deref(fx);
965965
}
966966
PlaceElem::OpaqueCast(ty) => bug!("encountered OpaqueCast({ty}) in codegen"),
967-
PlaceElem::Subtype(ty) => cplace = cplace.place_transmute_type(fx, fx.monomorphize(ty)),
967+
PlaceElem::Subtype(ty) | PlaceElem::UnsafeBinderCast(_, ty) => {
968+
cplace = cplace.place_transmute_type(fx, fx.monomorphize(ty));
969+
}
968970
PlaceElem::Field(field, _ty) => {
969971
cplace = cplace.place_field(fx, field);
970972
}

src/librustdoc/clean/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,9 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18551855
DynTrait(bounds, lifetime)
18561856
}
18571857
TyKind::BareFn(barefn) => BareFunction(Box::new(clean_bare_fn_ty(barefn, cx))),
1858+
TyKind::UnsafeBinder(unsafe_binder_ty) => {
1859+
UnsafeBinder(Box::new(clean_unsafe_binder_ty(unsafe_binder_ty, cx)))
1860+
}
18581861
// Rustdoc handles `TyKind::Err`s by turning them into `Type::Infer`s.
18591862
TyKind::Infer | TyKind::Err(_) | TyKind::Typeof(..) | TyKind::InferDelegation(..) => Infer,
18601863
TyKind::AnonAdt(..) => {
@@ -2080,6 +2083,11 @@ pub(crate) fn clean_middle_ty<'tcx>(
20802083
abi: sig.abi(),
20812084
}))
20822085
}
2086+
ty::UnsafeBinder(inner) => {
2087+
let generic_params = clean_bound_vars(inner.bound_vars());
2088+
let ty = clean_middle_ty(inner.into(), cx, None, None);
2089+
UnsafeBinder(Box::new(UnsafeBinderTy { generic_params, ty }))
2090+
}
20832091
ty::Adt(def, args) => {
20842092
let did = def.did();
20852093
let kind = match def.adt_kind() {
@@ -2577,6 +2585,21 @@ fn clean_bare_fn_ty<'tcx>(
25772585
BareFunctionDecl { safety: bare_fn.safety, abi: bare_fn.abi, decl, generic_params }
25782586
}
25792587

2588+
fn clean_unsafe_binder_ty<'tcx>(
2589+
unsafe_binder_ty: &hir::UnsafeBinderTy<'tcx>,
2590+
cx: &mut DocContext<'tcx>,
2591+
) -> UnsafeBinderTy {
2592+
// NOTE: generics must be cleaned before args
2593+
let generic_params = unsafe_binder_ty
2594+
.generic_params
2595+
.iter()
2596+
.filter(|p| !is_elided_lifetime(p))
2597+
.map(|x| clean_generic_param(cx, None, x))
2598+
.collect();
2599+
let ty = clean_ty(unsafe_binder_ty.inner_ty, cx);
2600+
UnsafeBinderTy { generic_params, ty }
2601+
}
2602+
25802603
pub(crate) fn reexport_chain(
25812604
tcx: TyCtxt<'_>,
25822605
import_def_id: LocalDefId,

src/librustdoc/clean/types.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use {rustc_ast as ast, rustc_hir as hir};
3737
pub(crate) use self::ItemKind::*;
3838
pub(crate) use self::Type::{
3939
Array, BareFunction, BorrowedRef, DynTrait, Generic, ImplTrait, Infer, Primitive, QPath,
40-
RawPointer, SelfTy, Slice, Tuple,
40+
RawPointer, SelfTy, Slice, Tuple, UnsafeBinder,
4141
};
4242
use crate::clean::cfg::Cfg;
4343
use crate::clean::clean_middle_path;
@@ -1503,6 +1503,8 @@ pub(crate) enum Type {
15031503

15041504
/// An `impl Trait`: `impl TraitA + TraitB + ...`
15051505
ImplTrait(Vec<GenericBound>),
1506+
1507+
UnsafeBinder(Box<UnsafeBinderTy>),
15061508
}
15071509

15081510
impl Type {
@@ -1695,7 +1697,7 @@ impl Type {
16951697
Type::Pat(..) => PrimitiveType::Pat,
16961698
RawPointer(..) => PrimitiveType::RawPointer,
16971699
QPath(box QPathData { ref self_type, .. }) => return self_type.def_id(cache),
1698-
Generic(_) | SelfTy | Infer | ImplTrait(_) => return None,
1700+
Generic(_) | SelfTy | Infer | ImplTrait(_) | UnsafeBinder(_) => return None,
16991701
};
17001702
Primitive(t).def_id(cache)
17011703
}
@@ -2335,6 +2337,12 @@ pub(crate) struct BareFunctionDecl {
23352337
pub(crate) abi: Abi,
23362338
}
23372339

2340+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
2341+
pub(crate) struct UnsafeBinderTy {
2342+
pub(crate) generic_params: Vec<GenericParamDef>,
2343+
pub(crate) ty: Type,
2344+
}
2345+
23382346
#[derive(Clone, Debug)]
23392347
pub(crate) struct Static {
23402348
pub(crate) type_: Box<Type>,

src/librustdoc/html/format.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,11 @@ fn fmt_type(
10351035
}
10361036
decl.decl.print(cx).fmt(f)
10371037
}
1038+
clean::UnsafeBinder(ref binder) => {
1039+
// FIXME(unsafe_binders): This should print `unsafe<...>`
1040+
print_higher_ranked_params_with_space(&binder.generic_params, cx).fmt(f)?;
1041+
binder.ty.print(cx).fmt(f)
1042+
}
10381043
clean::Tuple(ref typs) => match &typs[..] {
10391044
&[] => primitive_link(f, PrimitiveType::Unit, format_args!("()"), cx),
10401045
[one] => {

src/librustdoc/html/render/search_index.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,8 @@ fn get_index_type_id(
822822
| clean::Generic(_)
823823
| clean::SelfTy
824824
| clean::ImplTrait(_)
825-
| clean::Infer => None,
825+
| clean::Infer
826+
| clean::UnsafeBinder(_) => None,
826827
}
827828
}
828829

src/librustdoc/json/conversions.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ impl FromWithTcx<clean::Type> for Type {
576576
fn from_tcx(ty: clean::Type, tcx: TyCtxt<'_>) -> Self {
577577
use clean::Type::{
578578
Array, BareFunction, BorrowedRef, Generic, ImplTrait, Infer, Primitive, QPath,
579-
RawPointer, SelfTy, Slice, Tuple,
579+
RawPointer, SelfTy, Slice, Tuple, UnsafeBinder,
580580
};
581581

582582
match ty {
@@ -614,6 +614,8 @@ impl FromWithTcx<clean::Type> for Type {
614614
self_type: Box::new(self_type.into_tcx(tcx)),
615615
trait_: trait_.map(|trait_| trait_.into_tcx(tcx)),
616616
},
617+
// FIXME(unsafe_binder): Implement rustdoc-json.
618+
UnsafeBinder(_) => todo!(),
617619
}
618620
}
619621
}

src/librustdoc/passes/collect_intra_doc_links.rs

+1
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
506506
| ty::CoroutineClosure(..)
507507
| ty::Coroutine(..)
508508
| ty::CoroutineWitness(..)
509+
| ty::UnsafeBinder(..)
509510
| ty::Dynamic(..)
510511
| ty::Param(_)
511512
| ty::Bound(..)

src/tools/clippy/clippy_lints/src/dereference.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,8 @@ impl TyCoercionStability {
823823
| TyKind::Pat(..)
824824
| TyKind::Never
825825
| TyKind::Tup(_)
826-
| TyKind::Path(_) => Self::Deref,
826+
| TyKind::Path(_)
827+
| TyKind::UnsafeBinder(..) => Self::Deref,
827828
TyKind::OpaqueDef(..)
828829
| TyKind::Infer
829830
| TyKind::Typeof(..)
@@ -884,7 +885,8 @@ impl TyCoercionStability {
884885
| ty::CoroutineClosure(..)
885886
| ty::Never
886887
| ty::Tuple(_)
887-
| ty::Alias(ty::Projection, _) => Self::Deref,
888+
| ty::Alias(ty::Projection, _)
889+
| ty::UnsafeBinder(_) => Self::Deref,
888890
};
889891
}
890892
}

src/tools/clippy/clippy_lints/src/loops/never_loop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ fn never_loop_expr<'tcx>(
153153
ExprKind::Unary(_, e)
154154
| ExprKind::Cast(e, _)
155155
| ExprKind::Type(e, _)
156+
| ExprKind::UnsafeBinderCast(_, e, _)
156157
| ExprKind::Field(e, _)
157158
| ExprKind::AddrOf(_, _, e)
158159
| ExprKind::Repeat(e, _)

src/tools/clippy/clippy_lints/src/utils/author.rs

+4
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
626626
kind!("DropTemps({expr})");
627627
self.expr(expr);
628628
},
629+
ExprKind::UnsafeBinderCast(..) => {
630+
// FIXME(unsafe_binders): Implement this.
631+
todo!()
632+
}
629633
}
630634
}
631635

src/tools/clippy/clippy_utils/src/eager_or_lazy.rs

+1
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS
294294
| ExprKind::Lit(_)
295295
| ExprKind::Cast(..)
296296
| ExprKind::Type(..)
297+
| ExprKind::UnsafeBinderCast(..)
297298
| ExprKind::DropTemps(_)
298299
| ExprKind::Let(..)
299300
| ExprKind::If(..)

src/tools/clippy/clippy_utils/src/hir_utils.rs

+16
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ impl HirEqInterExpr<'_, '_, '_> {
347347
},
348348
(&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup),
349349
(&ExprKind::Type(le, lt), &ExprKind::Type(re, rt)) => self.eq_expr(le, re) && self.eq_ty(lt, rt),
350+
(&ExprKind::UnsafeBinderCast(lkind, le, None), &ExprKind::UnsafeBinderCast(rkind, re, None)) =>
351+
lkind == rkind && self.eq_expr(le, re),
352+
(&ExprKind::UnsafeBinderCast(lkind, le, Some(lt)), &ExprKind::UnsafeBinderCast(rkind, re, Some(rt))) =>
353+
lkind == rkind && self.eq_expr(le, re) && self.eq_ty(lt, rt),
350354
(&ExprKind::Unary(l_op, le), &ExprKind::Unary(r_op, re)) => l_op == r_op && self.eq_expr(le, re),
351355
(&ExprKind::Yield(le, _), &ExprKind::Yield(re, _)) => return self.eq_expr(le, re),
352356
(
@@ -381,6 +385,7 @@ impl HirEqInterExpr<'_, '_, '_> {
381385
| &ExprKind::Type(..)
382386
| &ExprKind::Unary(..)
383387
| &ExprKind::Yield(..)
388+
| &ExprKind::UnsafeBinderCast(..)
384389

385390
// --- Special cases that do not have a positive branch.
386391

@@ -928,6 +933,13 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
928933
std::mem::discriminant(&lop).hash(&mut self.s);
929934
self.hash_expr(le);
930935
},
936+
ExprKind::UnsafeBinderCast(kind, e, t) => {
937+
std::mem::discriminant(&kind).hash(&mut self.s);
938+
self.hash_expr(e);
939+
if let Some(t) = t {
940+
self.hash_ty(t);
941+
}
942+
}
931943
ExprKind::Err(_) => {},
932944
}
933945
}
@@ -1125,6 +1137,10 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
11251137
TyKind::Typeof(anon_const) => {
11261138
self.hash_body(anon_const.body);
11271139
},
1140+
TyKind::UnsafeBinder(binder) => {
1141+
// FIXME(unsafe_binder): Hash generics...
1142+
self.hash_ty(binder.inner_ty);
1143+
}
11281144
TyKind::Err(_) | TyKind::Infer | TyKind::Never | TyKind::InferDelegation(..) | TyKind::AnonAdt(_) => {},
11291145
}
11301146
}

src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ fn check_place<'tcx>(tcx: TyCtxt<'tcx>, place: Place<'tcx>, span: Span, body: &B
286286
| ProjectionElem::Downcast(..)
287287
| ProjectionElem::Subslice { .. }
288288
| ProjectionElem::Subtype(_)
289-
| ProjectionElem::Index(_) => {},
289+
| ProjectionElem::Index(_)
290+
| ProjectionElem::UnsafeBinderCast(..) => {},
290291
}
291292
}
292293

src/tools/clippy/clippy_utils/src/sugg.rs

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl<'a> Sugg<'a> {
151151
| ExprKind::Become(..)
152152
| ExprKind::Struct(..)
153153
| ExprKind::Tup(..)
154+
| ExprKind::UnsafeBinderCast(..)
154155
| ExprKind::Err(_) => Sugg::NonParen(get_snippet(expr.span)),
155156
ExprKind::DropTemps(inner) => Self::hir_from_snippet(inner, get_snippet),
156157
ExprKind::Assign(lhs, rhs, _) => {
@@ -223,6 +224,7 @@ impl<'a> Sugg<'a> {
223224
| ast::ExprKind::Array(..)
224225
| ast::ExprKind::While(..)
225226
| ast::ExprKind::Await(..)
227+
| ast::ExprKind::UnsafeBinderCast(..)
226228
| ast::ExprKind::Err(_)
227229
| ast::ExprKind::Dummy => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0),
228230
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp(

src/tools/clippy/clippy_utils/src/visitors.rs

+3
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,9 @@ pub fn for_each_unconsumed_temporary<'tcx, B>(
686686
ExprKind::Type(e, _) => {
687687
helper(typeck, consume, e, f)?;
688688
},
689+
ExprKind::UnsafeBinderCast(_, e, _) => {
690+
helper(typeck, consume, e, f)?;
691+
},
689692

690693
// Either drops temporaries, jumps out of the current expression, or has no sub expression.
691694
ExprKind::DropTemps(_)

src/tools/rustfmt/src/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ pub(crate) fn format_expr(
407407
ast::ExprKind::FormatArgs(..)
408408
| ast::ExprKind::Type(..)
409409
| ast::ExprKind::IncludedBytes(..)
410-
| ast::ExprKind::OffsetOf(..) => {
410+
| ast::ExprKind::OffsetOf(..)
411+
| ast::ExprKind::UnsafeBinderCast(..) => {
411412
// These don't normally occur in the AST because macros aren't expanded. However,
412413
// rustfmt tries to parse macro arguments when formatting macros, so it's not totally
413414
// impossible for rustfmt to come across one of these nodes when formatting a file.

src/tools/rustfmt/src/types.rs

+19
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,25 @@ impl Rewrite for ast::Ty {
895895
let pat = pat.rewrite(context, shape)?;
896896
Some(format!("{ty} is {pat}"))
897897
}
898+
ast::TyKind::UnsafeBinder(ref binder) => {
899+
let mut result = String::new()
900+
;
901+
if let Some(ref lifetime_str) = rewrite_bound_params(context, shape, &binder.generic_params) {
902+
result.push_str("unsafe<");
903+
result.push_str(lifetime_str);
904+
result.push_str("> ");
905+
}
906+
907+
let inner_ty_shape = if context.use_block_indent() {
908+
shape.offset_left(result.len())?
909+
} else {
910+
shape.visual_indent(result.len()).sub_width(result.len())?
911+
};
912+
913+
let rewrite = binder.inner_ty.rewrite(context, inner_ty_shape)?;
914+
result.push_str(&rewrite);
915+
Some(result)
916+
}
898917
}
899918
}
900919
}

src/tools/rustfmt/src/utils.rs

+1
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
497497
| ast::ExprKind::Await(..)
498498
| ast::ExprKind::Break(..)
499499
| ast::ExprKind::Cast(..)
500+
| ast::ExprKind::UnsafeBinderCast(..)
500501
| ast::ExprKind::Continue(..)
501502
| ast::ExprKind::Dummy
502503
| ast::ExprKind::Err(_)

0 commit comments

Comments
 (0)