Skip to content

Commit 144e27d

Browse files
committed
Make LevelAndSource a struct
1 parent 4be5f7e commit 144e27d

File tree

23 files changed

+105
-86
lines changed

23 files changed

+105
-86
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,9 @@ fn link_natively(
959959
}
960960
}
961961

962-
let (level, src) = codegen_results.crate_info.lint_levels.linker_messages;
962+
let level = codegen_results.crate_info.lint_levels.linker_messages;
963963
let lint = |msg| {
964-
lint_level(sess, LINKER_MESSAGES, level, src, None, |diag| {
964+
lint_level(sess, LINKER_MESSAGES, level, None, |diag| {
965965
LinkerOutput { inner: msg }.decorate_lint(diag)
966966
})
967967
};

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc_hir::CRATE_HIR_ID;
3434
use rustc_hir::def_id::CrateNum;
3535
use rustc_macros::{Decodable, Encodable, HashStable};
3636
use rustc_middle::dep_graph::WorkProduct;
37-
use rustc_middle::lint::LintLevelSource;
37+
use rustc_middle::lint::LevelAndSource;
3838
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
3939
use rustc_middle::middle::dependency_format::Dependencies;
4040
use rustc_middle::middle::exported_symbols::SymbolExportKind;
@@ -45,7 +45,6 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
4545
use rustc_session::Session;
4646
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
4747
use rustc_session::cstore::{self, CrateSource};
48-
use rustc_session::lint::Level;
4948
use rustc_session::lint::builtin::LINKER_MESSAGES;
5049
use rustc_session::utils::NativeLibKind;
5150
use rustc_span::Symbol;
@@ -341,7 +340,7 @@ impl CodegenResults {
341340
/// Instead, encode exactly the information we need.
342341
#[derive(Copy, Clone, Debug, Encodable, Decodable)]
343342
pub struct CodegenLintLevels {
344-
linker_messages: (Level, LintLevelSource),
343+
linker_messages: LevelAndSource,
345344
}
346345

347346
impl CodegenLintLevels {

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
546546
rustc_session::lint::builtin::LONG_RUNNING_CONST_EVAL,
547547
hir_id,
548548
)
549-
.0
549+
.level
550550
.is_error();
551551
let span = ecx.cur_span();
552552
ecx.tcx.emit_node_span_lint(

compiler/rustc_hir_typeck/src/upvar.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,8 +2325,9 @@ fn should_do_rust_2021_incompatible_closure_captures_analysis(
23252325
return false;
23262326
}
23272327

2328-
let (level, _) =
2329-
tcx.lint_level_at_node(lint::builtin::RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES, closure_id);
2328+
let level = tcx
2329+
.lint_level_at_node(lint::builtin::RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES, closure_id)
2330+
.level;
23302331

23312332
!matches!(level, lint::Level::Allow)
23322333
}

compiler/rustc_lint/src/builtin.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
2929
use rustc_hir::intravisit::FnKind as HirFnKind;
3030
use rustc_hir::{Body, FnDecl, GenericParamKind, PatKind, PredicateOrigin};
3131
use rustc_middle::bug;
32+
use rustc_middle::lint::LevelAndSource;
3233
use rustc_middle::ty::layout::LayoutOf;
3334
use rustc_middle::ty::print::with_no_trimmed_paths;
3435
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, Upcast, VariantDef};
@@ -694,7 +695,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingDebugImplementations {
694695
}
695696

696697
// Avoid listing trait impls if the trait is allowed.
697-
let (level, _) = cx.tcx.lint_level_at_node(MISSING_DEBUG_IMPLEMENTATIONS, item.hir_id());
698+
let LevelAndSource { level, .. } =
699+
cx.tcx.lint_level_at_node(MISSING_DEBUG_IMPLEMENTATIONS, item.hir_id());
698700
if level == Level::Allow {
699701
return;
700702
}

compiler/rustc_lint/src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ impl<'tcx> LintContext for LateContext<'tcx> {
643643
}
644644

645645
fn get_lint_level(&self, lint: &'static Lint) -> Level {
646-
self.tcx.lint_level_at_node(lint, self.last_node_with_lint_attrs).0
646+
self.tcx.lint_level_at_node(lint, self.last_node_with_lint_attrs).level
647647
}
648648
}
649649

@@ -664,7 +664,7 @@ impl LintContext for EarlyContext<'_> {
664664
}
665665

666666
fn get_lint_level(&self, lint: &'static Lint) -> Level {
667-
self.builder.lint_level(lint).0
667+
self.builder.lint_level(lint).level
668668
}
669669
}
670670

