Skip to content

Commit

Permalink
Update all lint diagnostics to store their span
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed May 17, 2024
1 parent a31ed16 commit c630da4
Show file tree
Hide file tree
Showing 77 changed files with 1,299 additions and 924 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ fn do_mir_borrowck<'tcx>(

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

tcx.emit_node_span_lint(UNUSED_MUT, lint_root, span, VarNeedNotMut { span: mut_span })
tcx.emit_node_lint(UNUSED_MUT, lint_root, VarNeedNotMut { span, mut_span })
}

let tainted_by_errors = mbcx.emit_errors();
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_borrowck/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ pub(crate) struct GenericDoesNotLiveLongEnough {
#[derive(LintDiagnostic)]
#[diag(borrowck_var_does_not_need_mut)]
pub(crate) struct VarNeedNotMut {
#[suggestion(style = "short", applicability = "machine-applicable", code = "")]
#[primary_span]
pub span: Span,
#[suggestion(style = "short", applicability = "machine-applicable", code = "")]
pub mut_span: Span,
}
#[derive(Diagnostic)]
#[diag(borrowck_var_cannot_escape_closure)]
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_const_eval/src/const_eval/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,17 @@ pub(super) fn lint<'tcx, 'mir, L>(
tcx: TyCtxtAt<'tcx>,
machine: &CompileTimeInterpreter<'mir, 'tcx>,
lint: &'static rustc_session::lint::Lint,
decorator: impl FnOnce(Vec<errors::FrameNote>) -> L,
decorator: impl FnOnce(Span, Vec<errors::FrameNote>) -> L,
) where
L: for<'a> rustc_errors::LintDiagnostic<'a, ()>,
{
let (span, frames) = get_span_and_frames(tcx, &machine.stack);

tcx.emit_node_span_lint(
tcx.emit_node_lint(
lint,
// We use the root frame for this so the crate that defines the const defines whether the
// lint is emitted.
machine.stack.first().and_then(|frame| frame.lint_root()).unwrap_or(CRATE_HIR_ID),
span,
decorator(frames),
decorator(span, frames),
);
}
6 changes: 2 additions & 4 deletions compiler/rustc_const_eval/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,10 @@ fn eval_body_using_ecx<'mir, 'tcx, R: InterpretationResult<'tcx>>(
}
Err(InternResult::FoundBadMutablePointer) => {
// only report mutable pointers if there were no dangling pointers
let err_diag = errors::MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind };
ecx.tcx.emit_node_span_lint(
ecx.tcx.emit_node_lint(
lint::builtin::CONST_EVAL_MUTABLE_PTR_IN_FINAL_VALUE,
ecx.best_lint_scope(),
err_diag.span,
err_diag,
errors::MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind },
)
}
}
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_const_eval/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,10 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
.0
.is_error();
let span = ecx.cur_span();
ecx.tcx.emit_node_span_lint(
ecx.tcx.emit_node_lint(
rustc_session::lint::builtin::LONG_RUNNING_CONST_EVAL,
hir_id,
span,
LongRunning { item_span: ecx.tcx.span },
LongRunning { span, item_span: ecx.tcx.span },
);
// If this was a hard error, don't bother continuing evaluation.
if is_error {
Expand Down Expand Up @@ -745,8 +744,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
}
// Reject writes through immutable pointers.
if immutable {
super::lint(tcx, machine, WRITES_THROUGH_IMMUTABLE_POINTER, |frames| {
crate::errors::WriteThroughImmutablePointer { frames }
super::lint(tcx, machine, WRITES_THROUGH_IMMUTABLE_POINTER, |span, frames| {
crate::errors::WriteThroughImmutablePointer { span, frames }
});
}
// Everything else is fine.
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ pub(crate) struct NestedStaticInThreadLocal {
#[derive(LintDiagnostic)]
#[diag(const_eval_mutable_ptr_in_final)]
pub(crate) struct MutablePtrInFinal {
// rust-lang/rust#122153: This was marked as `#[primary_span]` under
// `derive(Diagnostic)`. Since we expect we may hard-error in future, we are
// keeping the field (and skipping it under `derive(LintDiagnostic)`).
#[skip_arg]
#[primary_span]
pub span: Span,
pub kind: InternKind,
}
Expand Down Expand Up @@ -228,6 +225,8 @@ pub(crate) struct InteriorMutabilityBorrow {
#[diag(const_eval_long_running)]
#[note]
pub struct LongRunning {
#[primary_span]
pub span: Span,
#[help]
pub item_span: Span,
}
Expand Down Expand Up @@ -407,6 +406,8 @@ pub struct ConstEvalError {
#[derive(LintDiagnostic)]
#[diag(const_eval_write_through_immutable_pointer)]
pub struct WriteThroughImmutablePointer {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub frames: Vec<FrameNote>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,11 @@ fn report_mismatched_rpitit_signature<'tcx>(
});

let span = unmatched_bound.unwrap_or(span);
tcx.emit_node_span_lint(
tcx.emit_node_lint(
if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE },
tcx.local_def_id_to_hir_id(impl_m_def_id.expect_local()),
span,
crate::errors::ReturnPositionImplTraitInTraitRefined {
span,
impl_return_span,
trait_return_span,
pre,
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_hir_analysis/src/check/errs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ fn handle_static_mut_ref(
} else {
(errors::RefOfMutStaticSugg::Shared { span, var }, "shared")
};
tcx.emit_node_span_lint(
STATIC_MUT_REFS,
hir_id,
span,
errors::RefOfMutStatic { span, sugg, shared },
);
tcx.emit_node_lint(STATIC_MUT_REFS, hir_id, errors::RefOfMutStatic { span, sugg, shared });
}
}
7 changes: 4 additions & 3 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2114,11 +2114,10 @@ fn lint_redundant_lifetimes<'tcx>(
&& outlives_env.free_region_map().sub_free_regions(tcx, victim, candidate)
{
shadowed.insert(victim);
tcx.emit_node_span_lint(
tcx.emit_node_lint(
rustc_lint_defs::builtin::REDUNDANT_LIFETIMES,
tcx.local_def_id_to_hir_id(def_id.expect_local()),
tcx.def_span(def_id),
RedundantLifetimeArgsLint { candidate, victim },
RedundantLifetimeArgsLint { span: tcx.def_span(def_id), candidate, victim },
);
}
}
Expand All @@ -2129,6 +2128,8 @@ fn lint_redundant_lifetimes<'tcx>(
#[diag(hir_analysis_redundant_lifetime_args)]
#[note]
struct RedundantLifetimeArgsLint<'tcx> {
#[primary_span]
pub span: Span,
/// The lifetime we have found to be redundant.
victim: ty::Region<'tcx>,
// The lifetime we can replace the victim with.
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_hir_analysis/src/coherence/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,14 @@ fn lint_uncovered_ty_params<'tcx>(
let name = tcx.item_name(param_def_id);

match local_ty {
Some(local_type) => tcx.emit_node_span_lint(
Some(local_type) => tcx.emit_node_lint(
UNCOVERED_PARAM_IN_PROJECTION,
hir_id,
span,
errors::TyParamFirstLocalLint { span, note: (), param: name, local_type },
),
None => tcx.emit_node_span_lint(
None => tcx.emit_node_lint(
UNCOVERED_PARAM_IN_PROJECTION,
hir_id,
span,
errors::TyParamSomeLint { span, note: (), param: name },
),
};
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ pub(crate) enum LateBoundInApit {
#[diag(hir_analysis_unused_associated_type_bounds)]
#[note]
pub struct UnusedAssociatedTypeBounds {
#[primary_span]
#[suggestion(code = "")]
pub span: Span,
}
Expand All @@ -1117,6 +1118,8 @@ pub struct UnusedAssociatedTypeBounds {
#[note]
#[note(hir_analysis_feedback_note)]
pub(crate) struct ReturnPositionImplTraitInTraitRefined<'tcx> {
#[primary_span]
pub span: Span,
#[suggestion(applicability = "maybe-incorrect", code = "{pre}{return_ty}{post}")]
pub impl_return_span: Span,
#[label]
Expand Down Expand Up @@ -1374,6 +1377,7 @@ pub struct TyParamFirstLocal<'tcx> {
#[diag(hir_analysis_ty_param_first_local, code = E0210)]
#[note]
pub struct TyParamFirstLocalLint<'tcx> {
#[primary_span]
#[label]
pub span: Span,
#[note(hir_analysis_case_note)]
Expand All @@ -1398,6 +1402,7 @@ pub struct TyParamSome {
#[diag(hir_analysis_ty_param_some, code = E0210)]
#[note]
pub struct TyParamSomeLint {
#[primary_span]
#[label]
pub span: Span,
#[note(hir_analysis_only_note)]
Expand Down Expand Up @@ -1536,6 +1541,7 @@ pub enum StaticMutRefSugg {
#[note]
#[note(hir_analysis_why_note)]
pub struct RefOfMutStatic<'a> {
#[primary_span]
#[label]
pub span: Span,
#[subdiagnostic]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
// types's `DefId`, so the following loop removes all the `DefIds` of the associated types that have a
// corresponding `Projection` clause
for def_ids in associated_types.values_mut() {
for (projection_bound, span) in &projection_bounds {
for &(projection_bound, span) in &projection_bounds {
let def_id = projection_bound.projection_def_id();
// FIXME(#120456) - is `swap_remove` correct?
def_ids.swap_remove(&def_id);
if tcx.generics_require_sized_self(def_id) {
tcx.emit_node_span_lint(
tcx.emit_node_lint(
UNUSED_ASSOCIATED_TYPE_BOUNDS,
hir_id,
*span,
crate::errors::UnusedAssociatedTypeBounds { span: *span },
crate::errors::UnusedAssociatedTypeBounds { span },
);
}
}
Expand Down
22 changes: 8 additions & 14 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,11 +618,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
};
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
fcx.tcx.emit_node_span_lint(
fcx.tcx.emit_node_lint(
lint,
self.expr.hir_id,
self.span,
errors::TrivialCast { numeric, expr_ty, cast_ty },
errors::TrivialCast { span: self.span, numeric, expr_ty, cast_ty },
);
}

Expand Down Expand Up @@ -931,11 +930,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);

fcx.tcx.emit_node_span_lint(
fcx.tcx.emit_node_lint(
lint::builtin::CENUM_IMPL_DROP_CAST,
self.expr.hir_id,
self.span,
errors::CastEnumDrop { expr_ty, cast_ty },
errors::CastEnumDrop { span: self.span, expr_ty, cast_ty },
);
}
}
Expand Down Expand Up @@ -964,12 +962,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
(false, false) => errors::LossyProvenancePtr2IntSuggestion::Other { cast_span },
};

let lint = errors::LossyProvenancePtr2Int { expr_ty, cast_ty, sugg };
fcx.tcx.emit_node_span_lint(
fcx.tcx.emit_node_lint(
lint::builtin::LOSSY_PROVENANCE_CASTS,
self.expr.hir_id,
self.span,
lint,
errors::LossyProvenancePtr2Int { span: self.span, expr_ty, cast_ty, sugg },
);
}

Expand All @@ -980,12 +976,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
};
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
let lint = errors::LossyProvenanceInt2Ptr { expr_ty, cast_ty, sugg };
fcx.tcx.emit_node_span_lint(
fcx.tcx.emit_node_lint(
lint::builtin::FUZZY_PROVENANCE_CASTS,
self.expr.hir_id,
self.span,
lint,
errors::LossyProvenanceInt2Ptr { span: self.span, expr_ty, cast_ty, sugg },
);
}

Expand Down
34 changes: 29 additions & 5 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,34 @@ pub struct MissingParenthesesInRange {
pub enum NeverTypeFallbackFlowingIntoUnsafe {
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_call)]
Call,
Call {
#[primary_span]
span: Span,
},
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_method)]
Method,
Method {
#[primary_span]
span: Span,
},
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_path)]
Path,
Path {
#[primary_span]
span: Span,
},
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_union_field)]
UnionField,
UnionField {
#[primary_span]
span: Span,
},
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_deref)]
Deref,
Deref {
#[primary_span]
span: Span,
},
}

#[derive(Subdiagnostic)]
Expand Down Expand Up @@ -237,6 +252,8 @@ impl Subdiagnostic for TypeMismatchFruTypo {
#[diag(hir_typeck_lossy_provenance_int2ptr)]
#[help]
pub struct LossyProvenanceInt2Ptr<'tcx> {
#[primary_span]
pub span: Span,
pub expr_ty: Ty<'tcx>,
pub cast_ty: Ty<'tcx>,
#[subdiagnostic]
Expand All @@ -256,6 +273,8 @@ pub struct LossyProvenanceInt2PtrSuggestion {
#[diag(hir_typeck_lossy_provenance_ptr2int)]
#[help]
pub struct LossyProvenancePtr2Int<'tcx> {
#[primary_span]
pub span: Span,
pub expr_ty: Ty<'tcx>,
pub cast_ty: Ty<'tcx>,
#[subdiagnostic]
Expand Down Expand Up @@ -502,6 +521,8 @@ pub struct SuggestPtrNullMut {
#[diag(hir_typeck_trivial_cast)]
#[help]
pub struct TrivialCast<'tcx> {
#[primary_span]
pub span: Span,
pub numeric: bool,
pub expr_ty: Ty<'tcx>,
pub cast_ty: Ty<'tcx>,
Expand Down Expand Up @@ -542,6 +563,8 @@ pub struct CannotCastToBool<'tcx> {
#[derive(LintDiagnostic)]
#[diag(hir_typeck_cast_enum_drop)]
pub struct CastEnumDrop<'tcx> {
#[primary_span]
pub span: Span,
pub expr_ty: Ty<'tcx>,
pub cast_ty: Ty<'tcx>,
}
Expand Down Expand Up @@ -654,6 +677,7 @@ pub enum SuggestBoxingForReturnImplTrait {
#[derive(LintDiagnostic)]
#[diag(hir_typeck_dereferencing_mut_binding)]
pub struct DereferencingMutBinding {
#[primary_span]
#[label]
#[help]
pub span: Span,
Expand Down
Loading

0 comments on commit c630da4

Please sign in to comment.