Skip to content

std::env::remove_var() becomes unsafe even in Rust 2021 #125875

Closed
@SteveLauC

Description

@SteveLauC

I tried this code:

#![deny(unsafe_op_in_unsafe_fn)]

fn main() {
    unsafe { foo() };
}

unsafe fn foo() {
    std::env::remove_var("foo");
}
[package]
name = "remove_var_2021_reproduction"
version = "0.1.0"
edition = "2021"

[dependencies]

I expected to see this happen:

std::env::remove_var() will become unsafe in Rust 2024, the above code is using Rust 2021, so this should compile with no issue.

Instead, this happened:

$ cargo +nightly b
   Compiling remove_var_2021_reproduction v0.1.0 (remove_var_2021_reproduction)
error[E0133]: call to unsafe function `std::env::remove_var` is unsafe and requires unsafe block
 --> src/main.rs:8:5
  |
8 |     std::env::remove_var("foo");
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
  |
  = note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
  = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
 --> src/main.rs:7:1
  |
7 | unsafe fn foo() {
  | ^^^^^^^^^^^^^^^
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(unsafe_op_in_unsafe_fn)]
  |         ^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0133`.
error: could not compile `remove_var_2021_reproduction` (bin "remove_var_2021_reproduction") due to 1 previous error

Meta

rustc --version --verbose:

$ rustc +nightly --version --verbose
rustc 1.80.0-nightly (ada5e2c7b 2024-05-31)
binary: rustc
commit-hash: ada5e2c7b5427a591e30baeeee2698a5eb6db0bd
commit-date: 2024-05-31
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.6
Backtrace

Not needed

Possible root cause

I think the root cause is the lint unsafe_op_in_unsafe_fn, commenting it out would make the code compile:

$ rg unsafe_op src/main.rs
1:// #![deny(unsafe_op_in_unsafe_fn)]

$ cargo +nightly b
   Compiling remove_var_2021_reproduction v0.1.0 (remove_var_2021_reproduction)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.10s

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions