Skip to content

Commit 3175d03

Browse files
committed
Take a LocalDefId in hir::Visitor::visit_fn.
1 parent 2527416 commit 3175d03

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

+235
-211
lines changed

compiler/rustc_ast_lowering/src/index.rs

-13
Original file line numberDiff line numberDiff line change
@@ -275,19 +275,6 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
275275
});
276276
}
277277

278-
fn visit_fn(
279-
&mut self,
280-
fk: intravisit::FnKind<'hir>,
281-
fd: &'hir FnDecl<'hir>,
282-
b: BodyId,
283-
_: Span,
284-
id: HirId,
285-
) {
286-
assert_eq!(self.owner, id.owner);
287-
assert_eq!(self.parent_node, id.local_id);
288-
intravisit::walk_fn(self, fk, fd, b, id);
289-
}
290-
291278
fn visit_block(&mut self, block: &'hir Block<'hir>) {
292279
self.insert(block.span, block.hir_id, Node::Block(block));
293280
self.with_parent(block.hir_id, |this| {

compiler/rustc_hir/src/intravisit.rs

+27-21
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
use crate::hir::*;
6868
use rustc_ast::walk_list;
6969
use rustc_ast::{Attribute, Label};
70+
use rustc_span::def_id::LocalDefId;
7071
use rustc_span::symbol::{Ident, Symbol};
7172
use rustc_span::Span;
7273

@@ -364,7 +365,7 @@ pub trait Visitor<'v>: Sized {
364365
fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
365366
walk_fn_decl(self, fd)
366367
}
367-
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, _: Span, id: HirId) {
368+
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, _: Span, id: LocalDefId) {
368369
walk_fn(self, fk, fd, b, id)
369370
}
370371
fn visit_use(&mut self, path: &'v UsePath<'v>, hir_id: HirId) {
@@ -468,13 +469,16 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
468469
visitor.visit_ty(typ);
469470
visitor.visit_nested_body(body);
470471
}
471-
ItemKind::Fn(ref sig, ref generics, body_id) => visitor.visit_fn(
472-
FnKind::ItemFn(item.ident, generics, sig.header),
473-
sig.decl,
474-
body_id,
475-
item.span,
476-
item.hir_id(),
477-
),
472+
ItemKind::Fn(ref sig, ref generics, body_id) => {
473+
visitor.visit_id(item.hir_id());
474+
visitor.visit_fn(
475+
FnKind::ItemFn(item.ident, generics, sig.header),
476+
sig.decl,
477+
body_id,
478+
item.span,
479+
item.owner_id.def_id,
480+
)
481+
}
478482
ItemKind::Macro(..) => {
479483
visitor.visit_id(item.hir_id());
480484
}
@@ -733,7 +737,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
733737
walk_list!(visitor, visit_arm, arms);
734738
}
735739
ExprKind::Closure(&Closure {
736-
def_id: _,
740+
def_id,
737741
binder: _,
738742
bound_generic_params,
739743
fn_decl,
@@ -745,7 +749,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
745749
constness: _,
746750
}) => {
747751
walk_list!(visitor, visit_generic_param, bound_generic_params);
748-
visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, expression.hir_id)
752+
visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, def_id)
749753
}
750754
ExprKind::Block(ref block, ref opt_label) => {
751755
walk_list!(visitor, visit_label, opt_label);
@@ -923,9 +927,8 @@ pub fn walk_fn<'v, V: Visitor<'v>>(
923927
function_kind: FnKind<'v>,
924928
function_declaration: &'v FnDecl<'v>,
925929
body_id: BodyId,
926-
id: HirId,
930+
_: LocalDefId,
927931
) {
928-
visitor.visit_id(id);
929932
visitor.visit_fn_decl(function_declaration);
930933
walk_fn_kind(visitor, function_kind);
931934
visitor.visit_nested_body(body_id)
@@ -953,26 +956,30 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
953956
let TraitItem { ident, generics, ref defaultness, ref kind, span, owner_id: _ } = *trait_item;
954957
let hir_id = trait_item.hir_id();
955958
visitor.visit_ident(ident);
956-
visitor.visit_generics(generics);
957-
visitor.visit_defaultness(defaultness);
959+
visitor.visit_generics(&generics);
960+
visitor.visit_defaultness(&defaultness);
961+
visitor.visit_id(hir_id);
958962
match *kind {
959963
TraitItemKind::Const(ref ty, default) => {
960-
visitor.visit_id(hir_id);
961964
visitor.visit_ty(ty);
962965
walk_list!(visitor, visit_nested_body, default);
963966
}
964967
TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
965-
visitor.visit_id(hir_id);
966968
visitor.visit_fn_decl(sig.decl);
967969
for &param_name in param_names {
968970
visitor.visit_ident(param_name);
969971
}
970972
}
971973
TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
972-
visitor.visit_fn(FnKind::Method(ident, sig), sig.decl, body_id, span, hir_id);
974+
visitor.visit_fn(
975+
FnKind::Method(ident, sig),
976+
sig.decl,
977+
body_id,
978+
span,
979+
trait_item.owner_id.def_id,
980+
);
973981
}
974982
TraitItemKind::Type(bounds, ref default) => {
975-
visitor.visit_id(hir_id);
976983
walk_list!(visitor, visit_param_bound, bounds);
977984
walk_list!(visitor, visit_ty, default);
978985
}
@@ -1002,9 +1009,9 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
10021009
visitor.visit_ident(ident);
10031010
visitor.visit_generics(generics);
10041011
visitor.visit_defaultness(defaultness);
1012+
visitor.visit_id(impl_item.hir_id());
10051013
match *kind {
10061014
ImplItemKind::Const(ref ty, body) => {
1007-
visitor.visit_id(impl_item.hir_id());
10081015
visitor.visit_ty(ty);
10091016
visitor.visit_nested_body(body);
10101017
}
@@ -1014,11 +1021,10 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
10141021
sig.decl,
10151022
body_id,
10161023
impl_item.span,
1017-
impl_item.hir_id(),
1024+
impl_item.owner_id.def_id,
10181025
);
10191026
}
10201027
ImplItemKind::Type(ref ty) => {
1021-
visitor.visit_id(impl_item.hir_id());
10221028
visitor.visit_ty(ty);
10231029
}
10241030
}

