-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Remove TrustedLen requirement from BuilderMethods::switch #77161
Conversation
The main use case of TrustedLen is allowing APIs to specialize on it, but no use of it uses that specialization. Instead, only the .len() function provided by ExactSizeIterator is used, which is already required to be accurate. Thus, the TrustedLen requirement on BuilderMethods::switch is redundant.
(rust_highfive has picked a reviewer for you, use r? to override) |
The other use of |
It's not UB if the number is wrong. According to docs, it's only a hint for allocation:
|
Good catch! However, this is part of a trait signature (though it seems that LLVM is the only backend that implements it), so I think we should maintain the status quo here. Is there a reason you want this besides cleanup? |
Admittedly I wasn't aware of this guarantee that the trait gave, but I was lucky that it's not applicable :). Yeah, neither cranelift codegen nor gcc-rust implement the trait, only the in-tree llvm codegen. Even if they used it, the API provided is unstable anyways, and it's not really hard to update. I had to spend a lot of time reading github discussions on what TrustedLen means and I think the new API is much more easy to handle because implementors don't have this mental burden of having to understand a poorly documented and niche feature of the language (which also has still unresolved questions in its tracking issue). Btw the commit that introduced the TrustedLen was a similar cleanup commit: 35705de |
I'll defer to the original author then, although my preference is to close this. I don't think we should force implementers to handle cases where If that aspect of |
r? @bjorn3 |
I wouldn't agree with that. #37572 lists the question what TrustedLen means for ExactSizeIterator as open question. The only thing that implementors can assume is that |
When I introduced the bound I assumed that it would be UB for the length to be wrong. Once cg_clif uses |
I don't think that the fact that |
Thankfully this PR has more justification than only that, in that Also note that this PR doesn't propose removal of all uses of I'll r? @petrochenkov , maybe they are more amenable to simplification PRs :). |
Simpler is better. (Also, nobody is going to return unreasonable numbers from |
📌 Commit 12ada5c has been approved by |
…petrochenkov Remove TrustedLen requirement from BuilderMethods::switch The main use case of TrustedLen is allowing APIs to specialize on it, but no use of it uses that specialization. Instead, only the .len() function provided by ExactSizeIterator is used, which is already required to be accurate. Thus, the TrustedLen requirement on BuilderMethods::switch is redundant.
Rollup of 12 pull requests Successful merges: - rust-lang#75454 (Explicitly document the size guarantees that Option makes.) - rust-lang#76631 (Add `x.py setup`) - rust-lang#77076 (Add missing code examples on slice iter types) - rust-lang#77093 (merge `need_type_info_err(_const)`) - rust-lang#77122 (Add `#![feature(const_fn_floating_point_arithmetic)]`) - rust-lang#77127 (Update mdBook) - rust-lang#77161 (Remove TrustedLen requirement from BuilderMethods::switch) - rust-lang#77166 (update Miri) - rust-lang#77181 (Add doc alias for pointer primitive) - rust-lang#77204 (Remove stray word from `ClosureKind::extends` docs) - rust-lang#77207 (Rename `whence` to `span`) - rust-lang#77211 (Remove unused #[allow(...)] statements from compiler/) Failed merges: - rust-lang#77170 (Remove `#[rustc_allow_const_fn_ptr]` and add `#![feature(const_fn_fn_ptr_basics)]`) r? `@ghost`
The main use case of TrustedLen is allowing APIs to specialize on it,
but no use of it uses that specialization. Instead, only the .len()
function provided by ExactSizeIterator is used, which is already
required to be accurate.
Thus, the TrustedLen requirement on BuilderMethods::switch is redundant.