Skip to content

Commit dafb6bb

Browse files
committed
Refactor FnDecl and FnSig flags into packed structs
1 parent 0a742de commit dafb6bb

96 files changed

Lines changed: 752 additions & 538 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4578,7 +4578,6 @@ name = "rustc_query_impl"
45784578
version = "0.0.0"
45794579
dependencies = [
45804580
"measureme",
4581-
"rustc_abi",
45824581
"rustc_data_structures",
45834582
"rustc_errors",
45844583
"rustc_hir",

compiler/rustc_abi/src/extern_abi.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,29 @@ macro_rules! abi_impls {
131131
$($e_name::$variant $( { unwind: $uw } )* => $tok,)*
132132
}
133133
}
134+
// ALL_VARIANTS.iter().position(|v| v == self), but const
135+
// FIXME(FnSigKind): when PartialEq is stably const, use it instead
136+
const fn internal_const_eq(&self, other: &Self) -> bool {
137+
match (self, other) {
138+
$( ( $e_name::$variant $( { unwind: $uw } )* , $e_name::$variant $( { unwind: $uw } )* ) => true,)*
139+
_ => false,
140+
}
141+
}
142+
pub const fn as_packed(&self) -> u8 {
143+
let mut index = 0;
144+
while index < $e_name::ALL_VARIANTS.len() {
145+
if self.internal_const_eq(&$e_name::ALL_VARIANTS[index]) {
146+
return index as u8;
147+
}
148+
index += 1;
149+
}
150+
panic!("unreachable: invalid ExternAbi variant");
151+
}
152+
pub const fn from_packed(index: u8) -> Self {
153+
let index = index as usize;
154+
assert!(index < $e_name::ALL_VARIANTS.len(), "invalid ExternAbi index");
155+
$e_name::ALL_VARIANTS[index]
156+
}
134157
}
135158

136159
impl ::core::str::FromStr for $e_name {

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ use rustc_ast as ast;
4646
use rustc_ast::*;
4747
use rustc_data_structures::fx::FxHashSet;
4848
use rustc_errors::ErrorGuaranteed;
49-
use rustc_hir as hir;
5049
use rustc_hir::attrs::{AttributeKind, InlineAttr};
5150
use rustc_hir::def_id::DefId;
51+
use rustc_hir::{self as hir, FnDeclFlags};
5252
use rustc_middle::span_bug;
5353
use rustc_middle::ty::Asyncness;
5454
use rustc_span::symbol::kw;
@@ -271,7 +271,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
271271
// Function parameter count, including C variadic `...` if present.
272272
fn param_count(&self, def_id: DefId) -> (usize, bool /*c_variadic*/) {
273273
let sig = self.tcx.fn_sig(def_id).skip_binder().skip_binder();
274-
(sig.inputs().len() + usize::from(sig.c_variadic), sig.c_variadic)
274+
(sig.inputs().len() + usize::from(sig.c_variadic()), sig.c_variadic())
275275
}
276276

277277
fn lower_delegation_decl(
@@ -309,9 +309,9 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
309309
self.arena.alloc(hir::FnDecl {
310310
inputs,
311311
output: hir::FnRetTy::Return(output),
312-
c_variadic,
313-
lifetime_elision_allowed: true,
314-
implicit_self: hir::ImplicitSelfKind::None,
312+
fn_decl_kind: FnDeclFlags::default()
313+
.set_lifetime_elision_allowed(true)
314+
.set_c_variadic(c_variadic),
315315
})
316316
}
317317

@@ -331,11 +331,11 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
331331
safety: if self.tcx.codegen_fn_attrs(sig_id).safe_target_features {
332332
hir::HeaderSafety::SafeTargetFeatures
333333
} else {
334-
hir::HeaderSafety::Normal(sig.safety)
334+
hir::HeaderSafety::Normal(sig.safety())
335335
},
336336
constness: self.tcx.constness(sig_id),
337337
asyncness,
338-
abi: sig.abi,
338+
abi: sig.abi(),
339339
};
340340

