Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't lint for self-recursion when the function can diverge #70822

Merged
merged 6 commits into from
Apr 9, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/librustc_mir_build/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ fn check_fn_for_unconditional_recursion<'tcx>(
def_id: DefId,
) {
let self_calls = find_blocks_calling_self(tcx, &body, def_id);

// Stores a list of `Span`s for every basic block. Those are the spans of `Call` terminators
// where we know that one of them will definitely be reached.
jonas-schievink marked this conversation as resolved.
Show resolved Hide resolved
let mut results = IndexVec::from_elem_n(vec![], body.basic_blocks().len());

// We start the analysis at the self calls and work backwards.
let mut queue: VecDeque<_> = self_calls.iter().collect();

while let Some(bb) = queue.pop_front() {
Expand All @@ -39,6 +44,8 @@ fn check_fn_for_unconditional_recursion<'tcx>(

let locations = if self_calls.contains(bb) {
// `bb` *is* a self-call.
// We don't look at successors here because they are irrelevant here and we don't want
// to lint them (eg. `f(); f()` should only lint the first call).
vec![bb]
} else {
// If *all* successors of `bb` lead to a self-call, emit notes at all of their
Expand Down Expand Up @@ -69,12 +76,16 @@ fn check_fn_for_unconditional_recursion<'tcx>(
}
};

// If all our successors are known to lead to self-calls, then we do too.
let all_are_self_calls =
relevant_successors.clone().all(|&succ| !results[succ].is_empty());

if all_are_self_calls {
// We'll definitely lead to a self-call. Merge all call locations of the successors
// for linting them later.
relevant_successors.flat_map(|&succ| results[succ].iter().copied()).collect()
} else {
// At least 1 successor does not always lead to a self-call, so we also don't.
vec![]
}
};
Expand Down