rust-lang / rust-lang/rust

Diverging expressions does not lint for locals needing drop

Open
#158,761 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lints C-feature-request needs-triage T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

// this is psuedo code
fn goodbye() -> ! { ...assembly magic here, like a permanent context switch... }

fn non_returning(value: SomethingThatNeedsDrop) -> ! {
  value.do_something();
  // there's no lint that `value` needs to be manually dropped
  goodbye()
}

I expected to see this happen: Lint to inform that value is being leaked.

Instead, this happened: ¯⁠\⁠_⁠(⁠ツ⁠)⁠_⁠/⁠¯ compiles without warnings

So, hear me out. I'm writing no_std "kernel" code involving permanent context switches and thread exiting stuff. I have smart pointers for kernel objects and memory leaks would be quite bad because I only have 32mb of ram to work with.

So when I do these context switches (or "mega ultra permanent context destruction") I have to be very careful about drops for all codepaths that end up diverging.

For example, my SIGSYS signal handler implements a system call to exit the process or thread. There is associated cleanup required (marking KThread or KProcess as defunct, unmapping signal handler stacks) and this involves retaining a smart pointer to the kernel object up until the thread or process's final moments. Therefore I manually drop them right before the actual Linux munmap and exit syscalls.

But like, I have to remember to do this!

I don't want Rust to change its semantics (because this is perfectly fine with panics, which are most common), but I really would appreciate a lint for all vars in scope that needs drop. Of course this doesn't really work for functions that conditionally diverge (therefore not returning !), but it could make the developer realize they may have issues along the call stack up until divergence.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the issue's Rust examples involving a function returning ! and a local value that needs Drop. Investigate how the compiler currently handles locals at divergent exits and existing lint behavior. Done means a lint can identify relevant locals before unconditional divergence without changing Rust's drop semantics, with coverage for the described cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.