Skip to content

Commit 9de008a

Browse files
committed
Auto merge of rust-lang#125208 - fmease:lint-diags-store-their-span, r=<try>
Make lint diagnostics responsible for providing their primary span ### Summary and Rustc-Dev-Facing Changes Instead of passing the primary span of lint diagnostics separately — namely to the functions `tcx.emit_span_lint` and `tcx.emit_node_span_lint` —, make lint diagnostics responsible for “storing” (providing) their span. I've removed the two aforementioned methods. You are now expected to use `tcx.emit_lint` and `tcx.emit_node_lint` respectively and to provide the primary span when deriving `LintDiagnostic` (via `#[primary_span]`) / implementing `LintDiagnostic` manually (via `LintDiagnostic::span`). ### Motivation Making the general format of `#[derive(LintDiagnostic)]` identical to `#[derive(Diagnostic)]`. I bet this has lead to slight confusion or annoyances before. Moreover, it enables deriving both traits at once (see rust-lang#125169 for the motivation). ### Approach As outlined in rust-lang#125169 (comment), the naïve approach of doing the same as `Diagnostic` doesn't work for `LintDiagnostic`, i.e., setting the primary span with `Diag::span` inside `decorate_lint` (that's `into_diag` for `Diagnostic`) because `lint_level` (`lint_level_impl`) needs access to the primary spans before decorating the `Diag` to be able to filter out some lints (namely if they come from an external macro) etc. Therefore, I had to introduce a new method to `LintDiagnostic`: `fn span(&self) -> Option<MultiSpan>` (similar to the existing method `LintDiagnostic::msg`). ### Commits 1. The 1st commit addresses [rust-lang#125169 (comment)](rust-lang#125169 (comment)). It contains the necessary changes to the linting APIs. It doesn't build on its own due to the removed APIs. Split out for easier reviewing. 2. The 2nd commit updates all existing lint diagnostics to use the new APIs. Most of them are mindless, monotonous, obvious changes. Some changes are *slightly* more involved out of necessity. I've verified (partly programmatically, partly manually) that all lint diags that used to have a primary span still have a primary span. 3. The 3rd commit updates the fulldeps UI tests that exercise the (lint) diagnostic API 4. The 4th commit fixes [rust-lang#125169](rust-lang#125169). I've only deduplicated the lint diagnostic structs mentioned in the issue and haven't gone looking for other structs that may benefit from deriving both `Diagnostic` and `LintDiagnostic` because I didn't have the time to do so yet. ### Meta Fixes rust-lang#125169. I'll submit a rustc-dev-guide PR later. I hope this doesn't need an MCP, it's pretty straight forward. cc `@davidtwco` r? `@nnethercote` or anyone on compiler who has the energy to review 82 files
2 parents f61306d + b22eadf commit 9de008a

File tree

96 files changed

+1654
-1250
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1654
-1250
lines changed

Diff for: compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ fn do_mir_borrowck<'tcx>(
401401

402402
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
403403

404-
tcx.emit_node_span_lint(UNUSED_MUT, lint_root, span, VarNeedNotMut { span: mut_span })
404+
tcx.emit_node_lint(UNUSED_MUT, lint_root, VarNeedNotMut { span, mut_span })
405405
}
406406

407407
let tainted_by_errors = mbcx.emit_errors();

Diff for: compiler/rustc_borrowck/src/session_diagnostics.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ pub(crate) struct GenericDoesNotLiveLongEnough {
5050
#[derive(LintDiagnostic)]
5151
#[diag(borrowck_var_does_not_need_mut)]
5252
pub(crate) struct VarNeedNotMut {
53-
#[suggestion(style = "short", applicability = "machine-applicable", code = "")]
53+
#[primary_span]
5454
pub span: Span,
55+
#[suggestion(style = "short", applicability = "machine-applicable", code = "")]
56+
pub mut_span: Span,
5557
}
5658
#[derive(Diagnostic)]
5759
#[diag(borrowck_var_cannot_escape_closure)]

Diff for: compiler/rustc_const_eval/src/const_eval/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ pub(super) fn lint<'tcx, L>(
170170
tcx: TyCtxtAt<'tcx>,
171171
machine: &CompileTimeMachine<'tcx>,
172172
lint: &'static rustc_session::lint::Lint,
173-
decorator: impl FnOnce(Vec<errors::FrameNote>) -> L,
173+
decorator: impl FnOnce(Span, Vec<errors::FrameNote>) -> L,
174174
) where
175175
L: for<'a> rustc_errors::LintDiagnostic<'a, ()>,
176176
{
177177
let (span, frames) = get_span_and_frames(tcx, &machine.stack);
178178

179-
tcx.emit_node_span_lint(lint, machine.best_lint_scope(*tcx), span, decorator(frames));
179+
tcx.emit_node_lint(lint, machine.best_lint_scope(*tcx), decorator(span, frames));
180180
}

