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

Make NonZero constructors generic. #120521

Merged
merged 5 commits into from
Feb 8, 2024

Conversation

reitermarkus
Copy link
Contributor

@reitermarkus reitermarkus commented Jan 31, 2024

This makes NonZero constructors generic, so that NonZero::new can be used without turbofish syntax.

Tracking issue: #120257

I cannot figure out how to make this work with const traits. Not sure if I'm using it wrong or whether there's a bug:

101 |         if n == T::ZERO {
    |            ^^^^^^^^^^^^ expected `host`, found `true`
    |
    = note: expected constant `host`
               found constant `true`

r? @dtolnay

@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 Jan 31, 2024
@rust-log-analyzer

This comment has been minimized.

library/core/src/num/nonzero.rs Outdated Show resolved Hide resolved
@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 Jan 31, 2024
@Noratrieb
Copy link
Member

@rust-lang/project-const-traits looks like a bug. Should this be relying on const traits in stable code anyways, are they ready and bug free enough for that?

@oli-obk
Copy link
Contributor

oli-obk commented Jan 31, 2024

@rust-lang/project-const-traits looks like a bug.

just a diagnostics bug, the error was correct.

Should this be relying on const traits in stable code anyways, are they ready and bug free enough for that?

nope, should definitely not use them in stable code.

@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 Feb 1, 2024
Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

impl lgtm. the main PR message needs updating.

cc #120257 for backlinking

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks!

@dtolnay
Copy link
Member

dtolnay commented Feb 1, 2024

@bors r=dtolnay,oli-obk

@bors
Copy link
Contributor

bors commented Feb 1, 2024

📌 Commit a5042de has been approved by dtolnay,oli-obk

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Feb 1, 2024

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

@bors bors 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 1, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 3, 2024
…ructors, r=dtolnay,oli-obk

Make `NonZero` constructors generic.

This makes `NonZero` constructors generic, so that `NonZero::new` can be used without turbofish syntax.

Tracking issue: rust-lang#120257

~~I cannot figure out how to make this work with `const` traits. Not sure if I'm using it wrong or whether there's a bug:~~

```rust
101 |         if n == T::ZERO {
    |            ^^^^^^^^^^^^ expected `host`, found `true`
    |
    = note: expected constant `host`
               found constant `true`
```

r? `@dtolnay`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 4, 2024
…ructors, r=dtolnay,oli-obk

Make `NonZero` constructors generic.

This makes `NonZero` constructors generic, so that `NonZero::new` can be used without turbofish syntax.

Tracking issue: rust-lang#120257

~~I cannot figure out how to make this work with `const` traits. Not sure if I'm using it wrong or whether there's a bug:~~

```rust
101 |         if n == T::ZERO {
    |            ^^^^^^^^^^^^ expected `host`, found `true`
    |
    = note: expected constant `host`
               found constant `true`
```

r? ``@dtolnay``
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 5, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#120507 (Account for non-overlapping unmet trait bounds in suggestion)
 - rust-lang#120518 (riscv only supports split_debuginfo=off for now)
 - rust-lang#120521 (Make `NonZero` constructors generic.)
 - rust-lang#120527 (Switch OwnedStore handle count to AtomicU32)
 - rust-lang#120550 (Continue to borrowck even if there were previous errors)
 - rust-lang#120587 (miri: normalize struct tail in ABI compat check)
 - rust-lang#120590 (Remove unused args from functions)
 - rust-lang#120607 (fix rust-lang#120603 by adding a check in default_read_buf)

Failed merges:

 - rust-lang#120575 (Simplify codegen diagnostic handling)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 5, 2024
…ructors, r=dtolnay,oli-obk

Make `NonZero` constructors generic.

This makes `NonZero` constructors generic, so that `NonZero::new` can be used without turbofish syntax.

Tracking issue: rust-lang#120257

~~I cannot figure out how to make this work with `const` traits. Not sure if I'm using it wrong or whether there's a bug:~~

```rust
101 |         if n == T::ZERO {
    |            ^^^^^^^^^^^^ expected `host`, found `true`
    |
    = note: expected constant `host`
               found constant `true`
```

r? ```@dtolnay```
@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Feb 7, 2024
@bors
Copy link
Contributor

bors commented Feb 7, 2024

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented Feb 7, 2024

📌 Commit 4229875 has been approved by dtolnay

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Feb 8, 2024

⌛ Testing commit 4229875 with merge 384b02c...

@bors
Copy link
Contributor

bors commented Feb 8, 2024

☀️ Test successful - checks-actions
Approved by: dtolnay
Pushing 384b02c to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 8, 2024
@bors bors merged commit 384b02c into rust-lang:master Feb 8, 2024
12 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Feb 8, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (384b02c): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.7% [0.4%, 1.7%] 9
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.0% [-1.0%, -1.0%] 1
Improvements ✅
(secondary)
-1.3% [-1.3%, -1.3%] 1
All ❌✅ (primary) 0.5% [-1.0%, 1.7%] 10

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
3.3% [0.2%, 7.0%] 3
Regressions ❌
(secondary)
2.1% [2.1%, 2.1%] 1
Improvements ✅
(primary)
-3.0% [-3.0%, -3.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.8% [-3.0%, 7.0%] 4

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.1% [0.6%, 1.7%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.1% [0.6%, 1.7%] 3

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.8%] 30
Regressions ❌
(secondary)
0.9% [0.9%, 1.0%] 3
Improvements ✅
(primary)
-0.2% [-0.7%, -0.0%] 15
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-0.7%, 0.8%] 45

Bootstrap: 663.461s -> 663.578s (0.02%)
Artifact size: 308.30 MiB -> 308.27 MiB (-0.01%)

@rustbot rustbot added the perf-regression Performance regression. label Feb 8, 2024
pub const fn new(n: T) -> Option<Self> {
// SAFETY: Memory layout optimization guarantees that `Option<NonZero<T>>` has
// the same layout and size as `T`, with `0` representing `None`.
unsafe { ptr::read(ptr::addr_of!(n).cast()) }
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a reimplementation of either transmute_copy or intrinsics::transmute_unchecked?

Copy link
Contributor

Choose a reason for hiding this comment

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

it used to be. I assume this was changed to be able to remove the #[rustc_allow_const_fn_unstable(const_refs_to_cell)], but then that wasn't removed 😆

Copy link
Contributor Author

@reitermarkus reitermarkus Feb 8, 2024

Choose a reason for hiding this comment

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

No, it was changed to fix the failing niche-filling.rs test, caused by the assert! call in transmute_copy. Maybe transmute_unchecked is what I was looking for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried removing #[rustc_allow_const_fn_unstable(const_refs_to_cell)] as well, but it seems to be used by addr_of!.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 9, 2024
…ructors, r=Nilstrieb

Use `transmute_unchecked` in `NonZero::new`.

Tracking issue: rust-lang#120257

See rust-lang#120521 (comment).
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 9, 2024
…ructors, r=Nilstrieb

Use `transmute_unchecked` in `NonZero::new`.

Tracking issue: rust-lang#120257

See rust-lang#120521 (comment).
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 9, 2024
…ructors, r=Nilstrieb

Use `transmute_unchecked` in `NonZero::new`.

Tracking issue: rust-lang#120257

See rust-lang#120521 (comment).
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 9, 2024
…ructors, r=Nilstrieb

Use `transmute_unchecked` in `NonZero::new`.

Tracking issue: rust-lang#120257

See rust-lang#120521 (comment).
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 9, 2024
Rollup merge of rust-lang#120809 - reitermarkus:generic-nonzero-constructors, r=Nilstrieb

Use `transmute_unchecked` in `NonZero::new`.

Tracking issue: rust-lang#120257

See rust-lang#120521 (comment).
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Feb 10, 2024
…r=Nilstrieb

Use `transmute_unchecked` in `NonZero::new`.

Tracking issue: rust-lang/rust#120257

See rust-lang/rust#120521 (comment).
@Kobzol
Copy link
Contributor

Kobzol commented Feb 13, 2024

Regression mostly resolved by #120809.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Feb 13, 2024
oli-obk added a commit to oli-obk/rust that referenced this pull request Feb 13, 2024
…r=dtolnay

Make `NonZero::get` generic.

Tracking issue: rust-lang#120257

Depends on rust-lang#120521.

r? `@dtolnay`
oli-obk added a commit to oli-obk/rust that referenced this pull request Feb 13, 2024
…r=dtolnay

Make `NonZero::get` generic.

Tracking issue: rust-lang#120257

Depends on rust-lang#120521.

r? ``@dtolnay``
oli-obk added a commit to oli-obk/rust that referenced this pull request Feb 15, 2024
…r=dtolnay

Make `NonZero::get` generic.

Tracking issue: rust-lang#120257

Depends on rust-lang#120521.

r? ```@dtolnay```
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 16, 2024
…dtolnay

Make `NonZero::get` generic.

Tracking issue: rust-lang#120257

Depends on rust-lang#120521.

r? `@dtolnay`
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 17, 2024
…dtolnay

Make `NonZero::get` generic.

Tracking issue: rust-lang#120257

Depends on rust-lang#120521.

r? `@dtolnay`
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Feb 17, 2024
Make `NonZero::get` generic.

Tracking issue: rust-lang/rust#120257

Depends on rust-lang/rust#120521.

r? `@dtolnay`
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Apr 7, 2024
Make `NonZero::get` generic.

Tracking issue: rust-lang/rust#120257

Depends on rust-lang/rust#120521.

r? `@dtolnay`
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 27, 2024
Make `NonZero::get` generic.

Tracking issue: rust-lang/rust#120257

Depends on rust-lang/rust#120521.

r? `@dtolnay`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. 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.