1
Fork 0

Make Arguments constructors unsafe

This commit is contained in:
Cameron Steffen 2021-08-05 11:58:52 -05:00
parent 73d96b090b
commit 975bc18481
8 changed files with 114 additions and 41 deletions

View file

@ -3,8 +3,8 @@ use Position::*;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
use rustc_ast::token;
use rustc_ast::tokenstream::TokenStream; use rustc_ast::tokenstream::TokenStream;
use rustc_ast::{token, BlockCheckMode, UnsafeSource};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::{pluralize, Applicability, DiagnosticBuilder}; use rustc_errors::{pluralize, Applicability, DiagnosticBuilder};
use rustc_expand::base::{self, *}; use rustc_expand::base::{self, *};
@ -838,12 +838,15 @@ impl<'a, 'b> Context<'a, 'b> {
// //
// But the nested match expression is proved to perform not as well // But the nested match expression is proved to perform not as well
// as series of let's; the first approach does. // as series of let's; the first approach does.
let pat = self.ecx.pat_tuple(self.macsp, pats); let args_match = {
let arm = self.ecx.arm(self.macsp, pat, args_array); let pat = self.ecx.pat_tuple(self.macsp, pats);
let head = self.ecx.expr(self.macsp, ast::ExprKind::Tup(heads)); let arm = self.ecx.arm(self.macsp, pat, args_array);
let result = self.ecx.expr_match(self.macsp, head, vec![arm]); let head = self.ecx.expr(self.macsp, ast::ExprKind::Tup(heads));
self.ecx.expr_match(self.macsp, head, vec![arm])
};
let args_slice = self.ecx.expr_addr_of(self.macsp, result); let ident = Ident::from_str_and_span("args", self.macsp);
let args_slice = self.ecx.expr_ident(self.macsp, ident);
// Now create the fmt::Arguments struct with all our locals we created. // Now create the fmt::Arguments struct with all our locals we created.
let (fn_name, fn_args) = if self.all_pieces_simple { let (fn_name, fn_args) = if self.all_pieces_simple {
@ -857,7 +860,20 @@ impl<'a, 'b> Context<'a, 'b> {
}; };
let path = self.ecx.std_path(&[sym::fmt, sym::Arguments, Symbol::intern(fn_name)]); let path = self.ecx.std_path(&[sym::fmt, sym::Arguments, Symbol::intern(fn_name)]);
self.ecx.expr_call_global(self.macsp, path, fn_args) let arguments = self.ecx.expr_call_global(self.macsp, path, fn_args);
let body = self.ecx.expr_block(P(ast::Block {
stmts: vec![self.ecx.stmt_expr(arguments)],
id: ast::DUMMY_NODE_ID,
rules: BlockCheckMode::Unsafe(UnsafeSource::CompilerGenerated),
span: self.macsp,
tokens: None,
}));
let ident = Ident::from_str_and_span("args", self.macsp);
let binding_mode = ast::BindingMode::ByRef(ast::Mutability::Not);
let pat = self.ecx.pat_ident_binding_mode(self.macsp, ident, binding_mode);
let arm = self.ecx.arm(self.macsp, pat, body);
self.ecx.expr_match(self.macsp, args_match, vec![arm])
} }
fn format_arg( fn format_arg(

View file

@ -334,11 +334,29 @@ enum FlagV1 {
impl<'a> Arguments<'a> { impl<'a> Arguments<'a> {
/// When using the format_args!() macro, this function is used to generate the /// When using the format_args!() macro, this function is used to generate the
/// Arguments structure. /// Arguments structure.
#[cfg(not(bootstrap))]
#[doc(hidden)]
#[inline]
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
pub const unsafe fn new_v1(
pieces: &'a [&'static str],
args: &'a [ArgumentV1<'a>],
) -> Arguments<'a> {
if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
panic!("invalid args");
}
Arguments { pieces, fmt: None, args }
}
#[cfg(bootstrap)]
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")] #[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")] #[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> { pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
panic!("invalid args");
}
Arguments { pieces, fmt: None, args } Arguments { pieces, fmt: None, args }
} }
@ -348,6 +366,19 @@ impl<'a> Arguments<'a> {
/// `CountIsParam` or `CountIsNextParam` has to point to an argument /// `CountIsParam` or `CountIsNextParam` has to point to an argument
/// created with `argumentusize`. However, failing to do so doesn't cause /// created with `argumentusize`. However, failing to do so doesn't cause
/// unsafety, but will ignore invalid . /// unsafety, but will ignore invalid .
#[cfg(not(bootstrap))]
#[doc(hidden)]
#[inline]
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
pub const unsafe fn new_v1_formatted(
pieces: &'a [&'static str],
args: &'a [ArgumentV1<'a>],
fmt: &'a [rt::v1::Argument],
) -> Arguments<'a> {
Arguments { pieces, fmt: Some(fmt), args }
}
#[cfg(bootstrap)]
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")] #[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]

View file

@ -110,6 +110,7 @@
// //
// Language features: // Language features:
#![feature(abi_unadjusted)] #![feature(abi_unadjusted)]
#![feature(allow_internal_unsafe)]
#![feature(allow_internal_unstable)] #![feature(allow_internal_unstable)]
#![feature(asm)] #![feature(asm)]
#![feature(associated_type_bounds)] #![feature(associated_type_bounds)]

View file

@ -828,6 +828,7 @@ pub(crate) mod builtin {
/// assert_eq!(s, format!("hello {}", "world")); /// assert_eq!(s, format!("hello {}", "world"));
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unsafe]
#[allow_internal_unstable(fmt_internals)] #[allow_internal_unstable(fmt_internals)]
#[rustc_builtin_macro] #[rustc_builtin_macro]
#[macro_export] #[macro_export]

View file

@ -47,7 +47,15 @@ pub fn panic(expr: &'static str) -> ! {
// truncation and padding (even though none is used here). Using // truncation and padding (even though none is used here). Using
// Arguments::new_v1 may allow the compiler to omit Formatter::pad from the // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
// output binary, saving up to a few kilobytes. // output binary, saving up to a few kilobytes.
panic_fmt(fmt::Arguments::new_v1(&[expr], &[])); panic_fmt(
#[cfg(bootstrap)]
fmt::Arguments::new_v1(&[expr], &[]),
#[cfg(not(bootstrap))]
// SAFETY: Arguments::new_v1 is safe with exactly one str and zero args
unsafe {
fmt::Arguments::new_v1(&[expr], &[])
},
);
} }
#[inline] #[inline]

View file

@ -10,9 +10,11 @@ extern crate std;
fn main() { fn main() {
{ {
::std::io::_print(::core::fmt::Arguments::new_v1(&["rust\n"], ::std::io::_print(match match () { () => [], } {
&match () { ref args => unsafe {
() => [], ::core::fmt::Arguments::new_v1(&["rust\n"],
})); args)
}
});
}; };
} }

View file

@ -32,29 +32,39 @@ pub fn bar() ({
({ ({
let res = let res =
((::alloc::fmt::format as ((::alloc::fmt::format as
for<'r> fn(Arguments<'r>) -> String {format})(((::core::fmt::Arguments::new_v1 for<'r> fn(Arguments<'r>) -> String {format})((match (match (()
as as
fn(&[&'static str], &[ArgumentV1]) -> Arguments {Arguments::new_v1})((&([("test" ())
as {
&str)] ()
as =>
[&str; 1]) ([]
as as
&[&str; 1]), [ArgumentV1; 0]),
(&(match (() }
as as
()) [ArgumentV1; 0])
{ {
() ref args
=> =>
([] unsafe
as {
[ArgumentV1; 0]), ((::core::fmt::Arguments::new_v1
} as
as unsafe fn(&[&'static str], &[ArgumentV1]) -> Arguments {Arguments::new_v1})((&([("test"
[ArgumentV1; 0]) as
as &str)]
&[ArgumentV1; 0])) as
[&str; 1])
as
&[&str; 1]),
(args
as
&[ArgumentV1; 0]))
as
Arguments)
}
}
as as
Arguments)) Arguments))
as String); as String);

View file

@ -17,12 +17,16 @@ LL | bug!();
error: unexpected token: `{ error: unexpected token: `{
let res = let res =
::alloc::fmt::format(::core::fmt::Arguments::new_v1(&[""], ::alloc::fmt::format(match match (&"u8",) {
&match (&"u8",) { (arg0,) =>
(arg0,) => [::core::fmt::ArgumentV1::new(arg0,
[::core::fmt::ArgumentV1::new(arg0, ::core::fmt::Display::fmt)],
::core::fmt::Display::fmt)], } {
})); ref args => unsafe {
::core::fmt::Arguments::new_v1(&[""],
args)
}
});
res res
}.as_str()` }.as_str()`
--> $DIR/key-value-expansion.rs:48:23 --> $DIR/key-value-expansion.rs:48:23