Skip to content

Commit 2a9e358

Browse files
Lower AST and resolve lifetimes for unsafe binder types
1 parent 3b1adfa commit 2a9e358

File tree

9 files changed

+98
-0
lines changed

9 files changed

+98
-0
lines changed

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+7
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))),

Diff for: compiler/rustc_hir/src/hir.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2780,6 +2780,12 @@ pub struct BareFnTy<'hir> {
27802780
pub param_names: &'hir [Ident],
27812781
}
27822782

2783+
#[derive(Debug, Clone, Copy, HashStable_Generic)]
2784+
pub struct UnsafeBinderTy<'hir> {
2785+
pub generic_params: &'hir [GenericParam<'hir>],
2786+
pub inner_ty: &'hir Ty<'hir>,
2787+
}
2788+
27832789
#[derive(Debug, Clone, Copy, HashStable_Generic)]
27842790
pub struct OpaqueTy<'hir> {
27852791
pub hir_id: HirId,
@@ -2878,6 +2884,8 @@ pub enum TyKind<'hir> {
28782884
Ref(&'hir Lifetime, MutTy<'hir>),
28792885
/// A bare function (e.g., `fn(usize) -> bool`).
28802886
BareFn(&'hir BareFnTy<'hir>),
2887+
/// Uwu
2888+
UnsafeBinder(&'hir UnsafeBinderTy<'hir>),
28812889
/// The never type (`!`).
28822890
Never,
28832891
/// A tuple (`(A, B, C, D, ...)`).

Diff for: compiler/rustc_hir/src/intravisit.rs

+4
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,10 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) -> V::Resul
886886
walk_list!(visitor, visit_generic_param, function_declaration.generic_params);
887887
try_visit!(visitor.visit_fn_decl(function_declaration.decl));
888888
}
889+
TyKind::UnsafeBinder(ref unsafe_binder) => {
890+
walk_list!(visitor, visit_generic_param, unsafe_binder.generic_params);
891+
try_visit!(visitor.visit_ty(unsafe_binder.inner_ty));
892+
}
889893
TyKind::Path(ref qpath) => {
890894
try_visit!(visitor.visit_qpath(qpath, typ.hir_id, typ.span));
891895
}

Diff for: compiler/rustc_hir_analysis/src/collect/generics_of.rs

+6
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,12 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<S
470470
self.outer_index.shift_out(1);
471471
res
472472
}
473+
hir::TyKind::UnsafeBinder(_) => {
474+
self.outer_index.shift_in(1);
475+
let res = intravisit::walk_ty(self, ty);
476+
self.outer_index.shift_out(1);
477+
res
478+
}
473479
_ => intravisit::walk_ty(self, ty),
474480
}
475481
}

