1
Fork 0

Make LevelAndSource a struct

This commit is contained in:
Oli Scherer 2025-03-19 09:41:38 +00:00
parent f3eaf1624c
commit c51816ee59
23 changed files with 105 additions and 86 deletions

View file

@ -29,6 +29,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
use rustc_hir::intravisit::FnKind as HirFnKind;
use rustc_hir::{Body, FnDecl, GenericParamKind, PatKind, PredicateOrigin};
use rustc_middle::bug;
use rustc_middle::lint::LevelAndSource;
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, Upcast, VariantDef};
@ -694,7 +695,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingDebugImplementations {
}
// Avoid listing trait impls if the trait is allowed.
let (level, _) = cx.tcx.lint_level_at_node(MISSING_DEBUG_IMPLEMENTATIONS, item.hir_id());
let LevelAndSource { level, .. } =
cx.tcx.lint_level_at_node(MISSING_DEBUG_IMPLEMENTATIONS, item.hir_id());
if level == Level::Allow {
return;
}

View file

@ -643,7 +643,7 @@ impl<'tcx> LintContext for LateContext<'tcx> {
}
fn get_lint_level(&self, lint: &'static Lint) -> Level {
self.tcx.lint_level_at_node(lint, self.last_node_with_lint_attrs).0
self.tcx.lint_level_at_node(lint, self.last_node_with_lint_attrs).level
}
}
@ -664,7 +664,7 @@ impl LintContext for EarlyContext<'_> {
}
fn get_lint_level(&self, lint: &'static Lint) -> Level {
self.builder.lint_level(lint).0
self.builder.lint_level(lint).level
}
}

View file

@ -87,7 +87,7 @@ impl LintLevelSets {
let level = reveal_actual_level(level, &mut src, sess, lint, |id| {
self.raw_lint_id_level(id, idx, aux)
});
(level, src)
LevelAndSource { level, src }
}
fn raw_lint_id_level(
@ -97,14 +97,14 @@ impl LintLevelSets {
aux: Option<&FxIndexMap<LintId, LevelAndSource>>,
) -> (Option<Level>, LintLevelSource) {
if let Some(specs) = aux
&& let Some(&(level, src)) = specs.get(&id)
&& let Some(&LevelAndSource { level, src }) = specs.get(&id)
{
return (Some(level), src);
}
loop {
let LintSet { ref specs, parent } = self.list[idx];
if let Some(&(level, src)) = specs.get(&id) {
if let Some(&LevelAndSource { level, src }) = specs.get(&id) {
return (Some(level), src);
}
if idx == COMMAND_LINE {
@ -131,8 +131,8 @@ fn lints_that_dont_need_to_run(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LintId> {
})
.filter_map(|lint| {
let lint_level = map.lint_level_id_at_node(tcx, LintId::of(lint), CRATE_HIR_ID);
if matches!(lint_level, (Level::Allow, ..))
|| (matches!(lint_level, (.., LintLevelSource::Default)))
if matches!(lint_level.level, Level::Allow)
|| (matches!(lint_level.src, LintLevelSource::Default))
&& lint.default_level(tcx.sess.edition()) == Level::Allow
{
Some(LintId::of(lint))
@ -595,7 +595,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
};
for id in ids {
// ForceWarn and Forbid cannot be overridden
if let Some((Level::ForceWarn(_) | Level::Forbid, _)) =
if let Some(LevelAndSource { level: Level::ForceWarn(_) | Level::Forbid, .. }) =
self.current_specs().get(&id)
{
continue;
@ -603,7 +603,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
if self.check_gated_lint(id, DUMMY_SP, true) {
let src = LintLevelSource::CommandLine(lint_flag_val, orig_level);
self.insert(id, (level, src));
self.insert(id, LevelAndSource { level, src });
}
}
}
@ -612,8 +612,9 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
/// Attempts to insert the `id` to `level_src` map entry. If unsuccessful
/// (e.g. if a forbid was already inserted on the same scope), then emits a
/// diagnostic with no change to `specs`.
fn insert_spec(&mut self, id: LintId, (level, src): LevelAndSource) {
let (old_level, old_src) = self.provider.get_lint_level(id.lint, self.sess);
fn insert_spec(&mut self, id: LintId, LevelAndSource { level, src }: LevelAndSource) {
let LevelAndSource { level: old_level, src: old_src } =
self.provider.get_lint_level(id.lint, self.sess);
// Setting to a non-forbid level is an error if the lint previously had
// a forbid level. Note that this is not necessarily true even with a
@ -693,13 +694,16 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
match (old_level, level) {
// If the new level is an expectation store it in `ForceWarn`
(Level::ForceWarn(_), Level::Expect(expectation_id)) => {
self.insert(id, (Level::ForceWarn(Some(expectation_id)), old_src))
}
(Level::ForceWarn(_), Level::Expect(expectation_id)) => self.insert(
id,
LevelAndSource { level: Level::ForceWarn(Some(expectation_id)), src: old_src },
),
// Keep `ForceWarn` level but drop the expectation
(Level::ForceWarn(_), _) => self.insert(id, (Level::ForceWarn(None), old_src)),
(Level::ForceWarn(_), _) => {
self.insert(id, LevelAndSource { level: Level::ForceWarn(None), src: old_src })
}
// Set the lint level as normal
_ => self.insert(id, (level, src)),
_ => self.insert(id, LevelAndSource { level, src }),
};
}
@ -714,7 +718,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
if attr.has_name(sym::automatically_derived) {
self.insert(
LintId::of(SINGLE_USE_LIFETIMES),
(Level::Allow, LintLevelSource::Default),
LevelAndSource { level: Level::Allow, src: LintLevelSource::Default },
);
continue;
}
@ -725,7 +729,10 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
.meta_item_list()
.is_some_and(|l| ast::attr::list_contains_name(&l, sym::hidden))
{
self.insert(LintId::of(MISSING_DOCS), (Level::Allow, LintLevelSource::Default));
self.insert(
LintId::of(MISSING_DOCS),
LevelAndSource { level: Level::Allow, src: LintLevelSource::Default },
);
continue;
}
@ -933,7 +940,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
let src = LintLevelSource::Node { name, span: sp, reason };
for &id in ids {
if self.check_gated_lint(id, sp, false) {
self.insert_spec(id, (level, src));
self.insert_spec(id, LevelAndSource { level, src });
}
}
@ -964,7 +971,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
}
if self.lint_added_lints && !is_crate_node {
for (id, &(level, ref src)) in self.current_specs().iter() {
for (id, &LevelAndSource { level, ref src }) in self.current_specs().iter() {
if !id.lint.crate_level_only {
continue;
}
@ -1002,10 +1009,10 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
if self.lint_added_lints {
let lint = builtin::UNKNOWN_LINTS;
let (level, src) = self.lint_level(builtin::UNKNOWN_LINTS);
let level = self.lint_level(builtin::UNKNOWN_LINTS);
// FIXME: make this translatable
#[allow(rustc::diagnostic_outside_of_impl)]
lint_level(self.sess, lint, level, src, Some(span.into()), |lint| {
lint_level(self.sess, lint, level, Some(span.into()), |lint| {
lint.primary_message(fluent::lint_unknown_gated_lint);
lint.arg("name", lint_id.lint.name_lower());
lint.note(fluent::lint_note);
@ -1040,8 +1047,8 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
span: Option<MultiSpan>,
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
) {
let (level, src) = self.lint_level(lint);
lint_level(self.sess, lint, level, src, span, decorate)
let level = self.lint_level(lint);
lint_level(self.sess, lint, level, span, decorate)
}
#[track_caller]
@ -1051,16 +1058,16 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
span: MultiSpan,
decorate: impl for<'a> LintDiagnostic<'a, ()>,
) {
let (level, src) = self.lint_level(lint);
lint_level(self.sess, lint, level, src, Some(span), |lint| {
let level = self.lint_level(lint);
lint_level(self.sess, lint, level, Some(span), |lint| {
decorate.decorate_lint(lint);
});
}
#[track_caller]
pub fn emit_lint(&self, lint: &'static Lint, decorate: impl for<'a> LintDiagnostic<'a, ()>) {
let (level, src) = self.lint_level(lint);
lint_level(self.sess, lint, level, src, None, |lint| {
let level = self.lint_level(lint);
lint_level(self.sess, lint, level, None, |lint| {
decorate.decorate_lint(lint);
});
}

View file

@ -159,12 +159,13 @@ impl EarlyLintPass for NonAsciiIdents {
use rustc_span::Span;
use unicode_security::GeneralSecurityProfile;
let check_non_ascii_idents = cx.builder.lint_level(NON_ASCII_IDENTS).0 != Level::Allow;
let check_non_ascii_idents = cx.builder.lint_level(NON_ASCII_IDENTS).level != Level::Allow;
let check_uncommon_codepoints =
cx.builder.lint_level(UNCOMMON_CODEPOINTS).0 != Level::Allow;
let check_confusable_idents = cx.builder.lint_level(CONFUSABLE_IDENTS).0 != Level::Allow;
cx.builder.lint_level(UNCOMMON_CODEPOINTS).level != Level::Allow;
let check_confusable_idents =
cx.builder.lint_level(CONFUSABLE_IDENTS).level != Level::Allow;
let check_mixed_script_confusables =
cx.builder.lint_level(MIXED_SCRIPT_CONFUSABLES).0 != Level::Allow;
cx.builder.lint_level(MIXED_SCRIPT_CONFUSABLES).level != Level::Allow;
if !check_non_ascii_idents
&& !check_uncommon_codepoints