compiler/rustc_hir_analysis/src/collect/lifetimes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
829829
fd: &'tcx hir::FnDecl<'tcx>,
830830
body_id: hir::BodyId,
831831
_: Span,
832-
_: hir::HirId,
832+
_: LocalDefId,
833833
) {
834834
let output = match fd.output {
835835
hir::FnRetTy::DefaultReturn(_) => None,

compiler/rustc_hir_typeck/src/gather_locals.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_hir::PatKind;
55
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
66
use rustc_middle::ty::Ty;
77
use rustc_middle::ty::UserType;
8+
use rustc_span::def_id::LocalDefId;
89
use rustc_span::Span;
910
use rustc_trait_selection::traits;
1011

@@ -156,7 +157,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
156157
_: &'tcx hir::FnDecl<'tcx>,
157158
_: hir::BodyId,
158159
_: Span,
159-
_: hir::HirId,
160+
_: LocalDefId,
160161
) {
161162
}
162163
}

compiler/rustc_lint/src/builtin.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,13 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
583583

584584
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
585585
// If the method is an impl for a trait, don't doc.
586-
if method_context(cx, impl_item.hir_id()) == MethodLateContext::TraitImpl {
586+
let context = method_context(cx, impl_item.owner_id.def_id);
587+
if context == MethodLateContext::TraitImpl {
587588
return;
588589
}
589590

590591
// If the method is an impl for an item with docs_hidden, don't doc.
591-
if method_context(cx, impl_item.hir_id()) == MethodLateContext::PlainImpl {
592+
if context == MethodLateContext::PlainImpl {
592593
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
593594
let impl_ty = cx.tcx.type_of(parent);
594595
let outerdef = match impl_ty.kind() {
@@ -1296,19 +1297,18 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
12961297
_: &'tcx FnDecl<'_>,
12971298
_: &'tcx Body<'_>,
12981299
span: Span,
1299-
hir_id: HirId,
1300+
def_id: LocalDefId,
13001301
) {
13011302
if fn_kind.asyncness() == IsAsync::Async
13021303
&& !cx.tcx.features().closure_track_caller
1303-
&& let attrs = cx.tcx.hir().attrs(hir_id)
13041304
// Now, check if the function has the `#[track_caller]` attribute
1305-
&& let Some(attr) = attrs.iter().find(|attr| attr.has_name(sym::track_caller))
1306-
{
1307-
cx.emit_spanned_lint(UNGATED_ASYNC_FN_TRACK_CALLER, attr.span, BuiltinUngatedAsyncFnTrackCaller {
1308-
label: span,
1309-
parse_sess: &cx.tcx.sess.parse_sess,
1310-
});
1311-
}
1305+
&& let Some(attr) = cx.tcx.get_attr(def_id.to_def_id(), sym::track_caller)
1306+
{
1307+
cx.emit_spanned_lint(UNGATED_ASYNC_FN_TRACK_CALLER, attr.span, BuiltinUngatedAsyncFnTrackCaller {
1308+
label: span,
1309+
parse_sess: &cx.tcx.sess.parse_sess,
1310+
});
1311+
}
13121312
}
13131313
}
13141314

compiler/rustc_lint/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
180180
decl: &'tcx hir::FnDecl<'tcx>,
181181
body_id: hir::BodyId,
182182
span: Span,
183-
id: hir::HirId,
183+
id: LocalDefId,
184184
) {
185185
// Wrap in typeck results here, not just in visit_nested_body,
186186
// in order for `check_fn` to be able to use them.

compiler/rustc_lint/src/nonstandard_style.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::intravisit::FnKind;
1111
use rustc_hir::{GenericParamKind, PatKind};
1212
use rustc_middle::ty;
13-
use rustc_span::symbol::sym;
14-
use rustc_span::{symbol::Ident, BytePos, Span};
13+
use rustc_span::def_id::LocalDefId;
14+
use rustc_span::symbol::{sym, Ident};
15+
use rustc_span::{BytePos, Span};
1516
use rustc_target::spec::abi::Abi;
1617

1718
#[derive(PartialEq)]
@@ -21,8 +22,7 @@ pub enum MethodLateContext {
2122
PlainImpl,
2223
}
2324

24-
pub fn method_context(cx: &LateContext<'_>, id: hir::HirId) -> MethodLateContext {
25-
let def_id = cx.tcx.hir().local_def_id(id);
25+
pub fn method_context(cx: &LateContext<'_>, def_id: LocalDefId) -> MethodLateContext {
2626
let item = cx.tcx.associated_item(def_id);
2727
match item.container {
2828
ty::TraitContainer => MethodLateContext::TraitAutoImpl,
@@ -379,13 +379,13 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
379379
_: &hir::FnDecl<'_>,
380380
_: &hir::Body<'_>,
381381
_: Span,
382-
id: hir::HirId,
382+
id: LocalDefId,
383383
) {
384-
let attrs = cx.tcx.hir().attrs(id);
385384
match &fk {
386385
FnKind::Method(ident, sig, ..) => match method_context(cx, id) {
387386
MethodLateContext::PlainImpl => {
388-
if sig.header.abi != Abi::Rust && cx.sess().contains_name(attrs, sym::no_mangle)
387+
if sig.header.abi != Abi::Rust
388+
&& cx.tcx.has_attr(id.to_def_id(), sym::no_mangle)
389389
{
390390
return;
391391
}
@@ -398,7 +398,7 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
398398
},
399399
FnKind::ItemFn(ident, _, header) => {
400400
// Skip foreign-ABI #[no_mangle] functions (Issue #31924)
401-
if header.abi != Abi::Rust && cx.sess().contains_name(attrs, sym::no_mangle) {
401+
if header.abi != Abi::Rust && cx.tcx.has_attr(id.to_def_id(), sym::no_mangle) {
402402
return;
403403
}
404404
self.check_snake_case(cx, "function", ident);

compiler/rustc_lint/src/passes.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_ast as ast;
44
use rustc_hir as hir;
55
use rustc_session::lint::builtin::HardwiredLints;
66
use rustc_session::lint::LintPass;
7+
use rustc_span::def_id::LocalDefId;
78
use rustc_span::symbol::Ident;
89
use rustc_span::Span;
910

@@ -36,7 +37,7 @@ macro_rules! late_lint_methods {
3637
b: &'tcx hir::FnDecl<'tcx>,
3738
c: &'tcx hir::Body<'tcx>,
3839
d: Span,
39-
e: hir::HirId);
40+
e: LocalDefId);
4041
fn check_trait_item(a: &'tcx hir::TraitItem<'tcx>);
4142
fn check_impl_item(a: &'tcx hir::ImplItem<'tcx>);
4243
fn check_impl_item_post(a: &'tcx hir::ImplItem<'tcx>);

compiler/rustc_lint/src/types.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_hir::{is_range_literal, Expr, ExprKind, Node};
1414
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, SizeSkeleton};
1515
use rustc_middle::ty::subst::SubstsRef;
1616
use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable};
17+
use rustc_span::def_id::LocalDefId;
1718
use rustc_span::source_map;
1819
use rustc_span::symbol::sym;
1920
use rustc_span::{Span, Symbol};
@@ -1224,8 +1225,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12241225
}
12251226
}
12261227

1227-
fn check_foreign_fn(&mut self, id: hir::HirId, decl: &hir::FnDecl<'_>) {
1228-
let def_id = self.cx.tcx.hir().local_def_id(id);
1228+
fn check_foreign_fn(&mut self, def_id: LocalDefId, decl: &hir::FnDecl<'_>) {
12291229
let sig = self.cx.tcx.fn_sig(def_id).subst_identity();
12301230
let sig = self.cx.tcx.erase_late_bound_regions(sig);
12311231

@@ -1261,7 +1261,7 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDeclarations {
12611261
if !vis.is_internal_abi(abi) {
12621262
match it.kind {
12631263
hir::ForeignItemKind::Fn(ref decl, _, _) => {
1264-
vis.check_foreign_fn(it.hir_id(), decl);
1264+
vis.check_foreign_fn(it.owner_id.def_id, decl);
12651265
}
12661266
hir::ForeignItemKind::Static(ref ty, _) => {
12671267
vis.check_foreign_static(it.hir_id(), ty.span);
@@ -1280,7 +1280,7 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
12801280
decl: &'tcx hir::FnDecl<'_>,
12811281
_: &'tcx hir::Body<'_>,
12821282
_: Span,
1283-
hir_id: hir::HirId,
1283+
id: LocalDefId,
12841284
) {
12851285
use hir::intravisit::FnKind;
12861286

@@ -1292,7 +1292,7 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
12921292

12931293
let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Definition };
12941294
if !vis.is_internal_abi(abi) {
1295-
vis.check_foreign_fn(hir_id, decl);
1295+
vis.check_foreign_fn(id, decl);
12961296
}
12971297
}
12981298
}

compiler/rustc_mir_transform/src/check_unsafety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
445445
_fd: &'tcx hir::FnDecl<'tcx>,
446446
b: hir::BodyId,
447447
_s: rustc_span::Span,
448-
_id: HirId,
448+
_id: LocalDefId,
449449
) {
450450
if matches!(fk, intravisit::FnKind::Closure) {
451451
self.visit_body(self.tcx.hir().body(b))

compiler/rustc_passes/src/hir_stats.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_hir::HirId;
1212
use rustc_middle::hir::map::Map;
1313
use rustc_middle::ty::TyCtxt;
1414
use rustc_middle::util::common::to_readable_str;
15+
use rustc_span::def_id::LocalDefId;
1516
use rustc_span::Span;
1617

1718
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
@@ -363,7 +364,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
363364
fd: &'v hir::FnDecl<'v>,
364365
b: hir::BodyId,
365366
_: Span,
366-
id: hir::HirId,
367+
id: LocalDefId,
367368
) {
368369
self.record("FnDecl", Id::None, fd);
369370
hir_visit::walk_fn(self, fk, fd, b, id)

src/tools/clippy/clippy_lints/src/booleans.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use if_chain::if_chain;
66
use rustc_ast::ast::LitKind;
77
use rustc_errors::Applicability;
88
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
9-
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, UnOp};
9+
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
12+
use rustc_span::def_id::LocalDefId;
1213
use rustc_span::source_map::Span;
1314
use rustc_span::sym;
1415

@@ -82,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
8283
_: &'tcx FnDecl<'_>,
8384
body: &'tcx Body<'_>,
8485
_: Span,
85-
_: HirId,
86+
_: LocalDefId,
8687
) {
8788
NonminimalBoolVisitor { cx }.visit_body(body);
8889
}

0 commit comments

Comments
 (0)