Skip to content

Commit

Permalink
Auto merge of #58079 - ljedrz:HirIdification_phase_1, r=Zoxc
Browse files Browse the repository at this point in the history
hir: add HirId to main Hir nodes

This is the 1st PR in a series dedicated to `HirId`-ification, i.e. deprecating `ast::NodeId`s after the AST > HIR lowering process. The bigger proof of concept can be seen in #57578.

**Phase 1**: store `HirId` in all remaining (some already have it) main HIR nodes (excluding `*Id` objects).

- [x] `Field`
- [x] `FieldPat`
- [x] `ForeignItem`
- [x] `GenericParam`
- [x] `Lifetime`
- [x] `MacroDef`
- [x] `PathSegment`
- [x] `PatKind::Binding`
- [x] `Stmt`
- [x] `StructField`
- [x] `TypeBinding`
- [x] `VariantData`
- [x] `WhereClause`
- [x] `WhereEqPredicate`

r? @Zoxc
Cc @varkor
  • Loading branch information
bors committed Feb 3, 2019
2 parents 8a57831 + 55ef78e commit ec7ecb3
Show file tree
Hide file tree
Showing 24 changed files with 257 additions and 114 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ fn is_c_like_enum(item: &hir::Item) -> bool {
if let hir::ItemKind::Enum(ref def, _) = item.node {
for variant in &def.variants {
match variant.node.data {
hir::VariantData::Unit(_) => { /* continue */ }
hir::VariantData::Unit(..) => { /* continue */ }
_ => { return false; }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
PatKind::Ref(ref subpattern, _) => {
visitor.visit_pat(subpattern)
}
PatKind::Binding(_, canonical_id, ident, ref optional_subpattern) => {
PatKind::Binding(_, canonical_id, _hir_id, ident, ref optional_subpattern) => {
visitor.visit_def_mention(Def::Local(canonical_id));
visitor.visit_ident(ident);
walk_list!(visitor, visit_pat, optional_subpattern);
Expand Down
257 changes: 179 additions & 78 deletions src/librustc/hir/lowering.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ impl<'hir> Map<'hir> {
Node::Field(f) => f.ident.name,
Node::Lifetime(lt) => lt.name.ident().name,
Node::GenericParam(param) => param.name.ident().name,
Node::Binding(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.name,
Node::Binding(&Pat { node: PatKind::Binding(_, _, _, l, _), .. }) => l.name,
Node::StructCtor(_) => self.name(self.get_parent(id)),
_ => bug!("no name for {}", self.node_to_string(id))
}
Expand Down
37 changes: 31 additions & 6 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;
#[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
pub struct Lifetime {
pub id: NodeId,
pub hir_id: HirId,
pub span: Span,

/// Either "'a", referring to a named lifetime definition,
Expand Down Expand Up @@ -321,6 +322,7 @@ pub struct PathSegment {
// affected. (In general, we don't bother to get the defs for synthesized
// segments, only for segments which have come from the AST).
pub id: Option<NodeId>,
pub hir_id: Option<HirId>,
pub def: Option<Def>,

/// Type/lifetime parameters attached to this path. They come in
Expand All @@ -343,6 +345,7 @@ impl PathSegment {
PathSegment {
ident,
id: None,
hir_id: None,
def: None,
infer_types: true,
args: None,
Expand All @@ -352,13 +355,15 @@ impl PathSegment {
pub fn new(
ident: Ident,
id: Option<NodeId>,
hir_id: Option<HirId>,
def: Option<Def>,
args: GenericArgs,
infer_types: bool,
) -> Self {
PathSegment {
ident,
id,
hir_id,
def,
infer_types,
args: if args.is_empty() {
Expand Down Expand Up @@ -528,6 +533,7 @@ pub enum GenericParamKind {
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct GenericParam {
pub id: NodeId,
pub hir_id: HirId,
pub name: ParamName,
pub attrs: HirVec<Attribute>,
pub bounds: GenericBounds,
Expand Down Expand Up @@ -558,6 +564,7 @@ impl Generics {
params: HirVec::new(),
where_clause: WhereClause {
id: DUMMY_NODE_ID,
hir_id: DUMMY_HIR_ID,
predicates: HirVec::new(),
},
span: DUMMY_SP,
Expand Down Expand Up @@ -601,6 +608,7 @@ pub enum SyntheticTyParamKind {
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct WhereClause {
pub id: NodeId,
pub hir_id: HirId,
pub predicates: HirVec<WherePredicate>,
}

Expand Down Expand Up @@ -661,6 +669,7 @@ pub struct WhereRegionPredicate {
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct WhereEqPredicate {
pub id: NodeId,
pub hir_id: HirId,
pub span: Span,
pub lhs_ty: P<Ty>,
pub rhs_ty: P<Ty>,
Expand Down Expand Up @@ -789,6 +798,7 @@ pub struct MacroDef {
pub vis: Visibility,
pub attrs: HirVec<Attribute>,
pub id: NodeId,
pub hir_id: HirId,
pub span: Span,
pub body: TokenStream,
pub legacy: bool,
Expand Down Expand Up @@ -878,6 +888,7 @@ impl Pat {
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct FieldPat {
pub id: NodeId,
pub hir_id: HirId,
/// The identifier for the field
pub ident: Ident,
/// The pattern the field is destructured to
Expand Down Expand Up @@ -924,7 +935,7 @@ pub enum PatKind {
/// The `NodeId` is the canonical ID for the variable being bound,
/// e.g., in `Ok(x) | Err(x)`, both `x` use the same canonical ID,
/// which is the pattern ID of the first `x`.
Binding(BindingAnnotation, NodeId, Ident, Option<P<Pat>>),
Binding(BindingAnnotation, NodeId, HirId, Ident, Option<P<Pat>>),

/// A struct or struct variant pattern, e.g., `Variant {x, y, ..}`.
/// The `bool` is `true` in the presence of a `..`.
Expand Down Expand Up @@ -1137,6 +1148,7 @@ impl UnOp {
#[derive(Clone, RustcEncodable, RustcDecodable)]
pub struct Stmt {
pub id: NodeId,
pub hir_id: HirId,
pub node: StmtKind,
pub span: Span,
}
Expand Down Expand Up @@ -1204,6 +1216,7 @@ pub enum Guard {
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct Field {
pub id: NodeId,
pub hir_id: HirId,
pub ident: Ident,
pub expr: P<Expr>,
pub span: Span,
Expand Down Expand Up @@ -1711,6 +1724,7 @@ pub enum ImplItemKind {
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct TypeBinding {
pub id: NodeId,
pub hir_id: HirId,
pub ident: Ident,
pub ty: P<Ty>,
pub span: Span,
Expand Down Expand Up @@ -2106,6 +2120,7 @@ pub struct StructField {
pub ident: Ident,
pub vis: Visibility,
pub id: NodeId,
pub hir_id: HirId,
pub ty: P<Ty>,
pub attrs: HirVec<Attribute>,
}
Expand All @@ -2131,21 +2146,30 @@ impl StructField {
/// Id of the whole struct lives in `Item`.
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum VariantData {
Struct(HirVec<StructField>, NodeId),
Tuple(HirVec<StructField>, NodeId),
Unit(NodeId),
Struct(HirVec<StructField>, NodeId, HirId),
Tuple(HirVec<StructField>, NodeId, HirId),
Unit(NodeId, HirId),
}

impl VariantData {
pub fn fields(&self) -> &[StructField] {
match *self {
VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => fields,
VariantData::Struct(ref fields, ..) | VariantData::Tuple(ref fields, ..) => fields,
_ => &[],
}
}
pub fn id(&self) -> NodeId {
match *self {
VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id,
VariantData::Struct(_, id, ..)
| VariantData::Tuple(_, id, ..)
| VariantData::Unit(id, ..) => id,
}
}
pub fn hir_id(&self) -> HirId {
match *self {
VariantData::Struct(_, _, hir_id)
| VariantData::Tuple(_, _, hir_id)
| VariantData::Unit(_, hir_id) => hir_id,
}
}
pub fn is_struct(&self) -> bool {
Expand Down Expand Up @@ -2343,6 +2367,7 @@ pub struct ForeignItem {
pub attrs: HirVec<Attribute>,
pub node: ForeignItemKind,
pub id: NodeId,
pub hir_id: HirId,
pub span: Span,
pub vis: Visibility,
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/pat_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl hir::Pat {
where F: FnMut(hir::BindingAnnotation, HirId, Span, ast::Ident),
{
self.walk(|p| {
if let PatKind::Binding(binding_mode, _, ident, _) = p.node {
if let PatKind::Binding(binding_mode, _, _, ident, _) = p.node {
f(binding_mode, p.hir_id, p.span, ident);
}
true
Expand Down Expand Up @@ -123,8 +123,8 @@ impl hir::Pat {

pub fn simple_ident(&self) -> Option<ast::Ident> {
match self.node {
PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, None) |
PatKind::Binding(hir::BindingAnnotation::Mutable, _, ident, None) => Some(ident),
PatKind::Binding(hir::BindingAnnotation::Unannotated, _, _, ident, None) |
PatKind::Binding(hir::BindingAnnotation::Mutable, _, _, ident, None) => Some(ident),
_ => None,
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ impl<'a> State<'a> {
// is that it doesn't matter
match pat.node {
PatKind::Wild => self.s.word("_")?,
PatKind::Binding(binding_mode, _, ident, ref sub) => {
PatKind::Binding(binding_mode, _, _, ident, ref sub) => {
match binding_mode {
hir::BindingAnnotation::Ref => {
self.word_nbsp("ref")?;
Expand Down Expand Up @@ -2246,6 +2246,7 @@ impl<'a> State<'a> {
params: hir::HirVec::new(),
where_clause: hir::WhereClause {
id: ast::DUMMY_NODE_ID,
hir_id: hir::DUMMY_HIR_ID,
predicates: hir::HirVec::new(),
},
span: syntax_pos::DUMMY_SP,
Expand Down
20 changes: 16 additions & 4 deletions src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ impl_stable_hash_for!(struct ast::Label {

impl_stable_hash_for!(struct hir::Lifetime {
id,
hir_id,
span,
name
});
Expand All @@ -172,6 +173,7 @@ impl_stable_hash_for!(struct hir::Path {
impl_stable_hash_for!(struct hir::PathSegment {
ident -> (ident.name),
id,
hir_id,
def,
infer_types,
args
Expand Down Expand Up @@ -200,6 +202,7 @@ impl_stable_hash_for!(enum hir::TraitBoundModifier {

impl_stable_hash_for!(struct hir::GenericParam {
id,
hir_id,
name,
pure_wrt_drop,
attrs,
Expand Down Expand Up @@ -244,6 +247,7 @@ impl_stable_hash_for!(enum hir::SyntheticTyParamKind {

impl_stable_hash_for!(struct hir::WhereClause {
id,
hir_id,
predicates
});

Expand All @@ -268,6 +272,7 @@ impl_stable_hash_for!(struct hir::WhereRegionPredicate {

impl_stable_hash_for!(struct hir::WhereEqPredicate {
id,
hir_id,
span,
lhs_ty,
rhs_ty
Expand All @@ -285,6 +290,7 @@ impl_stable_hash_for!(struct hir::MethodSig {

impl_stable_hash_for!(struct hir::TypeBinding {
id,
hir_id,
ident -> (ident.name),
ty,
span
Expand Down Expand Up @@ -397,6 +403,7 @@ impl_stable_hash_for!(struct hir::MacroDef {
vis,
attrs,
id,
hir_id,
span,
legacy,
body
Expand All @@ -423,6 +430,7 @@ impl_stable_hash_for_spanned!(hir::FieldPat);

impl_stable_hash_for!(struct hir::FieldPat {
id -> _,
hir_id -> _,
ident -> (ident.name),
pat,
is_shorthand,
Expand All @@ -442,7 +450,7 @@ impl_stable_hash_for!(enum hir::RangeEnd {

impl_stable_hash_for!(enum hir::PatKind {
Wild,
Binding(binding_mode, var, name, sub),
Binding(binding_mode, var, hir_id, name, sub),
Struct(path, field_pats, dotdot),
TupleStruct(path, field_pats, dotdot),
Path(path),
Expand Down Expand Up @@ -485,6 +493,7 @@ impl_stable_hash_for!(enum hir::UnOp {

impl_stable_hash_for!(struct hir::Stmt {
id,
hir_id,
node,
span,
});
Expand Down Expand Up @@ -514,6 +523,7 @@ impl_stable_hash_for!(enum hir::Guard {

impl_stable_hash_for!(struct hir::Field {
id -> _,
hir_id -> _,
ident,
expr,
span,
Expand Down Expand Up @@ -836,14 +846,15 @@ impl_stable_hash_for!(struct hir::StructField {
ident -> (ident.name),
vis,
id,
hir_id,
ty,
attrs
});

impl_stable_hash_for!(enum hir::VariantData {
Struct(fields, id),
Tuple(fields, id),
Unit(id)
Struct(fields, id, hir_id),
Tuple(fields, id, hir_id),
Unit(id, hir_id)
});

impl<'a> HashStable<StableHashingContext<'a>> for hir::Item {
Expand Down Expand Up @@ -929,6 +940,7 @@ impl_stable_hash_for!(struct hir::ForeignItem {
attrs,
node,
id,
hir_id,
span,
vis
});
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ fn add_from_pat<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, pat: &P<hir::Pat>) {
while let Some(pat) = pats.pop_front() {
use hir::PatKind::*;
match pat.node {
Binding(_, _, _, ref inner_pat) => {
Binding(_, _, _, _, ref inner_pat) => {
pats.extend(inner_pat.iter());
}
Struct(_, ref fields, _) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
Node::Variant(&hir::Variant {
span,
node: hir::VariantKind {
data: hir::VariantData::Tuple(ref fields, _),
data: hir::VariantData::Tuple(ref fields, ..),
..
},
..
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn gather_move_from_pat<'a, 'c, 'tcx: 'c>(bccx: &BorrowckCtxt<'a, 'tcx>,
cmt: &'c mc::cmt_<'tcx>) {
let source = get_pattern_source(bccx.tcx,move_pat);
let pat_span_path_opt = match move_pat.node {
PatKind::Binding(_, _, ident, _) => {
PatKind::Binding(_, _, _, ident, _) => {
Some(MovePlace {
span: move_pat.span,
name: ident.name,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
// (Issue #49588)
continue;
}
if let PatKind::Binding(_, _, ident, None) = fieldpat.node.pat.node {
if let PatKind::Binding(_, _, _, ident, None) = fieldpat.node.pat.node {
if cx.tcx.find_field_index(ident, &variant) ==
Some(cx.tcx.field_index(fieldpat.node.id, cx.tables)) {
let mut err = cx.struct_span_lint(NON_SHORTHAND_FIELD_PATTERNS,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/nonstandard_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
}

fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) {
if let &PatKind::Binding(_, _, ident, _) = &p.node {
if let &PatKind::Binding(_, _, _, ident, _) = &p.node {
self.check_snake_case(cx, "variable", &ident);
}
}
Expand Down
Loading

0 comments on commit ec7ecb3

Please sign in to comment.