Skip to content

Commit

Permalink
Fix tools
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Sep 18, 2024
1 parent 285ab06 commit 3aa766a
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 9 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_cranelift/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,9 @@ pub(crate) fn codegen_place<'tcx>(
cplace = cplace.place_deref(fx);
}
PlaceElem::OpaqueCast(ty) => bug!("encountered OpaqueCast({ty}) in codegen"),
PlaceElem::Subtype(ty) => cplace = cplace.place_transmute_type(fx, fx.monomorphize(ty)),
PlaceElem::Subtype(ty) | PlaceElem::UnsafeBinderCast(_, ty) => {
cplace = cplace.place_transmute_type(fx, fx.monomorphize(ty));
}
PlaceElem::Field(field, _ty) => {
cplace = cplace.place_field(fx, field);
}
Expand Down
23 changes: 23 additions & 0 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,9 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
DynTrait(bounds, lifetime)
}
TyKind::BareFn(barefn) => BareFunction(Box::new(clean_bare_fn_ty(barefn, cx))),
TyKind::UnsafeBinder(unsafe_binder_ty) => {
UnsafeBinder(Box::new(clean_unsafe_binder_ty(unsafe_binder_ty, cx)))
}
// Rustdoc handles `TyKind::Err`s by turning them into `Type::Infer`s.
TyKind::Infer | TyKind::Err(_) | TyKind::Typeof(..) | TyKind::InferDelegation(..) => Infer,
TyKind::AnonAdt(..) => {
Expand Down Expand Up @@ -2080,6 +2083,11 @@ pub(crate) fn clean_middle_ty<'tcx>(
abi: sig.abi(),
}))
}
ty::UnsafeBinder(inner) => {
let generic_params = clean_bound_vars(inner.bound_vars());
let ty = clean_middle_ty(inner.into(), cx, None, None);
UnsafeBinder(Box::new(UnsafeBinderTy { generic_params, ty }))
}
ty::Adt(def, args) => {
let did = def.did();
let kind = match def.adt_kind() {
Expand Down Expand Up @@ -2577,6 +2585,21 @@ fn clean_bare_fn_ty<'tcx>(
BareFunctionDecl { safety: bare_fn.safety, abi: bare_fn.abi, decl, generic_params }
}

fn clean_unsafe_binder_ty<'tcx>(
unsafe_binder_ty: &hir::UnsafeBinderTy<'tcx>,
cx: &mut DocContext<'tcx>,
) -> UnsafeBinderTy {
// NOTE: generics must be cleaned before args
let generic_params = unsafe_binder_ty
.generic_params
.iter()
.filter(|p| !is_elided_lifetime(p))
.map(|x| clean_generic_param(cx, None, x))
.collect();
let ty = clean_ty(unsafe_binder_ty.inner_ty, cx);
UnsafeBinderTy { generic_params, ty }
}

pub(crate) fn reexport_chain(
tcx: TyCtxt<'_>,
import_def_id: LocalDefId,
Expand Down
12 changes: 10 additions & 2 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use {rustc_ast as ast, rustc_hir as hir};
pub(crate) use self::ItemKind::*;
pub(crate) use self::Type::{
Array, BareFunction, BorrowedRef, DynTrait, Generic, ImplTrait, Infer, Primitive, QPath,
RawPointer, SelfTy, Slice, Tuple,
RawPointer, SelfTy, Slice, Tuple, UnsafeBinder,
};
use crate::clean::cfg::Cfg;
use crate::clean::clean_middle_path;
Expand Down Expand Up @@ -1503,6 +1503,8 @@ pub(crate) enum Type {

/// An `impl Trait`: `impl TraitA + TraitB + ...`
ImplTrait(Vec<GenericBound>),

UnsafeBinder(Box<UnsafeBinderTy>),
}

impl Type {
Expand Down Expand Up @@ -1695,7 +1697,7 @@ impl Type {
Type::Pat(..) => PrimitiveType::Pat,
RawPointer(..) => PrimitiveType::RawPointer,
QPath(box QPathData { ref self_type, .. }) => return self_type.def_id(cache),
Generic(_) | SelfTy | Infer | ImplTrait(_) => return None,
Generic(_) | SelfTy | Infer | ImplTrait(_) | UnsafeBinder(_) => return None,
};
Primitive(t).def_id(cache)
}
Expand Down Expand Up @@ -2335,6 +2337,12 @@ pub(crate) struct BareFunctionDecl {
pub(crate) abi: Abi,
}

#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub(crate) struct UnsafeBinderTy {
pub(crate) generic_params: Vec<GenericParamDef>,
pub(crate) ty: Type,
}

