Skip to content

Commit

Permalink
rustc_lint: replace free function with extension trait
Browse files Browse the repository at this point in the history
This is a bit more discoverable.
  • Loading branch information
tamird committed Nov 22, 2023
1 parent 55393b6 commit e5139e6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use rustc_feature::find_gated_cfg;
use rustc_fluent_macro::fluent_messages;
use rustc_interface::util::{self, collect_crate_types, get_codegen_backend};
use rustc_interface::{interface, Queries};
use rustc_lint::unerased_lint_store;
use rustc_lint::SessionExt;
use rustc_metadata::locator;
use rustc_session::config::{nightly_options, CG_OPTIONS, Z_OPTIONS};
use rustc_session::config::{ErrorOutputType, Input, OutFileName, OutputType, TrimmedDefPaths};
Expand Down Expand Up @@ -979,7 +979,7 @@ Available lint options:
lints
}

let lint_store = unerased_lint_store(sess);
let lint_store = sess.lint_store();
let (loaded, builtin): (Vec<_>, _) =
lint_store.get_lints().iter().cloned().partition(|&lint| lint.is_loaded);
let loaded = sort_lints(sess, loaded);
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_expand::base::{ExtCtxt, LintStoreExpand};
use rustc_feature::Features;
use rustc_fs_util::try_canonicalize;
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
use rustc_lint::{unerased_lint_store, BufferedEarlyLint, EarlyCheckNode, LintStore};
use rustc_lint::{BufferedEarlyLint, EarlyCheckNode, LintStore, SessionExt};
use rustc_metadata::creader::CStore;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
Expand Down Expand Up @@ -127,7 +127,7 @@ fn configure_and_expand(
let tcx = resolver.tcx();
let sess = tcx.sess;
let features = tcx.features();
let lint_store = unerased_lint_store(tcx.sess);
let lint_store = tcx.sess.lint_store();
let crate_name = tcx.crate_name(LOCAL_CRATE);
let lint_check_node = (&krate, pre_configured_attrs);
pre_expansion_lint(
Expand Down Expand Up @@ -319,7 +319,7 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
}
});

let lint_store = unerased_lint_store(tcx.sess);
let lint_store = tcx.sess.lint_store();
rustc_lint::check_ast_node(
sess,
tcx.features(),
Expand Down
26 changes: 13 additions & 13 deletions compiler/rustc_lint/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ use rustc_span::Span;
use std::any::Any;
use std::cell::Cell;

/// Extract the [`LintStore`] from [`Session`].
///
/// This function exists because [`Session::lint_store`] is type-erased.
pub fn unerased_lint_store(sess: &Session) -> &LintStore {
let store: &Lrc<_> = sess.lint_store.as_ref().unwrap();
let store: &dyn Any = &**store;
store.downcast_ref().unwrap()
pub trait SessionExt {
fn lint_store(&self) -> &LintStore;
}

impl SessionExt for Session {
fn lint_store(&self) -> &LintStore {
let store: &Lrc<_> = self.lint_store.as_ref().unwrap();
let store: &dyn Any = &**store;
store.downcast_ref().unwrap()
}
}

macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
Expand Down Expand Up @@ -364,11 +367,8 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
// Note: `passes` is often empty. In that case, it's faster to run
// `builtin_lints` directly rather than bundling it up into the
// `RuntimeCombinedLateLintPass`.
let mut passes: Vec<_> = unerased_lint_store(tcx.sess)
.late_module_passes
.iter()
.map(|mk_pass| (mk_pass)(tcx))
.collect();
let mut passes: Vec<_> =
tcx.sess.lint_store().late_module_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();
if passes.is_empty() {
late_lint_mod_inner(tcx, module_def_id, context, builtin_lints);
} else {
Expand Down Expand Up @@ -405,7 +405,7 @@ fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>(
fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
// Note: `passes` is often empty.
let mut passes: Vec<_> =
unerased_lint_store(tcx.sess).late_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();
tcx.sess.lint_store().late_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();

if passes.is_empty() {
return;
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
builtin::MISSING_DOCS,
context::{CheckLintNameResult, LintStore},
fluent_generated as fluent,
late::unerased_lint_store,
late::SessionExt,
lints::{
DeprecatedLintName, IgnoredUnlessCrateSpecified, OverruledAttributeLint, RemovedLint,
RenamedLint, RenamedLintSuggestion, UnknownLint, UnknownLintSuggestion,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl LintLevelSets {
}

fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExpectation)> {
let store = unerased_lint_store(tcx.sess);
let store = tcx.sess.lint_store();

let mut builder = LintLevelsBuilder {
sess: tcx.sess,
Expand Down Expand Up @@ -151,7 +151,7 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp

#[instrument(level = "trace", skip(tcx), ret)]
fn shallow_lint_levels_on(tcx: TyCtxt<'_>, owner: hir::OwnerId) -> ShallowLintLevelMap {
let store = unerased_lint_store(tcx.sess);
let store = tcx.sess.lint_store();
let attrs = tcx.hir_attrs(owner);

let mut levels = LintLevelsBuilder {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub use builtin::{MissingDoc, SoftLints};
pub use context::{CheckLintNameResult, FindLintError, LintStore};
pub use context::{EarlyContext, LateContext, LintContext};
pub use early::{check_ast_node, EarlyCheckNode};
pub use late::{check_crate, late_lint_mod, unerased_lint_store};
pub use late::{check_crate, late_lint_mod, SessionExt};
pub use passes::{EarlyLintPass, LateLintPass};
pub use rustc_session::lint::Level::{self, *};
pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Lint, LintId};
Expand Down

0 comments on commit e5139e6

Please sign in to comment.