Skip to content

Commit 2629683

Browse files
committed
Migrate "function cannot return without recursing" diagnostic
1 parent b32223f commit 2629683

File tree

7 files changed

+26
-9
lines changed

7 files changed

+26
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3845,6 +3845,7 @@ dependencies = [
38453845
"rustc_hir",
38463846
"rustc_index",
38473847
"rustc_infer",
3848+
"rustc_macros",
38483849
"rustc_middle",
38493850
"rustc_serialize",
38503851
"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
@@ -45,6 +45,7 @@ fluent_messages! {
4545
interface => "../locales/en-US/interface.ftl",
4646
infer => "../locales/en-US/infer.ftl",
4747
lint => "../locales/en-US/lint.ftl",
48+
mir_build => "../locales/en-US/mir_build.ftl",
4849
monomorphize => "../locales/en-US/monomorphize.ftl",
4950
parser => "../locales/en-US/parser.ftl",
5051
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
@@ -18,6 +18,7 @@ extern crate rustc_middle;
1818

1919
mod build;
2020
mod check_unsafety;
21+
mod errors;
2122
mod lints;
2223
pub mod thir;
2324

compiler/rustc_mir_build/src/lints.rs

Lines changed: 4 additions & 9 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,15 +37,9 @@ 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(UNCONDITIONAL_RECURSION, hir_id, sp, |lint| {
40-
let mut db = lint.build("function cannot return without recursing");
41-
db.span_label(sp, "cannot return without recursing");
42-
// offer some help to the programmer.
43-
for call_span in vis.reachable_recursive_calls {
44-
db.span_label(call_span, "recursive call site");
45-
}
46-
db.help("a `loop` may express intention better if this is on purpose");
47-
db.emit();
40+
tcx.emit_spanned_lint(UNCONDITIONAL_RECURSION, hir_id, sp, UnconditionalRecursion {
41+
span: sp,
42+
call_sites: vis.reachable_recursive_calls,
4843
});
4944
}
5045
}

0 commit comments

Comments
 (0)