@@ -104,6 +104,18 @@ macro_rules! nonzero_integers {
104
104
#[ inline]
105
105
#[ rustc_const_stable( feature = "const_nonzero_get" , since = "1.34.0" ) ]
106
106
pub const fn get( self ) -> $Int {
107
+ // FIXME: Remove this after LLVM supports `!range` metadata for function
108
+ // arguments https://github.com/llvm/llvm-project/issues/76628
109
+ //
110
+ // Rustc can set range metadata only if it loads `self` from
111
+ // memory somewhere. If the value of `self` was from by-value argument
112
+ // of some not-inlined function, LLVM don't have range metadata
113
+ // to understand that the value cannot be zero.
114
+
115
+ // SAFETY: It is an invariant of this type.
116
+ unsafe {
117
+ intrinsics:: assume( self . 0 != 0 ) ;
118
+ }
107
119
self . 0
108
120
}
109
121
@@ -114,7 +126,9 @@ macro_rules! nonzero_integers {
114
126
#[ doc = concat!( "Converts a `" , stringify!( $Ty) , "` into an `" , stringify!( $Int) , "`" ) ]
115
127
#[ inline]
116
128
fn from( nonzero: $Ty) -> Self {
117
- nonzero. 0
129
+ // Call nonzero to keep information range information
130
+ // from get method.
131
+ nonzero. get( )
118
132
}
119
133
}
120
134
@@ -233,7 +247,7 @@ macro_rules! nonzero_leading_trailing_zeros {
233
247
#[ inline]
234
248
pub const fn leading_zeros( self ) -> u32 {
235
249
// SAFETY: since `self` cannot be zero, it is safe to call `ctlz_nonzero`.
236
- unsafe { intrinsics:: ctlz_nonzero( self . 0 as $Uint) as u32 }
250
+ unsafe { intrinsics:: ctlz_nonzero( self . get ( ) as $Uint) as u32 }
237
251
}
238
252
239
253
/// Returns the number of trailing zeros in the binary representation
@@ -257,7 +271,7 @@ macro_rules! nonzero_leading_trailing_zeros {
257
271
#[ inline]
258
272
pub const fn trailing_zeros( self ) -> u32 {
259
273
// SAFETY: since `self` cannot be zero, it is safe to call `cttz_nonzero`.
260
- unsafe { intrinsics:: cttz_nonzero( self . 0 as $Uint) as u32 }
274
+ unsafe { intrinsics:: cttz_nonzero( self . get ( ) as $Uint) as u32 }
261
275
}
262
276
263
277
}
@@ -515,7 +529,7 @@ macro_rules! nonzero_unsigned_operations {
515
529
without modifying the original"]
516
530
#[ inline]
517
531
pub const fn ilog10( self ) -> u32 {
518
- super :: int_log10:: $Int( self . 0 )
532
+ super :: int_log10:: $Int( self . get ( ) )
519
533
}
520
534
521
535
/// Calculates the middle point of `self` and `rhs`.
0 commit comments