Skip to content

Commit 6f109d8

Browse files
committed
Auto merge of #155223 - teor2345:fndef-refactor, r=mati865
Refactor FnDecl and FnSig non-type fields into a new wrapper type #### Why this Refactor? This PR is part of an initial cleanup for the [arg splat experiment](#153629), but it's a useful refactor by itself. It refactors the non-type fields of `FnDecl`, `FnSig`, and `FnHeader` into a new packed wrapper types, based on this comment in the `splat` experiment PR: #153697 (comment) It also refactors some common `FnSig` creation settings into their own methods. I did this instead of creating a struct with defaults. #### Relationship to `splat` Experiment I don't think we can use functional struct updates (`..default()`) to create `FnDecl` and `FnSig`, because we need the bit-packing for the `splat` experiment. Bit-packing will avoid breaking "type is small" assertions for commonly used types when `splat` is added. This PR packs these types: - ExternAbi: enum + `unwind` variants (38) -> 6 bits - ImplicitSelfKind: enum variants (5) -> 3 bits - lifetime_elision_allowed, safety, c_variadic: bool -> 1 bit #### Minor Changes Fixes some typos, and applies rustfmt to clippy files that got skipped somehow.
2 parents 0febdba + a70f37b commit 6f109d8

97 files changed

Lines changed: 758 additions & 544 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
@@ -4579,7 +4579,6 @@ name = "rustc_query_impl"
45794579
version = "0.0.0"
45804580
dependencies = [
45814581
"measureme",
4582-
"rustc_abi",
45834582
"rustc_data_structures",
45844583
"rustc_errors",
45854584
"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+
// FIXME(FnSigKind): when PartialEq is stably const, use it instead
135+
const fn internal_const_eq(&self, other: &Self) -> bool {
136+
match (self, other) {
137+
$( ( $e_name::$variant $( { unwind: $uw } )* , $e_name::$variant $( { unwind: $uw } )* ) => true,)*
138+
_ => false,
139+
}
140+
}
141+
// ALL_VARIANTS.iter().position(|v| v == self), but const
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
@@ -755,9 +755,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
755755
let fn_decl = self.arena.alloc(hir::FnDecl {
756756
inputs,
757757
output,
758-
c_variadic: false,
759-
implicit_self: hir::ImplicitSelfKind::None,
760-
lifetime_elision_allowed: false,
758+
fn_decl_kind: hir::FnDeclFlags::default(),
761759
});
762760

763761
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
@@ -1849,7 +1849,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
18491849
// as they are not explicit in HIR/Ty function signatures.
18501850
// (instead, the `c_variadic` flag is set to `true`)
18511851
let mut inputs = &decl.inputs[..];
1852-
if c_variadic {
1852+
if decl.c_variadic() {
18531853
inputs = &inputs[..inputs.len() - 1];
18541854
}
18551855
let inputs = self.arena.alloc_from_iter(inputs.iter().map(|param| {
@@ -1912,12 +1912,8 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
19121912
},
19131913
};
19141914

1915-
self.arena.alloc(hir::FnDecl {
1916-
inputs,
1917-
output,
1918-
c_variadic,
1919-
lifetime_elision_allowed: self.resolver.lifetime_elision_allowed(fn_node_id),
1920-
implicit_self: decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
1915+
let fn_decl_kind = hir::FnDeclFlags::default()
1916+
.set_implicit_self(decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
19211917
let is_mutable_pat = matches!(
19221918
arg.pat.kind,
19231919
PatKind::Ident(hir::BindingMode(_, Mutability::Mut), ..)
@@ -1939,8 +1935,11 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
19391935
}
19401936
_ => hir::ImplicitSelfKind::None,
19411937
}
1942-
}),
1943-
})
1938+
}))
1939+
.set_lifetime_elision_allowed(self.resolver.lifetime_elision_allowed(fn_node_id))
1940+
.set_c_variadic(c_variadic);
1941+
1942+
self.arena.alloc(hir::FnDecl { inputs, output, fn_decl_kind })
19441943
}
19451944

19461945
// 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;
@@ -1083,14 +1084,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
10831084
}
10841085

10851086
// Build a new closure where the return type is an owned value, instead of a ref.
1087+
let fn_sig_kind =
1088+
FnSigKind::default().set_safe(true).set_c_variadic(liberated_sig.c_variadic());
10861089
let closure_sig_as_fn_ptr_ty = Ty::new_fn_ptr(
10871090
tcx,
10881091
ty::Binder::dummy(tcx.mk_fn_sig(
10891092
liberated_sig.inputs().iter().copied(),
10901093
peeled_ty,
1091-
liberated_sig.c_variadic,
1092-
hir::Safety::Safe,
1093-
rustc_abi::ExternAbi::Rust,
1094+
fn_sig_kind,
10941095
)),
10951096
);
10961097
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)