Skip to content

Windows: don't error if access_mode is set on OpenOptions - #162984

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
ChrisDenton:access-allowed
Sep 21, 2026
Merged

rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
ChrisDenton:access-allowed

Conversation

@ChrisDenton

@ChrisDenton ChrisDenton commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

In RFC 1252 it was decided to artificially restrict the combination of File access modes and creation modes for the sake of cross-platform compatibility. This is tested in library/std/src/fs/tests.rs. As the RFC says, using platform specific function should allow overriding this.

On Windows, access_mode does indeed override read, write and append. However currently OpenOptions can still error if you don't use read, write or append. For example:

use std::os::windows::fs::OpenOptionsExt;
File::options()
    .create_new(true)
    .write(true) // useless except it prevents std from erroring
    .access_mode(0)

This PR makes it so you don't need the misleading write(true).

I also cleaned up the error handling for the access mode a bit. It was unnecessarily split between two functions.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 18, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 18, 2026
@rustbot

rustbot commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

r? @clarfonthey

rustbot has assigned @clarfonthey.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 13 candidates
  • Random selection from 7 candidates

@rust-log-analyzer

This comment has been minimized.

@ChrisDenton ChrisDenton added O-windows Operating system: Windows A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` labels Sep 18, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

So, just looking at this code, I'm a little confused why the existing code doesn't already work, since the (.., Some(mode)) branch should be equivalent to the if let Some(mode) you changed it to?

Branches are followed in order, and the other branches even explicitly mention that you need None to make them work, so, I don't know why this wouldn't apply.

@clarfonthey

Copy link
Copy Markdown
Contributor

Like, for reviewability purposes, it would be helpful if you left everything in the previous match format at least for one commit so I can more easily tell what the difference is, then you can add a second commit refactoring out the if let if you like.

@clarfonthey

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot 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 Sep 20, 2026
@ChrisDenton

Copy link
Copy Markdown
Member Author

I've added an intermediate commit so that the diff is more readable.

Moving the access_mode case out of the match does seem more reasonable to me because it's logically a different thing. It's either use the given access mode or else infer the access mode from read, write and append. The first and second things should not interact at all.

@ChrisDenton

Copy link
Copy Markdown
Member 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 Sep 20, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

Okay, yes, this is much easier to understand now. This looks good to me, especially with the motivation provided.

@bors try jobs=x86_64-msvc-*

(just because I keep getting burned by this…)

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 20, 2026
Windows: don't error if `access_mode` is set on `OpenOptions`


try-job: x86_64-msvc-*
@rust-bors rust-bors Bot 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 Sep 20, 2026
@rust-bors

rust-bors Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 3f2aae8 failed: CI. Failed job:

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

A job failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling glob-match v0.2.1
   Compiling citool v0.1.0 (/home/runner/work/rust/rust/src/ci/citool)
    Finished `dev` profile [unoptimized] target(s) in 33.48s
     Running `target/debug/citool calculate-job-matrix`
Run type: TryJob { job_patterns: Some(["x86_64-msvc-*"]), nolimit: false }
Error: Failed to calculate job matrix

Caused by:
    Patterns `x86_64-msvc-*` did not match any auto jobs
##[error]Process completed with exit code 1.
Post job cleanup.

@ChrisDenton

Copy link
Copy Markdown
Member Author

I do test locally but fair enough. Btw, job names have changed

@bors try jobs=test-x86_64-msvc-*

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 20, 2026
Windows: don't error if `access_mode` is set on `OpenOptions`


try-job: test-x86_64-msvc-*
@clarfonthey

Copy link
Copy Markdown
Contributor

@bors try jobs=test-x86_64-msvc-*

🙃

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 20, 2026
Windows: don't error if `access_mode` is set on `OpenOptions`


try-job: test-x86_64-msvc-*
@rust-bors

rust-bors Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 80f06fa (80f06fac542312ae886904376699bc6ce55753c5)
Base parent: 12c4d3f (12c4d3f34dcb4715784d2fa0e8443853e0592194)

@clarfonthey

Copy link
Copy Markdown
Contributor

@bors r+ rollup

Thanks as always, just superstitious about Windows PRs randomly failing at this point :p

@rust-bors

rust-bors Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 39f76c9 has been approved by clarfonthey

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 21, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 21, 2026
…fonthey

Windows: don't error if `access_mode` is set on `OpenOptions`

In [RFC 1252] it was decided to artificially restrict the combination of `File` access modes and creation modes for the sake of cross-platform compatibility. This is tested in [`library/std/src/fs/tests.rs`]. As the RFC says, using platform specific function should allow overriding this.

On Windows, [`access_mode`] does indeed override `read`, `write` and `append`. However currently `OpenOptions` can still error if you don't use `read`, `write` or `append`. For example:

```rust
use std::os::windows::fs::OpenOptionsExt;
File::options()
    .create_new(true)
    .write(true) // useless except it prevents std from erroring
    .access_mode(0)
```

This PR makes it so you don't need the misleading `write(true)`.

I also cleaned up the error handling for the access mode a bit. It was unnecessarily split between two functions.

[RFC 1252]: https://rust-lang.github.io/rfcs/1252-open-options.html#combination-of-access-modes-and-creation-modes
[`library/std/src/fs/tests.rs`]: https://github.com/rust-lang/rust/blob/420ed2a0c3d7225b1744266fd884d431b4d8cfe0/library/std/src/fs/tests.rs#L1517
[`access_mode`]: https://doc.rust-lang.org/stable/std/os/windows/fs/trait.OpenOptionsExt.html#tymethod.access_mode
rust-bors Bot pushed a commit that referenced this pull request Sep 21, 2026
Rollup of 15 pull requests

Successful merges:

 - #161051 (When error from local macro, include macro def span)
 - #162750 (add case mapping fast paths for Latin-1)
 - #162831 (Do not continue past `rustc_resolve` when encountering duplicated items)
 - #162835 (rustdoc: account for nested parens and split text events in bare urls lint)
 - #163044 (Report runtime range endpoints for runtime values)
 - #163062 (Use x30 register name with LLVM 23+)
 - #158102 (When compiling without a specified `--edition`, emit a note)
 - #162984 (Windows: don't error if `access_mode` is set on `OpenOptions`)
 - #163005 (Avoid unreachable integer underflow check in `CStr::count_bytes()`)
 - #163019 (Prepare for the introduction of forced keywords (`k#`))
 - #163020 (Dir: fix fallback impl for remove_dir)
 - #163047 (Use verbose suggestion for `mut binding` instead of `&mut binding`)
 - #163075 (Fix ArgAttributes mismatches in ABI UI tests for LoongArch64 and RiscV64)
 - #163079 (enable `f128` from `u64`/`i64` test)
 - #163082 (Remove `TypeChecker::root_cx`)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 21, 2026
…fonthey

Windows: don't error if `access_mode` is set on `OpenOptions`

In [RFC 1252] it was decided to artificially restrict the combination of `File` access modes and creation modes for the sake of cross-platform compatibility. This is tested in [`library/std/src/fs/tests.rs`]. As the RFC says, using platform specific function should allow overriding this.

On Windows, [`access_mode`] does indeed override `read`, `write` and `append`. However currently `OpenOptions` can still error if you don't use `read`, `write` or `append`. For example:

```rust
use std::os::windows::fs::OpenOptionsExt;
File::options()
    .create_new(true)
    .write(true) // useless except it prevents std from erroring
    .access_mode(0)
```

This PR makes it so you don't need the misleading `write(true)`.

I also cleaned up the error handling for the access mode a bit. It was unnecessarily split between two functions.

[RFC 1252]: https://rust-lang.github.io/rfcs/1252-open-options.html#combination-of-access-modes-and-creation-modes
[`library/std/src/fs/tests.rs`]: https://github.com/rust-lang/rust/blob/420ed2a0c3d7225b1744266fd884d431b4d8cfe0/library/std/src/fs/tests.rs#L1517
[`access_mode`]: https://doc.rust-lang.org/stable/std/os/windows/fs/trait.OpenOptionsExt.html#tymethod.access_mode
rust-bors Bot pushed a commit that referenced this pull request Sep 21, 2026
…uwer

Rollup of 18 pull requests

Successful merges:

 - #161051 (When error from local macro, include macro def span)
 - #161629 (Streamline `StateDiffCollector`)
 - #162750 (add case mapping fast paths for Latin-1)
 - #162831 (Do not continue past `rustc_resolve` when encountering duplicated items)
 - #162835 (rustdoc: account for nested parens and split text events in bare urls lint)
 - #162952 (Depend on lockfiles to prevent GC of the current session)
 - #163044 (Report runtime range endpoints for runtime values)
 - #163062 (Use x30 register name with LLVM 23+)
 - #158102 (When compiling without a specified `--edition`, emit a note)
 - #162939 (Declare multi-kind constants for MacroKinds)
 - #162984 (Windows: don't error if `access_mode` is set on `OpenOptions`)
 - #163005 (Avoid unreachable integer underflow check in `CStr::count_bytes()`)
 - #163019 (Prepare for the introduction of forced keywords (`k#`))
 - #163020 (Dir: fix fallback impl for remove_dir)
 - #163047 (Use verbose suggestion for `mut binding` instead of `&mut binding`)
 - #163075 (Fix ArgAttributes mismatches in ABI UI tests for LoongArch64 and RiscV64)
 - #163079 (enable `f128` from `u64`/`i64` test)
 - #163082 (Remove `TypeChecker::root_cx`)
rust-bors Bot pushed a commit that referenced this pull request Sep 21, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #161051 (When error from local macro, include macro def span)
 - #161629 (Streamline `StateDiffCollector`)
 - #162750 (add case mapping fast paths for Latin-1)
 - #162831 (Do not continue past `rustc_resolve` when encountering duplicated items)
 - #162835 (rustdoc: account for nested parens and split text events in bare urls lint)
 - #162952 (Depend on lockfiles to prevent GC of the current session)
 - #163044 (Report runtime range endpoints for runtime values)
 - #163062 (Use x30 register name with LLVM 23+)
 - #163091 (miri subtree update)
 - #162939 (Declare multi-kind constants for MacroKinds)
 - #162984 (Windows: don't error if `access_mode` is set on `OpenOptions`)
 - #163005 (Avoid unreachable integer underflow check in `CStr::count_bytes()`)
 - #163019 (Prepare for the introduction of forced keywords (`k#`))
 - #163020 (Dir: fix fallback impl for remove_dir)
 - #163041 (Be more explicit on suggestion type without changing how they are rendered)
 - #163047 (Use verbose suggestion for `mut binding` instead of `&mut binding`)
 - #163075 (Fix ArgAttributes mismatches in ABI UI tests for LoongArch64 and RiscV64)
 - #163079 (enable `f128` from `u64`/`i64` test)
 - #163082 (Remove `TypeChecker::root_cx`)
@rust-bors
rust-bors Bot merged commit 042e9bc into rust-lang:main Sep 21, 2026
14 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 21, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 21, 2026
Rollup merge of #162984 - ChrisDenton:access-allowed, r=clarfonthey

Windows: don't error if `access_mode` is set on `OpenOptions`

In [RFC 1252] it was decided to artificially restrict the combination of `File` access modes and creation modes for the sake of cross-platform compatibility. This is tested in [`library/std/src/fs/tests.rs`]. As the RFC says, using platform specific function should allow overriding this.

On Windows, [`access_mode`] does indeed override `read`, `write` and `append`. However currently `OpenOptions` can still error if you don't use `read`, `write` or `append`. For example:

```rust
use std::os::windows::fs::OpenOptionsExt;
File::options()
    .create_new(true)
    .write(true) // useless except it prevents std from erroring
    .access_mode(0)
```

This PR makes it so you don't need the misleading `write(true)`.

I also cleaned up the error handling for the access mode a bit. It was unnecessarily split between two functions.

[RFC 1252]: https://rust-lang.github.io/rfcs/1252-open-options.html#combination-of-access-modes-and-creation-modes
[`library/std/src/fs/tests.rs`]: https://github.com/rust-lang/rust/blob/420ed2a0c3d7225b1744266fd884d431b4d8cfe0/library/std/src/fs/tests.rs#L1517
[`access_mode`]: https://doc.rust-lang.org/stable/std/os/windows/fs/trait.OpenOptionsExt.html#tymethod.access_mode
@ChrisDenton
ChrisDenton deleted the access-allowed branch September 21, 2026 13:33
pull Bot pushed a commit to xtqqczze/rust-lang-miri that referenced this pull request Sep 22, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - rust-lang/rust#161051 (When error from local macro, include macro def span)
 - rust-lang/rust#161629 (Streamline `StateDiffCollector`)
 - rust-lang/rust#162750 (add case mapping fast paths for Latin-1)
 - rust-lang/rust#162831 (Do not continue past `rustc_resolve` when encountering duplicated items)
 - rust-lang/rust#162835 (rustdoc: account for nested parens and split text events in bare urls lint)
 - rust-lang/rust#162952 (Depend on lockfiles to prevent GC of the current session)
 - rust-lang/rust#163044 (Report runtime range endpoints for runtime values)
 - rust-lang/rust#163062 (Use x30 register name with LLVM 23+)
 - rust-lang/rust#163091 (miri subtree update)
 - rust-lang/rust#162939 (Declare multi-kind constants for MacroKinds)
 - rust-lang/rust#162984 (Windows: don't error if `access_mode` is set on `OpenOptions`)
 - rust-lang/rust#163005 (Avoid unreachable integer underflow check in `CStr::count_bytes()`)
 - rust-lang/rust#163019 (Prepare for the introduction of forced keywords (`k#`))
 - rust-lang/rust#163020 (Dir: fix fallback impl for remove_dir)
 - rust-lang/rust#163041 (Be more explicit on suggestion type without changing how they are rendered)
 - rust-lang/rust#163047 (Use verbose suggestion for `mut binding` instead of `&mut binding`)
 - rust-lang/rust#163075 (Fix ArgAttributes mismatches in ABI UI tests for LoongArch64 and RiscV64)
 - rust-lang/rust#163079 (enable `f128` from `u64`/`i64` test)
 - rust-lang/rust#163082 (Remove `TypeChecker::root_cx`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants