Skip to content

Use hir::Path instead of ast::Path in HIR Attribute's. #56480

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//! This order consistency is required in a few places in rustc, for
//! example generator inference, and possibly also HIR borrowck.

use syntax::ast::{NodeId, CRATE_NODE_ID, Ident, Name, Attribute};
use syntax::ast::{NodeId, CRATE_NODE_ID, Ident, Name};
use syntax_pos::Span;
use hir::*;
use hir::def::Def;
Expand Down
75 changes: 46 additions & 29 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ impl<'a> LoweringContext<'a> {
visit::walk_crate(&mut ItemLowerer { lctx: &mut self }, c);

let module = self.lower_mod(&c.module);
let attrs = self.lower_attrs(&c.attrs);
let attrs = self.lower_attrs(&c.attrs).into();
let body_ids = body_ids(&self.bodies);

self.resolver
Expand Down Expand Up @@ -1057,21 +1057,32 @@ impl<'a> LoweringContext<'a> {
}
}

fn lower_attrs(&mut self, attrs: &[Attribute]) -> hir::HirVec<Attribute> {
fn lower_attrs(&mut self, attrs: &[Attribute]) -> Vec<hir::Attribute> {
attrs
.iter()
.map(|a| self.lower_attr(a))
.collect()
}

fn lower_attr(&mut self, attr: &Attribute) -> Attribute {
// Note that we explicitly do not walk the path. Since we don't really
// lower attributes (we use the AST version) there is nowhere to keep
// the HirIds. We don't actually need HIR version of attributes anyway.
Attribute {
fn lower_attr(&mut self, attr: &Attribute) -> hir::Attribute {
hir::Attribute {
id: attr.id,
style: attr.style,
path: attr.path.clone(),
// HACK(eddyb) manual conversion because `lower_path(_extra)`
// use `lower_path_segment` and that allocates `ItemLocalId`s.
path: hir::Path {
def: Def::Err,
segments: attr.path.segments.iter().map(|segment| {
hir::PathSegment::new(
segment.ident,
None,
None,
hir::GenericArgs::none(),
false,
)
}).collect(),
span: attr.path.span,
},
tokens: self.lower_token_stream(attr.tokens.clone()),
is_sugared_doc: attr.is_sugared_doc,
span: attr.span,
Expand Down Expand Up @@ -1110,7 +1121,7 @@ impl<'a> LoweringContext<'a> {

fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {
hir::Arm {
attrs: self.lower_attrs(&arm.attrs),
attrs: self.lower_attrs(&arm.attrs).into(),
pats: arm.pats.iter().map(|x| self.lower_pat(x)).collect(),
guard: match arm.guard {
Some(Guard::If(ref x)) => Some(hir::Guard::If(P(self.lower_expr(x)))),
Expand Down Expand Up @@ -1576,7 +1587,7 @@ impl<'a> LoweringContext<'a> {
Spanned {
node: hir::VariantKind {
name: v.node.ident.name,
attrs: self.lower_attrs(&v.node.attrs),
attrs: self.lower_attrs(&v.node.attrs).into(),
data: self.lower_variant_data(&v.node.data),
disr_expr: v.node.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
},
Expand Down Expand Up @@ -1967,7 +1978,7 @@ impl<'a> LoweringContext<'a> {
pat: self.lower_pat(&l.pat),
init: l.init.as_ref().map(|e| P(self.lower_expr(e))),
span: l.span,
attrs: l.attrs.clone(),
attrs: self.lower_attrs(&l.attrs).into(),
source: hir::LocalSource::Normal,
}), ids)
}
Expand Down Expand Up @@ -2410,7 +2421,7 @@ impl<'a> LoweringContext<'a> {
name: param_name,
span: lt.span,
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
attrs: self.lower_attrs(&param.attrs),
attrs: self.lower_attrs(&param.attrs).into(),
bounds,
kind: hir::GenericParamKind::Lifetime {
kind: hir::LifetimeParamKind::Explicit,
Expand Down Expand Up @@ -2443,7 +2454,7 @@ impl<'a> LoweringContext<'a> {
id: self.lower_node_id(param.id).node_id,
name: hir::ParamName::Plain(ident),
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
attrs: self.lower_attrs(&param.attrs),
attrs: self.lower_attrs(&param.attrs).into(),
bounds,
span: ident.span,
kind: hir::GenericParamKind::Type {
Expand Down Expand Up @@ -2667,7 +2678,7 @@ impl<'a> LoweringContext<'a> {
},
vis: self.lower_visibility(&f.vis, None),
ty: self.lower_ty(&f.ty, ImplTraitContext::disallowed()),
attrs: self.lower_attrs(&f.attrs),
attrs: self.lower_attrs(&f.attrs).into(),
}
}

Expand Down Expand Up @@ -2750,7 +2761,7 @@ impl<'a> LoweringContext<'a> {
&mut self,
id: NodeId,
name: &mut Name,
attrs: &hir::HirVec<Attribute>,
attrs: &hir::HirVec<hir::Attribute>,
vis: &mut hir::Visibility,
i: &ItemKind,
) -> hir::ItemKind {
Expand Down Expand Up @@ -2956,7 +2967,7 @@ impl<'a> LoweringContext<'a> {
id: NodeId,
vis: &mut hir::Visibility,
name: &mut Name,
attrs: &hir::HirVec<Attribute>,
attrs: &hir::HirVec<hir::Attribute>,
) -> hir::ItemKind {
debug!("lower_use_tree(tree={:?})", tree);
debug!("lower_use_tree: vis = {:?}", vis);
Expand Down Expand Up @@ -3257,7 +3268,7 @@ impl<'a> LoweringContext<'a> {
id: node_id,
hir_id,
ident: i.ident,
attrs: self.lower_attrs(&i.attrs),
attrs: self.lower_attrs(&i.attrs).into(),
generics,
node,
span: i.span,
Expand Down Expand Up @@ -3333,7 +3344,7 @@ impl<'a> LoweringContext<'a> {
id: node_id,
hir_id,
ident: i.ident,
attrs: self.lower_attrs(&i.attrs),
attrs: self.lower_attrs(&i.attrs).into(),
generics,
vis: self.lower_visibility(&i.vis, None),
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
Expand Down Expand Up @@ -3427,7 +3438,7 @@ impl<'a> LoweringContext<'a> {
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
let mut name = i.ident.name;
let mut vis = self.lower_visibility(&i.vis, None);
let attrs = self.lower_attrs(&i.attrs);
let attrs = self.lower_attrs(&i.attrs).into();
if let ItemKind::MacroDef(ref def) = i.node {
if !def.legacy || attr::contains_name(&i.attrs, "macro_export") ||
attr::contains_name(&i.attrs, "rustc_doc_only_macro") {
Expand Down Expand Up @@ -3466,7 +3477,7 @@ impl<'a> LoweringContext<'a> {
hir::ForeignItem {
id: node_id,
name: i.ident.name,
attrs: self.lower_attrs(&i.attrs),
attrs: self.lower_attrs(&i.attrs).into(),
node: match i.node {
ForeignItemKind::Fn(ref fdec, ref generics) => {
let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
Expand Down Expand Up @@ -4025,7 +4036,7 @@ impl<'a> LoweringContext<'a> {
hir::ExprKind::Struct(struct_path, fields, None)
},
span: e.span,
attrs: e.attrs.clone(),
attrs: self.lower_attrs(&e.attrs).into(),
};
}
ExprKind::Path(ref qself, ref path) => {
Expand Down Expand Up @@ -4111,7 +4122,7 @@ impl<'a> LoweringContext<'a> {
ex.span = e.span;
}
// merge attributes into the inner expression.
let mut attrs = e.attrs.clone();
let mut attrs: ThinVec<_> = self.lower_attrs(&e.attrs).into();
attrs.extend::<Vec<_>>(ex.attrs.into());
ex.attrs = attrs;
return ex;
Expand Down Expand Up @@ -4394,7 +4405,8 @@ impl<'a> LoweringContext<'a> {
let result = P(self.expr_ident(e.span, result_ident, let_stmt_binding));
let block = P(self.block_all(e.span, hir_vec![let_stmt], Some(result)));
// add the attributes to the outer returned expr node
return self.expr_block(block, e.attrs.clone());
let attrs = self.lower_attrs(&e.attrs).into();
return self.expr_block(block, attrs);
}

// Desugar ExprKind::Try
Expand Down Expand Up @@ -4507,7 +4519,7 @@ impl<'a> LoweringContext<'a> {
hir_id,
node: kind,
span: e.span,
attrs: e.attrs.clone(),
attrs: self.lower_attrs(&e.attrs).into(),
}
}

Expand Down Expand Up @@ -4685,7 +4697,7 @@ impl<'a> LoweringContext<'a> {
}
}

fn expr_break(&mut self, span: Span, attrs: ThinVec<Attribute>) -> P<hir::Expr> {
fn expr_break(&mut self, span: Span, attrs: ThinVec<hir::Attribute>) -> P<hir::Expr> {
let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
P(self.expr(span, expr_break, attrs))
}
Expand All @@ -4708,7 +4720,7 @@ impl<'a> LoweringContext<'a> {
span: Span,
ident: Ident,
binding: NodeId,
attrs: ThinVec<Attribute>,
attrs: ThinVec<hir::Attribute>,
) -> hir::Expr {
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
None,
Expand All @@ -4731,7 +4743,7 @@ impl<'a> LoweringContext<'a> {
span: Span,
components: &[&str],
params: Option<P<hir::GenericArgs>>,
attrs: ThinVec<Attribute>,
attrs: ThinVec<hir::Attribute>,
) -> hir::Expr {
let path = self.std_path(span, components, params, true);
self.expr(
Expand All @@ -4751,15 +4763,20 @@ impl<'a> LoweringContext<'a> {
self.expr(span, hir::ExprKind::Match(arg, arms, source), ThinVec::new())
}

fn expr_block(&mut self, b: P<hir::Block>, attrs: ThinVec<Attribute>) -> hir::Expr {
fn expr_block(&mut self, b: P<hir::Block>, attrs: ThinVec<hir::Attribute>) -> hir::Expr {
self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
}

fn expr_tuple(&mut self, sp: Span, exprs: hir::HirVec<hir::Expr>) -> P<hir::Expr> {
P(self.expr(sp, hir::ExprKind::Tup(exprs), ThinVec::new()))
}

fn expr(&mut self, span: Span, node: hir::ExprKind, attrs: ThinVec<Attribute>) -> hir::Expr {
fn expr(
&mut self,
span: Span,
node: hir::ExprKind,
attrs: ThinVec<hir::Attribute>,
) -> hir::Expr {
let LoweredNodeId { node_id, hir_id } = self.next_id();
hir::Expr {
id: node_id,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/map/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

use hir as ast;
use hir::map;
use hir::{Expr, FnDecl, Node};
use hir::{Attribute, Expr, FnDecl, Node};
use hir::intravisit::FnKind;
use syntax::ast::{Attribute, Ident, Name, NodeId};
use syntax::ast::{Ident, Name, NodeId};
use syntax_pos::Span;

/// An FnLikeNode is a Node that is like a fn, in that it has a decl
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ impl<'hir> Map<'hir> {
/// Get the attributes on the krate. This is preferable to
/// invoking `krate.attrs` because it registers a tighter
/// dep-graph access.
pub fn krate_attrs(&self) -> &'hir [ast::Attribute] {
pub fn krate_attrs(&self) -> &'hir [Attribute] {
let def_path_hash = self.definitions.def_path_hash(CRATE_DEF_INDEX);

self.dep_graph.read(def_path_hash.to_dep_node(DepKind::Hir));
Expand Down Expand Up @@ -834,7 +834,7 @@ impl<'hir> Map<'hir> {

/// Given a node ID, get a list of attributes associated with the AST
/// corresponding to the Node ID
pub fn attrs(&self, id: NodeId) -> &'hir [ast::Attribute] {
pub fn attrs(&self, id: NodeId) -> &'hir [Attribute] {
self.read(id); // reveals attributes on the node
let attrs = match self.find(id) {
Some(Node::Item(i)) => Some(&i.attrs[..]),
Expand Down
49 changes: 47 additions & 2 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use syntax_pos::{Span, DUMMY_SP, symbol::InternedString};
use syntax::source_map::{self, Spanned};
use rustc_target::spec::abi::Abi;
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy};
use syntax::attr::InlineAttr;
use syntax::ast::{Lit, StrStyle, FloatTy, IntTy, UintTy};
use syntax::attr::{self, InlineAttr};
use syntax::ext::hygiene::SyntaxContext;
use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
Expand Down Expand Up @@ -150,6 +150,12 @@ pub const DUMMY_HIR_ID: HirId = HirId {

pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;

pub type Attribute = ast::Attribute<Path>;
pub type MetaItem = ast::MetaItem<Path>;
pub type MetaItemKind = ast::MetaItemKind<Path>;
pub type NestedMetaItem = ast::NestedMetaItem<Path>;
pub type NestedMetaItemKind = ast::NestedMetaItemKind<Path>;

#[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
pub struct Label {
pub ident: Ident,
Expand Down Expand Up @@ -327,6 +333,34 @@ impl fmt::Display for Path {
}
}

impl attr::Path for Path {
type Segment = PathSegment;

fn from_span_and_segments(span: Span, segments: Vec<Self::Segment>) -> Self {
Self {
span,
def: Def::Err,
segments: HirVec::from(segments),
}
}

fn from_nt(
_: &::syntax::parse::token::Nonterminal,
) -> Result<Self, Option<ast::MetaItem<Self>>> {
bug!("interpolated tokens should not be present in the HIR")
}

#[inline]
fn span(&self) -> Span {
self.span
}

#[inline]
fn segments(&self) -> &[Self::Segment] {
&self.segments
}
}

/// A segment of a path: an identifier, an optional lifetime, and a set of
/// types.
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
Expand Down Expand Up @@ -401,6 +435,17 @@ impl PathSegment {
}
}

impl attr::PathSegment for PathSegment {
fn from_ident(ident: Ident) -> Self {
Self::from_ident(ident)
}

#[inline]
fn ident(&self) -> Ident {
self.ident
}
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum GenericArg {
Lifetime(Lifetime),
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl<'a> State<'a> {
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |e| e.span)
}

pub fn print_mod(&mut self, _mod: &hir::Mod, attrs: &[ast::Attribute]) -> io::Result<()> {
pub fn print_mod(&mut self, _mod: &hir::Mod, attrs: &[hir::Attribute]) -> io::Result<()> {
self.print_inner_attributes(attrs)?;
for &item_id in &_mod.item_ids {
self.ann.nested(self, Nested::Item(item_id))?;
Expand All @@ -349,7 +349,7 @@ impl<'a> State<'a> {

pub fn print_foreign_mod(&mut self,
nmod: &hir::ForeignMod,
attrs: &[ast::Attribute])
attrs: &[hir::Attribute])
-> io::Result<()> {
self.print_inner_attributes(attrs)?;
for item in &nmod.items {
Expand Down Expand Up @@ -1035,15 +1035,15 @@ impl<'a> State<'a> {

pub fn print_block_with_attrs(&mut self,
blk: &hir::Block,
attrs: &[ast::Attribute])
attrs: &[hir::Attribute])
-> io::Result<()> {
self.print_block_maybe_unclosed(blk, indent_unit, attrs, true)
}

pub fn print_block_maybe_unclosed(&mut self,
blk: &hir::Block,
indented: usize,
attrs: &[ast::Attribute],
attrs: &[hir::Attribute],
close_box: bool)
-> io::Result<()> {
match blk.rules {
Expand Down
Loading