Skip to content

Add TryFromIntError::kind method and IntErrorKind::NotAPowerOfTwo variant#153178

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
sorairolake:feature/try-from-int-error-kind
Apr 17, 2026
Merged

Add TryFromIntError::kind method and IntErrorKind::NotAPowerOfTwo variant#153178
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
sorairolake:feature/try-from-int-error-kind

Conversation

@sorairolake
Copy link
Copy Markdown
Contributor

@sorairolake sorairolake commented Feb 27, 2026

View all comments

This pull request adds kind method to TryFromIntError, which outputs the detailed cause of converting an integer failing.

This is primarily intended for use in cases where there are multiple causes for failure, such as converting from a large signed integer type to a small signed integer type.

At the moment, this method returns &IntErrorKind. This type implements the Copy trait and could return IntErrorKind, but returns a reference to align with the behavior of ParseIntError::kind. If it is not necessary to align the behavior of TryFromIntError::kind with ParseIntError::kind, I'll change this method to return IntErrorKind.

Before this pull request, IntErrorKind was only used by ParseIntError, but I changed it to be used by TryFromIntError as well, so I updated IntErrorKind's documentation.

impl TryFrom<usize> for Alignment returns TryFromIntError as an error if the given value is not a power of two. However, IntErrorKind does not have a variant that represents this. So I added the variant NotAPowerOfTwo to represent this. For this variant to stabilize, both try_from_int_error_kind and ptr_alignment_type features must be stabilized.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 27, 2026
@sorairolake
Copy link
Copy Markdown
Contributor Author

@rustbot label +T-libs-api -T-libs
r? libs-api

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 27, 2026
@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch 2 times, most recently from 38cf8bf to 20c70f9 Compare February 27, 2026 15:21
@sorairolake sorairolake changed the title feat: Add TryFromIntError::kind feat: Add TryFromIntError::kind method and IntErrorKind::NotAPowerOfTwo variant Feb 28, 2026
@sorairolake sorairolake changed the title feat: Add TryFromIntError::kind method and IntErrorKind::NotAPowerOfTwo variant Add TryFromIntError::kind method and IntErrorKind::NotAPowerOfTwo variant Feb 28, 2026
@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch from 20c70f9 to a0a5c69 Compare February 28, 2026 11:07
@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch from a0a5c69 to 989926f Compare March 17, 2026 02:23
@sorairolake sorairolake marked this pull request as ready for review March 17, 2026 02:27
@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 Mar 17, 2026
@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch from 989926f to e8c4298 Compare March 17, 2026 06:34
@sorairolake
Copy link
Copy Markdown
Contributor Author

r? libs-api

@rustbot rustbot assigned Amanieu and unassigned the8472 Mar 17, 2026
@rust-bors

This comment has been minimized.

@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch from e8c4298 to 775108a Compare April 10, 2026 14:55
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 10, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Comment thread library/core/src/num/error.rs
Comment thread library/core/src/num/error.rs Outdated
@tgross35
Copy link
Copy Markdown
Contributor

r? tgross35

@rustbot rustbot assigned tgross35 and unassigned Amanieu Apr 16, 2026
@sorairolake sorairolake marked this pull request as draft April 17, 2026 04:20
@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 Apr 17, 2026
@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch from b6f5330 to ace8aa1 Compare April 17, 2026 04:28
@sorairolake sorairolake marked this pull request as ready for review April 17, 2026 04:28
@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 Apr 17, 2026
@sorairolake sorairolake requested a review from tgross35 April 17, 2026 04:32
Copy link
Copy Markdown
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Comment thread library/core/src/num/error.rs Outdated
@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch from ace8aa1 to c35ed1a Compare April 17, 2026 05:05
@tgross35
Copy link
Copy Markdown
Contributor

Thanks!
@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 17, 2026

📌 Commit c35ed1a has been approved by tgross35

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 Apr 17, 2026
rust-bors bot pushed a commit that referenced this pull request Apr 17, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - #154781 (Fix attribute order implementation)
 - #155242 (resolve: Introduce `(Local,Extern)Module` newtypes for local and external modules respectively)
 - #149614 (Use `MaybeDangling` in `std`)
 - #153178 (Add `TryFromIntError::kind` method and `IntErrorKind::NotAPowerOfTwo` variant)
 - #155049 (Documenting the case of `Weak::upgrade` returning `None` when the value behind the reference is missing)

Failed merges:

 - #155308 (Make `OnDuplicate::Error` the default for attributes)
rust-bors bot pushed a commit that referenced this pull request Apr 17, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - #154781 (Fix attribute order implementation)
 - #155242 (resolve: Introduce `(Local,Extern)Module` newtypes for local and external modules respectively)
 - #149614 (Use `MaybeDangling` in `std`)
 - #153178 (Add `TryFromIntError::kind` method and `IntErrorKind::NotAPowerOfTwo` variant)
 - #155049 (Documenting the case of `Weak::upgrade` returning `None` when the value behind the reference is missing)

Failed merges:

 - #155308 (Make `OnDuplicate::Error` the default for attributes)
@rust-bors rust-bors bot merged commit 932d807 into rust-lang:main Apr 17, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 17, 2026
rust-timer added a commit that referenced this pull request Apr 17, 2026
Rollup merge of #153178 - sorairolake:feature/try-from-int-error-kind, r=tgross35

Add `TryFromIntError::kind` method and `IntErrorKind::NotAPowerOfTwo` variant

- ACP: rust-lang/libs-team#746
- Tracking issue: #153978

This pull request adds `kind` method to [`TryFromIntError`](https://doc.rust-lang.org/core/num/struct.TryFromIntError.html), which outputs the detailed cause of converting an integer failing.

This is primarily intended for use in cases where there are multiple causes for failure, such as converting from a large signed integer type to a small signed integer type.

At the moment, this method returns [`&IntErrorKind`](https://doc.rust-lang.org/core/num/enum.IntErrorKind.html). This type implements the `Copy` trait and could return `IntErrorKind`, but returns a reference to align with the behavior of [`ParseIntError::kind`](https://doc.rust-lang.org/core/num/struct.ParseIntError.html#method.kind). If it is not necessary to align the behavior of `TryFromIntError::kind` with `ParseIntError::kind`, I'll change this method to return `IntErrorKind`.

Before this pull request, `IntErrorKind` was only used by [`ParseIntError`](https://doc.rust-lang.org/core/num/struct.ParseIntError.html), but I changed it to be used by `TryFromIntError` as well, so I updated `IntErrorKind`'s documentation.

[`impl TryFrom<usize> for Alignment`](https://doc.rust-lang.org/std/ptr/struct.Alignment.html#impl-TryFrom%3Cusize%3E-for-Alignment) returns `TryFromIntError` as an error if the given value is not a power of two. However, `IntErrorKind` does not have a variant that represents this. So I added the variant `NotAPowerOfTwo` to represent this. For this variant to stabilize, both `try_from_int_error_kind` and `ptr_alignment_type` features must be stabilized.
@sorairolake sorairolake deleted the feature/try-from-int-error-kind branch April 17, 2026 23:57
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@rust-timer build c1d01b6

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (c1d01b6): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.1% [0.1%, 0.1%] 2
Regressions ❌
(secondary)
0.2% [0.0%, 0.2%] 8
Improvements ✅
(primary)
-0.3% [-0.3%, -0.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [-0.3%, 0.1%] 3

Max RSS (memory usage)

Results (primary -2.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
4.2% [4.2%, 4.2%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-5.4% [-8.4%, -2.3%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.2% [-8.4%, 4.2%] 3

Cycles

Results (primary -2.2%, secondary -3.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.2% [-2.2%, -2.2%] 1
Improvements ✅
(secondary)
-3.8% [-3.8%, -3.8%] 1
All ❌✅ (primary) -2.2% [-2.2%, -2.2%] 1

Binary size

Results (primary -0.0%, secondary 0.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 1
Improvements ✅
(primary)
-0.0% [-0.0%, -0.0%] 4
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-0.0%, -0.0%] 4

Bootstrap: 490.91s -> 492.735s (0.37%)
Artifact size: 394.18 MiB -> 394.30 MiB (0.03%)

@rustbot rustbot added the perf-regression Performance regression. label Apr 18, 2026
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

Caused the perf regression in #155432

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants