Skip to content

Commit 213d579

Browse files
committed
Expose attached attributes to FnKind abstraction so that I can look at them in borrowck.
1 parent baeae78 commit 213d579

File tree

14 files changed

+84
-57
lines changed

14 files changed

+84
-57
lines changed

src/librustc/front/map/blocks.rs

+28-19
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ pub use self::Code::*;
2626
use front::map::{self, Node};
2727
use syntax::abi;
2828
use rustc_front::hir::{Block, FnDecl};
29-
use syntax::ast::{Name, NodeId};
29+
use syntax::ast::{Attribute, Name, NodeId};
30+
use syntax::attr::ThinAttributesExt;
3031
use rustc_front::hir as ast;
3132
use syntax::codemap::Span;
3233
use rustc_front::intravisit::FnKind;
@@ -116,7 +117,8 @@ struct ItemFnParts<'a> {
116117
generics: &'a ast::Generics,
117118
body: &'a Block,
118119
id: NodeId,
119-
span: Span
120+
span: Span,
121+
attrs: &'a [Attribute],
120122
}
121123

122124
/// These are all the components one can extract from a closure expr
@@ -125,12 +127,13 @@ struct ClosureParts<'a> {
125127
decl: &'a FnDecl,
126128
body: &'a Block,
127129
id: NodeId,
128-
span: Span
130+
span: Span,
131+
attrs: &'a [Attribute],
129132
}
130133

131134
impl<'a> ClosureParts<'a> {
132-
fn new(d: &'a FnDecl, b: &'a Block, id: NodeId, s: Span) -> ClosureParts<'a> {
133-
ClosureParts { decl: d, body: b, id: id, span: s }
135+
fn new(d: &'a FnDecl, b: &'a Block, id: NodeId, s: Span, attrs: &'a [Attribute]) -> Self {
136+
ClosureParts { decl: d, body: b, id: id, span: s, attrs: attrs }
134137
}
135138
}
136139

