Skip to content

Commit 23ee908

Browse files
committed
apply comments
1 parent 1251b2b commit 23ee908

File tree

4 files changed

+18
-28
lines changed

4 files changed

+18
-28
lines changed

compiler/rustc_mir_build/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ mir_build_pointer_pattern = function pointers and raw pointers not derived from
265265
266266
mir_build_privately_uninhabited = pattern `{$witness_1}` is currently uninhabited, but this variant contains private fields which may become inhabited in the future
267267
268-
mir_build_recursive_default_impl = ..default() in the Default impl does not apply a default for each struct field
268+
mir_build_recursive_default_impl = calling ..Default::default() in a Default implementation leads to infinite recursion, and does not initialize the fields of a struct or enum
269269
270270
mir_build_rust_2024_incompatible_pat = patterns are not allowed to reset the default binding mode in edition 2024
271271

compiler/rustc_mir_build/src/errors.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@ use crate::fluent_generated as fluent;
1818
pub(crate) struct UnconditionalRecursion {
1919
#[label]
2020
pub(crate) span: Span,
21-
#[subdiagnostic]
22-
pub(crate) default_impl_note: Option<RecursiveDefaultImpl>,
21+
#[help(mir_build_recursive_default_impl)]
22+
pub(crate) default_impl_note: Option<()>,
2323
#[label(mir_build_unconditional_recursion_call_site_label)]
2424
pub(crate) call_sites: Vec<Span>,
2525
}
2626

27-
#[derive(Subdiagnostic)]
28-
#[help(mir_build_recursive_default_impl)]
29-
pub(crate) struct RecursiveDefaultImpl {}
30-
3127
#[derive(LintDiagnostic)]
3228
#[diag(mir_build_call_to_deprecated_safe_fn_requires_unsafe)]
3329
pub(crate) struct CallToDeprecatedSafeFnRequiresUnsafe {

compiler/rustc_mir_build/src/lints.rs

+13-19
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use rustc_hir::def::DefKind;
77
use rustc_middle::mir::{self, BasicBlock, BasicBlocks, Body, Terminator, TerminatorKind};
88
use rustc_middle::ty::{self, GenericArg, GenericArgs, Instance, Ty, TyCtxt};
99
use rustc_session::lint::builtin::UNCONDITIONAL_RECURSION;
10-
use rustc_span::Span;
10+
use rustc_span::def_id::LocalDefId;
11+
use rustc_span::{Span, sym};
1112

12-
use crate::errors::{RecursiveDefaultImpl, UnconditionalRecursion};
13+
use crate::errors::UnconditionalRecursion;
1314

1415
pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
1516
check_call_recursion(tcx, body);
@@ -31,21 +32,16 @@ fn check_call_recursion<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
3132
check_recursion(tcx, body, CallRecursion { trait_args })
3233
}
3334
}
34-
use rustc_span::def_id::LocalDefId;
35-
use rustc_span::sym;
35+
3636
fn is_default_impl<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
37-
if tcx.def_kind(def_id) == DefKind::AssocFn {
38-
// Check if this is a trait impl and get the defid of the trait if it is
39-
if let Some(parent_def_id) = tcx.opt_parent(def_id.into()) {
40-
if tcx.def_kind(parent_def_id) == (DefKind::Impl { of_trait: true }) {
41-
if let Some(trait_def_id) = tcx.trait_id_of_impl(parent_def_id) {
42-
// check if it is a default impl
43-
if tcx.get_diagnostic_name(trait_def_id) == Some(sym::Default) {
44-
return true;
45-
}
46-
}
47-
}
48-
}
37+
// Check if this is a trait impl and get the defid of the trait if it is
38+
if tcx.def_kind(def_id) == DefKind::AssocFn
39+
&& let Some(parent_def_id) = tcx.opt_parent(def_id.into())
40+
&& tcx.def_kind(parent_def_id) == (DefKind::Impl { of_trait: true })
41+
&& let Some(trait_def_id) = tcx.trait_id_of_impl(parent_def_id)
42+
{
43+
// check if it is a default impl
44+
return tcx.get_diagnostic_name(trait_def_id) == Some(sym::Default);
4945
}
5046
false
5147
}
@@ -72,12 +68,10 @@ fn check_recursion<'tcx>(
7268

7369
let sp = tcx.def_span(def_id);
7470
let hir_id = tcx.local_def_id_to_hir_id(def_id);
75-
let default_impl_note =
76-
{ if is_default_impl(tcx, def_id) { Some(RecursiveDefaultImpl {}) } else { None } };
7771
tcx.emit_node_span_lint(UNCONDITIONAL_RECURSION, hir_id, sp, UnconditionalRecursion {
7872
span: sp,
7973
call_sites: vis.reachable_recursive_calls,
80-
default_impl_note,
74+
default_impl_note: is_default_impl(tcx, def_id).then(|| ()),
8175
});
8276
}
8377
}

tests/ui/lint/lint-unconditional-recursion.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ LL | let x = Default::default();
122122
| ------------------ recursive call site
123123
|
124124
= help: a `loop` may express intention better if this is on purpose
125-
= help: ..default() in the Default impl does not apply a default for each struct field
125+
= help: calling ..Default::default() in a Default implementation leads to infinite recursion, and does not initialize the fields of a struct or enum
126126

127127
error: function cannot return without recursing
128128
--> $DIR/lint-unconditional-recursion.rs:102:5
@@ -197,7 +197,7 @@ LL | ..Default::default()
197197
| ------------------ recursive call site
198198
|
199199
= help: a `loop` may express intention better if this is on purpose
200-
= help: ..default() in the Default impl does not apply a default for each struct field
200+
= help: calling ..Default::default() in a Default implementation leads to infinite recursion, and does not initialize the fields of a struct or enum
201201

202202
error: aborting due to 18 previous errors
203203

0 commit comments

Comments
 (0)