-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update ty::VariantDef
to use IndexVec<FieldIdx, FieldDef>
#109762
Conversation
(rustbot has picked a reviewer for you, use r? to override) |
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt Some changes occurred in compiler/rustc_codegen_cranelift cc @bjorn3 Some changes occurred to the CTFE / Miri engine cc @rust-lang/miri Some changes occurred to the core trait solver cc @rust-lang/initiative-trait-system-refactor |
ty::Adt(def, substs) => def.variant(FIRST_VARIANT).fields | ||
[FieldIdx::from_usize(i)] | ||
.ty(tcx, substs), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems like it might be a wonky formatting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but this is what rustfmt did -- I didn't do any manual reformattings, just ran x.py fmt
to make tidy happy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe I'll do some shadowing for these cases to try to get something less weird...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes rustfmt just does literally no formatting, for example when there are if-let-chains, maybe this is the case here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, this is definitely rustfmt
doing it -- all I did was add the FieldIdx::from_usize
inline, without adding any newlines. Then tidy made me change it to that weirdness 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r=me with formatting fixed (@fbstj's comment) and minor comments addressed.
I think one interesting thing for future work may be to search for FieldIdx::from_usize
and FieldIdx::from_u32
(and maybe ::new
and .index
) and see if they can be removed via changing the caller.
@@ -2238,7 +2237,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||
self.apply_adjustments(base, adjustments); | |||
self.register_predicates(autoderef.into_obligations()); | |||
|
|||
self.write_field_index(expr.hir_id, index); | |||
self.write_field_index(expr.hir_id, FieldIdx::from_usize(index)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes me wonder why do we need such cruel string manipulation few lines above O_o
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it's because field
is an Ident
. So I guess the fstr == index.to_string()
is basically just checking that it's not tup.0001
? (It has an as_u32
, but that's the index in the string interner, so would be very, very wrong here.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aha, found a little tweak that should be clearer and avoid the String
for every .0
, .1
, through .9
:
field.name == sym::integer(index)
TyMaybeWithLayout::Ty(def.variant(index).fields[i].ty(tcx, substs)) | ||
} | ||
Variants::Single { index } => TyMaybeWithLayout::Ty( | ||
def.variant(index).fields[FieldIdx::new(i)].ty(tcx, substs), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this use ::new
instead of ::from_usize
?
def.variant(index).fields[FieldIdx::new(i)].ty(tcx, substs), | |
def.variant(index).fields[FieldIdx::from_usize(i)].ty(tcx, substs), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because that file already had VariantIdx::new
, apparently. I'll change it since I do like the concrete one better.
(It's kinda annoying that new
(from Idx
), from_usize
and .into()
(from newtype_index!
) all exist and work so there's nothing pushing code to consistency.)
let unique_ty = adt.non_enum_variant().fields[FieldIdx::new(0)].ty(self.tcx(), substs); | ||
let nonnull_ty = unique_ty.ty_adt_def().unwrap().non_enum_variant().fields | ||
[FieldIdx::new(0)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let unique_ty = adt.non_enum_variant().fields[FieldIdx::new(0)].ty(self.tcx(), substs); | |
let nonnull_ty = unique_ty.ty_adt_def().unwrap().non_enum_variant().fields | |
[FieldIdx::new(0)] | |
let unique_ty = adt.non_enum_variant().fields[FieldIdx::from_u32(0)].ty(self.tcx(), substs); | |
let nonnull_ty = unique_ty.ty_adt_def().unwrap().non_enum_variant().fields | |
[FieldIdx::from_u32(0)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also []
on the new line looks a bit weird, is this rustfmt's formatting? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, this is rustfmt's choice 🤷
@@ -1725,7 +1725,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { | |||
AggregateKind::Adt(adt_did, variant_index, substs, _, active_field_index) => { | |||
let def = tcx.adt_def(adt_did); | |||
let variant = &def.variant(variant_index); | |||
let adj_field_index = active_field_index.unwrap_or(field_index); | |||
let adj_field_index = | |||
FieldIdx::from_usize(active_field_index.unwrap_or(field_index)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the kind of place that will hopefully be temporary -- changing AggregateKind::Adt
to hold a FieldIdx
and the function to use field_index: FieldIdx
instead of field_index: usize
should get rid of this in an upcoming PR in this series, but I needed to stop the tendrils somewhere 🙃
And while doing the updates for that, also uses `FieldIdx` in `ProjectionKind::Field` and `TypeckResults::field_indices`. There's more places that could use it (like `rustc_const_eval` and `LayoutS`), but I tried to keep this PR from exploding to *even more* places. Part 2/? of rust-lang/compiler-team#606
b123407
to
4abb455
Compare
Added some extra |
☀️ Test successful - checks-actions |
Finished benchmarking commit (eb3e9c1): comparison URL. Overall result: ✅ improvements - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)This benchmark run did not return any relevant results for this metric. CyclesThis benchmark run did not return any relevant results for this metric. |
And while doing the updates for that, also uses
FieldIdx
inProjectionKind::Field
andTypeckResults::field_indices
.There's more places that could use it (like
rustc_const_eval
andLayoutS
), but I tried to keep this PR from exploding to even more places.Part 2/? of rust-lang/compiler-team#606