#[derive(Clone, Debug)]
pub(crate) struct Static {
pub(crate) type_: Box<Type>,
Expand Down
5 changes: 5 additions & 0 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,11 @@ fn fmt_type(
}
decl.decl.print(cx).fmt(f)
}
clean::UnsafeBinder(ref binder) => {
// FIXME(unsafe_binders): This should print `unsafe<...>`
print_higher_ranked_params_with_space(&binder.generic_params, cx).fmt(f)?;
binder.ty.print(cx).fmt(f)
}
clean::Tuple(ref typs) => match &typs[..] {
&[] => primitive_link(f, PrimitiveType::Unit, format_args!("()"), cx),
[one] => {
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/html/render/search_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,8 @@ fn get_index_type_id(
| clean::Generic(_)
| clean::SelfTy
| clean::ImplTrait(_)
| clean::Infer => None,
| clean::Infer
| clean::UnsafeBinder(_) => None,
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ impl FromWithTcx<clean::Type> for Type {
fn from_tcx(ty: clean::Type, tcx: TyCtxt<'_>) -> Self {
use clean::Type::{
Array, BareFunction, BorrowedRef, Generic, ImplTrait, Infer, Primitive, QPath,
RawPointer, SelfTy, Slice, Tuple,
RawPointer, SelfTy, Slice, Tuple, UnsafeBinder,
};

match ty {
Expand Down Expand Up @@ -614,6 +614,8 @@ impl FromWithTcx<clean::Type> for Type {
self_type: Box::new(self_type.into_tcx(tcx)),
trait_: trait_.map(|trait_| trait_.into_tcx(tcx)),
},
// FIXME(unsafe_binder): Implement rustdoc-json.
UnsafeBinder(_) => todo!(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
| ty::CoroutineClosure(..)
| ty::Coroutine(..)
| ty::CoroutineWitness(..)
| ty::UnsafeBinder(..)
| ty::Dynamic(..)
| ty::Param(_)
| ty::Bound(..)
Expand Down
6 changes: 4 additions & 2 deletions src/tools/clippy/clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,8 @@ impl TyCoercionStability {
| TyKind::Pat(..)
| TyKind::Never
| TyKind::Tup(_)
| TyKind::Path(_) => Self::Deref,
| TyKind::Path(_)
| TyKind::UnsafeBinder(..) => Self::Deref,
TyKind::OpaqueDef(..)
| TyKind::Infer
| TyKind::Typeof(..)
Expand Down Expand Up @@ -884,7 +885,8 @@ impl TyCoercionStability {
| ty::CoroutineClosure(..)
| ty::Never
| ty::Tuple(_)
| ty::Alias(ty::Projection, _) => Self::Deref,
| ty::Alias(ty::Projection, _)
| ty::UnsafeBinder(_) => Self::Deref,
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/clippy/clippy_lints/src/loops/never_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ fn never_loop_expr<'tcx>(
ExprKind::Unary(_, e)
| ExprKind::Cast(e, _)
| ExprKind::Type(e, _)
| ExprKind::UnsafeBinderCast(_, e, _)
| ExprKind::Field(e, _)
| ExprKind::AddrOf(_, _, e)
| ExprKind::Repeat(e, _)
Expand Down
4 changes: 4 additions & 0 deletions src/tools/clippy/clippy_lints/src/utils/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,10 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
kind!("DropTemps({expr})");
self.expr(expr);
},
ExprKind::UnsafeBinderCast(..) => {
// FIXME(unsafe_binders): Implement this.
todo!()
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/tools/clippy/clippy_utils/src/eager_or_lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS
| ExprKind::Lit(_)
| ExprKind::Cast(..)
| ExprKind::Type(..)
| ExprKind::UnsafeBinderCast(..)
| ExprKind::DropTemps(_)
| ExprKind::Let(..)
| ExprKind::If(..)
Expand Down
16 changes: 16 additions & 0 deletions src/tools/clippy/clippy_utils/src/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ impl HirEqInterExpr<'_, '_, '_> {
},
(&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup),
(&ExprKind::Type(le, lt), &ExprKind::Type(re, rt)) => self.eq_expr(le, re) && self.eq_ty(lt, rt),
(&ExprKind::UnsafeBinderCast(lkind, le, None), &ExprKind::UnsafeBinderCast(rkind, re, None)) =>
lkind == rkind && self.eq_expr(le, re),
(&ExprKind::UnsafeBinderCast(lkind, le, Some(lt)), &ExprKind::UnsafeBinderCast(rkind, re, Some(rt))) =>
lkind == rkind && self.eq_expr(le, re) && self.eq_ty(lt, rt),
(&ExprKind::Unary(l_op, le), &ExprKind::Unary(r_op, re)) => l_op == r_op && self.eq_expr(le, re),
(&ExprKind::Yield(le, _), &ExprKind::Yield(re, _)) => return self.eq_expr(le, re),
(
Expand Down Expand Up @@ -381,6 +385,7 @@ impl HirEqInterExpr<'_, '_, '_> {
| &ExprKind::Type(..)
| &ExprKind::Unary(..)
| &ExprKind::Yield(..)
| &ExprKind::UnsafeBinderCast(..)

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

Expand Down Expand Up @@ -928,6 +933,13 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
std::mem::discriminant(&lop).hash(&mut self.s);
self.hash_expr(le);
},
ExprKind::UnsafeBinderCast(kind, e, t) => {
std::mem::discriminant(&kind).hash(&mut self.s);
self.hash_expr(e);
if let Some(t) = t {
self.hash_ty(t);
}
}
ExprKind::Err(_) => {},
}
}
Expand Down Expand Up @@ -1125,6 +1137,10 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
TyKind::Typeof(anon_const) => {
self.hash_body(anon_const.body);
},
TyKind::UnsafeBinder(binder) => {
// FIXME(unsafe_binder): Hash generics...
self.hash_ty(binder.inner_ty);
}
TyKind::Err(_) | TyKind::Infer | TyKind::Never | TyKind::InferDelegation(..) | TyKind::AnonAdt(_) => {},
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ fn check_place<'tcx>(tcx: TyCtxt<'tcx>, place: Place<'tcx>, span: Span, body: &B
| ProjectionElem::Downcast(..)
| ProjectionElem::Subslice { .. }
| ProjectionElem::Subtype(_)
| ProjectionElem::Index(_) => {},
| ProjectionElem::Index(_)
| ProjectionElem::UnsafeBinderCast(..) => {},
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/tools/clippy/clippy_utils/src/sugg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl<'a> Sugg<'a> {
| ExprKind::Become(..)
| ExprKind::Struct(..)
| ExprKind::Tup(..)
| ExprKind::UnsafeBinderCast(..)
| ExprKind::Err(_) => Sugg::NonParen(get_snippet(expr.span)),
ExprKind::DropTemps(inner) => Self::hir_from_snippet(inner, get_snippet),
ExprKind::Assign(lhs, rhs, _) => {
Expand Down Expand Up @@ -223,6 +224,7 @@ impl<'a> Sugg<'a> {
| ast::ExprKind::Array(..)
| ast::ExprKind::While(..)
| ast::ExprKind::Await(..)
| ast::ExprKind::UnsafeBinderCast(..)
| ast::ExprKind::Err(_)
| ast::ExprKind::Dummy => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0),
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp(
Expand Down
3 changes: 3 additions & 0 deletions src/tools/clippy/clippy_utils/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,9 @@ pub fn for_each_unconsumed_temporary<'tcx, B>(
ExprKind::Type(e, _) => {
helper(typeck, consume, e, f)?;
},
ExprKind::UnsafeBinderCast(_, e, _) => {
helper(typeck, consume, e, f)?;
},

// Either drops temporaries, jumps out of the current expression, or has no sub expression.
ExprKind::DropTemps(_)
Expand Down
3 changes: 2 additions & 1 deletion src/tools/rustfmt/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ pub(crate) fn format_expr(
ast::ExprKind::FormatArgs(..)
| ast::ExprKind::Type(..)
| ast::ExprKind::IncludedBytes(..)
| ast::ExprKind::OffsetOf(..) => {
| ast::ExprKind::OffsetOf(..)
| ast::ExprKind::UnsafeBinderCast(..) => {
// These don't normally occur in the AST because macros aren't expanded. However,
// rustfmt tries to parse macro arguments when formatting macros, so it's not totally
// impossible for rustfmt to come across one of these nodes when formatting a file.
Expand Down
19 changes: 19 additions & 0 deletions src/tools/rustfmt/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,25 @@ impl Rewrite for ast::Ty {
let pat = pat.rewrite(context, shape)?;
Some(format!("{ty} is {pat}"))
}
ast::TyKind::UnsafeBinder(ref binder) => {
let mut result = String::new()
;
if let Some(ref lifetime_str) = rewrite_bound_params(context, shape, &binder.generic_params) {
result.push_str("unsafe<");
result.push_str(lifetime_str);
result.push_str("> ");
}

let inner_ty_shape = if context.use_block_indent() {
shape.offset_left(result.len())?
} else {
shape.visual_indent(result.len()).sub_width(result.len())?
};

let rewrite = binder.inner_ty.rewrite(context, inner_ty_shape)?;
result.push_str(&rewrite);
Some(result)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/rustfmt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
| ast::ExprKind::Await(..)
| ast::ExprKind::Break(..)
| ast::ExprKind::Cast(..)
| ast::ExprKind::UnsafeBinderCast(..)
| ast::ExprKind::Continue(..)
| ast::ExprKind::Dummy
| ast::ExprKind::Err(_)
Expand Down

0 comments on commit 3aa766a

Please sign in to comment.