Skip to content

Conversation

@clubby789
Copy link
Contributor

@clubby789 clubby789 commented Nov 18, 2025

Closes #149042

Attempts to be smarter about identifying 'real'/user-written unreachable code to raise the lint

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 18, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 18, 2025

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.

@cjgillot
Copy link
Contributor

Iirc some parts of mir building uses end_point for drop spans. Should this do the same?

@Zalathar
Copy link
Member

You'll probably need to bless the coverage tests with:

./x test coverage coverage-run-rustdoc --set=build.profiler=true --bless

Coverage instrumentation unfortunately relies on a bunch of fragile heuristics that get disturbed by changes like this one, but luckily the new output seems reasonable.

@clubby789
Copy link
Contributor Author

Iirc some parts of mir building uses end_point for drop spans. Should this do the same?

Some examples of diagnostic changes with this

error: unreachable expression
  --> $DIR/void-branch.rs:10:9
   |
LL |             std::mem::uninitialized::<Void>();
   |             --------------------------------- any code following this expression is unreachable
LL |         }
   |         ^ unreachable expression
LL | #[define_opaque(WithoutLt)]
LL | fn without_lt() -> impl for<'a> Trait<'a, Assoc = WithoutLt> {}
   |                             --                                ^
   |                             |
   |                             hidden type `&'a str` captures the lifetime `'a` as defined here

I'm not sure if there's any performance implications with using sourcemap parsing on a pretty hot path, I'll perf it

@rustbot
Copy link
Collaborator

rustbot commented Nov 18, 2025

Some changes occurred in coverage tests.

cc @Zalathar

@clubby789
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Nov 18, 2025
Use a more accurate span for the implicit block return assignment
@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 18, 2025
Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first commit seems like a small improvement. The second commit touches a lot of test and coverage output for seemingly little gain, and doesn't seem worthwhile.

Supposedly this fixes #149042 but no test is added to cover that case. Seems like a test should be added?

View changes since this review

@rust-log-analyzer

This comment has been minimized.

@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 18, 2025
@clubby789
Copy link
Contributor Author

clubby789 commented Nov 18, 2025

The second commit touches a lot of test and coverage output for seemingly little gain, and doesn't seem worthwhile.

Agree, I think I'll back it out (though the coverage changes were originally just not blessed with the first commit, so there's still going to be some sweeping changes there).

Seems like a test should be added?

Imo, tests/ui/uninhabited/void-branch.rs (which was added with the PR the lint change) demonstrates the span issue and fix well, do you think a dedicated test is needed?

@clubby789 clubby789 force-pushed the implicit-return-span branch 2 times, most recently from d1bc16c to aed6c5d Compare November 18, 2025 10:50
@nnethercote
Copy link
Contributor

do you think a dedicated test is needed?

#149042's description said a lint was issued in a case where it shouldn't be issued. void-branch.rs just sees some minor changes in output, which seems much less compelling. If you think it's good enough I will defer, it just seemed surprising.

@clubby789
Copy link
Contributor Author

Ah, this change is about turning

  |
7 |   fn check() -> () {
  |  __________________^
8 | |     make_never();
  | |     ------------ any code following this expression is unreachable
9 | | }
  | |_^ unreachable expression

into

8 |     make_never();
  |     ------------^ unreachable expression
  |     |
  |     any code following this expression is unreachable

I think there's a good argument to be made that the implicit void return should not be counted as 'code' that's unreachable. I'll see if that's feasible to reliably detect

@rust-bors
Copy link

rust-bors bot commented Nov 18, 2025

☀️ Try build successful (CI)
Build commit: b2b48b3 (b2b48b3941a98a5ab0c2d3f77d25a989ed59d04f, parent: 217cb73577ed6f30a2005dd75bab01d23ec4cd60)

@rust-timer

This comment has been minimized.

@rust-timer

This comment was marked as outdated.

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 18, 2025
@clubby789 clubby789 force-pushed the implicit-return-span branch from aed6c5d to d793f62 Compare November 18, 2025 17:40
@clubby789 clubby789 removed the perf-regression Performance regression. label Nov 18, 2025
@clubby789 clubby789 changed the title Use a more accurate span for the implicit block return assignment Reduce confusing unreachable_code lints Nov 18, 2025
@clubby789
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 18, 2025
std::mem::uninitialized::<Void>();
println!();
//~^ ERROR unreachable expression
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the error point on the unchanged test? Do you mind adding another if false block with the println an keep the original one?


let term = bb.terminator();
match term.kind {
TerminatorKind::Goto { .. } | TerminatorKind::Return => None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding a comment explaining why we return None here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unreachable_code lint falsely warns entire function body is unreachable

8 participants