compiler/rustc_lint/src/levels.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl LintLevelSets {
8787
let level = reveal_actual_level(level, &mut src, sess, lint, |id| {
8888
self.raw_lint_id_level(id, idx, aux)
8989
});
90-
(level, src)
90+
LevelAndSource { level, src }
9191
}
9292

9393
fn raw_lint_id_level(
@@ -97,14 +97,14 @@ impl LintLevelSets {
9797
aux: Option<&FxIndexMap<LintId, LevelAndSource>>,
9898
) -> (Option<Level>, LintLevelSource) {
9999
if let Some(specs) = aux
100-
&& let Some(&(level, src)) = specs.get(&id)
100+
&& let Some(&LevelAndSource { level, src }) = specs.get(&id)
101101
{
102102
return (Some(level), src);
103103
}
104104

105105
loop {
106106
let LintSet { ref specs, parent } = self.list[idx];
107-
if let Some(&(level, src)) = specs.get(&id) {
107+
if let Some(&LevelAndSource { level, src }) = specs.get(&id) {
108108
return (Some(level), src);
109109
}
110110
if idx == COMMAND_LINE {
@@ -131,8 +131,8 @@ fn lints_that_dont_need_to_run(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LintId> {
131131
})
132132
.filter_map(|lint| {
133133
let lint_level = map.lint_level_id_at_node(tcx, LintId::of(lint), CRATE_HIR_ID);
134-
if matches!(lint_level, (Level::Allow, ..))
135-
|| (matches!(lint_level, (.., LintLevelSource::Default)))
134+
if matches!(lint_level.level, Level::Allow)
135+
|| (matches!(lint_level.src, LintLevelSource::Default))
136136
&& lint.default_level(tcx.sess.edition()) == Level::Allow
137137
{
138138
Some(LintId::of(lint))
@@ -582,15 +582,15 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
582582
};
583583
for id in ids {
584584
// ForceWarn and Forbid cannot be overridden
585-
if let Some((Level::ForceWarn(_) | Level::Forbid, _)) =
585+
if let Some(LevelAndSource { level: Level::ForceWarn(_) | Level::Forbid, .. }) =
586586
self.current_specs().get(&id)
587587
{
588588
continue;
589589
}
590590

591591
if self.check_gated_lint(id, DUMMY_SP, true) {
592592
let src = LintLevelSource::CommandLine(lint_flag_val, orig_level);
593-
self.insert(id, (level, src));
593+
self.insert(id, LevelAndSource { level, src });
594594
}
595595
}
596596
}
@@ -599,8 +599,9 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
599599
/// Attempts to insert the `id` to `level_src` map entry. If unsuccessful
600600
/// (e.g. if a forbid was already inserted on the same scope), then emits a
601601
/// diagnostic with no change to `specs`.
602-
fn insert_spec(&mut self, id: LintId, (level, src): LevelAndSource) {
603-
let (old_level, old_src) = self.provider.get_lint_level(id.lint, self.sess);
602+
fn insert_spec(&mut self, id: LintId, LevelAndSource { level, src }: LevelAndSource) {
603+
let LevelAndSource { level: old_level, src: old_src } =
604+
self.provider.get_lint_level(id.lint, self.sess);
604605

605606
// Setting to a non-forbid level is an error if the lint previously had
606607
// a forbid level. Note that this is not necessarily true even with a
@@ -680,13 +681,16 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
680681

681682
match (old_level, level) {
682683
// If the new level is an expectation store it in `ForceWarn`
683-
(Level::ForceWarn(_), Level::Expect(expectation_id)) => {
684-
self.insert(id, (Level::ForceWarn(Some(expectation_id)), old_src))
685-
}
684+
(Level::ForceWarn(_), Level::Expect(expectation_id)) => self.insert(
685+
id,
686+
LevelAndSource { level: Level::ForceWarn(Some(expectation_id)), src: old_src },
687+
),
686688
// Keep `ForceWarn` level but drop the expectation
687-
(Level::ForceWarn(_), _) => self.insert(id, (Level::ForceWarn(None), old_src)),
689+
(Level::ForceWarn(_), _) => {
690+
self.insert(id, LevelAndSource { level: Level::ForceWarn(None), src: old_src })
691+
}
688692
// Set the lint level as normal
689-
_ => self.insert(id, (level, src)),
693+
_ => self.insert(id, LevelAndSource { level, src }),
690694
};
691695
}
692696

@@ -701,7 +705,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
701705
if attr.has_name(sym::automatically_derived) {
702706
self.insert(
703707
LintId::of(SINGLE_USE_LIFETIMES),
704-
(Level::Allow, LintLevelSource::Default),
708+
LevelAndSource { level: Level::Allow, src: LintLevelSource::Default },
705709
);
706710
continue;
707711
}
@@ -712,7 +716,10 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
712716
.meta_item_list()
713717
.is_some_and(|l| ast::attr::list_contains_name(&l, sym::hidden))
714718
{
715-
self.insert(LintId::of(MISSING_DOCS), (Level::Allow, LintLevelSource::Default));
719+
self.insert(
720+
LintId::of(MISSING_DOCS),
721+
LevelAndSource { level: Level::Allow, src: LintLevelSource::Default },
722+
);
716723
continue;
717724
}
718725

@@ -920,7 +927,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
920927
let src = LintLevelSource::Node { name, span: sp, reason };
921928
for &id in ids {
922929
if self.check_gated_lint(id, sp, false) {
923-
self.insert_spec(id, (level, src));
930+
self.insert_spec(id, LevelAndSource { level, src });
924931
}
925932
}
926933

@@ -951,7 +958,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
951958
}
952959

953960
if self.lint_added_lints && !is_crate_node {
954-
for (id, &(level, ref src)) in self.current_specs().iter() {
961+
for (id, &LevelAndSource { level, ref src }) in self.current_specs().iter() {
955962
if !id.lint.crate_level_only {
956963
continue;
957964
}
@@ -989,10 +996,10 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
989996

990997
if self.lint_added_lints {
991998
let lint = builtin::UNKNOWN_LINTS;
992-
let (level, src) = self.lint_level(builtin::UNKNOWN_LINTS);
999+
let level = self.lint_level(builtin::UNKNOWN_LINTS);
9931000
// FIXME: make this translatable
9941001
#[allow(rustc::diagnostic_outside_of_impl)]
995-
lint_level(self.sess, lint, level, src, Some(span.into()), |lint| {
1002+
lint_level(self.sess, lint, level, Some(span.into()), |lint| {
9961003
lint.primary_message(fluent::lint_unknown_gated_lint);
9971004
lint.arg("name", lint_id.lint.name_lower());
9981005
lint.note(fluent::lint_note);
@@ -1027,8 +1034,8 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
10271034
span: Option<MultiSpan>,
10281035
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
10291036
) {
1030-
let (level, src) = self.lint_level(lint);
1031-
lint_level(self.sess, lint, level, src, span, decorate)
1037+
let level = self.lint_level(lint);
1038+
lint_level(self.sess, lint, level, span, decorate)
10321039
}
10331040

10341041
#[track_caller]
@@ -1038,16 +1045,16 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
10381045
span: MultiSpan,
10391046
decorate: impl for<'a> LintDiagnostic<'a, ()>,
10401047
) {
1041-
let (level, src) = self.lint_level(lint);
1042-
lint_level(self.sess, lint, level, src, Some(span), |lint| {
1048+
let level = self.lint_level(lint);
1049+
lint_level(self.sess, lint, level, Some(span), |lint| {
10431050
decorate.decorate_lint(lint);
10441051
});
10451052
}
10461053

10471054
#[track_caller]
10481055
pub fn emit_lint(&self, lint: &'static Lint, decorate: impl for<'a> LintDiagnostic<'a, ()>) {
1049-
let (level, src) = self.lint_level(lint);
1050-
lint_level(self.sess, lint, level, src, None, |lint| {
1056+
let level = self.lint_level(lint);
1057+
lint_level(self.sess, lint, level, None, |lint| {
10511058
decorate.decorate_lint(lint);
10521059
});
10531060
}

compiler/rustc_lint/src/non_ascii_idents.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,13 @@ impl EarlyLintPass for NonAsciiIdents {
159159
use rustc_span::Span;
160160
use unicode_security::GeneralSecurityProfile;
161161

162-
let check_non_ascii_idents = cx.builder.lint_level(NON_ASCII_IDENTS).0 != Level::Allow;
162+
let check_non_ascii_idents = cx.builder.lint_level(NON_ASCII_IDENTS).level != Level::Allow;
163163
let check_uncommon_codepoints =
164-
cx.builder.lint_level(UNCOMMON_CODEPOINTS).0 != Level::Allow;
165-
let check_confusable_idents = cx.builder.lint_level(CONFUSABLE_IDENTS).0 != Level::Allow;
164+
cx.builder.lint_level(UNCOMMON_CODEPOINTS).level != Level::Allow;
165+
let check_confusable_idents =
166+
cx.builder.lint_level(CONFUSABLE_IDENTS).level != Level::Allow;
166167
let check_mixed_script_confusables =
167-
cx.builder.lint_level(MIXED_SCRIPT_CONFUSABLES).0 != Level::Allow;
168+
cx.builder.lint_level(MIXED_SCRIPT_CONFUSABLES).level != Level::Allow;
168169

169170
if !check_non_ascii_idents
170171
&& !check_uncommon_codepoints

compiler/rustc_metadata/src/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl CStore {
340340
}
341341
let level = tcx
342342
.lint_level_at_node(lint::builtin::UNUSED_CRATE_DEPENDENCIES, rustc_hir::CRATE_HIR_ID)
343-
.0;
343+
.level;
344344
if level != lint::Level::Allow {
345345
let unused_externs =
346346
self.unused_externs.iter().map(|ident| ident.to_ident_string()).collect::<Vec<_>>();

compiler/rustc_middle/src/lint.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ impl LintLevelSource {
5151
}
5252
}
5353

54-
/// A tuple of a lint level and its source.
55-
pub type LevelAndSource = (Level, LintLevelSource);
54+
/// Convenience helper for moving things around together that frequently are paired
55+
#[derive(Copy, Clone, Debug, HashStable, Encodable, Decodable)]
56+
pub struct LevelAndSource {
57+
pub level: Level,
58+
pub src: LintLevelSource,
59+
}
5660

5761
/// Return type for the `shallow_lint_levels_on` query.
5862
///
@@ -123,7 +127,7 @@ impl ShallowLintLevelMap {
123127
start: HirId,
124128
) -> (Option<Level>, LintLevelSource) {
125129
if let Some(map) = self.specs.get(&start.local_id)
126-
&& let Some(&(level, src)) = map.get(&id)
130+
&& let Some(&LevelAndSource { level, src }) = map.get(&id)
127131
{
128132
return (Some(level), src);
129133
}
@@ -137,7 +141,7 @@ impl ShallowLintLevelMap {
137141
specs = &tcx.shallow_lint_levels_on(owner).specs;
138142
}
139143
if let Some(map) = specs.get(&parent.local_id)
140-
&& let Some(&(level, src)) = map.get(&id)
144+
&& let Some(&LevelAndSource { level, src }) = map.get(&id)
141145
{
142146
return (Some(level), src);
143147
}
@@ -153,18 +157,18 @@ impl ShallowLintLevelMap {
153157
tcx: TyCtxt<'_>,
154158
lint: LintId,
155159
cur: HirId,
156-
) -> (Level, LintLevelSource) {
160+
) -> LevelAndSource {
157161
let (level, mut src) = self.probe_for_lint_level(tcx, lint, cur);
158162
let level = reveal_actual_level(level, &mut src, tcx.sess, lint, |lint| {
159163
self.probe_for_lint_level(tcx, lint, cur)
160164
});
161-
(level, src)
165+
LevelAndSource { level, src }
162166
}
163167
}
164168

165169
impl TyCtxt<'_> {
166170
/// Fetch and return the user-visible lint level for the given lint at the given HirId.
167-
pub fn lint_level_at_node(self, lint: &'static Lint, id: HirId) -> (Level, LintLevelSource) {
171+
pub fn lint_level_at_node(self, lint: &'static Lint, id: HirId) -> LevelAndSource {
168172
self.shallow_lint_levels_on(id.owner).lint_level_id_at_node(self, LintId::of(lint), id)
169173
}
170174
}
@@ -267,8 +271,7 @@ fn explain_lint_level_source(
267271
pub fn lint_level(
268272
sess: &Session,
269273
lint: &'static Lint,
270-
level: Level,
271-
src: LintLevelSource,
274+
level: LevelAndSource,
272275
span: Option<MultiSpan>,
273276
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
274277
) {
@@ -278,11 +281,12 @@ pub fn lint_level(
278281
fn lint_level_impl(
279282
sess: &Session,
280283
lint: &'static Lint,
281-
level: Level,
282-
src: LintLevelSource,
284+
level: LevelAndSource,
283285
span: Option<MultiSpan>,
284286
decorate: Box<dyn '_ + for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>)>,
285287
) {
288+
let LevelAndSource { level, src } = level;
289+
286290
// Check for future incompatibility lints and issue a stronger warning.
287291
let future_incompatible = lint.future_incompatible;
288292

@@ -421,5 +425,5 @@ pub fn lint_level(
421425
explain_lint_level_source(lint, level, src, &mut err);
422426
err.emit()
423427
}
424-
lint_level_impl(sess, lint, level, src, span, Box::new(decorate))
428+
lint_level_impl(sess, lint, level, span, Box::new(decorate))
425429
}

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ fn late_report_deprecation(
255255
// Calculating message for lint involves calling `self.def_path_str`,
256256
// which will by default invoke the expensive `visible_parent_map` query.
257257
// Skip all that work if the lint is allowed anyway.
258-
if tcx.lint_level_at_node(lint, hir_id).0 == Level::Allow {
258+
if tcx.lint_level_at_node(lint, hir_id).level == Level::Allow {
259259
return;
260260
}
261261

0 commit comments

Comments
 (0)