@@ -165,37 +168,37 @@ impl<'a> FnLikeNode<'a> {
165168

166169
pub fn body(self) -> &'a Block {
167170
self.handle(|i: ItemFnParts<'a>| &*i.body,
168-
|_, _, _: &'a ast::MethodSig, _, body: &'a ast::Block, _| body,
171+
|_, _, _: &'a ast::MethodSig, _, body: &'a ast::Block, _, _| body,
169172
|c: ClosureParts<'a>| c.body)
170173
}
171174

172175
pub fn decl(self) -> &'a FnDecl {
173176
self.handle(|i: ItemFnParts<'a>| &*i.decl,
174-
|_, _, sig: &'a ast::MethodSig, _, _, _| &sig.decl,
177+
|_, _, sig: &'a ast::MethodSig, _, _, _, _| &sig.decl,
175178
|c: ClosureParts<'a>| c.decl)
176179
}
177180

178181
pub fn span(self) -> Span {
179182
self.handle(|i: ItemFnParts| i.span,
180-
|_, _, _: &'a ast::MethodSig, _, _, span| span,
183+
|_, _, _: &'a ast::MethodSig, _, _, span, _| span,
181184
|c: ClosureParts| c.span)
182185
}
183186

184187
pub fn id(self) -> NodeId {
185188
self.handle(|i: ItemFnParts| i.id,
186-
|id, _, _: &'a ast::MethodSig, _, _, _| id,
189+
|id, _, _: &'a ast::MethodSig, _, _, _, _| id,
187190
|c: ClosureParts| c.id)
188191
}
189192

190193
pub fn kind(self) -> FnKind<'a> {
191194
let item = |p: ItemFnParts<'a>| -> FnKind<'a> {
192-
FnKind::ItemFn(p.name, p.generics, p.unsafety, p.constness, p.abi, p.vis)
195+
FnKind::ItemFn(p.name, p.generics, p.unsafety, p.constness, p.abi, p.vis, p.attrs)
193196
};
194-
let closure = |_: ClosureParts| {
195-
FnKind::Closure
197+
let closure = |c: ClosureParts<'a>| {
198+
FnKind::Closure(c.attrs)
196199
};
197-
let method = |_, name: Name, sig: &'a ast::MethodSig, vis, _, _| {
198-
FnKind::Method(name, sig, vis)
200+
let method = |_, name: Name, sig: &'a ast::MethodSig, vis, _, _, attrs| {
201+
FnKind::Method(name, sig, vis, attrs)
199202
};
200203
self.handle(item, method, closure)
201204
}
@@ -207,7 +210,8 @@ impl<'a> FnLikeNode<'a> {
207210
&'a ast::MethodSig,
208211
Option<ast::Visibility>,
209212
&'a ast::Block,
210-
Span)
213+
Span,
214+
&'a [Attribute])
211215
-> A,
212216
C: FnOnce(ClosureParts<'a>) -> A,
213217
{
@@ -224,20 +228,21 @@ impl<'a> FnLikeNode<'a> {
224228
abi: abi,
225229
vis: i.vis,
226230
constness: constness,
227-
span: i.span
231+
span: i.span,
232+
attrs: &i.attrs,
228233
}),
229234
_ => panic!("item FnLikeNode that is not fn-like"),
230235
},
231236
map::NodeTraitItem(ti) => match ti.node {
232237
ast::MethodTraitItem(ref sig, Some(ref body)) => {
233-
method(ti.id, ti.name, sig, None, body, ti.span)
238+
method(ti.id, ti.name, sig, None, body, ti.span, &ti.attrs)
234239
}
235240
_ => panic!("trait method FnLikeNode that is not fn-like"),
236241
},
237242
map::NodeImplItem(ii) => {
238243
match ii.node {
239244
ast::ImplItemKind::Method(ref sig, ref body) => {
240-
method(ii.id, ii.name, sig, Some(ii.vis), body, ii.span)
245+
method(ii.id, ii.name, sig, Some(ii.vis), body, ii.span, &ii.attrs)
241246
}
242247
_ => {
243248
panic!("impl method FnLikeNode that is not fn-like")
@@ -246,7 +251,11 @@ impl<'a> FnLikeNode<'a> {
246251
}
247252
map::NodeExpr(e) => match e.node {
248253
ast::ExprClosure(_, ref decl, ref block) =>
249-
closure(ClosureParts::new(&decl, &block, e.id, e.span)),
254+
closure(ClosureParts::new(&decl,
255+
&block,
256+
e.id,
257+
e.span,
258+
e.attrs.as_attr_slice())),
250259
_ => panic!("expr FnLikeNode that is not fn-like"),
251260
},
252261
_ => panic!("other FnLikeNode that is not fn-like"),

src/librustc/middle/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ fn check_fn(cx: &mut MatchCheckCtxt,
10171017
sp: Span,
10181018
fn_id: NodeId) {
10191019
match kind {
1020-
FnKind::Closure => {}
1020+
FnKind::Closure(_) => {}
10211021
_ => cx.param_env = ParameterEnvironment::for_item(cx.tcx, fn_id),
10221022
}
10231023

src/librustc/middle/const_eval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ pub fn lookup_const_fn_by_id<'tcx>(tcx: &TyCtxt<'tcx>, def_id: DefId)
226226
};
227227

228228
match fn_like.kind() {
229-
FnKind::ItemFn(_, _, _, hir::Constness::Const, _, _) => {
229+
FnKind::ItemFn(_, _, _, hir::Constness::Const, _, _, _) => {
230230
Some(fn_like)
231231
}
232-
FnKind::Method(_, m, _) => {
232+
FnKind::Method(_, m, _, _) => {
233233
if m.constness == hir::Constness::Const {
234234
Some(fn_like)
235235
} else {

src/librustc/middle/effect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
8282
block: &'v hir::Block, span: Span, _: ast::NodeId) {
8383

8484
let (is_item_fn, is_unsafe_fn) = match fn_kind {
85-
FnKind::ItemFn(_, _, unsafety, _, _, _) =>
85+
FnKind::ItemFn(_, _, unsafety, _, _, _, _) =>
8686
(true, unsafety == hir::Unsafety::Unsafe),
87-
FnKind::Method(_, sig, _) =>
87+
FnKind::Method(_, sig, _, _) =>
8888
(true, sig.unsafety == hir::Unsafety::Unsafe),
8989
_ => (false, false),
9090
};

src/librustc/middle/intrinsicck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for IntrinsicCheckingVisitor<'a, 'tcx> {
226226
intravisit::walk_fn(self, fk, fd, b, s);
227227
self.param_envs.pop();
228228
}
229-
FnKind::Closure => {
229+
FnKind::Closure(..) => {
230230
intravisit::walk_fn(self, fk, fd, b, s);
231231
}
232232
}

src/librustc/middle/resolve_lifetime.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,17 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
182182
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v hir::FnDecl,
183183
b: &'v hir::Block, s: Span, fn_id: ast::NodeId) {
184184
match fk {
185-
FnKind::ItemFn(_, generics, _, _, _, _) => {
185+
FnKind::ItemFn(_, generics, _, _, _, _, _) => {
186186
self.visit_early_late(subst::FnSpace, generics, |this| {
187187
this.add_scope_and_walk_fn(fk, fd, b, s, fn_id)
188188
})
189189
}
190-
FnKind::Method(_, sig, _) => {
190+
FnKind::Method(_, sig, _, _) => {
191191
self.visit_early_late(subst::FnSpace, &sig.generics, |this| {
192192
this.add_scope_and_walk_fn(fk, fd, b, s, fn_id)
193193
})
194194
}
195-
FnKind::Closure => {
195+
FnKind::Closure(_) => {
196196
self.add_scope_and_walk_fn(fk, fd, b, s, fn_id)
197197
}
198198
}
@@ -471,16 +471,16 @@ impl<'a> LifetimeContext<'a> {
471471
fn_id: ast::NodeId) {
472472

473473
match fk {
474-
FnKind::ItemFn(_, generics, _, _, _, _) => {
474+
FnKind::ItemFn(_, generics, _, _, _, _, _) => {
475475
intravisit::walk_fn_decl(self, fd);
476476
self.visit_generics(generics);
477477
}
478-
FnKind::Method(_, sig, _) => {
478+
FnKind::Method(_, sig, _, _) => {
479479
intravisit::walk_fn_decl(self, fd);
480480
self.visit_generics(&sig.generics);
481481
self.visit_explicit_self(&sig.explicit_self);
482482
}
483-
FnKind::Closure => {
483+
FnKind::Closure(_) => {
484484
intravisit::walk_fn_decl(self, fd);
485485
}
486486
}

src/librustc_borrowck/borrowck/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for BorrowckCtxt<'a, 'tcx> {
7373
self.free_region_map = old_free_region_map;
7474
}
7575

76-
FnKind::Closure => {
76+
FnKind::Closure(..) => {
7777
borrowck_fn(self, fk, fd, b, s, id);
7878
}
7979
}

src/librustc_front/intravisit.rs

+28-10
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,30 @@
2727
2828
use syntax::abi::Abi;
2929
use syntax::ast::{NodeId, CRATE_NODE_ID, Name, Attribute};
30+
use syntax::attr::ThinAttributesExt;
3031
use syntax::codemap::Span;
3132
use hir::*;
3233

3334
#[derive(Copy, Clone, PartialEq, Eq)]
3435
pub enum FnKind<'a> {
3536
/// fn foo() or extern "Abi" fn foo()
36-
ItemFn(Name, &'a Generics, Unsafety, Constness, Abi, Visibility),
37+
ItemFn(Name, &'a Generics, Unsafety, Constness, Abi, Visibility, &'a [Attribute]),
3738

3839
/// fn foo(&self)
39-
Method(Name, &'a MethodSig, Option<Visibility>),
40+
Method(Name, &'a MethodSig, Option<Visibility>, &'a [Attribute]),
4041

4142
/// |x, y| {}
42-
Closure,
43+
Closure(&'a [Attribute]),
44+
}
45+
46+
impl<'a> FnKind<'a> {
47+
pub fn attrs(&self) -> &'a [Attribute] {
48+
match *self {
49+
FnKind::ItemFn(_, _, _, _, _, _, attrs) => attrs,
50+
FnKind::Method(_, _, _, attrs) => attrs,
51+
FnKind::Closure(attrs) => attrs,
52+
}
53+
}
4354
}
4455

4556
/// Each method of the Visitor trait is a hook to be potentially
@@ -310,7 +321,8 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
310321
unsafety,
311322
constness,
312323
abi,
313-
item.vis),
324+
item.vis,
325+
&item.attrs),
314326
declaration,
315327
body,
316328
item.span,
@@ -595,14 +607,14 @@ pub fn walk_fn_decl_nopat<'v, V: Visitor<'v>>(visitor: &mut V, function_declarat
595607

596608
pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
597609
match function_kind {
598-
FnKind::ItemFn(_, generics, _, _, _, _) => {
610+
FnKind::ItemFn(_, generics, _, _, _, _, _) => {
599611
visitor.visit_generics(generics);
600612
}
601-
FnKind::Method(_, sig, _) => {
613+
FnKind::Method(_, sig, _, _) => {
602614
visitor.visit_generics(&sig.generics);
603615
visitor.visit_explicit_self(&sig.explicit_self);
604616
}
605-
FnKind::Closure => {}
617+
FnKind::Closure(_) => {}
606618
}
607619
}
608620

@@ -630,7 +642,10 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
630642
walk_fn_decl(visitor, &sig.decl);
631643
}
632644
MethodTraitItem(ref sig, Some(ref body)) => {
633-
visitor.visit_fn(FnKind::Method(trait_item.name, sig, None),
645+
visitor.visit_fn(FnKind::Method(trait_item.name,
646+
sig,
647+
None,
648+
&trait_item.attrs),
634649
&sig.decl,
635650
body,
636651
trait_item.span,
@@ -652,7 +667,10 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
652667
visitor.visit_expr(expr);
653668
}
654669
ImplItemKind::Method(ref sig, ref body) => {
655-
visitor.visit_fn(FnKind::Method(impl_item.name, sig, Some(impl_item.vis)),
670+
visitor.visit_fn(FnKind::Method(impl_item.name,
671+
sig,
672+
Some(impl_item.vis),
673+
&impl_item.attrs),
656674
&sig.decl,
657675
body,
658676
impl_item.span,
@@ -758,7 +776,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
758776
walk_list!(visitor, visit_arm, arms);
759777
}
760778
ExprClosure(_, ref function_declaration, ref body) => {
761-
visitor.visit_fn(FnKind::Closure,
779+
visitor.visit_fn(FnKind::Closure(expression.attrs.as_attr_slice()),
762780
function_declaration,
763781
body,
764782
expression.span,

src/librustc_front/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,13 @@ impl<'a, 'v, O: ast_util::IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O>
254254
self.operation.visit_id(node_id);
255255

256256
match function_kind {
257-
FnKind::ItemFn(_, generics, _, _, _, _) => {
257+
FnKind::ItemFn(_, generics, _, _, _, _, _) => {
258258
self.visit_generics_helper(generics)
259259
}
260-
FnKind::Method(_, sig, _) => {
260+
FnKind::Method(_, sig, _, _) => {
261261
self.visit_generics_helper(&sig.generics)
262262
}
263-
FnKind::Closure => {}
263+
FnKind::Closure(_) => {}
264264
}
265265

266266
for argument in &function_declaration.inputs {

src/librustc_lint/bad_style.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl LateLintPass for NonSnakeCase {
237237
fk: FnKind, _: &hir::FnDecl,
238238
_: &hir::Block, span: Span, id: ast::NodeId) {
239239
match fk {
240-
FnKind::Method(name, _, _) => match method_context(cx, id, span) {
240+
FnKind::Method(name, _, _, _) => match method_context(cx, id, span) {
241241
MethodLateContext::PlainImpl => {
242242
self.check_snake_case(cx, "method", &name.as_str(), Some(span))
243243
},
@@ -246,10 +246,10 @@ impl LateLintPass for NonSnakeCase {
246246
},
247247
_ => (),
248248
},
249-
FnKind::ItemFn(name, _, _, _, _, _) => {
249+
FnKind::ItemFn(name, _, _, _, _, _, _) => {
250250
self.check_snake_case(cx, "function", &name.as_str(), Some(span))
251251
},
252-
_ => (),
252+
FnKind::Closure(_) => (),
253253
}
254254
}
255255

src/librustc_lint/builtin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ impl LateLintPass for UnsafeCode {
223223
fn check_fn(&mut self, cx: &LateContext, fk: FnKind, _: &hir::FnDecl,
224224
_: &hir::Block, span: Span, _: ast::NodeId) {
225225
match fk {
226-
FnKind::ItemFn(_, _, hir::Unsafety::Unsafe, _, _, _) =>
226+
FnKind::ItemFn(_, _, hir::Unsafety::Unsafe, _, _, _, _) =>
227227
cx.span_lint(UNSAFE_CODE, span, "declaration of an `unsafe` function"),
228228

229-
FnKind::Method(_, sig, _) => {
229+
FnKind::Method(_, sig, _, _) => {
230230
if sig.unsafety == hir::Unsafety::Unsafe {
231231
cx.span_lint(UNSAFE_CODE, span, "implementation of an `unsafe` method")
232232
}
@@ -670,7 +670,7 @@ impl LateLintPass for UnconditionalRecursion {
670670
cx.tcx.impl_or_trait_item(cx.tcx.map.local_def_id(id)).as_opt_method()
671671
}
672672
// closures can't recur, so they don't matter.
673-
FnKind::Closure => return
673+
FnKind::Closure(_) => return
674674
};
675675

676676
// Walk through this function (say `f`) looking to see if

src/librustc_mir/mir_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a, 'm, 'tcx> Visitor<'tcx> for InnerDump<'a,'m,'tcx> {
132132
body: &'tcx hir::Block,
133133
span: Span,
134134
id: ast::NodeId) {
135-
let implicit_arg_tys = if let intravisit::FnKind::Closure = fk {
135+
let implicit_arg_tys = if let intravisit::FnKind::Closure(..) = fk {
136136
vec![closure_self_ty(&self.tcx, id, body.id)]
137137
} else {
138138
vec![]

0 commit comments

Comments
 (0)