]> git.lizzy.rs Git - rust.git/commitdiff
Move lint levels machanism in librustc_lint.
authorCamille GILLOT <gillot.camille@gmail.com>
Mon, 30 Dec 2019 18:02:52 +0000 (19:02 +0100)
committerCamille GILLOT <gillot.camille@gmail.com>
Wed, 1 Jan 2020 12:20:33 +0000 (13:20 +0100)
src/librustc/lint/levels.rs
src/librustc/lint/mod.rs
src/librustc_interface/passes.rs
src/librustc_lint/levels.rs [new file with mode: 0644]
src/librustc_lint/lib.rs

index edf7df16c87faefa49d5ff5ad70349b0173e537f..97211f7082457b8c0d229ab2b8a7c05a946b758b 100644 (file)
@@ -155,7 +155,7 @@ pub struct LintLevelsBuilder<'a> {
 
 pub struct BuilderPush {
     prev: u32,
-    pub(super) changed: bool,
+    pub changed: bool,
 }
 
 impl<'a> LintLevelsBuilder<'a> {
index ba9076f2e35b2425d973689f13f3b05452c186b3..31f3f0d1737e26ccbc7ee38856beecee66fab9af 100644 (file)
 use rustc_data_structures::sync;
 
 use crate::hir;
-use crate::hir::def_id::{CrateNum, LOCAL_CRATE};
-use crate::hir::intravisit;
 use crate::lint::builtin::BuiltinLintDiagnostics;
 use crate::session::{DiagnosticMessageId, Session};
-use crate::ty::query::Providers;
 use crate::ty::TyCtxt;
 use crate::util::nodemap::NodeMap;
 use errors::{DiagnosticBuilder, DiagnosticId};
@@ -375,7 +372,7 @@ pub enum LintSource {
 pub mod internal;
 mod levels;
 
-pub use self::levels::{LintLevelMap, LintLevelSets};
+pub use self::levels::{LintLevelMap, LintLevelSets, LintLevelsBuilder};
 
 #[derive(Default)]
 pub struct LintBuffer {
@@ -563,122 +560,6 @@ pub fn maybe_lint_level_root(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
     attrs.iter().any(|attr| Level::from_symbol(attr.name_or_empty()).is_some())
 }
 
-fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
-    assert_eq!(cnum, LOCAL_CRATE);
-    let store = &tcx.lint_store;
-    let mut builder = LintLevelMapBuilder {
-        levels: LintLevelSets::builder(tcx.sess, false, &store),
-        tcx: tcx,
-        store: store,
-    };
-    let krate = tcx.hir().krate();
-
-    let push = builder.levels.push(&krate.attrs, &store);
-    builder.levels.register_id(hir::CRATE_HIR_ID);
-    for macro_def in krate.exported_macros {
-        builder.levels.register_id(macro_def.hir_id);
-    }
-    intravisit::walk_crate(&mut builder, krate);
-    builder.levels.pop(push);
-
-    tcx.arena.alloc(builder.levels.build_map())
-}
-
-struct LintLevelMapBuilder<'a, 'tcx> {
-    levels: levels::LintLevelsBuilder<'tcx>,
-    tcx: TyCtxt<'tcx>,
-    store: &'a LintStore,
-}
-
-impl LintLevelMapBuilder<'_, '_> {
-    fn with_lint_attrs<F>(&mut self, id: hir::HirId, attrs: &[ast::Attribute], f: F)
-    where
-        F: FnOnce(&mut Self),
-    {
-        let push = self.levels.push(attrs, self.store);
-        if push.changed {
-            self.levels.register_id(id);
-        }
-        f(self);
-        self.levels.pop(push);
-    }
-}
-
-impl intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
-    fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
-        intravisit::NestedVisitorMap::All(&self.tcx.hir())
-    }
-
-    fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
-        self.with_lint_attrs(param.hir_id, &param.attrs, |builder| {
-            intravisit::walk_param(builder, param);
-        });
-    }
-
-    fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
-        self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
-            intravisit::walk_item(builder, it);
-        });
-    }
-
-    fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
-        self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
-            intravisit::walk_foreign_item(builder, it);
-        })
-    }
-
-    fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
-        self.with_lint_attrs(e.hir_id, &e.attrs, |builder| {
-            intravisit::walk_expr(builder, e);
-        })
-    }
-
-    fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) {
-        self.with_lint_attrs(s.hir_id, &s.attrs, |builder| {
-            intravisit::walk_struct_field(builder, s);
-        })
-    }
-
-    fn visit_variant(
-        &mut self,
-        v: &'tcx hir::Variant<'tcx>,
-        g: &'tcx hir::Generics<'tcx>,
-        item_id: hir::HirId,
-    ) {
-        self.with_lint_attrs(v.id, &v.attrs, |builder| {
-            intravisit::walk_variant(builder, v, g, item_id);
-        })
-    }
-
-    fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
-        self.with_lint_attrs(l.hir_id, &l.attrs, |builder| {
-            intravisit::walk_local(builder, l);
-        })
-    }
-
-    fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
-        self.with_lint_attrs(a.hir_id, &a.attrs, |builder| {
-            intravisit::walk_arm(builder, a);
-        })
-    }
-
-    fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
-        self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |builder| {
-            intravisit::walk_trait_item(builder, trait_item);
-        });
-    }
-
-    fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
-        self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |builder| {
-            intravisit::walk_impl_item(builder, impl_item);
-        });
-    }
-}
-
-pub fn provide(providers: &mut Providers<'_>) {
-    providers.lint_levels = lint_levels;
-}
-
 /// Returns whether `span` originates in a foreign crate's external macro.
 ///
 /// This is used to test whether a lint should not even begin to figure out whether it should
