Skip to content

Conversation

@oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Jul 8, 2025

coercing NonZero<T> to T if all the types are available at the cast site.

This is fairly limited, as (as the tests show), this does not allow "reborrowing" &NonZero<T> as &T. It also fails quickly if the types aren't fully clear at the coercion site, but only resolved later.

Also cannot aid in trait selection or similar, even if there is clearly only one option.

Related:

cc @traviscross

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 8, 2025
@compiler-errors
Copy link
Member

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jul 8, 2025

⌛ Trying commit e341923 with merge 4a4dd8e

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jul 8, 2025
Limited nonzero coercion

coercing `NonZero<T>` to `T` if all the types are available at the cast site.

This is fairly limited, as (as the tests show), this does not allow "reborrowing" `&NonZero<T>` as `&T`. It also fails quickly if the types aren't fully clear at the coercion site, but only resolved later.

Also cannot aid in trait selection or similar, even if there is clearly only one option.

Related:

* #143594
* rust-lang/rfcs#3786

cc `@traviscross`
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 8, 2025
@rust-bors
Copy link

rust-bors bot commented Jul 8, 2025

☀️ Try build successful (CI)
Build commit: 4a4dd8e (4a4dd8eb0e65f6bfdacfb10fa73970e2a130d69a, parent: 040e2f8b9ff2d76fbe2146d6003e297ed4532088)

@rust-timer

This comment has been minimized.

@traviscross traviscross added the T-lang Relevant to the language team label Jul 8, 2025
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4a4dd8e): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@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)
1.6% [1.6%, 1.6%] 1
Regressions ❌
(secondary)
1.6% [1.6%, 1.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 2
All ❌✅ (primary) 1.6% [1.6%, 1.6%] 1

Max RSS (memory usage)

Results (primary -4.6%)

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)
-4.6% [-4.6%, -4.6%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -4.6% [-4.6%, -4.6%] 1

Cycles

Results (primary -3.1%, secondary -4.0%)

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)
-3.1% [-3.1%, -3.1%] 1
Improvements ✅
(secondary)
-4.0% [-4.0%, -4.0%] 1
All ❌✅ (primary) -3.1% [-3.1%, -3.1%] 1

Binary size

Results (secondary 0.0%)

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.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Bootstrap: 467.351s -> 465.395s (-0.42%)
Artifact size: 372.36 MiB -> 372.37 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 8, 2025
@bors
Copy link
Collaborator

bors commented Jul 25, 2025

☔ The latest upstream changes (presumably #144449) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 25, 2025
@oli-obk oli-obk force-pushed the limited-nonzero-coercion branch from e341923 to 871e2e3 Compare November 4, 2025 09:25
@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Nov 4, 2025
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
To only update this specific test, also pass `--test-args mismatched_types/non_zero_coerce_fail.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/mismatched_types/non_zero_coerce_fail.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/mismatched_types/non_zero_coerce_fail" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0599]: no method named `foo` found for struct `NonZero<T>` in the current scope
##[error]  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail.rs:15:7
   |
LL |     x.foo();
   |       ^^^ method not found in `NonZero<u8>`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
note: `Foo` defines an item `foo`, perhaps you need to implement it
  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail.rs:3:1
   |
LL | trait Foo: Sized {
   | ^^^^^^^^^^^^^^^^

error[E0599]: no method named `bar` found for struct `NonZero<T>` in the current scope
##[error]  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail.rs:17:7
   |
LL |     x.bar();
   |       ^^^ method not found in `NonZero<u8>`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
note: `Foo` defines an item `bar`, perhaps you need to implement it
  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail.rs:3:1
   |
LL | trait Foo: Sized {
   | ^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `NonZero<u8>: Foo` is not satisfied
---
LL |     a.foo();
   |       ^^^ method not found in `NonZero<{integer}>`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
note: `Foo` defines an item `foo`, perhaps you need to implement it
  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail.rs:3:1
   |
LL | trait Foo: Sized {
   | ^^^^^^^^^^^^^^^^

error[E0599]: no method named `bar` found for struct `NonZero<T>` in the current scope
##[error]  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail.rs:26:7
   |
LL |     a.bar();
   |       ^^^ method not found in `NonZero<{integer}>`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
note: `Foo` defines an item `bar`, perhaps you need to implement it
  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail.rs:3:1
   |
LL | trait Foo: Sized {
   | ^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `NonZero<{integer}>: Foo` is not satisfied
##[error]  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail.rs:28:9
   |
LL |     foo(a);
   |     --- ^ the trait `Foo` is not implemented for `NonZero<{integer}>`
   |     |
   |     required by a bound introduced by this call
---
+    | ^^^^^^^^^^^^^^^^^^^ `u8` implements `Foo<u8>`
+ LL | impl Foo<u16> for u16 {}
+    | ^^^^^^^^^^^^^^^^^^^^^ `u16` implements `Foo<u16>`
28 
29 error[E0277]: the trait bound `{integer}: Foo<NonZero<u8>>` is not satisfied
30   --> $DIR/non_zero_coerce_fail2.rs:18:11

34    |       |
35    |       required by a bound introduced by this call
36    |
-    = help: the following other types implement trait `Foo<T>`:
---
+    | ^^^^^^^^^^^^^^^^^^^ `u8` implements `Foo<u8>`
+ LL | impl Foo<u16> for u16 {}
+    | ^^^^^^^^^^^^^^^^^^^^^ `u16` implements `Foo<u16>`
68 
69 error[E0277]: the trait bound `{integer}: Foo<NonZero<u8>>` is not satisfied
70   --> $DIR/non_zero_coerce_fail2.rs:27:11

74    |       |
75    |       required by a bound introduced by this call
76    |
-    = help: the following other types implement trait `Foo<T>`:
---
To only update this specific test, also pass `--test-args mismatched_types/non_zero_coerce_fail2.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/mismatched_types/non_zero_coerce_fail2" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:13:14
   |
LL |     5_u8.foo(&x);
   |          --- ^^ expected `&u8`, found `&NonZero<u8>`
   |          |
   |          arguments to this method are incorrect
   |
   = note: expected reference `&u8`
              found reference `&NonZero<u8>`
note: method defined here
  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:4:8
   |
LL |     fn foo(&self, other: &T) {}
   |        ^^^        ---------

error[E0277]: the trait bound `{integer}: Foo<NonZero<u8>>` is not satisfied
##[error]  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:16:11
   |
LL |     5.foo(&x);
   |       --- ^^ the trait `Foo<NonZero<u8>>` is not implemented for `{integer}`
   |       |
   |       required by a bound introduced by this call
   |
help: the following other types implement trait `Foo<T>`
  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:8:1
   |
LL | impl Foo<u8> for u8 {}
   | ^^^^^^^^^^^^^^^^^^^ `u8` implements `Foo<u8>`
LL | impl Foo<u16> for u16 {}
   | ^^^^^^^^^^^^^^^^^^^^^ `u16` implements `Foo<u16>`

error[E0277]: the trait bound `{integer}: Foo<NonZero<u8>>` is not satisfied
##[error]  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:18:11
   |
LL |     5.bar(x);
   |       --- ^ the trait `Foo<NonZero<u8>>` is not implemented for `{integer}`
   |       |
   |       required by a bound introduced by this call
   |
help: the following other types implement trait `Foo<T>`
  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:8:1
---

error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:22:14
   |
LL |     5_u8.foo(&a);
   |          --- ^^ expected `&u8`, found `&NonZero<{integer}>`
   |          |
   |          arguments to this method are incorrect
   |
   = note: expected reference `&u8`
              found reference `&NonZero<{integer}>`
note: method defined here
  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:4:8
   |
LL |     fn foo(&self, other: &T) {}
   |        ^^^        ---------

error[E0277]: the trait bound `{integer}: Foo<NonZero<u8>>` is not satisfied
##[error]  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:25:11
   |
LL |     5.foo(&a);
   |       --- ^^ the trait `Foo<NonZero<u8>>` is not implemented for `{integer}`
   |       |
   |       required by a bound introduced by this call
   |
help: the following other types implement trait `Foo<T>`
  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:8:1
   |
LL | impl Foo<u8> for u8 {}
   | ^^^^^^^^^^^^^^^^^^^ `u8` implements `Foo<u8>`
LL | impl Foo<u16> for u16 {}
   | ^^^^^^^^^^^^^^^^^^^^^ `u16` implements `Foo<u16>`

error[E0277]: the trait bound `{integer}: Foo<NonZero<u8>>` is not satisfied
##[error]  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:27:11
   |
LL |     5.bar(a);
   |       --- ^ the trait `Foo<NonZero<u8>>` is not implemented for `{integer}`
   |       |
   |       required by a bound introduced by this call
   |
help: the following other types implement trait `Foo<T>`
  --> /checkout/tests/ui/mismatched_types/non_zero_coerce_fail2.rs:8:1

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team 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.

7 participants