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

Start using pattern types in libcore #136006

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Jan 24, 2025

cc #135996

blocked on rust-analyzer getting support for computing the right layout for pattern types

cc @Veykril no rush here, as long as we can't replace NonNull, there's no point in doing this change just yet

@rustbot

This comment was marked as outdated.

@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 24, 2025
@oli-obk oli-obk added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 24, 2025
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

Well r=me when/if it does get unblocked.

@rust-log-analyzer

This comment has been minimized.

#[repr(transparent)]
$(#[$m])*
#[cfg(not(bootstrap))]
$vis struct $name(pattern_type!($int is $low..=$high));
Copy link
Member

Choose a reason for hiding this comment

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

Unsure: at least for the NonZeroBlahInner ones, it'd be nicer if these could just be type $name = pattern_type!($int is $low..=$high);, without the extra wrapper. Is that feasible, or are the trait implementations too far off?

(Relatedly, I'd love to be able to just derive traits on these again, particularly to not have to manually StructuralPartialEq.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea... I wanna get there, but directly using pattern types is not a great experience at present

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Jan 27, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@Veykril
Copy link
Member

Veykril commented Jan 27, 2025

Re rust-analyzer blocker, as it turns out to implement pattern types (to a degree that would unblock this) it comes down to the same architecture changes required as for rust-lang/rust-analyzer#7434 (which I am currently looking into), so that will take a bit.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Jan 27, 2025

Some changes occurred to the CTFE machinery

cc @rust-lang/wg-const-eval

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@@ -763,6 +763,9 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
}
interp_ok(true)
}
ty::Pat(base_ty, ..) => self.try_visit_primitive(
&value.transmute(self.ecx().layout_of(*base_ty)?, self.ecx())?,
Copy link
Member

Choose a reason for hiding this comment

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

Primitive types basically by definition do not have fields, so having recursion here is very odd. This should probably be handled elsewhere.

Does the pattern not affect validity?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it does, and validity checking is still handling it accidentally because all use cases have the pattern type wrapped in a struct 🤦

Copy link
Contributor Author

Choose a reason for hiding this comment

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

gonna pull this all out into a separate PR with separate tests

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling std v0.0.0 (/checkout/library/std)
error[E0080]: evaluation of constant value failed
##[error]    --> /checkout/library/core/src/num/nonzero.rs:1664:9
     |
1664 |           pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
     |           ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
...
2132 | / nonzero_integer! {
2133 | |     Self = NonZeroI8,
2134 | |     Primitive = signed i8,
2135 | |     UnsignedPrimitive = u8,
2141 | |     reversed = "0x48",
2142 | | }
     | |_- in this macro invocation
     |
---
     |
2005 |               Self::MAX
     |               ^^^^^^^^^
...
2132 | / nonzero_integer! {
2133 | |     Self = NonZeroI8,
2134 | |     Primitive = signed i8,
2135 | |     UnsignedPrimitive = u8,
2141 | |     reversed = "0x48",
2142 | | }
     | |_- in this macro invocation
     |
     |
     = note: this note originates in the macro `nonzero_integer_signedness_dependent_methods` which comes from the expansion of the macro `nonzero_integer` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0080]: evaluation of constant value failed
##[error]    --> /checkout/library/core/src/num/nonzero.rs:1664:9
     |
1664 |           pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
     |           ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
...
2144 | / nonzero_integer! {
2145 | |     Self = NonZeroI16,
2146 | |     Primitive = signed i16,
2147 | |     UnsignedPrimitive = u16,
2153 | |     reversed = "0x2c48",
2154 | | }
     | |_- in this macro invocation
     |
---
     |
2005 |               Self::MAX
     |               ^^^^^^^^^
...
2144 | / nonzero_integer! {
2145 | |     Self = NonZeroI16,
2146 | |     Primitive = signed i16,
2147 | |     UnsignedPrimitive = u16,
2153 | |     reversed = "0x2c48",
2154 | | }
     | |_- in this macro invocation
     |
     |
     = note: this note originates in the macro `nonzero_integer_signedness_dependent_methods` which comes from the expansion of the macro `nonzero_integer` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0080]: evaluation of constant value failed
##[error]    --> /checkout/library/core/src/num/nonzero.rs:1664:9
     |
1664 |           pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
     |           ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
...
2156 | / nonzero_integer! {
2157 | |     Self = NonZeroI32,
2158 | |     Primitive = signed i32,
2159 | |     UnsignedPrimitive = u32,
2165 | |     reversed = "0x1e6a2c48",
2166 | | }
     | |_- in this macro invocation
     |
---
     |
2005 |               Self::MAX
     |               ^^^^^^^^^
...
2156 | / nonzero_integer! {
2157 | |     Self = NonZeroI32,
2158 | |     Primitive = signed i32,
2159 | |     UnsignedPrimitive = u32,
2165 | |     reversed = "0x1e6a2c48",
2166 | | }
     | |_- in this macro invocation
     |
     |
     = note: this note originates in the macro `nonzero_integer_signedness_dependent_methods` which comes from the expansion of the macro `nonzero_integer` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0080]: evaluation of constant value failed
##[error]    --> /checkout/library/core/src/num/nonzero.rs:1664:9
     |
1664 |           pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
     |           ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
...
2168 | / nonzero_integer! {
2169 | |     Self = NonZeroI64,
2170 | |     Primitive = signed i64,
2171 | |     UnsignedPrimitive = u64,
2177 | |     reversed = "0x6a2c48091e6a2c48",
2178 | | }
     | |_- in this macro invocation
     |
---
     |
2005 |               Self::MAX
     |               ^^^^^^^^^
...
2168 | / nonzero_integer! {
2169 | |     Self = NonZeroI64,
2170 | |     Primitive = signed i64,
2171 | |     UnsignedPrimitive = u64,
2177 | |     reversed = "0x6a2c48091e6a2c48",
2178 | | }
     | |_- in this macro invocation
     |
     |
     = note: this note originates in the macro `nonzero_integer_signedness_dependent_methods` which comes from the expansion of the macro `nonzero_integer` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0080]: evaluation of constant value failed
##[error]    --> /checkout/library/core/src/num/nonzero.rs:1664:9
     |
1664 |           pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
     |           ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
...
2180 | / nonzero_integer! {
2181 | |     Self = NonZeroI128,
2182 | |     Primitive = signed i128,
2183 | |     UnsignedPrimitive = u128,
2189 | |     reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2190 | | }
     | |_- in this macro invocation
     |
---
     |
2005 |               Self::MAX
     |               ^^^^^^^^^
...
2180 | / nonzero_integer! {
2181 | |     Self = NonZeroI128,
2182 | |     Primitive = signed i128,
2183 | |     UnsignedPrimitive = u128,
2189 | |     reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2190 | | }
     | |_- in this macro invocation
     |
     |
     = note: this note originates in the macro `nonzero_integer_signedness_dependent_methods` which comes from the expansion of the macro `nonzero_integer` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0080]: evaluation of constant value failed
##[error]    --> /checkout/library/core/src/num/nonzero.rs:1664:9
     |
1664 |           pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
     |           ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
...
2219 | / nonzero_integer! {
2220 | |     Self = NonZeroIsize,
2221 | |     Primitive = signed isize,
2222 | |     UnsignedPrimitive = usize,
2228 | |     reversed = "0x6a2c48091e6a2c48",
2229 | | }
     | |_- in this macro invocation
     |
---
     |
2005 |               Self::MAX
     |               ^^^^^^^^^
...
2219 | / nonzero_integer! {
2220 | |     Self = NonZeroIsize,
2221 | |     Primitive = signed isize,
2222 | |     UnsignedPrimitive = usize,
2228 | |     reversed = "0x6a2c48091e6a2c48",
2229 | | }
     | |_- in this macro invocation
     |
---
    |