341341
hir::FnSig { decl, header, span }
@@ -603,13 +603,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
603603
span: Span,
604604
delegation: &Delegation,
605605
) -> DelegationResults<'hir> {
606-
let decl = self.arena.alloc(hir::FnDecl {
607-
inputs: &[],
608-
output: hir::FnRetTy::DefaultReturn(span),
609-
c_variadic: false,
610-
lifetime_elision_allowed: true,
611-
implicit_self: hir::ImplicitSelfKind::None,
612-
});
606+
let decl = self.arena.alloc(hir::FnDecl::dummy(span));
613607

614608
let header = self.generate_header_error();
615609
let sig = hir::FnSig { decl, header, span };

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
762762
let fn_decl = self.arena.alloc(hir::FnDecl {
763763
inputs,
764764
output,
765-
c_variadic: false,
766-
implicit_self: hir::ImplicitSelfKind::None,
767-
lifetime_elision_allowed: false,
765+
fn_decl_kind: hir::FnDeclFlags::default(),
768766
});
769767

770768
let body = self.lower_body(move |this| {

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,7 +1831,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
18311831
// as they are not explicit in HIR/Ty function signatures.
18321832
// (instead, the `c_variadic` flag is set to `true`)
18331833
let mut inputs = &decl.inputs[..];
1834-
if c_variadic {
1834+
if decl.c_variadic() {
18351835
inputs = &inputs[..inputs.len() - 1];
18361836
}
18371837
let inputs = self.arena.alloc_from_iter(inputs.iter().map(|param| {
@@ -1894,12 +1894,8 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
18941894
},
18951895
};
18961896

1897-
self.arena.alloc(hir::FnDecl {
1898-
inputs,
1899-
output,
1900-
c_variadic,
1901-
lifetime_elision_allowed: self.resolver.lifetime_elision_allowed(fn_node_id),
1902-
implicit_self: decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
1897+
let fn_decl_kind = hir::FnDeclFlags::default()
1898+
.set_implicit_self(decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
19031899
let is_mutable_pat = matches!(
19041900
arg.pat.kind,
19051901
PatKind::Ident(hir::BindingMode(_, Mutability::Mut), ..)
@@ -1921,8 +1917,11 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
19211917
}
19221918
_ => hir::ImplicitSelfKind::None,
19231919
}
1924-
}),
1925-
})
1920+
}))
1921+
.set_lifetime_elision_allowed(self.resolver.lifetime_elision_allowed(fn_node_id))
1922+
.set_c_variadic(c_variadic);
1923+
1924+
self.arena.alloc(hir::FnDecl { inputs, output, fn_decl_kind })
19261925
}
19271926

19281927
// Transforms `-> T` for `async fn` into `-> OpaqueTy { .. }`

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
493493
for (_, node) in self.infcx.tcx.hir_parent_iter(upvar_hir_id) {
494494
if let Some(fn_decl) = node.fn_decl() {
495495
if !matches!(
496-
fn_decl.implicit_self,
496+
fn_decl.implicit_self(),
497497
hir::ImplicitSelfKind::RefImm | hir::ImplicitSelfKind::RefMut
498498
) {
499499
err.span_suggestion_verbose(
@@ -810,7 +810,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
810810
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
811811
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
812812
&& let hir::Mutability::Not = mut_ty.mutbl
813-
&& sig.decl.implicit_self.has_implicit_self()
813+
&& sig.decl.implicit_self().has_implicit_self()
814814
{
815815
Some(ty.span)
816816
} else {
@@ -1147,7 +1147,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11471147
arg_pos
11481148
.and_then(|pos| {
11491149
sig.decl.inputs.get(
1150-
pos + if sig.decl.implicit_self.has_implicit_self() {
1150+
pos + if sig.decl.implicit_self().has_implicit_self() {
11511151
1
11521152
} else {
11531153
0

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use rustc_middle::bug;
1515
use rustc_middle::hir::place::PlaceBase;
1616
use rustc_middle::mir::{AnnotationSource, ConstraintCategory, ReturnConstraint};
1717
use rustc_middle::ty::{
18-
self, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitor, fold_regions,
18+
self, FnSigKind, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitor,
19+
fold_regions,
1920
};
2021
use rustc_span::{Ident, Span, kw};
2122
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
@@ -1081,14 +1082,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
10811082
}
10821083

10831084
// Build a new closure where the return type is an owned value, instead of a ref.
1085+
let fn_sig_kind =
1086+
FnSigKind::default().set_safe(true).set_c_variadic(liberated_sig.c_variadic());
10841087
let closure_sig_as_fn_ptr_ty = Ty::new_fn_ptr(
10851088
tcx,
10861089
ty::Binder::dummy(tcx.mk_fn_sig(
10871090
liberated_sig.inputs().iter().copied(),
10881091
peeled_ty,
1089-
liberated_sig.c_variadic,
1090-
hir::Safety::Safe,
1091-
rustc_abi::ExternAbi::Rust,
1092+
fn_sig_kind,
10921093
)),
10931094
);
10941095
let closure_ty = Ty::new_closure(

compiler/rustc_borrowck/src/type_check/input_output.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
103103
user_provided_sig = self.tcx().mk_fn_sig(
104104
user_provided_sig.inputs().iter().copied(),
105105
output_ty,
106-
user_provided_sig.c_variadic,
107-
user_provided_sig.safety,
108-
user_provided_sig.abi,
106+
user_provided_sig.fn_sig_kind,
109107
);
110108
}
111109

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10151015
if let ty::FnDef(def_id, _) = *src_ty.kind()
10161016
&& let ty::FnPtr(_, target_hdr) = *ty.kind()
10171017
&& tcx.codegen_fn_attrs(def_id).safe_target_features
1018-
&& target_hdr.safety.is_safe()
1018+
&& target_hdr.safety().is_safe()
10191019
&& let Some(safe_sig) = tcx.adjust_target_feature_sig(
10201020
def_id,
10211021
src_sig,
@@ -1971,7 +1971,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19711971
term_location: Location,
19721972
call_source: CallSource,
19731973
) {
1974-
if args.len() < sig.inputs().len() || (args.len() > sig.inputs().len() && !sig.c_variadic) {
1974+
if args.len() < sig.inputs().len() || (args.len() > sig.inputs().len() && !sig.c_variadic())
1975+
{
19751976
span_mirbug!(self, term, "call to {:?} with wrong # of args", sig);
19761977
}
19771978

compiler/rustc_codegen_cranelift/src/value_and_place.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -870,20 +870,10 @@ pub(crate) fn assert_assignable<'tcx>(
870870
let from_sig = fx
871871
.tcx
872872
.normalize_erasing_late_bound_regions(fx.typing_env(), from_ty.fn_sig(fx.tcx));
873-
let FnSig {
874-
inputs_and_output: types_from,
875-
c_variadic: c_variadic_from,
876-
safety: unsafety_from,
877-
abi: abi_from,
878-
} = from_sig;
873+
let FnSig { inputs_and_output: types_from, fn_sig_kind: fn_sig_kind_from } = from_sig;
879874
let to_sig =
880875
fx.tcx.normalize_erasing_late_bound_regions(fx.typing_env(), to_ty.fn_sig(fx.tcx));
881-
let FnSig {
882-
inputs_and_output: types_to,
883-
c_variadic: c_variadic_to,
884-
safety: unsafety_to,
885-
abi: abi_to,
886-
} = to_sig;
876+
let FnSig { inputs_and_output: types_to, fn_sig_kind: fn_sig_kind_to } = to_sig;
887877
let mut types_from = types_from.iter();
888878
let mut types_to = types_to.iter();
889879
loop {
@@ -894,17 +884,7 @@ pub(crate) fn assert_assignable<'tcx>(
894884
}
895885
}
896886
assert_eq!(
897-
c_variadic_from, c_variadic_to,
898-
"Can't write fn ptr with incompatible sig {:?} to place with sig {:?}\n\n{:#?}",
899-
from_sig, to_sig, fx,
900-
);
901-
assert_eq!(
902-
unsafety_from, unsafety_to,
903-
"Can't write fn ptr with incompatible sig {:?} to place with sig {:?}\n\n{:#?}",
904-
from_sig, to_sig, fx,
905-
);
906-
assert_eq!(
907-
abi_from, abi_to,
887+
fn_sig_kind_from, fn_sig_kind_to,
908888
"Can't write fn ptr with incompatible sig {:?} to place with sig {:?}\n\n{:#?}",
909889
from_sig, to_sig, fx,
910890
);

0 commit comments

Comments
 (0)