Skip to content

Commit b58b721

Browse files
committed
Auto merge of #51321 - zackmdavis:hiridification_generations, r=eddyb
HirId-ification, continued Another incremental step towards the vision of #50928 (previously: #50929). r? @michaelwoerister
2 parents 4faaf7e + f23d90a commit b58b721

File tree

8 files changed

+85
-74
lines changed

8 files changed

+85
-74
lines changed

src/librustc/middle/expr_use_visitor.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
319319
let fn_body_scope_r =
320320
self.tcx().mk_region(ty::ReScope(region::Scope::Node(body.value.hir_id.local_id)));
321321
let arg_cmt = Rc::new(self.mc.cat_rvalue(
322-
arg.id,
322+
arg.hir_id,
323323
arg.pat.span,
324324
fn_body_scope_r, // Args live only as long as the fn body.
325325
arg_ty));
@@ -860,7 +860,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
860860
// Each match binding is effectively an assignment to the
861861
// binding being produced.
862862
let def = Def::Local(canonical_id);
863-
if let Ok(ref binding_cmt) = mc.cat_def(pat.id, pat.span, pat_ty, def) {
863+
if let Ok(ref binding_cmt) = mc.cat_def(pat.hir_id, pat.span, pat_ty, def) {
864864
delegate.mutate(pat.id, pat.span, binding_cmt, MutateMode::Init);
865865
}
866866

@@ -923,7 +923,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
923923
closure_expr_id: closure_def_id.to_local(),
924924
};
925925
let upvar_capture = self.mc.tables.upvar_capture(upvar_id);
926-
let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.id,
926+
let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.hir_id,
927927
fn_decl_span,
928928
freevar));
929929
match upvar_capture {
@@ -948,15 +948,15 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
948948
}
949949

950950
fn cat_captured_var(&mut self,
951-
closure_id: ast::NodeId,
951+
closure_hir_id: hir::HirId,
952952
closure_span: Span,
953953
upvar: &hir::Freevar)
954954
-> mc::McResult<mc::cmt_<'tcx>> {
955955
// Create the cmt for the variable being borrowed, from the
956956
// caller's perspective
957957
let var_hir_id = self.tcx().hir.node_to_hir_id(upvar.var_id());
958958
let var_ty = self.mc.node_ty(var_hir_id)?;
959-
self.mc.cat_def(closure_id, closure_span, var_ty, upvar.def)
959+
self.mc.cat_def(closure_hir_id, closure_span, var_ty, upvar.def)
960960
}
961961
}
962962

src/librustc/middle/mem_categorization.rs

+57-52
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,21 @@ pub enum Note {
179179
// and how it is located, as well as the mutability of the memory in
180180
// which the value is stored.
181181
//
182-
// *WARNING* The field `cmt.ty` is NOT necessarily the same as the
183-
// result of `node_id_to_type(cmt.id)`. This is because the `id` is
184-
// always the `id` of the node producing the type; in an expression
185-
// like `*x`, the type of this deref node is the deref'd type (`T`),
186-
// but in a pattern like `@x`, the `@x` pattern is again a
187-
// dereference, but its type is the type *before* the dereference
188-
// (`@T`). So use `cmt.ty` to find the type of the value in a consistent
189-
// fashion. For more details, see the method `cat_pattern`
182+
// *WARNING* The field `cmt.type` is NOT necessarily the same as the
183+
// result of `node_id_to_type(cmt.id)`.
184+
//
185+
// (FIXME: rewrite the following comment given that `@x` managed
186+
// pointers have been obsolete for quite some time.)
187+
//
188+
// This is because the `id` is always the `id` of the node producing the
189+
// type; in an expression like `*x`, the type of this deref node is the
190+
// deref'd type (`T`), but in a pattern like `@x`, the `@x` pattern is
191+
// again a dereference, but its type is the type *before* the
192+
// dereference (`@T`). So use `cmt.ty` to find the type of the value in
193+
// a consistent fashion. For more details, see the method `cat_pattern`
190194
#[derive(Clone, Debug, PartialEq)]
191195
pub struct cmt_<'tcx> {
192-
pub id: ast::NodeId, // id of expr/pat producing this value
196+
pub hir_id: hir::HirId, // HIR id of expr/pat producing this value
193197
pub span: Span, // span of same expr/pat
194198
pub cat: Categorization<'tcx>, // categorization of expr
195199
pub mutbl: MutabilityCategory, // mutability of expr as place
@@ -271,18 +275,18 @@ impl<'tcx> cmt_<'tcx> {
271275
}
272276
}
273277

274-
pub trait ast_node {
275-
fn id(&self) -> ast::NodeId;
278+
pub trait HirNode {
279+
fn hir_id(&self) -> hir::HirId;
276280
fn span(&self) -> Span;
277281
}
278282

279-
impl ast_node for hir::Expr {
280-
fn id(&self) -> ast::NodeId { self.id }
283+
impl HirNode for hir::Expr {
284+
fn hir_id(&self) -> hir::HirId { self.hir_id }
281285
fn span(&self) -> Span { self.span }
282286
}
283287

284-
impl ast_node for hir::Pat {
285-
fn id(&self) -> ast::NodeId { self.id }
288+
impl HirNode for hir::Pat {
289+
fn hir_id(&self) -> hir::HirId { self.hir_id }
286290
fn span(&self) -> Span { self.span }
287291
}
288292

@@ -610,7 +614,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
610614
ty: target,
611615
mutbl: deref.mutbl,
612616
});
613-
self.cat_rvalue_node(expr.id, expr.span, ref_ty)
617+
self.cat_rvalue_node(expr.hir_id, expr.span, ref_ty)
614618
} else {
615619
previous()?
616620
});
@@ -625,7 +629,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
625629
adjustment::Adjust::Borrow(_) |
626630
adjustment::Adjust::Unsize => {
627631
// Result is an rvalue.
628-
Ok(self.cat_rvalue_node(expr.id, expr.span, target))
632+
Ok(self.cat_rvalue_node(expr.hir_id, expr.span, target))
629633
}
630634
}
631635
}
@@ -669,8 +673,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
669673
}
670674

671675
hir::ExprPath(ref qpath) => {
672-
let def = self.tables.qpath_def(qpath, expr.hir_id);
673-
self.cat_def(expr.id, expr.span, expr_ty, def)
676+
let def = self.tables.qpath_def(qpath, expr.hir_id);
677+
self.cat_def(expr.hir_id, expr.span, expr_ty, def)
674678
}
675679

676680
hir::ExprType(ref e, _) => {
@@ -688,35 +692,35 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
688692
hir::ExprLit(..) | hir::ExprBreak(..) |
689693
hir::ExprContinue(..) | hir::ExprStruct(..) | hir::ExprRepeat(..) |
690694
hir::ExprInlineAsm(..) | hir::ExprBox(..) => {
691-
Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty))
695+
Ok(self.cat_rvalue_node(expr.hir_id, expr.span, expr_ty))
692696
}
693697
}
694698
}
695699

696700
pub fn cat_def(&self,
697-
id: ast::NodeId,
701+
hir_id: hir::HirId,
698702
span: Span,
699703
expr_ty: Ty<'tcx>,
700704
def: Def)
701705
-> McResult<cmt_<'tcx>> {
702-
debug!("cat_def: id={} expr={:?} def={:?}",
703-
id, expr_ty, def);
706+
debug!("cat_def: id={:?} expr={:?} def={:?}",
707+
hir_id, expr_ty, def);
704708

705709
match def {
706710
Def::StructCtor(..) | Def::VariantCtor(..) | Def::Const(..) |
707711
Def::AssociatedConst(..) | Def::Fn(..) | Def::Method(..) => {
708-
Ok(self.cat_rvalue_node(id, span, expr_ty))
712+
Ok(self.cat_rvalue_node(hir_id, span, expr_ty))
709713
}
710714

711715
Def::Static(def_id, mutbl) => {
712716
// `#[thread_local]` statics may not outlive the current function.
713717
for attr in &self.tcx.get_attrs(def_id)[..] {
714718
if attr.check_name("thread_local") {
715-
return Ok(self.cat_rvalue_node(id, span, expr_ty));
719+
return Ok(self.cat_rvalue_node(hir_id, span, expr_ty));
716720
}
717721
}
718722
Ok(cmt_ {
719-
id:id,
723+
hir_id,
720724
span:span,
721725
cat:Categorization::StaticItem,
722726
mutbl: if mutbl { McDeclared } else { McImmutable},
@@ -726,12 +730,12 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
726730
}
727731

728732
Def::Upvar(var_id, _, fn_node_id) => {
729-
self.cat_upvar(id, span, var_id, fn_node_id)
733+
self.cat_upvar(hir_id, span, var_id, fn_node_id)
730734
}
731735

732736
Def::Local(vid) => {
733737
Ok(cmt_ {
734-
id,
738+
hir_id,
735739
span,
736740
cat: Categorization::Local(vid),
737741
mutbl: MutabilityCategory::from_local(self.tcx, self.tables, vid),
@@ -747,7 +751,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
747751
// Categorize an upvar, complete with invisible derefs of closure
748752
// environment and upvar reference as appropriate.
749753
fn cat_upvar(&self,
750-
id: ast::NodeId,
754+
hir_id: hir::HirId,
751755
span: Span,
752756
var_id: ast::NodeId,
753757
fn_node_id: ast::NodeId)
@@ -814,7 +818,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
814818
// from the environment (perhaps we should eventually desugar
815819
// this field further, but it will do for now).
816820
let cmt_result = cmt_ {
817-
id,
821+
hir_id,
818822
span,
819823
cat: Categorization::Upvar(Upvar {id: upvar_id, kind: kind}),
820824
mutbl: var_mutbl,
@@ -830,10 +834,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
830834
cmt_result
831835
}
832836
ty::ClosureKind::FnMut => {
833-
self.env_deref(id, span, upvar_id, var_mutbl, ty::MutBorrow, cmt_result)
837+
self.env_deref(hir_id, span, upvar_id, var_mutbl, ty::MutBorrow, cmt_result)
834838
}
835839
ty::ClosureKind::Fn => {
836-
self.env_deref(id, span, upvar_id, var_mutbl, ty::ImmBorrow, cmt_result)
840+
self.env_deref(hir_id, span, upvar_id, var_mutbl, ty::ImmBorrow, cmt_result)
837841
}
838842
};
839843

@@ -848,7 +852,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
848852
ty::UpvarCapture::ByRef(upvar_borrow) => {
849853
let ptr = BorrowedPtr(upvar_borrow.kind, upvar_borrow.region);
850854
cmt_ {
851-
id,
855+
hir_id,
852856
span,
853857
cat: Categorization::Deref(Rc::new(cmt_result), ptr),
854858
mutbl: MutabilityCategory::from_borrow_kind(upvar_borrow.kind),
@@ -864,7 +868,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
864868
}
865869

866870
fn env_deref(&self,
867-
id: ast::NodeId,
871+
hir_id: hir::HirId,
868872
span: Span,
869873
upvar_id: ty::UpvarId,
870874
upvar_mutbl: MutabilityCategory,
@@ -908,7 +912,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
908912
}
909913

910914
let ret = cmt_ {
911-
id,
915+
hir_id,
912916
span,
913917
cat: Categorization::Deref(Rc::new(cmt_result), env_ptr),
914918
mutbl: deref_mutbl,
@@ -932,17 +936,16 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
932936
}
933937

934938
pub fn cat_rvalue_node(&self,
935-
id: ast::NodeId,
939+
hir_id: hir::HirId,
936940
span: Span,
937941
expr_ty: Ty<'tcx>)
938942
-> cmt_<'tcx> {
939943
debug!(
940944
"cat_rvalue_node(id={:?}, span={:?}, expr_ty={:?})",
941-
id,
945+
hir_id,
942946
span,
943947
expr_ty,
944948
);
945-
let hir_id = self.tcx.hir.node_to_hir_id(id);
946949
let promotable = self.rvalue_promotable_map.as_ref().map(|m| m.contains(&hir_id.local_id))
947950
.unwrap_or(false);
948951

@@ -970,18 +973,18 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
970973
} else {
971974
self.temporary_scope(hir_id.local_id)
972975
};
973-
let ret = self.cat_rvalue(id, span, re, expr_ty);
976+
let ret = self.cat_rvalue(hir_id, span, re, expr_ty);
974977
debug!("cat_rvalue_node ret {:?}", ret);
975978
ret
976979
}
977980

978981
pub fn cat_rvalue(&self,
979-
cmt_id: ast::NodeId,
982+
cmt_hir_id: hir::HirId,
980983
span: Span,
981984
temp_scope: ty::Region<'tcx>,
982985
expr_ty: Ty<'tcx>) -> cmt_<'tcx> {
983986
let ret = cmt_ {
984-
id:cmt_id,
987+
hir_id: cmt_hir_id,
985988
span:span,
986989
cat:Categorization::Rvalue(temp_scope),
987990
mutbl:McDeclared,
@@ -992,15 +995,15 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
992995
ret
993996
}
994997

995-
pub fn cat_field<N:ast_node>(&self,
998+
pub fn cat_field<N: HirNode>(&self,
996999
node: &N,
9971000
base_cmt: cmt<'tcx>,
9981001
f_index: usize,
9991002
f_ident: ast::Ident,
10001003
f_ty: Ty<'tcx>)
10011004
-> cmt_<'tcx> {
10021005
let ret = cmt_ {
1003-
id: node.id(),
1006+
hir_id: node.hir_id(),
10041007
span: node.span(),
10051008
mutbl: base_cmt.mutbl.inherit(),
10061009
cat: Categorization::Interior(base_cmt,
@@ -1042,13 +1045,13 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
10421045
mutbl,
10431046
});
10441047

1045-
let base_cmt = Rc::new(self.cat_rvalue_node(expr.id, expr.span, ref_ty));
1048+
let base_cmt = Rc::new(self.cat_rvalue_node(expr.hir_id, expr.span, ref_ty));
10461049
self.cat_deref(expr, base_cmt, note)
10471050
}
10481051

10491052
pub fn cat_deref(
10501053
&self,
1051-
node: &impl ast_node,
1054+
node: &impl HirNode,
10521055
base_cmt: cmt<'tcx>,
10531056
note: Note,
10541057
) -> McResult<cmt_<'tcx>> {
@@ -1074,7 +1077,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
10741077
ref ty => bug!("unexpected type in cat_deref: {:?}", ty)
10751078
};
10761079
let ret = cmt_ {
1077-
id: node.id(),
1080+
hir_id: node.hir_id(),
10781081
span: node.span(),
10791082
// For unique ptrs, we inherit mutability from the owning reference.
10801083
mutbl: MutabilityCategory::from_pointer_kind(base_cmt.mutbl, ptr),
@@ -1086,7 +1089,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
10861089
Ok(ret)
10871090
}
10881091

1089-
fn cat_index<N:ast_node>(&self,
1092+
fn cat_index<N: HirNode>(&self,
10901093
elt: &N,
10911094
base_cmt: cmt<'tcx>,
10921095
element_ty: Ty<'tcx>,
@@ -1106,7 +1109,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
11061109
//! presuming that `base_cmt` is not of fixed-length type.
11071110
//!
11081111
//! # Parameters
1109-
//! - `elt`: the AST node being indexed
1112+
//! - `elt`: the HIR node being indexed
11101113
//! - `base_cmt`: the cmt of `elt`
11111114
11121115
let interior_elem = InteriorElement(context);
@@ -1115,14 +1118,14 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
11151118
return Ok(ret);
11161119
}
11171120

1118-
pub fn cat_imm_interior<N:ast_node>(&self,
1121+
pub fn cat_imm_interior<N:HirNode>(&self,
11191122
node: &N,
11201123
base_cmt: cmt<'tcx>,
11211124
interior_ty: Ty<'tcx>,
11221125
interior: InteriorKind)
11231126
-> cmt_<'tcx> {
11241127
let ret = cmt_ {
1125-
id: node.id(),
1128+
hir_id: node.hir_id(),
11261129
span: node.span(),
11271130
mutbl: base_cmt.mutbl.inherit(),
11281131
cat: Categorization::Interior(base_cmt, interior),
@@ -1133,7 +1136,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
11331136
ret
11341137
}
11351138

1136-
pub fn cat_downcast_if_needed<N:ast_node>(&self,
1139+
pub fn cat_downcast_if_needed<N:HirNode>(&self,
11371140
node: &N,
11381141
base_cmt: cmt<'tcx>,
11391142
variant_did: DefId)
@@ -1143,7 +1146,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
11431146
if self.tcx.adt_def(base_did).variants.len() != 1 {
11441147
let base_ty = base_cmt.ty;
11451148
let ret = Rc::new(cmt_ {
1146-
id: node.id(),
1149+
hir_id: node.hir_id(),
11471150
span: node.span(),
11481151
mutbl: base_cmt.mutbl.inherit(),
11491152
cat: Categorization::Downcast(base_cmt, variant_did),
@@ -1193,6 +1196,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
11931196
// value, and I consider them to produce the value that was
11941197
// matched. So if you have something like:
11951198
//
1199+
// (FIXME: `@@3` is not legal code anymore!)
1200+
//
11961201
// let x = @@3;
11971202
// match x {
11981203
// @@y { ... }

0 commit comments

Comments
 (0)