@@ -518,6 +518,12 @@ macro_rules! uint_impl {
518518 /// This function will panic if `n` is greater than or equal to the number of
519519 /// bits in `self`.
520520 ///
521+ /// ## Overflow behavior
522+ ///
523+ /// The above panic happens only if overflow checks are enabled (default in debug mode).
524+ /// With overflow checks disabled (default in release mode), there is no panic; instead,
525+ /// the value is shifted by `n % Self::BITS`.
526+ ///
521527 /// # Examples
522528 ///
523529 /// ```
@@ -543,21 +549,31 @@ macro_rules! uint_impl {
543549 ///
544550 /// ```should_panic
545551 /// #![feature(funnel_shifts)]
552+ /// # #![feature(cfg_overflow_checks)]
553+ /// # #[cfg(overflow_checks)] {
546554 ///
547555 #[ doc = concat!( "let a = " , stringify!( $SelfT) , "::MAX;" ) ]
548556 /// // Okay
549557 #[ doc = concat!( "let _ = a.rotate_left(" , stringify!( $SelfT) , "::BITS);" ) ]
550- /// // Panics
558+ /// // Panics (only when overflow checks are enabled)
551559 #[ doc = concat!( "let _ = a.funnel_shl(a, " , stringify!( $SelfT) , "::BITS);" ) ]
560+ /// # }
561+ /// # #[cfg(not(overflow_checks))] panic!("fulfill should_panic");
552562 /// ```
553563 #[ rustc_const_unstable( feature = "funnel_shifts" , issue = "145686" ) ]
554564 #[ unstable( feature = "funnel_shifts" , issue = "145686" ) ]
555565 #[ must_use = "this returns the result of the operation, without modifying the original" ]
556566 #[ inline( always) ]
567+ #[ rustc_inherit_overflow_checks]
557568 pub const fn funnel_shl( self , right: Self , n: u32 ) -> Self {
558- assert!( n < Self :: BITS , "attempt to funnel shift left with overflow" ) ;
559- // SAFETY: just checked that `shift` is in-range
560- unsafe { self . unchecked_funnel_shl( right, n) }
569+ if intrinsics:: overflow_checks( ) {
570+ assert!( n < Self :: BITS , "attempt to funnel shift left with overflow" ) ;
571+ }
572+ // SAFETY: `n` is wrapped to within range
573+ unsafe {
574+ let n = n & ( Self :: BITS - 1 ) ;
575+ self . unchecked_funnel_shl( right, n)
576+ }
561577 }
562578
563579 /// Performs a right funnel shift.
@@ -574,6 +590,12 @@ macro_rules! uint_impl {
574590 /// This function will panic if `n` is greater than or equal to the number of
575591 /// bits in `self`.
576592 ///
593+ /// ## Overflow behavior
594+ ///
595+ /// The above panic happens only if overflow checks are enabled (default in debug mode).
596+ /// With overflow checks disabled (default in release mode), there is no panic; instead,
597+ /// the value is shifted by `n % Self::BITS`.
598+ ///
577599 /// # Examples
578600 ///
579601 /// ```
@@ -599,21 +621,31 @@ macro_rules! uint_impl {
599621 ///
600622 /// ```should_panic
601623 /// #![feature(funnel_shifts)]
624+ /// # #![feature(cfg_overflow_checks)]
625+ /// # #[cfg(overflow_checks)] {
602626 ///
603627 #[ doc = concat!( "let a = " , stringify!( $SelfT) , "::MAX;" ) ]
604628 /// // Okay
605629 #[ doc = concat!( "let _ = a.rotate_right(" , stringify!( $SelfT) , "::BITS);" ) ]
606- /// // Panics
630+ /// // Panics (only when overflow checks are enabled)
607631 #[ doc = concat!( "let _ = a.funnel_shr(a, " , stringify!( $SelfT) , "::BITS);" ) ]
632+ /// # }
633+ /// # #[cfg(not(overflow_checks))] panic!("fulfill should_panic");
608634 /// ```
609635 #[ rustc_const_unstable( feature = "funnel_shifts" , issue = "145686" ) ]
610636 #[ unstable( feature = "funnel_shifts" , issue = "145686" ) ]
611637 #[ must_use = "this returns the result of the operation, without modifying the original" ]
612638 #[ inline( always) ]
639+ #[ rustc_inherit_overflow_checks]
613640 pub const fn funnel_shr( self , right: Self , n: u32 ) -> Self {
614- assert!( n < Self :: BITS , "attempt to funnel shift right with overflow" ) ;
615- // SAFETY: just checked that `shift` is in-range
616- unsafe { self . unchecked_funnel_shr( right, n) }
641+ if intrinsics:: overflow_checks( ) {
642+ assert!( n < Self :: BITS , "attempt to funnel shift right with overflow" ) ;
643+ }
644+ // SAFETY: `n` is wrapped to within range
645+ unsafe {
646+ let n = n & ( Self :: BITS - 1 ) ;
647+ self . unchecked_funnel_shr( right, n)
648+ }
617649 }
618650
619651 /// Unchecked funnel shift left.
0 commit comments