drive-by: move field_index to typeck results
This commit is contained in:
parent
19c250aa12
commit
26b24cd755
8 changed files with 23 additions and 19 deletions
|
@ -540,9 +540,9 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
||||||
ty::Adt(adt, substs) if adt.is_struct() => {
|
ty::Adt(adt, substs) if adt.is_struct() => {
|
||||||
// Consume those fields of the with expression that are needed.
|
// Consume those fields of the with expression that are needed.
|
||||||
for (f_index, with_field) in adt.non_enum_variant().fields.iter().enumerate() {
|
for (f_index, with_field) in adt.non_enum_variant().fields.iter().enumerate() {
|
||||||
let is_mentioned = fields.iter().any(|f| {
|
let is_mentioned = fields
|
||||||
self.tcx().field_index(f.hir_id, self.mc.typeck_results) == f_index
|
.iter()
|
||||||
});
|
.any(|f| self.mc.typeck_results.field_index(f.hir_id) == f_index);
|
||||||
if !is_mentioned {
|
if !is_mentioned {
|
||||||
let field_place = self.mc.cat_projection(
|
let field_place = self.mc.cat_projection(
|
||||||
&*with_expr,
|
&*with_expr,
|
||||||
|
|
|
@ -259,7 +259,7 @@ impl<'tcx> LateLintPass<'tcx> for NonShorthandFieldPatterns {
|
||||||
}
|
}
|
||||||
if let PatKind::Binding(binding_annot, _, ident, None) = fieldpat.pat.kind {
|
if let PatKind::Binding(binding_annot, _, ident, None) = fieldpat.pat.kind {
|
||||||
if cx.tcx.find_field_index(ident, &variant)
|
if cx.tcx.find_field_index(ident, &variant)
|
||||||
== Some(cx.tcx.field_index(fieldpat.hir_id, cx.typeck_results()))
|
== Some(cx.typeck_results().field_index(fieldpat.hir_id))
|
||||||
{
|
{
|
||||||
cx.struct_span_lint(
|
cx.struct_span_lint(
|
||||||
NON_SHORTHAND_FIELD_PATTERNS,
|
NON_SHORTHAND_FIELD_PATTERNS,
|
||||||
|
|
|
@ -671,6 +671,14 @@ impl<'tcx> TypeckResults<'tcx> {
|
||||||
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.field_indices }
|
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.field_indices }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn field_index(&self, id: hir::HirId) -> usize {
|
||||||
|
self.field_indices().get(id).cloned().expect("no index for a field")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn opt_field_index(&self, id: hir::HirId) -> Option<usize> {
|
||||||
|
self.field_indices().get(id).cloned()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn user_provided_types(&self) -> LocalTableInContext<'_, CanonicalUserType<'tcx>> {
|
pub fn user_provided_types(&self) -> LocalTableInContext<'_, CanonicalUserType<'tcx>> {
|
||||||
LocalTableInContext { hir_owner: self.hir_owner, data: &self.user_provided_types }
|
LocalTableInContext { hir_owner: self.hir_owner, data: &self.user_provided_types }
|
||||||
}
|
}
|
||||||
|
|
|
@ -2228,10 +2228,6 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn field_index(self, hir_id: hir::HirId, typeck_results: &TypeckResults<'_>) -> usize {
|
|
||||||
typeck_results.field_indices().get(hir_id).cloned().expect("no index for a field")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn find_field_index(self, ident: Ident, variant: &VariantDef) -> Option<usize> {
|
pub fn find_field_index(self, ident: Ident, variant: &VariantDef) -> Option<usize> {
|
||||||
variant
|
variant
|
||||||
.fields
|
.fields
|
||||||
|
|
|
@ -704,7 +704,7 @@ impl<'tcx> Cx<'tcx> {
|
||||||
hir::ExprKind::Field(ref source, ..) => ExprKind::Field {
|
hir::ExprKind::Field(ref source, ..) => ExprKind::Field {
|
||||||
lhs: self.mirror_expr(source),
|
lhs: self.mirror_expr(source),
|
||||||
variant_index: VariantIdx::new(0),
|
variant_index: VariantIdx::new(0),
|
||||||
name: Field::new(tcx.field_index(expr.hir_id, self.typeck_results)),
|
name: Field::new(self.typeck_results.field_index(expr.hir_id)),
|
||||||
},
|
},
|
||||||
hir::ExprKind::Cast(ref source, ref cast_ty) => {
|
hir::ExprKind::Cast(ref source, ref cast_ty) => {
|
||||||
// Check for a user-given type annotation on this `cast`
|
// Check for a user-given type annotation on this `cast`
|
||||||
|
@ -1079,7 +1079,7 @@ impl<'tcx> Cx<'tcx> {
|
||||||
fields
|
fields
|
||||||
.iter()
|
.iter()
|
||||||
.map(|field| FieldExpr {
|
.map(|field| FieldExpr {
|
||||||
name: Field::new(self.tcx.field_index(field.hir_id, self.typeck_results)),
|
name: Field::new(self.typeck_results.field_index(field.hir_id)),
|
||||||
expr: self.mirror_expr(field.expr),
|
expr: self.mirror_expr(field.expr),
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
|
|
|
@ -321,7 +321,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
|
||||||
let subpatterns = fields
|
let subpatterns = fields
|
||||||
.iter()
|
.iter()
|
||||||
.map(|field| FieldPat {
|
.map(|field| FieldPat {
|
||||||
field: Field::new(self.tcx.field_index(field.hir_id, self.typeck_results)),
|
field: Field::new(self.typeck_results.field_index(field.hir_id)),
|
||||||
pattern: self.lower_pattern(&field.pat),
|
pattern: self.lower_pattern(&field.pat),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
@ -124,7 +124,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
|
||||||
fn handle_field_access(&mut self, lhs: &hir::Expr<'_>, hir_id: hir::HirId) {
|
fn handle_field_access(&mut self, lhs: &hir::Expr<'_>, hir_id: hir::HirId) {
|
||||||
match self.typeck_results().expr_ty_adjusted(lhs).kind() {
|
match self.typeck_results().expr_ty_adjusted(lhs).kind() {
|
||||||
ty::Adt(def, _) => {
|
ty::Adt(def, _) => {
|
||||||
let index = self.tcx.field_index(hir_id, self.typeck_results());
|
let index = self.typeck_results().field_index(hir_id);
|
||||||
self.insert_def_id(def.non_enum_variant().fields[index].did);
|
self.insert_def_id(def.non_enum_variant().fields[index].did);
|
||||||
}
|
}
|
||||||
ty::Tuple(..) => {}
|
ty::Tuple(..) => {}
|
||||||
|
@ -208,7 +208,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
|
||||||
if let PatKind::Wild = pat.pat.kind {
|
if let PatKind::Wild = pat.pat.kind {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let index = self.tcx.field_index(pat.hir_id, self.typeck_results());
|
let index = self.typeck_results().field_index(pat.hir_id);
|
||||||
self.insert_def_id(variant.fields[index].did);
|
self.insert_def_id(variant.fields[index].did);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -341,7 +341,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
|
||||||
fn mark_as_used_if_union(&mut self, adt: ty::AdtDef<'tcx>, fields: &[hir::ExprField<'_>]) {
|
fn mark_as_used_if_union(&mut self, adt: ty::AdtDef<'tcx>, fields: &[hir::ExprField<'_>]) {
|
||||||
if adt.is_union() && adt.non_enum_variant().fields.len() > 1 && adt.did().is_local() {
|
if adt.is_union() && adt.non_enum_variant().fields.len() > 1 && adt.did().is_local() {
|
||||||
for field in fields {
|
for field in fields {
|
||||||
let index = self.tcx.field_index(field.hir_id, self.typeck_results());
|
let index = self.typeck_results().field_index(field.hir_id);
|
||||||
self.insert_def_id(adt.non_enum_variant().fields[index].did);
|
self.insert_def_id(adt.non_enum_variant().fields[index].did);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1065,9 +1065,9 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
|
||||||
// are checked for privacy (RFC 736). Rather than computing the set of
|
// are checked for privacy (RFC 736). Rather than computing the set of
|
||||||
// unmentioned fields, just check them all.
|
// unmentioned fields, just check them all.
|
||||||
for (vf_index, variant_field) in variant.fields.iter().enumerate() {
|
for (vf_index, variant_field) in variant.fields.iter().enumerate() {
|
||||||
let field = fields.iter().find(|f| {
|
let field = fields
|
||||||
self.tcx.field_index(f.hir_id, self.typeck_results()) == vf_index
|
.iter()
|
||||||
});
|
.find(|f| self.typeck_results().field_index(f.hir_id) == vf_index);
|
||||||
let (use_ctxt, span) = match field {
|
let (use_ctxt, span) = match field {
|
||||||
Some(field) => (field.ident.span, field.span),
|
Some(field) => (field.ident.span, field.span),
|
||||||
None => (base.span, base.span),
|
None => (base.span, base.span),
|
||||||
|
@ -1077,7 +1077,7 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
|
||||||
} else {
|
} else {
|
||||||
for field in fields {
|
for field in fields {
|
||||||
let use_ctxt = field.ident.span;
|
let use_ctxt = field.ident.span;
|
||||||
let index = self.tcx.field_index(field.hir_id, self.typeck_results());
|
let index = self.typeck_results().field_index(field.hir_id);
|
||||||
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
|
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1093,7 +1093,7 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
|
||||||
let variant = adt.variant_of_res(res);
|
let variant = adt.variant_of_res(res);
|
||||||
for field in fields {
|
for field in fields {
|
||||||
let use_ctxt = field.ident.span;
|
let use_ctxt = field.ident.span;
|
||||||
let index = self.tcx.field_index(field.hir_id, self.typeck_results());
|
let index = self.typeck_results().field_index(field.hir_id);
|
||||||
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
|
self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue