Skip to content

Commit ce3fbcb

Browse files
Fix tools
1 parent 3b15ead commit ce3fbcb

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
@@ -969,7 +969,9 @@ pub(crate) fn codegen_place<'tcx>(
969969
cplace = cplace.place_deref(fx);
970970
}
971971
PlaceElem::OpaqueCast(ty) => bug!("encountered OpaqueCast({ty}) in codegen"),
972-
PlaceElem::Subtype(ty) => cplace = cplace.place_transmute_type(fx, fx.monomorphize(ty)),
972+
PlaceElem::Subtype(ty) | PlaceElem::UnsafeBinderCast(_, ty) => {
973+
cplace = cplace.place_transmute_type(fx, fx.monomorphize(ty));
974+
}
973975
PlaceElem::Field(field, _ty) => {
974976
cplace = cplace.place_field(fx, field);
975977
}

src/librustdoc/clean/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,9 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18441844
DynTrait(bounds, lifetime)
18451845
}
18461846
TyKind::BareFn(barefn) => BareFunction(Box::new(clean_bare_fn_ty(barefn, cx))),
1847+
TyKind::UnsafeBinder(unsafe_binder_ty) => {
1848+
UnsafeBinder(Box::new(clean_unsafe_binder_ty(unsafe_binder_ty, cx)))
1849+
}
18471850
// Rustdoc handles `TyKind::Err`s by turning them into `Type::Infer`s.
18481851
TyKind::Infer | TyKind::Err(_) | TyKind::Typeof(..) | TyKind::InferDelegation(..) => Infer,
18491852
TyKind::AnonAdt(..) => {
@@ -2069,6 +2072,11 @@ pub(crate) fn clean_middle_ty<'tcx>(
20692072
abi: sig.abi(),
20702073
}))
20712074
}
2075+
ty::UnsafeBinder(inner) => {
2076+
let generic_params = clean_bound_vars(inner.bound_vars());
2077+
let ty = clean_middle_ty(inner.into(), cx, None, None);
2078+
UnsafeBinder(Box::new(UnsafeBinderTy { generic_params, ty }))
2079+
}
20722080
ty::Adt(def, args) => {
20732081
let did = def.did();
20742082
let kind = match def.adt_kind() {
@@ -2566,6 +2574,21 @@ fn clean_bare_fn_ty<'tcx>(
25662574
BareFunctionDecl { safety: bare_fn.safety, abi: bare_fn.abi, decl, generic_params }
25672575
}
25682576

2577+
fn clean_unsafe_binder_ty<'tcx>(
2578+
unsafe_binder_ty: &hir::UnsafeBinderTy<'tcx>,
2579+
cx: &mut DocContext<'tcx>,
2580+
) -> UnsafeBinderTy {
2581+
// NOTE: generics must be cleaned before args
2582+
let generic_params = unsafe_binder_ty
2583+
.generic_params
2584+
.iter()
2585+
.filter(|p| !is_elided_lifetime(p))
2586+
.map(|x| clean_generic_param(cx, None, x))
2587+
.collect();
2588+
let ty = clean_ty(unsafe_binder_ty.inner_ty, cx);
2589+
UnsafeBinderTy { generic_params, ty }
2590+
}
2591+
25692592
pub(crate) fn reexport_chain(
25702593
tcx: TyCtxt<'_>,
25712594
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;
@@ -1507,6 +1507,8 @@ pub(crate) enum Type {
15071507

15081508
/// An `impl Trait`: `impl TraitA + TraitB + ...`
15091509
ImplTrait(Vec<GenericBound>),
1510+
1511+
UnsafeBinder(Box<UnsafeBinderTy>),
15101512
}
15111513

15121514
impl Type {
@@ -1699,7 +1701,7 @@ impl Type {
16991701
Type::Pat(..) => PrimitiveType::Pat,
17001702
RawPointer(..) => PrimitiveType::RawPointer,
17011703
QPath(box QPathData { ref self_type, .. }) => return self_type.def_id(cache),
1702-
Generic(_) | SelfTy | Infer | ImplTrait(_) => return None,
1704+
Generic(_) | SelfTy | Infer | ImplTrait(_) | UnsafeBinder(_) => return None,
17031705
};
17041706
Primitive(t).def_id(cache)
17051707
}
@@ -2339,6 +2341,12 @@ pub(crate) struct BareFunctionDecl {
23392341
pub(crate) abi: Abi,
23402342
}
23412343

2344+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
2345+
pub(crate) struct UnsafeBinderTy {
2346+
pub(crate) generic_params: Vec<GenericParamDef>,
2347+
pub(crate) ty: Type,
2348+
}
2349+
23422350
#[derive(Clone, Debug)]
23432351
pub(crate) struct Static {
23442352
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
@@ -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/tools/rustfmt/src/types.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,25 @@ impl Rewrite for ast::Ty {
10091009
let pat = pat.rewrite_result(context, shape)?;
10101010
Ok(format!("{ty} is {pat}"))
10111011
}
1012+
ast::TyKind::UnsafeBinder(ref binder) => {
1013+
let mut result = String::new()
1014+
;
1015+
if let Some(ref lifetime_str) = rewrite_bound_params(context, shape, &binder.generic_params) {
1016+
result.push_str("unsafe<");
1017+
result.push_str(lifetime_str);
1018+
result.push_str("> ");
1019+
}
1020+
1021+
let inner_ty_shape = if context.use_block_indent() {
1022+
shape.offset_left(result.len())?
1023+
} else {
1024+
shape.visual_indent(result.len()).sub_width(result.len())?
1025+
};
1026+
1027+
let rewrite = binder.inner_ty.rewrite(context, inner_ty_shape)?;
1028+
result.push_str(&rewrite);
1029+
Some(result)
1030+
}
10121031
}
10131032
}
10141033
}

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)