53  |                   unsafe { $name(crate::mem::transmute(val)) }
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
...
107 | / define_valid_range_type! {
108 | |     pub struct Nanoseconds(u32 as u32 in 0..=999_999_999);
    | |_- in this macro invocation
    |
note: inside `num::niche_types::Nanoseconds::new_unchecked`
   --> /checkout/library/core/src/num/niche_types.rs:53:26
   --> /checkout/library/core/src/num/niche_types.rs:53:26
    |
53  |                   unsafe { $name(crate::mem::transmute(val)) }
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
107 | / define_valid_range_type! {
108 | |     pub struct Nanoseconds(u32 as u32 in 0..=999_999_999);
    | |_- in this macro invocation
note: inside `num::niche_types::Nanoseconds::ZERO`
   --> /checkout/library/core/src/num/niche_types.rs:113:37
    |
    |
113 |     pub const ZERO: Self = unsafe { Nanoseconds::new_unchecked(0) };
    |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: this error originates in the macro `define_valid_range_type` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
   --> /checkout/library/core/src/time.rs:225:33
    |
225 |         Duration { secs, nanos: Nanoseconds::ZERO }

error[E0080]: evaluation of constant value failed
##[error]   --> /checkout/library/core/src/num/niche_types.rs:53:26
    |
    |
53  |                   unsafe { $name(crate::mem::transmute(val)) }
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
...
107 | / define_valid_range_type! {
108 | |     pub struct Nanoseconds(u32 as u32 in 0..=999_999_999);
    | |_- in this macro invocation
    |
note: inside `num::niche_types::Nanoseconds::new_unchecked`
   --> /checkout/library/core/src/num/niche_types.rs:53:26
   --> /checkout/library/core/src/num/niche_types.rs:53:26
    |
53  |                   unsafe { $name(crate::mem::transmute(val)) }
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
107 | / define_valid_range_type! {
108 | |     pub struct Nanoseconds(u32 as u32 in 0..=999_999_999);
    | |_- in this macro invocation
note: inside `time::Duration::new`
   --> /checkout/library/core/src/time.rs:197:46
    |
    |
197 |             Duration { secs, nanos: unsafe { Nanoseconds::new_unchecked(nanos) } }
note: inside `time::Duration::MAX`
   --> /checkout/library/core/src/time.rs:170:31
    |
    |
170 |     pub const MAX: Duration = Duration::new(u64::MAX, NANOS_PER_SEC - 1);
    = note: this error originates in the macro `define_valid_range_type` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
   --> /checkout/library/core/src/time.rs:673:21
---
    |
53  |                   unsafe { $name(crate::mem::transmute(val)) }
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
...
107 | / define_valid_range_type! {
108 | |     pub struct Nanoseconds(u32 as u32 in 0..=999_999_999);
    | |_- in this macro invocation
    |
note: inside `num::niche_types::Nanoseconds::new_unchecked`
   --> /checkout/library/core/src/num/niche_types.rs:53:26
   --> /checkout/library/core/src/num/niche_types.rs:53:26
    |
53  |                   unsafe { $name(crate::mem::transmute(val)) }
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
107 | / define_valid_range_type! {
108 | |     pub struct Nanoseconds(u32 as u32 in 0..=999_999_999);
    | |_- in this macro invocation
note: inside `time::Duration::from_nanos`
   --> /checkout/library/core/src/time.rs:306:37
    |
    |
306 |         let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_nanos) };
note: inside `time::Duration::ZERO`
   --> /checkout/library/core/src/time.rs:151:32
    |
151 |     pub const ZERO: Duration = Duration::from_nanos(0);
---
    |                     ^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0080`.
error: could not compile `core` (lib) due to 9 previous errors
fatal error: failed to build sysroot: sysroot build failed
Command has failed. Rerun with -v to see more details.
  local time: Tue Jan 28 12:26:13 UTC 2025
  network time: Tue, 28 Jan 2025 12:26:13 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-blocked Status: Blocked on something else such as an RFC or other implementation work. 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.

8 participants