Diff for: compiler/rustc_const_eval/src/const_eval/machine.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,10 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
622622
.0
623623
.is_error();
624624
let span = ecx.cur_span();
625-
ecx.tcx.emit_node_span_lint(
625+
ecx.tcx.emit_node_lint(
626626
rustc_session::lint::builtin::LONG_RUNNING_CONST_EVAL,
627627
hir_id,
628-
span,
629-
LongRunning { item_span: ecx.tcx.span },
628+
LongRunning { span, item_span: ecx.tcx.span },
630629
);
631630
// If this was a hard error, don't bother continuing evaluation.
632631
if is_error {

Diff for: compiler/rustc_const_eval/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ pub(crate) struct InteriorMutableRefEscaping {
197197
#[diag(const_eval_long_running)]
198198
#[note]
199199
pub struct LongRunning {
200+
#[primary_span]
201+
pub span: Span,
200202
#[help]
201203
pub item_span: Span,
202204
}

Diff for: compiler/rustc_errors/src/diagnostic.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ pub trait SubdiagMessageOp<G: EmissionGuarantee> =
195195
/// `#[derive(LintDiagnostic)]` -- see [rustc_macros::LintDiagnostic].
196196
#[rustc_diagnostic_item = "LintDiagnostic"]
197197
pub trait LintDiagnostic<'a, G: EmissionGuarantee> {
198-
/// Decorate and emit a lint.
198+
/// Decorate a lint.
199199
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, G>);
200+
201+
fn span(&self) -> Option<MultiSpan>;
200202
}
201203

202204
#[derive(Clone, Debug, Encodable, Decodable)]

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ fn report_mismatched_rpitit_signature<'tcx>(
297297
});
298298

299299
let span = unmatched_bound.unwrap_or(span);
300-
tcx.emit_node_span_lint(
300+
tcx.emit_node_lint(
301301
if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE },
302302
tcx.local_def_id_to_hir_id(impl_m_def_id.expect_local()),
303-
span,
304303
crate::errors::ReturnPositionImplTraitInTraitRefined {
304+
span,
305305
impl_return_span,
306306
trait_return_span,
307307
pre,

Diff for: compiler/rustc_hir_analysis/src/check/errs.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ fn handle_static_mut_ref(
7979
} else {
8080
(errors::MutRefSugg::Shared { lo, hi }, "shared")
8181
};
82-
tcx.emit_node_span_lint(STATIC_MUT_REFS, hir_id, span, errors::RefOfMutStatic {
83-
span,
84-
sugg,
85-
shared,
86-
});
82+
tcx.emit_node_lint(STATIC_MUT_REFS, hir_id, errors::RefOfMutStatic { span, sugg, shared });
8783
}
8884
}

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2334,11 +2334,10 @@ fn lint_redundant_lifetimes<'tcx>(
23342334
&& outlives_env.free_region_map().sub_free_regions(tcx, victim, candidate)
23352335
{
23362336
shadowed.insert(victim);
2337-
tcx.emit_node_span_lint(
2337+
tcx.emit_node_lint(
23382338
rustc_lint_defs::builtin::REDUNDANT_LIFETIMES,
23392339
tcx.local_def_id_to_hir_id(def_id.expect_local()),
2340-
tcx.def_span(def_id),
2341-
RedundantLifetimeArgsLint { candidate, victim },
2340+
RedundantLifetimeArgsLint { span: tcx.def_span(def_id), candidate, victim },
23422341
);
23432342
}
23442343
}
@@ -2349,6 +2348,8 @@ fn lint_redundant_lifetimes<'tcx>(
23492348
#[diag(hir_analysis_redundant_lifetime_args)]
23502349
#[note]
23512350
struct RedundantLifetimeArgsLint<'tcx> {
2351+
#[primary_span]
2352+
pub span: Span,
23522353
/// The lifetime we have found to be redundant.
23532354
victim: ty::Region<'tcx>,
23542355
// The lifetime we can replace the victim with.

Diff for: compiler/rustc_hir_analysis/src/coherence/orphan.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -494,18 +494,18 @@ fn lint_uncovered_ty_params<'tcx>(
494494
let name = tcx.item_name(param_def_id);
495495

496496
match local_ty {
497-
Some(local_type) => tcx.emit_node_span_lint(
497+
Some(local_type) => tcx.emit_node_lint(
498498
UNCOVERED_PARAM_IN_PROJECTION,
499499
hir_id,
500-
span,
501-
errors::TyParamFirstLocalLint { span, note: (), param: name, local_type },
502-
),
503-
None => tcx.emit_node_span_lint(
504-
UNCOVERED_PARAM_IN_PROJECTION,
505-
hir_id,
506-
span,
507-
errors::TyParamSomeLint { span, note: (), param: name },
500+
errors::TyParamFirstLocal { span, note: (), param: name, local_type },
508501
),
502+
None => {
503+
tcx.emit_node_lint(UNCOVERED_PARAM_IN_PROJECTION, hir_id, errors::TyParamSome {
504+
span,
505+
note: (),
506+
param: name,
507+
})
508+
}
509509
};
510510
}
511511
}

Diff for: compiler/rustc_hir_analysis/src/errors.rs

+5-27
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,7 @@ pub(crate) enum LateBoundInApit {
11321132
#[diag(hir_analysis_unused_associated_type_bounds)]
11331133
#[note]
11341134
pub(crate) struct UnusedAssociatedTypeBounds {
1135+
#[primary_span]
11351136
#[suggestion(code = "")]
11361137
pub span: Span,
11371138
}
@@ -1141,6 +1142,8 @@ pub(crate) struct UnusedAssociatedTypeBounds {
11411142
#[note]
11421143
#[note(hir_analysis_feedback_note)]
11431144
pub(crate) struct ReturnPositionImplTraitInTraitRefined<'tcx> {
1145+
#[primary_span]
1146+
pub span: Span,
11441147
#[suggestion(applicability = "maybe-incorrect", code = "{pre}{return_ty}{post}")]
11451148
pub impl_return_span: Span,
11461149
#[label]
@@ -1379,9 +1382,7 @@ pub(crate) struct CrossCrateTraitsDefined {
13791382
pub traits: String,
13801383
}
13811384

1382-
// FIXME(fmease): Deduplicate:
1383-
1384-
#[derive(Diagnostic)]
1385+
#[derive(Diagnostic, LintDiagnostic)]
13851386
#[diag(hir_analysis_ty_param_first_local, code = E0210)]
13861387
#[note]
13871388
pub(crate) struct TyParamFirstLocal<'tcx> {
@@ -1394,19 +1395,7 @@ pub(crate) struct TyParamFirstLocal<'tcx> {
13941395
pub local_type: Ty<'tcx>,
13951396
}
13961397

1397-
#[derive(LintDiagnostic)]
1398-
#[diag(hir_analysis_ty_param_first_local, code = E0210)]
1399-
#[note]
1400-
pub(crate) struct TyParamFirstLocalLint<'tcx> {
1401-
#[label]
1402-
pub span: Span,
1403-
#[note(hir_analysis_case_note)]
1404-
pub note: (),
1405-
pub param: Symbol,
1406-
pub local_type: Ty<'tcx>,
1407-
}
1408-
1409-
#[derive(Diagnostic)]
1398+
#[derive(Diagnostic, LintDiagnostic)]
14101399
#[diag(hir_analysis_ty_param_some, code = E0210)]
14111400
#[note]
14121401
pub(crate) struct TyParamSome {
@@ -1418,17 +1407,6 @@ pub(crate) struct TyParamSome {
14181407
pub param: Symbol,
14191408
}
14201409

1421-
#[derive(LintDiagnostic)]
1422-
#[diag(hir_analysis_ty_param_some, code = E0210)]
1423-
#[note]
1424-
pub(crate) struct TyParamSomeLint {
1425-
#[label]
1426-
pub span: Span,
1427-
#[note(hir_analysis_only_note)]
1428-
pub note: (),
1429-
pub param: Symbol,
1430-
}
1431-
14321410
#[derive(Diagnostic)]
14331411
pub(crate) enum OnlyCurrentTraits {
14341412
#[diag(hir_analysis_only_current_traits_outside, code = E0117)]

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
193193
// types's `DefId`, so the following loop removes all the `DefIds` of the associated types that have a
194194
// corresponding `Projection` clause
195195
for def_ids in associated_types.values_mut() {
196-
for (projection_bound, span) in &projection_bounds {
196+
for &(projection_bound, span) in &projection_bounds {
197197
let def_id = projection_bound.projection_def_id();
198198
// FIXME(#120456) - is `swap_remove` correct?
199199
def_ids.swap_remove(&def_id);
200200
if tcx.generics_require_sized_self(def_id) {
201-
tcx.emit_node_span_lint(
201+
tcx.emit_node_lint(
202202
UNUSED_ASSOCIATED_TYPE_BOUNDS,
203203
hir_id,
204-
*span,
205-
crate::errors::UnusedAssociatedTypeBounds { span: *span },
204+
crate::errors::UnusedAssociatedTypeBounds { span },
206205
);
207206
}
208207
}

Diff for: compiler/rustc_hir_typeck/src/cast.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
663663
};
664664
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
665665
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
666-
fcx.tcx.emit_node_span_lint(lint, self.expr.hir_id, self.span, errors::TrivialCast {
666+
fcx.tcx.emit_node_lint(lint, self.expr.hir_id, errors::TrivialCast {
667+
span: self.span,
667668
numeric,
668669
expr_ty,
669670
cast_ty,
@@ -934,11 +935,11 @@ impl<'a, 'tcx> CastCheck<'tcx> {
934935
.collect::<Vec<_>>();
935936

936937
if !added.is_empty() {
937-
tcx.emit_node_span_lint(
938+
tcx.emit_node_lint(
938939
lint::builtin::PTR_CAST_ADD_AUTO_TO_OBJECT,
939940
self.expr.hir_id,
940-
self.span,
941941
errors::PtrCastAddAutoToObject {
942+
span: self.span,
942943
traits_len: added.len(),
943944
traits: {
944945
let mut traits: Vec<_> = added
@@ -1097,11 +1098,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10971098
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
10981099
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
10991100

1100-
fcx.tcx.emit_node_span_lint(
1101+
fcx.tcx.emit_node_lint(
11011102
lint::builtin::CENUM_IMPL_DROP_CAST,
11021103
self.expr.hir_id,
1103-
self.span,
1104-
errors::CastEnumDrop { expr_ty, cast_ty },
1104+
errors::CastEnumDrop { span: self.span, expr_ty, cast_ty },
11051105
);
11061106
}
11071107
}
@@ -1130,12 +1130,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
11301130
(false, false) => errors::LossyProvenancePtr2IntSuggestion::Other { cast_span },
11311131
};
11321132

1133-
let lint = errors::LossyProvenancePtr2Int { expr_ty, cast_ty, sugg };
1134-
fcx.tcx.emit_node_span_lint(
1133+
fcx.tcx.emit_node_lint(
11351134
lint::builtin::LOSSY_PROVENANCE_CASTS,
11361135
self.expr.hir_id,
1137-
self.span,
1138-
lint,
1136+
errors::LossyProvenancePtr2Int { span: self.span, expr_ty, cast_ty, sugg },
11391137
);
11401138
}
11411139

@@ -1146,12 +1144,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
11461144
};
11471145
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
11481146
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1149-
let lint = errors::LossyProvenanceInt2Ptr { expr_ty, cast_ty, sugg };
1150-
fcx.tcx.emit_node_span_lint(
1147+
fcx.tcx.emit_node_lint(
11511148
lint::builtin::FUZZY_PROVENANCE_CASTS,
11521149
self.expr.hir_id,
1153-
self.span,
1154-
lint,
1150+
errors::LossyProvenanceInt2Ptr { span: self.span, expr_ty, cast_ty, sugg },
11551151
);
11561152
}
11571153

0 commit comments

Comments
 (0)