Skip to content

Commit b8d7bad

Browse files
committed
Use hir::Path instead of ast::Path in HIR Attribute's.
1 parent 9abc231 commit b8d7bad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+689
-427
lines changed

src/librustc/hir/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
//! This order consistency is required in a few places in rustc, for
4242
//! example generator inference, and possibly also HIR borrowck.
4343
44-
use syntax::ast::{NodeId, CRATE_NODE_ID, Ident, Name, Attribute};
44+
use syntax::ast::{NodeId, CRATE_NODE_ID, Ident, Name};
4545
use syntax_pos::Span;
4646
use hir::*;
4747
use hir::def::Def;

src/librustc/hir/lowering.rs

+46-29
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ impl<'a> LoweringContext<'a> {
483483
visit::walk_crate(&mut ItemLowerer { lctx: &mut self }, c);
484484

485485
let module = self.lower_mod(&c.module);
486-
let attrs = self.lower_attrs(&c.attrs);
486+
let attrs = self.lower_attrs(&c.attrs).into();
487487
let body_ids = body_ids(&self.bodies);
488488

489489
self.resolver
@@ -1057,21 +1057,32 @@ impl<'a> LoweringContext<'a> {
10571057
}
10581058
}
10591059

1060-
fn lower_attrs(&mut self, attrs: &[Attribute]) -> hir::HirVec<Attribute> {
1060+
fn lower_attrs(&mut self, attrs: &[Attribute]) -> Vec<hir::Attribute> {
10611061
attrs
10621062
.iter()
10631063
.map(|a| self.lower_attr(a))
10641064
.collect()
10651065
}
10661066

1067-
fn lower_attr(&mut self, attr: &Attribute) -> Attribute {
1068-
// Note that we explicitly do not walk the path. Since we don't really
1069-
// lower attributes (we use the AST version) there is nowhere to keep
1070-
// the HirIds. We don't actually need HIR version of attributes anyway.
1071-
Attribute {
1067+
fn lower_attr(&mut self, attr: &Attribute) -> hir::Attribute {
1068+
hir::Attribute {
10721069
id: attr.id,
10731070
style: attr.style,
1074-
path: attr.path.clone(),
1071+
// HACK(eddyb) manual conversion because `lower_path(_extra)`
1072+
// use `lower_path_segment` and that allocates `ItemLocalId`s.
1073+
path: hir::Path {
1074+
def: Def::Err,
1075+
segments: attr.path.segments.iter().map(|segment| {
1076+
hir::PathSegment::new(
1077+
segment.ident,
1078+
None,
1079+
None,
1080+
hir::GenericArgs::none(),
1081+
false,
1082+
)
1083+
}).collect(),
1084+
span: attr.path.span,
1085+
},
10751086
tokens: self.lower_token_stream(attr.tokens.clone()),
10761087
is_sugared_doc: attr.is_sugared_doc,
10771088
span: attr.span,
@@ -1110,7 +1121,7 @@ impl<'a> LoweringContext<'a> {
11101121

11111122
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {
11121123
hir::Arm {
1113-
attrs: self.lower_attrs(&arm.attrs),
1124+
attrs: self.lower_attrs(&arm.attrs).into(),
11141125
pats: arm.pats.iter().map(|x| self.lower_pat(x)).collect(),
11151126
guard: match arm.guard {
11161127
Some(Guard::If(ref x)) => Some(hir::Guard::If(P(self.lower_expr(x)))),
@@ -1576,7 +1587,7 @@ impl<'a> LoweringContext<'a> {
15761587
Spanned {
15771588
node: hir::VariantKind {
15781589
name: v.node.ident.name,
1579-
attrs: self.lower_attrs(&v.node.attrs),
1590+
attrs: self.lower_attrs(&v.node.attrs).into(),
15801591
data: self.lower_variant_data(&v.node.data),
15811592
disr_expr: v.node.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
15821593
},
@@ -1967,7 +1978,7 @@ impl<'a> LoweringContext<'a> {
19671978
pat: self.lower_pat(&l.pat),
19681979
init: l.init.as_ref().map(|e| P(self.lower_expr(e))),
19691980
span: l.span,
1970-
attrs: l.attrs.clone(),
1981+
attrs: self.lower_attrs(&l.attrs).into(),
19711982
source: hir::LocalSource::Normal,
19721983
}), ids)
19731984
}
@@ -2410,7 +2421,7 @@ impl<'a> LoweringContext<'a> {
24102421
name: param_name,
24112422
span: lt.span,
24122423
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
2413-
attrs: self.lower_attrs(&param.attrs),
2424+
attrs: self.lower_attrs(&param.attrs).into(),
24142425
bounds,
24152426
kind: hir::GenericParamKind::Lifetime {
24162427
kind: hir::LifetimeParamKind::Explicit,
@@ -2443,7 +2454,7 @@ impl<'a> LoweringContext<'a> {
24432454
id: self.lower_node_id(param.id).node_id,
24442455
name: hir::ParamName::Plain(ident),
24452456
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
2446-
attrs: self.lower_attrs(&param.attrs),
2457+
attrs: self.lower_attrs(&param.attrs).into(),
24472458
bounds,
24482459
span: ident.span,
24492460
kind: hir::GenericParamKind::Type {
@@ -2667,7 +2678,7 @@ impl<'a> LoweringContext<'a> {
26672678
},
26682679
vis: self.lower_visibility(&f.vis, None),
26692680
ty: self.lower_ty(&f.ty, ImplTraitContext::disallowed()),
2670-
attrs: self.lower_attrs(&f.attrs),
2681+
attrs: self.lower_attrs(&f.attrs).into(),
26712682
}
26722683
}
26732684

@@ -2750,7 +2761,7 @@ impl<'a> LoweringContext<'a> {
27502761
&mut self,
27512762
id: NodeId,
27522763
name: &mut Name,
2753-
attrs: &hir::HirVec<Attribute>,
2764+
attrs: &hir::HirVec<hir::Attribute>,
27542765
vis: &mut hir::Visibility,
27552766
i: &ItemKind,
27562767
) -> hir::ItemKind {
@@ -2956,7 +2967,7 @@ impl<'a> LoweringContext<'a> {
29562967
id: NodeId,
29572968
vis: &mut hir::Visibility,
29582969
name: &mut Name,
2959-
attrs: &hir::HirVec<Attribute>,
2970+
attrs: &hir::HirVec<hir::Attribute>,
29602971
) -> hir::ItemKind {
29612972
debug!("lower_use_tree(tree={:?})", tree);
29622973
debug!("lower_use_tree: vis = {:?}", vis);
@@ -3257,7 +3268,7 @@ impl<'a> LoweringContext<'a> {
32573268
id: node_id,
32583269
hir_id,
32593270
ident: i.ident,
3260-
attrs: self.lower_attrs(&i.attrs),
3271+
attrs: self.lower_attrs(&i.attrs).into(),
32613272
generics,
32623273
node,
32633274
span: i.span,
@@ -3333,7 +3344,7 @@ impl<'a> LoweringContext<'a> {
33333344
id: node_id,
33343345
hir_id,
33353346
ident: i.ident,
3336-
attrs: self.lower_attrs(&i.attrs),
3347+
attrs: self.lower_attrs(&i.attrs).into(),
33373348
generics,
33383349
vis: self.lower_visibility(&i.vis, None),
33393350
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
@@ -3427,7 +3438,7 @@ impl<'a> LoweringContext<'a> {
34273438
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
34283439
let mut name = i.ident.name;
34293440
let mut vis = self.lower_visibility(&i.vis, None);
3430-
let attrs = self.lower_attrs(&i.attrs);
3441+
let attrs = self.lower_attrs(&i.attrs).into();
34313442
if let ItemKind::MacroDef(ref def) = i.node {
34323443
if !def.legacy || attr::contains_name(&i.attrs, "macro_export") ||
34333444
attr::contains_name(&i.attrs, "rustc_doc_only_macro") {
@@ -3466,7 +3477,7 @@ impl<'a> LoweringContext<'a> {
34663477
hir::ForeignItem {
34673478
id: node_id,
34683479
name: i.ident.name,
3469-
attrs: self.lower_attrs(&i.attrs),
3480+
attrs: self.lower_attrs(&i.attrs).into(),
34703481
node: match i.node {
34713482
ForeignItemKind::Fn(ref fdec, ref generics) => {
34723483
let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
@@ -4025,7 +4036,7 @@ impl<'a> LoweringContext<'a> {
40254036
hir::ExprKind::Struct(struct_path, fields, None)
40264037
},
40274038
span: e.span,
4028-
attrs: e.attrs.clone(),
4039+
attrs: self.lower_attrs(&e.attrs).into(),
40294040
};
40304041
}
40314042
ExprKind::Path(ref qself, ref path) => {
@@ -4111,7 +4122,7 @@ impl<'a> LoweringContext<'a> {
41114122
ex.span = e.span;
41124123
}
41134124
// merge attributes into the inner expression.
4114-
let mut attrs = e.attrs.clone();
4125+
let mut attrs: ThinVec<_> = self.lower_attrs(&e.attrs).into();
41154126
attrs.extend::<Vec<_>>(ex.attrs.into());
41164127
ex.attrs = attrs;
41174128
return ex;
@@ -4394,7 +4405,8 @@ impl<'a> LoweringContext<'a> {
43944405
let result = P(self.expr_ident(e.span, result_ident, let_stmt_binding));
43954406
let block = P(self.block_all(e.span, hir_vec![let_stmt], Some(result)));
43964407
// add the attributes to the outer returned expr node
4397-
return self.expr_block(block, e.attrs.clone());
4408+
let attrs = self.lower_attrs(&e.attrs).into();
4409+
return self.expr_block(block, attrs);
43984410
}
43994411

44004412
// Desugar ExprKind::Try
@@ -4507,7 +4519,7 @@ impl<'a> LoweringContext<'a> {
45074519
hir_id,
45084520
node: kind,
45094521
span: e.span,
4510-
attrs: e.attrs.clone(),
4522+
attrs: self.lower_attrs(&e.attrs).into(),
45114523
}
45124524
}
45134525

@@ -4685,7 +4697,7 @@ impl<'a> LoweringContext<'a> {
46854697
}
46864698
}
46874699

4688-
fn expr_break(&mut self, span: Span, attrs: ThinVec<Attribute>) -> P<hir::Expr> {
4700+
fn expr_break(&mut self, span: Span, attrs: ThinVec<hir::Attribute>) -> P<hir::Expr> {
46894701
let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
46904702
P(self.expr(span, expr_break, attrs))
46914703
}
@@ -4708,7 +4720,7 @@ impl<'a> LoweringContext<'a> {
47084720
span: Span,
47094721
ident: Ident,
47104722
binding: NodeId,
4711-
attrs: ThinVec<Attribute>,
4723+
attrs: ThinVec<hir::Attribute>,
47124724
) -> hir::Expr {
47134725
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
47144726
None,
@@ -4731,7 +4743,7 @@ impl<'a> LoweringContext<'a> {
47314743
span: Span,
47324744
components: &[&str],
47334745
params: Option<P<hir::GenericArgs>>,
4734-
attrs: ThinVec<Attribute>,
4746+
attrs: ThinVec<hir::Attribute>,
47354747
) -> hir::Expr {
47364748
let path = self.std_path(span, components, params, true);
47374749
self.expr(
@@ -4751,15 +4763,20 @@ impl<'a> LoweringContext<'a> {
47514763
self.expr(span, hir::ExprKind::Match(arg, arms, source), ThinVec::new())
47524764
}
47534765

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

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

4762-
fn expr(&mut self, span: Span, node: hir::ExprKind, attrs: ThinVec<Attribute>) -> hir::Expr {
4774+
fn expr(
4775+
&mut self,
4776+
span: Span,
4777+
node: hir::ExprKind,
4778+
attrs: ThinVec<hir::Attribute>,
4779+
) -> hir::Expr {
47634780
let LoweredNodeId { node_id, hir_id } = self.next_id();
47644781
hir::Expr {
47654782
id: node_id,

src/librustc/hir/map/blocks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
2424
use hir as ast;
2525
use hir::map;
26-
use hir::{Expr, FnDecl, Node};
26+
use hir::{Attribute, Expr, FnDecl, Node};
2727
use hir::intravisit::FnKind;
28-
use syntax::ast::{Attribute, Ident, Name, NodeId};
28+
use syntax::ast::{Ident, Name, NodeId};
2929
use syntax_pos::Span;
3030

3131
/// An FnLikeNode is a Node that is like a fn, in that it has a decl

src/librustc/hir/map/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ impl<'hir> Map<'hir> {
504504
/// Get the attributes on the krate. This is preferable to
505505
/// invoking `krate.attrs` because it registers a tighter
506506
/// dep-graph access.
507-
pub fn krate_attrs(&self) -> &'hir [ast::Attribute] {
507+
pub fn krate_attrs(&self) -> &'hir [Attribute] {
508508
let def_path_hash = self.definitions.def_path_hash(CRATE_DEF_INDEX);
509509

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

835835
/// Given a node ID, get a list of attributes associated with the AST
836836
/// corresponding to the Node ID
837-
pub fn attrs(&self, id: NodeId) -> &'hir [ast::Attribute] {
837+
pub fn attrs(&self, id: NodeId) -> &'hir [Attribute] {
838838
self.read(id); // reveals attributes on the node
839839
let attrs = match self.find(id) {
840840
Some(Node::Item(i)) => Some(&i.attrs[..]),

src/librustc/hir/mod.rs

+47-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use syntax_pos::{Span, DUMMY_SP, symbol::InternedString};
2727
use syntax::source_map::{self, Spanned};
2828
use rustc_target::spec::abi::Abi;
2929
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
30-
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy};
31-
use syntax::attr::InlineAttr;
30+
use syntax::ast::{Lit, StrStyle, FloatTy, IntTy, UintTy};
31+
use syntax::attr::{self, InlineAttr};
3232
use syntax::ext::hygiene::SyntaxContext;
3333
use syntax::ptr::P;
3434
use syntax::symbol::{Symbol, keywords};
@@ -150,6 +150,12 @@ pub const DUMMY_HIR_ID: HirId = HirId {
150150

151151
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;
152152

153+
pub type Attribute = ast::Attribute<Path>;
154+
pub type MetaItem = ast::MetaItem<Path>;
155+
pub type MetaItemKind = ast::MetaItemKind<Path>;
156+
pub type NestedMetaItem = ast::NestedMetaItem<Path>;
157+
pub type NestedMetaItemKind = ast::NestedMetaItemKind<Path>;
158+
153159
#[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
154160
pub struct Label {
155161
pub ident: Ident,
@@ -327,6 +333,34 @@ impl fmt::Display for Path {
327333
}
328334
}
329335

336+
impl attr::Path for Path {
337+
type Segment = PathSegment;
338+
339+
fn from_span_and_segments(span: Span, segments: Vec<Self::Segment>) -> Self {
340+
Self {
341+
span,
342+
def: Def::Err,
343+
segments: HirVec::from(segments),
344+
}
345+
}
346+
347+
fn from_nt(
348+
_: &::syntax::parse::token::Nonterminal,
349+
) -> Result<Self, Option<ast::MetaItem<Self>>> {
350+
bug!("interpolated tokens should not be present in the HIR")
351+
}
352+
353+
#[inline]
354+
fn span(&self) -> Span {
355+
self.span
356+
}
357+
358+
#[inline]
359+
fn segments(&self) -> &[Self::Segment] {
360+
&self.segments
361+
}
362+
}
363+
330364
/// A segment of a path: an identifier, an optional lifetime, and a set of
331365
/// types.
332366
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@@ -401,6 +435,17 @@ impl PathSegment {
401435
}
402436
}
403437

438+
impl attr::PathSegment for PathSegment {
439+
fn from_ident(ident: Ident) -> Self {
440+
Self::from_ident(ident)
441+
}
442+
443+
#[inline]
444+
fn ident(&self) -> Ident {
445+
self.ident
446+
}
447+
}
448+
404449
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
405450
pub enum GenericArg {
406451
Lifetime(Lifetime),

src/librustc/hir/print.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl<'a> State<'a> {
339339
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |e| e.span)
340340
}
341341

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

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

10361036
pub fn print_block_with_attrs(&mut self,
10371037
blk: &hir::Block,
1038-
attrs: &[ast::Attribute])
1038+
attrs: &[hir::Attribute])
10391039
-> io::Result<()> {
10401040
self.print_block_maybe_unclosed(blk, indent_unit, attrs, true)
10411041
}
10421042

10431043
pub fn print_block_maybe_unclosed(&mut self,
10441044
blk: &hir::Block,
10451045
indented: usize,
1046-
attrs: &[ast::Attribute],
1046+
attrs: &[hir::Attribute],
10471047
close_box: bool)
10481048
-> io::Result<()> {
10491049
match blk.rules {

0 commit comments

Comments
 (0)