Diff for: compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+30
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,36 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
781781
intravisit::walk_ty(this, ty);
782782
});
783783
}
784+
hir::TyKind::UnsafeBinder(binder) => {
785+
let (mut bound_vars, binders): (FxIndexMap<LocalDefId, ResolvedArg>, Vec<_>) =
786+
binder
787+
.generic_params
788+
.iter()
789+
.enumerate()
790+
.map(|(late_bound_idx, param)| {
791+
(
792+
(param.def_id, ResolvedArg::late(late_bound_idx as u32, param)),
793+
late_arg_as_bound_arg(self.tcx, param),
794+
)
795+
})
796+
.unzip();
797+
798+
deny_non_region_late_bound(self.tcx, &mut bound_vars, "function pointer types");
799+
800+
self.record_late_bound_vars(ty.hir_id, binders);
801+
let scope = Scope::Binder {
802+
hir_id: ty.hir_id,
803+
bound_vars,
804+
s: self.scope,
805+
scope_type: BinderScopeType::Normal,
806+
where_bound_origin: None,
807+
};
808+
self.with(scope, |this| {
809+
// a bare fn has no bounds, so everything
810+
// contained within is scoped within its binder.
811+
intravisit::walk_ty(this, ty);
812+
});
813+
}
784814
hir::TyKind::TraitObject(bounds, lifetime, _) => {
785815
debug!(?bounds, ?lifetime, "TraitObject");
786816
let scope = Scope::TraitRefBoundary { s: self.scope };

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2312,6 +2312,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23122312
self.lower_fn_ty(hir_ty.hir_id, bf.safety, bf.abi, bf.decl, None, Some(hir_ty)),
23132313
)
23142314
}
2315+
hir::TyKind::UnsafeBinder(_binder) => {
2316+
let guar = self
2317+
.dcx()
2318+
.struct_span_err(hir_ty.span, "unsafe binders are not yet implemented")
2319+
.emit();
2320+
Ty::new_error(tcx, guar)
2321+
}
23152322
hir::TyKind::TraitObject(bounds, lifetime, repr) => {
23162323
if let Some(guar) = self.prohibit_or_lint_bare_trait_object_ty(hir_ty) {
23172324
// Don't continue with type analysis if the `dyn` keyword is missing

Diff for: compiler/rustc_hir_pretty/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ impl<'a> State<'a> {
288288
hir::TyKind::BareFn(f) => {
289289
self.print_ty_fn(f.abi, f.safety, f.decl, None, f.generic_params, f.param_names);
290290
}
291+
hir::TyKind::UnsafeBinder(unsafe_binder) => {
292+
self.print_unsafe_binder(unsafe_binder);
293+
}
291294
hir::TyKind::OpaqueDef(..) => self.word("/*impl Trait*/"),
292295
hir::TyKind::Path(ref qpath) => self.print_qpath(qpath, false),
293296
hir::TyKind::TraitObject(bounds, lifetime, syntax) => {
@@ -339,6 +342,15 @@ impl<'a> State<'a> {
339342
self.end()
340343
}
341344

345+
fn print_unsafe_binder(&mut self, unsafe_binder: &hir::UnsafeBinderTy<'_>) {
346+
self.ibox(INDENT_UNIT);
347+
self.word("unsafe");
348+
self.print_generic_params(unsafe_binder.generic_params);
349+
self.nbsp();
350+
self.print_type(unsafe_binder.inner_ty);
351+
self.end();
352+
}
353+
342354
fn print_foreign_item(&mut self, item: &hir::ForeignItem<'_>) {
343355
self.hardbreak_if_not_bol();
344356
self.maybe_print_comment(item.span.lo());

Diff for: compiler/rustc_passes/src/input_stats.rs

+2
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
335335
Ptr,
336336
Ref,
337337
BareFn,
338+
UnsafeBinder,
338339
Never,
339340
Tup,
340341
Path,
@@ -585,6 +586,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
585586
Ref,
586587
PinnedRef,
587588
BareFn,
589+
UnsafeBinder,
588590
Never,
589591
Tup,
590592
Path,

Diff for: compiler/rustc_resolve/src/late.rs

+22
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,28 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
887887
},
888888
)
889889
}
890+
TyKind::UnsafeBinder(unsafe_binder) => {
891+
// FIXME(unsafe_binder): Better span
892+
let span = ty.span;
893+
self.with_generic_param_rib(
894+
&unsafe_binder.generic_params,
895+
RibKind::Normal,
896+
LifetimeRibKind::Generics {
897+
binder: ty.id,
898+
kind: LifetimeBinderKind::BareFnType,
899+
span,
900+
},
901+
|this| {
902+
this.visit_generic_params(&unsafe_binder.generic_params, false);
903+
this.with_lifetime_rib(
904+
// We don't allow anonymous `unsafe &'_ ()` binders,
905+
// although I guess we could.
906+
LifetimeRibKind::AnonymousReportError,
907+
|this| this.visit_ty(&unsafe_binder.inner_ty),
908+
);
909+
},
910+
)
911+
}
890912
TyKind::Array(element_ty, length) => {
891913
self.visit_ty(element_ty);
892914
self.resolve_anon_const(length, AnonConstKind::ConstArg(IsRepeatExpr::No));

0 commit comments

Comments
 (0)