index a796408491f9f3dad2c3c9b66e9c795089293157..22dc0b2de8bf0eb812534ec17b82485d48d05125 100644 (file)
@@ -692,7 +692,6 @@ pub fn default_provide(providers: &mut ty::query::Providers<'_>) {
     rustc_resolve::provide(providers);
     rustc_traits::provide(providers);
     rustc_metadata::provide(providers);
-    lint::provide(providers);
     rustc_lint::provide(providers);
     rustc_codegen_utils::provide(providers);
     rustc_codegen_ssa::provide(providers);
diff --git a/src/librustc_lint/levels.rs b/src/librustc_lint/levels.rs
new file mode 100644 (file)
index 0000000..c148d39
--- /dev/null
@@ -0,0 +1,125 @@
+use rustc::hir;
+use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
+use rustc::hir::intravisit;
+use rustc::lint::{LintLevelMap, LintLevelSets, LintLevelsBuilder, LintStore};
+use rustc::ty::query::Providers;
+use rustc::ty::TyCtxt;
+use syntax::ast;
+
+pub use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintId};
+
+fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
+    assert_eq!(cnum, LOCAL_CRATE);
+    let store = &tcx.lint_store;
+    let mut builder = LintLevelMapBuilder {
+        levels: LintLevelSets::builder(tcx.sess, false, &store),
+        tcx: tcx,
+        store: store,
+    };
+    let krate = tcx.hir().krate();
+
+    let push = builder.levels.push(&krate.attrs, &store);
+    builder.levels.register_id(hir::CRATE_HIR_ID);
+    for macro_def in krate.exported_macros {
+        builder.levels.register_id(macro_def.hir_id);
+    }
+    intravisit::walk_crate(&mut builder, krate);
+    builder.levels.pop(push);
+
+    tcx.arena.alloc(builder.levels.build_map())
+}
+
+struct LintLevelMapBuilder<'a, 'tcx> {
+    levels: LintLevelsBuilder<'tcx>,
+    tcx: TyCtxt<'tcx>,
+    store: &'a LintStore,
+}
+
+impl LintLevelMapBuilder<'_, '_> {
+    fn with_lint_attrs<F>(&mut self, id: hir::HirId, attrs: &[ast::Attribute], f: F)
+    where
+        F: FnOnce(&mut Self),
+    {
+        let push = self.levels.push(attrs, self.store);
+        if push.changed {
+            self.levels.register_id(id);
+        }
+        f(self);
+        self.levels.pop(push);
+    }
+}
+
+impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
+    fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
+        intravisit::NestedVisitorMap::All(&self.tcx.hir())
+    }
+
+    fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
+        self.with_lint_attrs(param.hir_id, &param.attrs, |builder| {
+            intravisit::walk_param(builder, param);
+        });
+    }
+
+    fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
+        self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
+            intravisit::walk_item(builder, it);
+        });
+    }
+
+    fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
+        self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
+            intravisit::walk_foreign_item(builder, it);
+        })
+    }
+
+    fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
+        self.with_lint_attrs(e.hir_id, &e.attrs, |builder| {
+            intravisit::walk_expr(builder, e);
+        })
+    }
+
+    fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) {
+        self.with_lint_attrs(s.hir_id, &s.attrs, |builder| {
+            intravisit::walk_struct_field(builder, s);
+        })
+    }
+
+    fn visit_variant(
+        &mut self,
+        v: &'tcx hir::Variant<'tcx>,
+        g: &'tcx hir::Generics<'tcx>,
+        item_id: hir::HirId,
+    ) {
+        self.with_lint_attrs(v.id, &v.attrs, |builder| {
+            intravisit::walk_variant(builder, v, g, item_id);
+        })
+    }
+
+    fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
+        self.with_lint_attrs(l.hir_id, &l.attrs, |builder| {
+            intravisit::walk_local(builder, l);
+        })
+    }
+
+    fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
+        self.with_lint_attrs(a.hir_id, &a.attrs, |builder| {
+            intravisit::walk_arm(builder, a);
+        })
+    }
+
+    fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
+        self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |builder| {
+            intravisit::walk_trait_item(builder, trait_item);
+        });
+    }
+
+    fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
+        self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |builder| {
+            intravisit::walk_impl_item(builder, impl_item);
+        });
+    }
+}
+
+pub fn provide(providers: &mut Providers<'_>) {
+    providers.lint_levels = lint_levels;
+}
index 42fc353b2b064995a0004d4ebf3ed9c408a05d61..2a3b90f8c93b41f0e14f56556e3b4c1dab219fd6 100644 (file)
@@ -26,6 +26,7 @@
 pub mod builtin;
 mod early;
 mod late;
+mod levels;
 mod non_ascii_idents;
 mod nonstandard_style;
 mod redundant_semicolon;
@@ -63,6 +64,7 @@
 pub use late::check_crate;
 
 pub fn provide(providers: &mut Providers<'_>) {
+    levels::provide(providers);
     *providers = Providers { lint_mod, ..*providers };
 }