Skip to content

Conversation

@hanna-kruppe
Copy link
Contributor

@hanna-kruppe hanna-kruppe commented Feb 3, 2026

Tracking issue: #150836

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 3, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 3, 2026

r? @ChrisDenton

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

/// whenever possible. However, there are situations where touching the `std::io` handles (or most
/// other parts of the standard library) risks deadlocks or other subtle bugs. For example:
///
/// - Global allocators must be careful to [avoid reentrancy][global-alloc-reentrancy], and the
Copy link
Member

Choose a reason for hiding this comment

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

Could this be moved to a new section in the BorrowedFd docs rather than being on STDERR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which parts and why? I could see some of it being relevant or at least instructive for general BorrowedFd docs (e.g., discussion of BorrowedFd<'static> implications, or the std::io handle vs. underlying FD differences, since that also matters for using AsFd on the handles). But e.g. for the bullet point your comment is attached to, I don't see how it would relate to BorrowedFd docs at all.

Copy link
Member

@ChrisDenton ChrisDenton Feb 6, 2026

Choose a reason for hiding this comment

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

The STDOUT and STDIN both forward readers to STDERR for more documentation. This part of the docs could arguably be on STDOUT or STDIN, it makes no less or more sense than having it on STDERR, In cases such as these it does often make sense to have module or type-level.

The reason I asked about BorrowedFd specifically is that these constants aren't special to std. They can be constructed manually by users, no? So the docs apply equally to those too.

Copy link
Contributor Author

@hanna-kruppe hanna-kruppe Feb 7, 2026

Choose a reason for hiding this comment

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

Oh, I see what you mean now. FWIW, there are some (weak) reasons why I chose STDERR as the "canonical" place to host the docs: in my experience stderr is the one that's needed most frequently (so documenting it directly saves the most people a click), and both stdin and stdout have unique additional concerns that don't apply to stderr but may be worth discussing (so if STDERR referenced one of the others, some of the referenced docs would not apply to STDERR at all). But if we had a module like std::os::fd::stdio, I would agree that the docs common to all three constants should live there. That's better than semi-arbitrarily picking one item to host the docs, and the alternatives of duplicating the doc comments or generating them with a macro are really unappealing.

However, putting the stdio FD docs on BorrowedFd feels weird to me, because I think that the overwhelming majority of BorrowedFd uses don't have anything to do with stdio. Conversely, while you're right that these constants aren't special, I don't think putting these docs (only) on BorrowedFd is likely to reach the users who would benefit from reading them. I think the vast majority of code that ends up handling a stdio fd falls into one of three categories:

  • Get the BorrowedFd from the std::io handles (locked or not) via AsFd impls. In this case they might check the BorrowedFd docs, but I wouldn't count on it (personally I wouldn't expect to find docs about the stdio case specifically on such a broadly useful type). An explicitly cross-reference in the docs of impl AsFd for Std{in,out,err} and/or of the handle types themselves seems useful in any case.
  • Use a library such as rustix or nix that wrap syscalls or libc functions to work with the safe FD types. These libraries either expose and document the stdio FDs themselves (see rustix::stdio), or currently document usage with the std::io handles via impl AsFd arguments (e.g., see the nix::unistd::dup2_stdout example)
  • Don't deal with BorrowedFd at all, e.g., just use RawFd and the integer constants 0, 1, 2 with libc functions.

Many of these uses ought to be able to migrate to the constants added in this PR once they're stable (but not all, e.g. if the locked handles are involved). But IMO the most likely way this will happen is if the constants get cross-referenced by docs or discovered by searching for the terms "stdin" / "stdout" / "stderr". In either case, going to the documentation for the constant and then having to follow a "See this section of BorrowedFd docs" link for the bulk of the docs feels weird.

Copy link
Contributor Author

@hanna-kruppe hanna-kruppe Feb 7, 2026

Choose a reason for hiding this comment

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

I've also considered splitting the docs conceptually along the lines of "what to consider when dealing with stdio via FDs" (useful even if you mostly use the handle and sometimes call as_fd() on it) vs. "why would you need to use the constants" (more esoteric). The former could be useful to document on the std::io handles, but there we face the analogous problem: there's no good place to put docs common to all three handles. The module docs are already very long just surveying the key items and concepts, so adding detailed docs along those lines to the module docs seems like a bad idea.

Maybe duplicating docs is the least bad option here. I don't like the risk of docs diverging due to future improvements being applied to one copy and not another, but in std::io, there's already significant duplication between handle types and the free functions for getting the handles, as well as between different streams (e.g., the Windows portability consideration is repeated four six times between stdout(), stderr(), Stdout, Stderr, StdoutLock, and StderrLock).

Copy link
Member

Choose a reason for hiding this comment

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

Ok, thanks. I'm happy to accept this as is for now at least. If someone does have any ideas there's plenty of time for the docs to be adjusted.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, thanks. I'm happy to accept this as is for now at least. If someone does have any ideas there's plenty of time for the docs to be adjusted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added as unresolved question to the tracking issue

@ChrisDenton
Copy link
Member

@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 8, 2026

📌 Commit 8b5be2b has been approved by ChrisDenton

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-review Status: Awaiting review from the assignee but also interested parties. labels Feb 8, 2026
rust-bors bot pushed a commit that referenced this pull request Feb 9, 2026
Rollup of 4 pull requests

Successful merges:

 - #150823 (Implement MVP for opaque generic const arguments)
 - #152071 (Implement stdio FD constants)
 - #152171 (Port `rustc_strict_coherence` to the new attribute parser)
 - #152291 (Port `rustc_insignificant_dtor`)

Failed merges:

 - #152180 (Port `rustc_reservation_impl` to the new attribute parser)
@rust-bors rust-bors bot merged commit 11c7917 into rust-lang:main Feb 9, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Feb 9, 2026
rust-timer added a commit that referenced this pull request Feb 9, 2026
Rollup merge of #152071 - hanna-kruppe:stdio-fd-consts, r=ChrisDenton

Implement stdio FD constants

Tracking issue: #150836
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants