Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking Issue for os_str_display #120048

Open
2 of 4 tasks
riverbl opened this issue Jan 17, 2024 · 16 comments
Open
2 of 4 tasks

Tracking Issue for os_str_display #120048

riverbl opened this issue Jan 17, 2024 · 16 comments
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@riverbl
Copy link
Contributor

riverbl commented Jan 17, 2024

Feature gate: #![feature(os_str_display)]

This is a tracking issue for the OsStr::display method.

Public API

// std::ffi

pub mod os_str {
    pub struct Display<'a> { /* private fields */ }
}

impl OsStr {
    pub fn display(&self) -> os_str::Display<'_>;
}

Steps / History

Unresolved Questions

  • None yet.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@riverbl riverbl added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jan 17, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 27, 2024
Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 27, 2024
Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 27, 2024
Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 28, 2024
Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 28, 2024
Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 28, 2024
Rollup merge of rust-lang#120051 - riverbl:os-str-display, r=m-ou-se

Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
@ijackson
Copy link
Contributor

ijackson commented Mar 7, 2024

I'm concerned about the name. I think OsStr::to_string_lossy is the better precedent. Path has just display and I think this was a mistake.

These methods are really only suitable for use in diagnostics. Places where the stringlike thing is transformed for further use must necessarily either give up on handling invalid unicode (so should use a fallible method) or do something more complicated and awkward.

@ijackson
Copy link
Contributor

ijackson commented Mar 7, 2024

(Worst case behaviour from inappropriate use of these methods is modifying the wrong file.)

@riverbl
Copy link
Contributor Author

riverbl commented Apr 6, 2024

I'm concerned about the name. I think OsStr::to_string_lossy is the better precedent. Path has just display and I think this was a mistake.

Path has both to_string_lossy and display. OsStr already has to_string_lossy, which works in the same way as Path::to_string_lossy. This issue is for adding OsStr::display, which would work in the same way as Path::display.

These methods are really only suitable for use in diagnostics. Places where the stringlike thing is transformed for further use must necessarily either give up on handling invalid unicode (so should use a fallible method) or do something more complicated and awkward.

I've encountered situations where OsStr::display would be useful for displaying a potentially non Unicode file name to the user (example). In these cases, the lossy Unicode representation of the OsStr is not used beyond being displayed.

EliteTK added a commit to EliteTK/getargs that referenced this issue Oct 2, 2024
This is an idea stolen from rust-lang/rust#120048. It's useful for the
next commit.
EliteTK added a commit to EliteTK/getargs that referenced this issue Oct 2, 2024
This is an idea stolen from rust-lang/rust#120048. It's useful for the
next commit.
@clarfonthey
Copy link
Contributor

Are there any objections to stabilising this? It's been close to a year.

@dtolnay
Copy link
Member

dtolnay commented Feb 6, 2025

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Feb 6, 2025

Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Feb 6, 2025
@ijackson
Copy link
Contributor

ijackson commented Feb 6, 2025

Apparently my earlier comment #120048 (comment) was unclear.

I object to stabilising this with this name. It should be called .display_lossy().

That the analogous method on Path is called .display() is a mistake. Indeed I have seen bugs in real code where Path::display was used inappropriately, where the author probably wouldn't have called a method with lossy in its name. That Path::display has a name encouraging erroneous use, doesn't mean we should make the same mistake again with OsStr.

I've encountered situations where OsStr::display would be useful for displaying a potentially non Unicode file name to the user (example). In these cases, the lossy Unicode representation of the OsStr is not used beyond being displayed.

For the avoidance of doubt, yes, I agree that this method is useful and ought to exist.

All I mean is that it should have lossy in its name. If people agree with me, I would be happy to make an MR to change the name here.

(I would also love to try to deprecate Path::display and replace it with Path::display_lossy. Would that be an ACP?)

@bjoernager
Copy link
Contributor

I don't think that there is anything inherent that says display must equate to lossless. The Display implementations for f16, f32, etc. are all lossy in the sense that many raw representations yield the same pretty output. I don't see why std::os_str::Display (and std::path::Display) can't also discard some data as long as it's done predictably (unless it's the platform-dependence that is the issue). The display functions themselves already add a layer of indirection.

Additionally, I don't see why Path and OsStr can't just directly implement Display as well, given that Debug exists.

@joshtriplett
Copy link
Member

Display is not in general expected to be lossless. The fact that you have to call .display() is already enough of a roadblock. (Personally, I would advocate implementing Display directly.) I don't think there's value in making the method even more cumbersome.

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Feb 6, 2025
@rfcbot
Copy link

rfcbot commented Feb 6, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@BurntSushi
Copy link
Member

I agree with @joshtriplett here, and to add, I also put some value on making this consistent in name with Path::display.

@ijackson
Copy link
Contributor

ijackson commented Feb 6, 2025

Display is not in general expected to be lossless.

Where is this currently stated? I think it needs to be a lot more prominent. For example, it's not stated in the trait docs for Display.

@bjoernager
Copy link
Contributor

Where is this currently stated? I think it needs to be a lot more prominent. For example, it's not stated in the trait docs for Display.

It's not necessary (or possible) to list every allowed behaviour of a trait. One can usually assume that a specific implementation is allowed as long as the opposite isn't explicitely stated.

@ijackson
Copy link
Contributor

ijackson commented Feb 6, 2025

It's not necessary (or possible) to list every allowed behaviour of a trait. One can usually assume that a specific implementation is allowed as long as the opposite isn't explicitely stated.

I'm sorry to say that this is not the attitude I normally expect within the Rust community. We should be trying to help Rust programmers write correct software. That means absolutely not accepting "the implementation is allowed to do anything that's not explicitly forbidden". That way lies C.

I might be persuaded if this were some obscure detail. But it's not. It's core to the semantics of the whole trait (and of ToString::to_string). That .to_string().parse().unwrap() is buggy is not merely non-obvious; I think it will be surprising to many people.

As I say, trying to use something that came from Display (perhaps via .to_string()) as the value, elsewhere in the program (or the whole system) is a foreseeable mistake. One I have come across people actually making.

At the very least we should explain that this is not the intended use of Display. And if we're going for consistency then why do we have .to_string_lossy()?

@joshtriplett
Copy link
Member

That .to_string().parse().unwrap() is buggy is not merely non-obvious; I think it will be surprising to many people.

As I say, trying to use something that came from Display (perhaps via .to_string()) as the value, elsewhere in the program (or the whole system) is a foreseeable mistake.

That might make a good clippy lint to add.

@joshtriplett
Copy link
Member

I submitted PR #136687 to expand the documentation and document the established practice that Display need not be a full representation of a type, and that FromStr need not accept the format that Display emits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants