Skip to content

Commit fe09291

Browse files
committed
migrate: deref_into_dyn_supertrait.rs
1 parent f38db48 commit fe09291

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

compiler/rustc_error_messages/locales/en-US/lint.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ lint_builtin_special_module_name_used_lib = found module declaration for lib.rs
457457
lint_builtin_special_module_name_used_main = found module declaration for main.rs
458458
.note = a binary crate cannot be used as library
459459
460+
lint_supertrait_as_deref_target = `{$t}` implements `Deref` with supertrait `{$target_principal}` as target
461+
.label = target type is set here
462+
460463
lint_overruled_attribute = {$lint_level}({$lint_source}) incompatible with previous forbid
461464
.label = overruled by previous forbid
462465

compiler/rustc_lint/src/deref_into_dyn_supertrait.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use crate::{LateContext, LateLintPass, LintContext};
1+
use crate::{
2+
lints::{SupertraitAsDerefTarget, SupertraitAsDerefTargetLabel},
3+
LateContext, LateLintPass, LintContext,
4+
};
25

3-
use rustc_errors::DelayDm;
46
use rustc_hir as hir;
57
use rustc_middle::{traits::util::supertraits, ty};
68
use rustc_span::sym;
@@ -71,22 +73,14 @@ impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait {
7173
&& supertraits(cx.tcx, t_principal.with_self_ty(cx.tcx, cx.tcx.types.trait_object_dummy_self))
7274
.any(|sup| sup.map_bound(|x| ty::ExistentialTraitRef::erase_self_ty(cx.tcx, x)) == target_principal)
7375
{
74-
cx.struct_span_lint(
75-
DEREF_INTO_DYN_SUPERTRAIT,
76-
cx.tcx.def_span(item.owner_id.def_id),
77-
DelayDm(|| {
78-
format!(
79-
"`{t}` implements `Deref` with supertrait `{target_principal}` as target"
80-
)
81-
}),
82-
|lint| {
83-
if let Some(target_span) = impl_.items.iter().find_map(|i| (i.ident.name == sym::Target).then_some(i.span)) {
84-
lint.span_label(target_span, "target type is set here");
85-
}
86-
87-
lint
88-
},
89-
)
76+
let label = impl_.items.iter().find_map(|i| (i.ident.name == sym::Target).then_some(i.span)).map(|label| SupertraitAsDerefTargetLabel {
77+
label,
78+
});
79+
cx.emit_spanned_lint(DEREF_INTO_DYN_SUPERTRAIT, cx.tcx.def_span(item.owner_id.def_id), SupertraitAsDerefTarget {
80+
t,
81+
target_principal: target_principal.to_string(),
82+
label,
83+
});
9084
}
9185
}
9286
}

compiler/rustc_lint/src/lints.rs

+18
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,24 @@ pub struct BuiltinUnexpectedCliConfigValue {
551551
pub value: Symbol,
552552
}
553553

554+
// deref_into_dyn_supertrait.rs
555+
#[derive(LintDiagnostic)]
556+
#[diag(lint_supertrait_as_deref_target)]
557+
pub struct SupertraitAsDerefTarget<'a> {
558+
pub t: Ty<'a>,
559+
pub target_principal: String,
560+
// pub target_principal: Binder<'a, ExistentialTraitRef<'b>>,
561+
#[subdiagnostic]
562+
pub label: Option<SupertraitAsDerefTargetLabel>,
563+
}
564+
565+
#[derive(Subdiagnostic)]
566+
#[label(label)]
567+
pub struct SupertraitAsDerefTargetLabel {
568+
#[primary_span]
569+
pub label: Span,
570+
}
571+
554572
// enum_intrinsics_non_enums.rs
555573
#[derive(LintDiagnostic)]
556574
#[diag(lint_enum_intrinsics_mem_discriminant)]

0 commit comments

Comments
 (0)