Auto merge of #88880 - cjgillot:no-krate, r=oli-obk
Rework HIR API to make invocations of the hir_crate query harder. `hir_crate` forces the recomputation of queries that depend on it. This PR aims at avoiding useless invocations of `hir_crate` by making dependent code go through `tcx.hir()`.
This commit is contained in:
commit
c02371c442
64 changed files with 193 additions and 209 deletions
|
|
@ -230,8 +230,7 @@ impl ExternalCrate {
|
|||
};
|
||||
if root.is_local() {
|
||||
tcx.hir()
|
||||
.krate()
|
||||
.module()
|
||||
.root_module()
|
||||
.item_ids
|
||||
.iter()
|
||||
.filter_map(|&id| {
|
||||
|
|
@ -297,8 +296,7 @@ impl ExternalCrate {
|
|||
|
||||
if root.is_local() {
|
||||
tcx.hir()
|
||||
.krate()
|
||||
.module()
|
||||
.root_module()
|
||||
.item_ids
|
||||
.iter()
|
||||
.filter_map(|&id| {
|
||||
|
|
|
|||
|
|
@ -26,8 +26,7 @@ mod tests;
|
|||
crate fn krate(cx: &mut DocContext<'_>) -> Crate {
|
||||
use crate::visit_lib::LibEmbargoVisitor;
|
||||
|
||||
let krate = cx.tcx.hir().krate();
|
||||
let module = crate::visit_ast::RustdocVisitor::new(cx).visit(krate);
|
||||
let module = crate::visit_ast::RustdocVisitor::new(cx).visit();
|
||||
|
||||
let mut externs = Vec::new();
|
||||
for &cnum in cx.tcx.crates(()).iter() {
|
||||
|
|
|
|||
|
|
@ -71,12 +71,12 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
|
|||
self.exact_paths.entry(did).or_insert_with(|| def_id_to_path(tcx, did));
|
||||
}
|
||||
|
||||
crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> {
|
||||
let span = krate.module().inner;
|
||||
crate fn visit(mut self) -> Module<'tcx> {
|
||||
let span = self.cx.tcx.def_span(CRATE_DEF_ID);
|
||||
let mut top_level_module = self.visit_mod_contents(
|
||||
&Spanned { span, node: hir::VisibilityKind::Public },
|
||||
hir::CRATE_HIR_ID,
|
||||
&krate.module(),
|
||||
self.cx.tcx.hir().root_module(),
|
||||
self.cx.tcx.crate_name(LOCAL_CRATE),
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,6 @@ fn main() {
|
|||
pub struct CompilerCalls;
|
||||
|
||||
impl rustc_driver::Callbacks for CompilerCalls {
|
||||
|
||||
// In this callback we override the mir_borrowck query.
|
||||
fn config(&mut self, config: &mut Config) {
|
||||
assert!(config.override_queries.is_none());
|
||||
|
|
@ -64,12 +63,10 @@ impl rustc_driver::Callbacks for CompilerCalls {
|
|||
) -> Compilation {
|
||||
compiler.session().abort_if_errors();
|
||||
queries.global_ctxt().unwrap().peek_mut().enter(|tcx| {
|
||||
|
||||
// Collect definition ids of MIR bodies.
|
||||
let hir = tcx.hir();
|
||||
let krate = hir.krate();
|
||||
let mut visitor = HirVisitor { bodies: Vec::new() };
|
||||
krate.visit_all_item_likes(&mut visitor);
|
||||
hir.visit_all_item_likes(&mut visitor);
|
||||
|
||||
// Trigger borrow checking of all bodies.
|
||||
for def_id in visitor.bodies {
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ extern crate rustc_ast;
|
|||
use rustc_ast::attr;
|
||||
use rustc_driver::plugin::Registry;
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext, LintPass};
|
||||
use rustc_span::def_id::CRATE_DEF_ID;
|
||||
use rustc_span::symbol::Symbol;
|
||||
|
||||
macro_rules! fake_lint_pass {
|
||||
|
|
@ -26,13 +27,14 @@ macro_rules! fake_lint_pass {
|
|||
}
|
||||
|
||||
impl LateLintPass<'_> for $struct {
|
||||
fn check_crate(&mut self, cx: &LateContext, krate: &rustc_hir::Crate) {
|
||||
fn check_crate(&mut self, cx: &LateContext) {
|
||||
let attrs = cx.tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
|
||||
let span = cx.tcx.def_span(CRATE_DEF_ID);
|
||||
$(
|
||||
if !cx.sess().contains_name(attrs, $attr) {
|
||||
cx.lint(CRATE_NOT_OKAY, |lint| {
|
||||
let msg = format!("crate is not marked with #![{}]", $attr);
|
||||
lint.build(&msg).set_span(krate.module().inner).emit()
|
||||
lint.build(&msg).set_span(span).emit()
|
||||
});
|
||||
}
|
||||
)*
|
||||
|
|
|
|||
|
|
@ -8,13 +8,13 @@ extern crate rustc_hir;
|
|||
extern crate rustc_lint;
|
||||
#[macro_use]
|
||||
extern crate rustc_session;
|
||||
extern crate rustc_span;
|
||||
extern crate rustc_ast;
|
||||
extern crate rustc_span;
|
||||
|
||||
use rustc_driver::plugin::Registry;
|
||||
use rustc_lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_span::def_id::CRATE_DEF_ID;
|
||||
use rustc_span::symbol::Symbol;
|
||||
use rustc_ast::attr;
|
||||
|
||||
declare_lint! {
|
||||
CRATE_NOT_OKAY,
|
||||
|
|
@ -25,13 +25,12 @@ declare_lint! {
|
|||
declare_lint_pass!(Pass => [CRATE_NOT_OKAY]);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for Pass {
|
||||
fn check_crate(&mut self, cx: &LateContext, krate: &rustc_hir::Crate) {
|
||||
fn check_crate(&mut self, cx: &LateContext) {
|
||||
let attrs = cx.tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
|
||||
let span = cx.tcx.def_span(CRATE_DEF_ID);
|
||||
if !cx.sess().contains_name(attrs, Symbol::intern("crate_okay")) {
|
||||
cx.lint(CRATE_NOT_OKAY, |lint| {
|
||||
lint.build("crate is not marked with #![crate_okay]")
|
||||
.set_span(krate.module().inner)
|
||||
.emit()
|
||||
lint.build("crate is not marked with #![crate_okay]").set_span(span).emit()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,13 @@ LL | pub fn foo() {}
|
|||
|
||||
error: requires `sized` lang_item
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
error: requires `sized` lang_item
|
||||
|
||||
error: requires `sized` lang_item
|
||||
|
||||
error: requires `sized` lang_item
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0432, E0603.
|
||||
For more information about an error, try `rustc --explain E0432`.
|
||||
|
|
|
|||
|
|
@ -6,6 +6,12 @@ LL | use bar::gpriv;
|
|||
|
||||
error: requires `sized` lang_item
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
error: requires `sized` lang_item
|
||||
|
||||
error: requires `sized` lang_item
|
||||
|
||||
error: requires `sized` lang_item
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0432`.
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
//! lint on missing cargo common metadata
|
||||
|
||||
use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
|
||||
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
|
||||
use rustc_hir::hir_id::CRATE_HIR_ID;
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
use rustc_span::source_map::DUMMY_SP;
|
||||
|
|
@ -77,7 +77,7 @@ fn is_empty_vec(value: &[String]) -> bool {
|
|||
}
|
||||
|
||||
impl LateLintPass<'_> for CargoCommonMetadata {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
||||
if is_lint_allowed(cx, CARGO_COMMON_METADATA, CRATE_HIR_ID) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
use clippy_utils::fn_def_id;
|
||||
|
||||
use rustc_hir::{def::Res, def_id::DefIdMap, Crate, Expr};
|
||||
use rustc_hir::{def::Res, def_id::DefIdMap, Expr};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
|
||||
|
|
@ -70,7 +70,7 @@ impl DisallowedMethod {
|
|||
impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
||||
for conf in &self.conf_disallowed {
|
||||
let (path, reason) = match conf {
|
||||
conf::DisallowedMethod::Simple(path) => (path, None),
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint;
|
|||
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_hir::{
|
||||
def::Res, def_id::DefId, Crate, Item, ItemKind, PolyTraitRef, PrimTy, TraitBoundModifier, Ty, TyKind, UseKind,
|
||||
def::Res, def_id::DefId, Item, ItemKind, PolyTraitRef, PrimTy, TraitBoundModifier, Ty, TyKind, UseKind,
|
||||
};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
|
|
@ -75,7 +75,7 @@ impl DisallowedType {
|
|||
impl_lint_pass!(DisallowedType => [DISALLOWED_TYPE]);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for DisallowedType {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
||||
for path in &self.disallowed {
|
||||
let segs = path.iter().map(ToString::to_string).collect::<Vec<_>>();
|
||||
match clippy_utils::path_to_res(cx, &segs.iter().map(String::as_str).collect::<Vec<_>>()) {
|
||||
|
|
|
|||
|
|
@ -212,7 +212,7 @@ impl_lint_pass!(DocMarkdown =>
|
|||
);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
|
||||
fn check_crate(&mut self, cx: &LateContext<'tcx>, _: &'tcx hir::Crate<'_>) {
|
||||
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
|
||||
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
|
||||
check_attrs(cx, &self.valid_idents, attrs);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_help;
|
||||
use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
|
||||
use rustc_hir::{Crate, CRATE_HIR_ID};
|
||||
use rustc_hir::CRATE_HIR_ID;
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::source_map::DUMMY_SP;
|
||||
|
|
@ -110,7 +110,7 @@ fn lint(cx: &LateContext<'_>, feature: &str, substring: &str, is_prefix: bool) {
|
|||
}
|
||||
|
||||
impl LateLintPass<'_> for FeatureName {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
||||
if is_lint_allowed(cx, REDUNDANT_FEATURE_NAMES, CRATE_HIR_ID)
|
||||
&& is_lint_allowed(cx, NEGATIVE_FEATURE_NAMES, CRATE_HIR_ID)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_note;
|
||||
use clippy_utils::{in_macro, is_lint_allowed};
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_hir::{def_id::LocalDefId, Crate, Item, ItemKind, Node};
|
||||
use rustc_hir::{def_id::LocalDefId, Item, ItemKind, Node};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::Span;
|
||||
|
|
@ -44,7 +44,7 @@ declare_clippy_lint! {
|
|||
declare_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
|
||||
fn check_crate_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Crate<'_>) {
|
||||
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
|
||||
// Map from a type to it's first impl block. Needed to distinguish generic arguments.
|
||||
// e.g. `Foo<Bar>` and `Foo<Baz>`
|
||||
let mut type_map = FxHashMap::default();
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
|
|||
}
|
||||
}
|
||||
#[allow(clippy::too_many_lines)]
|
||||
fn check_crate_post(&mut self, cx: &LateContext<'_>, _krate: &hir::Crate<'_>) {
|
||||
fn check_crate_post(&mut self, cx: &LateContext<'_>) {
|
||||
let mut used = FxHashMap::default();
|
||||
let mut check_dup = vec![];
|
||||
for (import, span) in &self.imports {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
|
|||
use clippy_utils::source::snippet;
|
||||
use clippy_utils::{is_entrypoint_fn, is_no_std_crate};
|
||||
use if_chain::if_chain;
|
||||
use rustc_hir::{Crate, Expr, ExprKind, QPath};
|
||||
use rustc_hir::{Expr, ExprKind, QPath};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ pub struct MainRecursion {
|
|||
impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);
|
||||
|
||||
impl LateLintPass<'_> for MainRecursion {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
||||
self.has_no_std_attr = is_no_std_crate(cx);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ use rustc_hir as hir;
|
|||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_middle::ty;
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
use rustc_span::def_id::CRATE_DEF_ID;
|
||||
use rustc_span::source_map::Span;
|
||||
use rustc_span::sym;
|
||||
|
||||
|
|
@ -78,9 +79,7 @@ impl MissingDoc {
|
|||
return;
|
||||
}
|
||||
|
||||
let has_doc = attrs
|
||||
.iter()
|
||||
.any(|a| a.doc_str().is_some());
|
||||
let has_doc = attrs.iter().any(|a| a.doc_str().is_some());
|
||||
if !has_doc {
|
||||
span_lint(
|
||||
cx,
|
||||
|
|
@ -104,9 +103,9 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
|
|||
self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
|
||||
}
|
||||
|
||||
fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
|
||||
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
|
||||
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
|
||||
self.check_missing_docs_attrs(cx, attrs, krate.module().inner, "the", "crate");
|
||||
self.check_missing_docs_attrs(cx, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
|
||||
}
|
||||
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_opt};
|
|||
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{def::Res, def_id::DefId, Crate, Item, ItemKind, UseKind};
|
||||
use rustc_hir::{def::Res, def_id::DefId, Item, ItemKind, UseKind};
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
use rustc_span::Symbol;
|
||||
|
|
@ -55,7 +55,7 @@ impl ImportRename {
|
|||
impl_lint_pass!(ImportRename => [MISSING_ENFORCED_IMPORT_RENAMES]);
|
||||
|
||||
impl LateLintPass<'_> for ImportRename {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
||||
for Rename { path, rename } in &self.conf_renames {
|
||||
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &path.split("::").collect::<Vec<_>>()) {
|
||||
self.renames.insert(id, Symbol::intern(rename));
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
use clippy_utils::diagnostics::span_lint;
|
||||
use clippy_utils::is_lint_allowed;
|
||||
use rustc_hir::def_id::LOCAL_CRATE;
|
||||
use rustc_hir::{Crate, CRATE_HIR_ID};
|
||||
use rustc_hir::CRATE_HIR_ID;
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::source_map::DUMMY_SP;
|
||||
|
|
@ -41,7 +41,7 @@ declare_clippy_lint! {
|
|||
declare_lint_pass!(MultipleCrateVersions => [MULTIPLE_CRATE_VERSIONS]);
|
||||
|
||||
impl LateLintPass<'_> for MultipleCrateVersions {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
||||
if is_lint_allowed(cx, MULTIPLE_CRATE_VERSIONS, CRATE_HIR_ID) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_hir::def::{DefKind, Res};
|
||||
use rustc_hir::{Crate, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
|
||||
use rustc_hir::{Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::AssocKind;
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
|
|
@ -46,10 +46,10 @@ struct ExistingName {
|
|||
}
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
|
||||
fn check_crate_post(&mut self, cx: &LateContext<'tcx>, krate: &'tcx Crate<'tcx>) {
|
||||
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
|
||||
let mut map = FxHashMap::<Res, ExistingName>::default();
|
||||
|
||||
for item in krate.items() {
|
||||
for item in cx.tcx.hir().items() {
|
||||
if let ItemKind::Impl(Impl {
|
||||
items,
|
||||
of_trait,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
|
||||
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
|
||||
use rustc_hir::hir_id::CRATE_HIR_ID;
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::source_map::DUMMY_SP;
|
||||
|
|
@ -28,7 +28,7 @@ declare_clippy_lint! {
|
|||
declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
|
||||
|
||||
impl LateLintPass<'_> for WildcardDependencies {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
||||
if is_lint_allowed(cx, WILDCARD_DEPENDENCIES, CRATE_HIR_ID) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue