@@ -164,6 +164,27 @@ where
164164 }
165165 }
166166 }
167+
168+ /// Returns the contained value as a primitive type.
169+ #[ stable( feature = "nonzero" , since = "1.28.0" ) ]
170+ #[ rustc_const_stable( feature = "const_nonzero_get" , since = "1.34.0" ) ]
171+ #[ inline]
172+ pub const fn get ( self ) -> T {
173+ // FIXME: This can be changed to simply `self.0` once LLVM supports `!range` metadata
174+ // for function arguments: https://github.com/llvm/llvm-project/issues/76628
175+ //
176+ // Rustc can set range metadata only if it loads `self` from
177+ // memory somewhere. If the value of `self` was from by-value argument
178+ // of some not-inlined function, LLVM don't have range metadata
179+ // to understand that the value cannot be zero.
180+ match Self :: new ( self . 0 ) {
181+ Some ( Self ( n) ) => n,
182+ None => {
183+ // SAFETY: `NonZero` is guaranteed to only contain non-zero values, so this is unreachable.
184+ unsafe { intrinsics:: unreachable ( ) }
185+ }
186+ }
187+ }
167188}
168189
169190macro_rules! impl_nonzero_fmt {
@@ -225,26 +246,6 @@ macro_rules! nonzero_integer {
225246 pub type $Ty = NonZero <$Int>;
226247
227248 impl $Ty {
228- /// Returns the value as a primitive type.
229- #[ $stability]
230- #[ inline]
231- #[ rustc_const_stable( feature = "const_nonzero_get" , since = "1.34.0" ) ]
232- pub const fn get( self ) -> $Int {
233- // FIXME: Remove this after LLVM supports `!range` metadata for function
234- // arguments https://github.com/llvm/llvm-project/issues/76628
235- //
236- // Rustc can set range metadata only if it loads `self` from
237- // memory somewhere. If the value of `self` was from by-value argument
238- // of some not-inlined function, LLVM don't have range metadata
239- // to understand that the value cannot be zero.
240-
241- // SAFETY: It is an invariant of this type.
242- unsafe {
243- intrinsics:: assume( self . 0 != 0 ) ;
244- }
245- self . 0
246- }
247-
248249 /// The size of this non-zero integer type in bits.
249250 ///
250251 #[ doc = concat!( "This value is equal to [`" , stringify!( $Int) , "::BITS`]." ) ]
0 commit comments