11#![ allow( missing_debug_implementations) ]
22#![ unstable( feature = "fmt_internals" , reason = "internal to format_args!" , issue = "none" ) ]
33
4- //! These are the lang items used by format_args!().
4+ //! All types and methods in this file are used by the compiler in
5+ //! the expansion/lowering of format_args!().
6+ //!
7+ //! Do not modify them without understanding the consequences for the format_args!() macro.
58
69use super :: * ;
710use crate :: hint:: unreachable_unchecked;
@@ -110,46 +113,45 @@ macro_rules! argument_new {
110113 } ;
111114}
112115
113- #[ rustc_diagnostic_item = "ArgumentMethods" ]
114116impl Argument < ' _ > {
115117 #[ inline]
116- pub fn new_display < T : Display > ( x : & T ) -> Argument < ' _ > {
118+ pub const fn new_display < T : Display > ( x : & T ) -> Argument < ' _ > {
117119 argument_new ! ( T , x, <T as Display >:: fmt)
118120 }
119121 #[ inline]
120- pub fn new_debug < T : Debug > ( x : & T ) -> Argument < ' _ > {
122+ pub const fn new_debug < T : Debug > ( x : & T ) -> Argument < ' _ > {
121123 argument_new ! ( T , x, <T as Debug >:: fmt)
122124 }
123125 #[ inline]
124- pub fn new_debug_noop < T : Debug > ( x : & T ) -> Argument < ' _ > {
126+ pub const fn new_debug_noop < T : Debug > ( x : & T ) -> Argument < ' _ > {
125127 argument_new ! ( T , x, |_: & T , _| Ok ( ( ) ) )
126128 }
127129 #[ inline]
128- pub fn new_octal < T : Octal > ( x : & T ) -> Argument < ' _ > {
130+ pub const fn new_octal < T : Octal > ( x : & T ) -> Argument < ' _ > {
129131 argument_new ! ( T , x, <T as Octal >:: fmt)
130132 }
131133 #[ inline]
132- pub fn new_lower_hex < T : LowerHex > ( x : & T ) -> Argument < ' _ > {
134+ pub const fn new_lower_hex < T : LowerHex > ( x : & T ) -> Argument < ' _ > {
133135 argument_new ! ( T , x, <T as LowerHex >:: fmt)
134136 }
135137 #[ inline]
136- pub fn new_upper_hex < T : UpperHex > ( x : & T ) -> Argument < ' _ > {
138+ pub const fn new_upper_hex < T : UpperHex > ( x : & T ) -> Argument < ' _ > {
137139 argument_new ! ( T , x, <T as UpperHex >:: fmt)
138140 }
139141 #[ inline]
140- pub fn new_pointer < T : Pointer > ( x : & T ) -> Argument < ' _ > {
142+ pub const fn new_pointer < T : Pointer > ( x : & T ) -> Argument < ' _ > {
141143 argument_new ! ( T , x, <T as Pointer >:: fmt)
142144 }
143145 #[ inline]
144- pub fn new_binary < T : Binary > ( x : & T ) -> Argument < ' _ > {
146+ pub const fn new_binary < T : Binary > ( x : & T ) -> Argument < ' _ > {
145147 argument_new ! ( T , x, <T as Binary >:: fmt)
146148 }
147149 #[ inline]
148- pub fn new_lower_exp < T : LowerExp > ( x : & T ) -> Argument < ' _ > {
150+ pub const fn new_lower_exp < T : LowerExp > ( x : & T ) -> Argument < ' _ > {
149151 argument_new ! ( T , x, <T as LowerExp >:: fmt)
150152 }
151153 #[ inline]
152- pub fn new_upper_exp < T : UpperExp > ( x : & T ) -> Argument < ' _ > {
154+ pub const fn new_upper_exp < T : UpperExp > ( x : & T ) -> Argument < ' _ > {
153155 argument_new ! ( T , x, <T as UpperExp >:: fmt)
154156 }
155157 #[ inline]
@@ -200,15 +202,8 @@ impl Argument<'_> {
200202 /// let f = format_args!("{}", "a");
201203 /// println!("{f}");
202204 /// ```
203- ///
204- /// This function should _not_ be const, to make sure we don't accept
205- /// format_args!() and panic!() with arguments in const, even when not evaluated:
206- ///
207- /// ```compile_fail,E0015
208- /// const _: () = if false { panic!("a {}", "a") };
209- /// ```
210205 #[ inline]
211- pub fn none ( ) -> [ Self ; 0 ] {
206+ pub const fn none ( ) -> [ Self ; 0 ] {
212207 [ ]
213208 }
214209}
@@ -229,3 +224,57 @@ impl UnsafeArg {
229224 Self { _private : ( ) }
230225 }
231226}
227+
228+ /// Used by the format_args!() macro to create a fmt::Arguments object.
229+ #[ doc( hidden) ]
230+ #[ unstable( feature = "fmt_internals" , issue = "none" ) ]
231+ #[ rustc_diagnostic_item = "FmtArgumentsNew" ]
232+ impl < ' a > Arguments < ' a > {
233+ #[ inline]
234+ pub const fn new_const < const N : usize > ( pieces : & ' a [ & ' static str ; N ] ) -> Self {
235+ const { assert ! ( N <= 1 ) } ;
236+ Arguments { pieces, fmt : None , args : & [ ] }
237+ }
238+
239+ /// When using the format_args!() macro, this function is used to generate the
240+ /// Arguments structure.
241+ ///
242+ /// This function should _not_ be const, to make sure we don't accept
243+ /// format_args!() and panic!() with arguments in const, even when not evaluated:
244+ ///
245+ /// ```compile_fail,E0015
246+ /// const _: () = if false { panic!("a {}", "a") };
247+ /// ```
248+ #[ inline]
249+ pub fn new_v1 < const P : usize , const A : usize > (
250+ pieces : & ' a [ & ' static str ; P ] ,
251+ args : & ' a [ rt:: Argument < ' a > ; A ] ,
252+ ) -> Arguments < ' a > {
253+ const { assert ! ( P >= A && P <= A + 1 , "invalid args" ) }
254+ Arguments { pieces, fmt : None , args }
255+ }
256+
257+ /// Specifies nonstandard formatting parameters.
258+ ///
259+ /// An `rt::UnsafeArg` is required because the following invariants must be held
260+ /// in order for this function to be safe:
261+ /// 1. The `pieces` slice must be at least as long as `fmt`.
262+ /// 2. Every `rt::Placeholder::position` value within `fmt` must be a valid index of `args`.
263+ /// 3. Every `rt::Count::Param` within `fmt` must contain a valid index of `args`.
264+ ///
265+ /// This function should _not_ be const, to make sure we don't accept
266+ /// format_args!() and panic!() with arguments in const, even when not evaluated:
267+ ///
268+ /// ```compile_fail,E0015
269+ /// const _: () = if false { panic!("a {:1}", "a") };
270+ /// ```
271+ #[ inline]
272+ pub fn new_v1_formatted (
273+ pieces : & ' a [ & ' static str ] ,
274+ args : & ' a [ rt:: Argument < ' a > ] ,
275+ fmt : & ' a [ rt:: Placeholder ] ,
276+ _unsafe_arg : rt:: UnsafeArg ,
277+ ) -> Arguments < ' a > {
278+ Arguments { pieces, fmt : Some ( fmt) , args }
279+ }
280+ }
0 commit comments