Skip to content

Commit 82f0544

Browse files
lzcuntmejrs
authored andcommitted
Migrate "function cannot return without recursing" diagnostic
1 parent 65c53c3 commit 82f0544

File tree

7 files changed

+27
-14
lines changed

7 files changed

+27
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4070,6 +4070,7 @@ dependencies = [
40704070
"rustc_hir",
40714071
"rustc_index",
40724072
"rustc_infer",
4073+
"rustc_macros",
40734074
"rustc_middle",
40744075
"rustc_serialize",
40754076
"rustc_session",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mir_build_unconditional_recursion = function cannot return without recursing
2+
.label = cannot return without recursing
3+
.help = a `loop` may express intention better if this is on purpose
4+
5+
mir_build_unconditional_recursion_call_site_label = recursive call site

compiler/rustc_error_messages/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fluent_messages! {
5858
metadata => "../locales/en-US/metadata.ftl",
5959
middle => "../locales/en-US/middle.ftl",
6060
mir_dataflow => "../locales/en-US/mir_dataflow.ftl",
61+
mir_build => "../locales/en-US/mir_build.ftl",
6162
monomorphize => "../locales/en-US/monomorphize.ftl",
6263
parse => "../locales/en-US/parse.ftl",
6364
passes => "../locales/en-US/passes.ftl",

compiler/rustc_mir_build/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rustc_index = { path = "../rustc_index" }
1717
rustc_errors = { path = "../rustc_errors" }
1818
rustc_hir = { path = "../rustc_hir" }
1919
rustc_infer = { path = "../rustc_infer" }
20+
rustc_macros = { path = "../rustc_macros" }
2021
rustc_serialize = { path = "../rustc_serialize" }
2122
rustc_session = { path = "../rustc_session" }
2223
rustc_span = { path = "../rustc_span" }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use rustc_macros::LintDiagnostic;
2+
use rustc_span::Span;
3+
4+
#[derive(LintDiagnostic)]
5+
#[lint(mir_build::unconditional_recursion)]
6+
#[help]
7+
pub struct UnconditionalRecursion {
8+
#[primary_span]
9+
#[label]
10+
pub span: Span,
11+
#[label(mir_build::unconditional_recursion_call_site_label)]
12+
pub call_sites: Vec<Span>,
13+
}

compiler/rustc_mir_build/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern crate rustc_middle;
1919

2020
mod build;
2121
mod check_unsafety;
22+
mod errors;
2223
mod lints;
2324
pub mod thir;
2425

compiler/rustc_mir_build/src/lints.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::errors::UnconditionalRecursion;
12
use rustc_data_structures::graph::iterate::{
23
NodeStatus, TriColorDepthFirstSearch, TriColorVisitor,
34
};
@@ -36,20 +37,10 @@ pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
3637

3738
let sp = tcx.def_span(def_id);
3839
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
39-
tcx.struct_span_lint_hir(
40-
UNCONDITIONAL_RECURSION,
41-
hir_id,
42-
sp,
43-
"function cannot return without recursing",
44-
|lint| {
45-
lint.span_label(sp, "cannot return without recursing");
46-
// offer some help to the programmer.
47-
for call_span in vis.reachable_recursive_calls {
48-
lint.span_label(call_span, "recursive call site");
49-
}
50-
lint.help("a `loop` may express intention better if this is on purpose")
51-
},
52-
);
40+
tcx.emit_spanned_lint(UNCONDITIONAL_RECURSION, hir_id, sp, UnconditionalRecursion {
41+
span: sp,
42+
call_sites: vis.reachable_recursive_calls,
43+
});
5344
}
5445
}
5546

0 commit comments

Comments
 (0)