Auto merge of #49969 - mark-i-m:allocator_fmt, r=estebank
Various rustfmt and commenting changes These are factored out of #49320 There aren't actually any changes in functionality, just rustfmt and doccomments.
This commit is contained in:
commit
dcb44ca2f7
3 changed files with 739 additions and 644 deletions
|
@ -11,12 +11,12 @@
|
|||
use rustc::middle::allocator::AllocatorKind;
|
||||
use rustc_errors;
|
||||
use syntax::abi::Abi;
|
||||
use syntax::ast::{Crate, Attribute, LitKind, StrStyle};
|
||||
use syntax::ast::{Unsafety, Constness, Generics, Mutability, Ty, Mac, Arg};
|
||||
use syntax::ast::{self, Ident, Item, ItemKind, TyKind, VisibilityKind, Expr};
|
||||
use syntax::ast::{Attribute, Crate, LitKind, StrStyle};
|
||||
use syntax::ast::{Arg, Constness, Generics, Mac, Mutability, Ty, Unsafety};
|
||||
use syntax::ast::{self, Expr, Ident, Item, ItemKind, TyKind, VisibilityKind};
|
||||
use syntax::attr;
|
||||
use syntax::codemap::{dummy_spanned, respan};
|
||||
use syntax::codemap::{ExpnInfo, NameAndSpan, MacroAttribute};
|
||||
use syntax::codemap::{ExpnInfo, MacroAttribute, NameAndSpan};
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
use syntax::ext::base::Resolver;
|
||||
use syntax::ext::build::AstBuilder;
|
||||
|
@ -31,10 +31,12 @@ use syntax_pos::{Span, DUMMY_SP};
|
|||
|
||||
use {AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
|
||||
|
||||
pub fn modify(sess: &ParseSess,
|
||||
resolver: &mut Resolver,
|
||||
krate: Crate,
|
||||
handler: &rustc_errors::Handler) -> ast::Crate {
|
||||
pub fn modify(
|
||||
sess: &ParseSess,
|
||||
resolver: &mut Resolver,
|
||||
krate: Crate,
|
||||
handler: &rustc_errors::Handler,
|
||||
) -> ast::Crate {
|
||||
ExpandAllocatorDirectives {
|
||||
handler,
|
||||
sess,
|
||||
|
@ -55,20 +57,24 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
|
|||
let name = if attr::contains_name(&item.attrs, "global_allocator") {
|
||||
"global_allocator"
|
||||
} else {
|
||||
return fold::noop_fold_item(item, self)
|
||||
return fold::noop_fold_item(item, self);
|
||||
};
|
||||
match item.node {
|
||||
ItemKind::Static(..) => {}
|
||||
_ => {
|
||||
self.handler.span_err(item.span, "allocators must be statics");
|
||||
return SmallVector::one(item)
|
||||
self.handler
|
||||
.span_err(item.span, "allocators must be statics");
|
||||
return SmallVector::one(item);
|
||||
}
|
||||
}
|
||||
|
||||
if self.found {
|
||||
self.handler.span_err(item.span, "cannot define more than one \
|
||||
#[global_allocator]");
|
||||
return SmallVector::one(item)
|
||||
self.handler.span_err(
|
||||
item.span,
|
||||
"cannot define more than one \
|
||||
#[global_allocator]",
|
||||
);
|
||||
return SmallVector::one(item);
|
||||
}
|
||||
self.found = true;
|
||||
|
||||
|
@ -80,7 +86,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
|
|||
span: None,
|
||||
allow_internal_unstable: true,
|
||||
allow_internal_unsafe: false,
|
||||
}
|
||||
},
|
||||
});
|
||||
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
|
||||
let ecfg = ExpansionConfig::default(name.to_string());
|
||||
|
@ -91,10 +97,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
|
|||
core: Ident::from_str("core"),
|
||||
cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
|
||||
};
|
||||
let super_path = f.cx.path(f.span, vec![
|
||||
Ident::from_str("super"),
|
||||
f.global,
|
||||
]);
|
||||
let super_path = f.cx.path(f.span, vec![Ident::from_str("super"), f.global]);
|
||||
let mut items = vec![
|
||||
f.cx.item_extern_crate(f.span, f.core),
|
||||
f.cx.item_use_simple(
|
||||
|
@ -114,7 +117,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
|
|||
let mut ret = SmallVector::new();
|
||||
ret.push(item);
|
||||
ret.push(module);
|
||||
return ret
|
||||
return ret;
|
||||
}
|
||||
|
||||
fn fold_mac(&mut self, mac: Mac) -> Mac {
|
||||
|
@ -139,30 +142,39 @@ impl<'a> AllocFnFactory<'a> {
|
|||
i += 1;
|
||||
name
|
||||
};
|
||||
let args = method.inputs.iter().map(|ty| {
|
||||
self.arg_ty(ty, &mut abi_args, mk)
|
||||
}).collect();
|
||||
let args = method
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|ty| self.arg_ty(ty, &mut abi_args, mk))
|
||||
.collect();
|
||||
let result = self.call_allocator(method.name, args);
|
||||
let (output_ty, output_expr) = self.ret_ty(&method.output, result);
|
||||
let kind = ItemKind::Fn(self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)),
|
||||
Unsafety::Unsafe,
|
||||
dummy_spanned(Constness::NotConst),
|
||||
Abi::Rust,
|
||||
Generics::default(),
|
||||
self.cx.block_expr(output_expr));
|
||||
self.cx.item(self.span,
|
||||
Ident::from_str(&self.kind.fn_name(method.name)),
|
||||
self.attrs(),
|
||||
kind)
|
||||
let kind = ItemKind::Fn(
|
||||
self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)),
|
||||
Unsafety::Unsafe,
|
||||
dummy_spanned(Constness::NotConst),
|
||||
Abi::Rust,
|
||||
Generics::default(),
|
||||
self.cx.block_expr(output_expr),
|
||||
);
|
||||
self.cx.item(
|
||||
self.span,
|
||||
Ident::from_str(&self.kind.fn_name(method.name)),
|
||||
self.attrs(),
|
||||
kind,
|
||||
)
|
||||
}
|
||||
|
||||
fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
|
||||
let method = self.cx.path(self.span, vec![
|
||||
self.core,
|
||||
Ident::from_str("alloc"),
|
||||
Ident::from_str("GlobalAlloc"),
|
||||
Ident::from_str(method),
|
||||
]);
|
||||
let method = self.cx.path(
|
||||
self.span,
|
||||
vec![
|
||||
self.core,
|
||||
Ident::from_str("alloc"),
|
||||
Ident::from_str("GlobalAlloc"),
|
||||
Ident::from_str(method),
|
||||
],
|
||||
);
|
||||
let method = self.cx.expr_path(method);
|
||||
let allocator = self.cx.path_ident(self.span, self.global);
|
||||
let allocator = self.cx.expr_path(allocator);
|
||||
|
@ -189,10 +201,12 @@ impl<'a> AllocFnFactory<'a> {
|
|||
]
|
||||
}
|
||||
|
||||
fn arg_ty(&self,
|
||||
ty: &AllocatorTy,
|
||||
args: &mut Vec<Arg>,
|
||||
ident: &mut FnMut() -> Ident) -> P<Expr> {
|
||||
fn arg_ty(
|
||||
&self,
|
||||
ty: &AllocatorTy,
|
||||
args: &mut Vec<Arg>,
|
||||
ident: &mut FnMut() -> Ident,
|
||||
) -> P<Expr> {
|
||||
match *ty {
|
||||
AllocatorTy::Layout => {
|
||||
let usize = self.cx.path_ident(self.span, Ident::from_str("usize"));
|
||||
|
@ -202,18 +216,19 @@ impl<'a> AllocFnFactory<'a> {
|
|||
args.push(self.cx.arg(self.span, size, ty_usize.clone()));
|
||||
args.push(self.cx.arg(self.span, align, ty_usize));
|
||||
|
||||
let layout_new = self.cx.path(self.span, vec![
|
||||
self.core,
|
||||
Ident::from_str("alloc"),
|
||||
Ident::from_str("Layout"),
|
||||
Ident::from_str("from_size_align_unchecked"),
|
||||
]);
|
||||
let layout_new = self.cx.path(
|
||||
self.span,
|
||||
vec![
|
||||
self.core,
|
||||
Ident::from_str("alloc"),
|
||||
Ident::from_str("Layout"),
|
||||
Ident::from_str("from_size_align_unchecked"),
|
||||
],
|
||||
);
|
||||
let layout_new = self.cx.expr_path(layout_new);
|
||||
let size = self.cx.expr_ident(self.span, size);
|
||||
let align = self.cx.expr_ident(self.span, align);
|
||||
let layout = self.cx.expr_call(self.span,
|
||||
layout_new,
|
||||
vec![size, align]);
|
||||
let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
|
||||
layout
|
||||
}
|
||||
|
||||
|
@ -230,9 +245,7 @@ impl<'a> AllocFnFactory<'a> {
|
|||
self.cx.expr_ident(self.span, ident)
|
||||
}
|
||||
|
||||
AllocatorTy::ResultPtr |
|
||||
AllocatorTy::Bang |
|
||||
AllocatorTy::Unit => {
|
||||
AllocatorTy::ResultPtr | AllocatorTy::Bang | AllocatorTy::Unit => {
|
||||
panic!("can't convert AllocatorTy to an argument")
|
||||
}
|
||||
}
|
||||
|
@ -249,17 +262,11 @@ impl<'a> AllocFnFactory<'a> {
|
|||
(self.ptr_u8(), expr)
|
||||
}
|
||||
|
||||
AllocatorTy::Bang => {
|
||||
(self.cx.ty(self.span, TyKind::Never), expr)
|
||||
}
|
||||
AllocatorTy::Bang => (self.cx.ty(self.span, TyKind::Never), expr),
|
||||
|
||||
AllocatorTy::Unit => {
|
||||
(self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr)
|
||||
}
|
||||
AllocatorTy::Unit => (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr),
|
||||
|
||||
AllocatorTy::Layout |
|
||||
AllocatorTy::Usize |
|
||||
AllocatorTy::Ptr => {
|
||||
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
||||
panic!("can't convert AllocatorTy to an output")
|
||||
}
|
||||
}
|
||||
|
@ -277,11 +284,14 @@ impl<'a> AllocFnFactory<'a> {
|
|||
}
|
||||
|
||||
fn ptr_opaque(&self) -> P<Ty> {
|
||||
let opaque = self.cx.path(self.span, vec![
|
||||
self.core,
|
||||
Ident::from_str("alloc"),
|
||||
Ident::from_str("Opaque"),
|
||||
]);
|
||||
let opaque = self.cx.path(
|
||||
self.span,
|
||||
vec![
|
||||
self.core,
|
||||
Ident::from_str("alloc"),
|
||||
Ident::from_str("Opaque"),
|
||||
],
|
||||
);
|
||||
let ty_opaque = self.cx.ty_path(opaque);
|
||||
self.cx.ty_ptr(self.span, ty_opaque, Mutability::Mutable)
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -962,38 +962,38 @@ enum TypeParameters<'a, 'b> {
|
|||
RibKind<'a>),
|
||||
}
|
||||
|
||||
// The rib kind controls the translation of local
|
||||
// definitions (`Def::Local`) to upvars (`Def::Upvar`).
|
||||
/// The rib kind controls the translation of local
|
||||
/// definitions (`Def::Local`) to upvars (`Def::Upvar`).
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
enum RibKind<'a> {
|
||||
// No translation needs to be applied.
|
||||
/// No translation needs to be applied.
|
||||
NormalRibKind,
|
||||
|
||||
// We passed through a closure scope at the given node ID.
|
||||
// Translate upvars as appropriate.
|
||||
/// We passed through a closure scope at the given node ID.
|
||||
/// Translate upvars as appropriate.
|
||||
ClosureRibKind(NodeId /* func id */),
|
||||
|
||||
// We passed through an impl or trait and are now in one of its
|
||||
// methods or associated types. Allow references to ty params that impl or trait
|
||||
// binds. Disallow any other upvars (including other ty params that are
|
||||
// upvars).
|
||||
/// We passed through an impl or trait and are now in one of its
|
||||
/// methods or associated types. Allow references to ty params that impl or trait
|
||||
/// binds. Disallow any other upvars (including other ty params that are
|
||||
/// upvars).
|
||||
TraitOrImplItemRibKind,
|
||||
|
||||
// We passed through an item scope. Disallow upvars.
|
||||
/// We passed through an item scope. Disallow upvars.
|
||||
ItemRibKind,
|
||||
|
||||
// We're in a constant item. Can't refer to dynamic stuff.
|
||||
/// We're in a constant item. Can't refer to dynamic stuff.
|
||||
ConstantItemRibKind,
|
||||
|
||||
// We passed through a module.
|
||||
/// We passed through a module.
|
||||
ModuleRibKind(Module<'a>),
|
||||
|
||||
// We passed through a `macro_rules!` statement
|
||||
/// We passed through a `macro_rules!` statement
|
||||
MacroDefinition(DefId),
|
||||
|
||||
// All bindings in this rib are type parameters that can't be used
|
||||
// from the default of a type parameter because they're not declared
|
||||
// before said type parameter. Also see the `visit_generics` override.
|
||||
/// All bindings in this rib are type parameters that can't be used
|
||||
/// from the default of a type parameter because they're not declared
|
||||
/// before said type parameter. Also see the `visit_generics` override.
|
||||
ForwardTyParamBanRibKind,
|
||||
}
|
||||
|
||||
|
@ -1198,7 +1198,7 @@ impl<'a> fmt::Debug for ModuleData<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
// Records a possibly-private value, type, or module definition.
|
||||
/// Records a possibly-private value, type, or module definition.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct NameBinding<'a> {
|
||||
kind: NameBindingKind<'a>,
|
||||
|
@ -1408,36 +1408,36 @@ pub struct Resolver<'a> {
|
|||
|
||||
prelude: Option<Module<'a>>,
|
||||
|
||||
// n.b. This is used only for better diagnostics, not name resolution itself.
|
||||
/// n.b. This is used only for better diagnostics, not name resolution itself.
|
||||
has_self: FxHashSet<DefId>,
|
||||
|
||||
// Names of fields of an item `DefId` accessible with dot syntax.
|
||||
// Used for hints during error reporting.
|
||||
/// Names of fields of an item `DefId` accessible with dot syntax.
|
||||
/// Used for hints during error reporting.
|
||||
field_names: FxHashMap<DefId, Vec<Name>>,
|
||||
|
||||
// All imports known to succeed or fail.
|
||||
/// All imports known to succeed or fail.
|
||||
determined_imports: Vec<&'a ImportDirective<'a>>,
|
||||
|
||||
// All non-determined imports.
|
||||
/// All non-determined imports.
|
||||
indeterminate_imports: Vec<&'a ImportDirective<'a>>,
|
||||
|
||||
// The module that represents the current item scope.
|
||||
/// The module that represents the current item scope.
|
||||
current_module: Module<'a>,
|
||||
|
||||
// The current set of local scopes for types and values.
|
||||
// FIXME #4948: Reuse ribs to avoid allocation.
|
||||
/// The current set of local scopes for types and values.
|
||||
/// FIXME #4948: Reuse ribs to avoid allocation.
|
||||
ribs: PerNS<Vec<Rib<'a>>>,
|
||||
|
||||
// The current set of local scopes, for labels.
|
||||
/// The current set of local scopes, for labels.
|
||||
label_ribs: Vec<Rib<'a>>,
|
||||
|
||||
// The trait that the current context can refer to.
|
||||
/// The trait that the current context can refer to.
|
||||
current_trait_ref: Option<(Module<'a>, TraitRef)>,
|
||||
|
||||
// The current self type if inside an impl (used for better errors).
|
||||
/// The current self type if inside an impl (used for better errors).
|
||||
current_self_type: Option<Ty>,
|
||||
|
||||
// The idents for the primitive types.
|
||||
/// The idents for the primitive types.
|
||||
primitive_type_table: PrimitiveTypeTable,
|
||||
|
||||
def_map: DefMap,
|
||||
|
@ -1446,20 +1446,20 @@ pub struct Resolver<'a> {
|
|||
pub export_map: ExportMap,
|
||||
pub trait_map: TraitMap,
|
||||
|
||||
// A map from nodes to anonymous modules.
|
||||
// Anonymous modules are pseudo-modules that are implicitly created around items
|
||||
// contained within blocks.
|
||||
//
|
||||
// For example, if we have this:
|
||||
//
|
||||
// fn f() {
|
||||
// fn g() {
|
||||
// ...
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// There will be an anonymous module created around `g` with the ID of the
|
||||
// entry block for `f`.
|
||||
/// A map from nodes to anonymous modules.
|
||||
/// Anonymous modules are pseudo-modules that are implicitly created around items
|
||||
/// contained within blocks.
|
||||
///
|
||||
/// For example, if we have this:
|
||||
///
|
||||
/// fn f() {
|
||||
/// fn g() {
|
||||
/// ...
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// There will be an anonymous module created around `g` with the ID of the
|
||||
/// entry block for `f`.
|
||||
block_map: NodeMap<Module<'a>>,
|
||||
module_map: FxHashMap<DefId, Module<'a>>,
|
||||
extern_module_map: FxHashMap<(DefId, bool /* MacrosOnly? */), Module<'a>>,
|
||||
|
@ -1487,7 +1487,8 @@ pub struct Resolver<'a> {
|
|||
|
||||
arenas: &'a ResolverArenas<'a>,
|
||||
dummy_binding: &'a NameBinding<'a>,
|
||||
use_extern_macros: bool, // true if `#![feature(use_extern_macros)]`
|
||||
/// true if `#![feature(use_extern_macros)]`
|
||||
use_extern_macros: bool,
|
||||
|
||||
crate_loader: &'a mut CrateLoader,
|
||||
macro_names: FxHashSet<Ident>,
|
||||
|
@ -1501,29 +1502,29 @@ pub struct Resolver<'a> {
|
|||
pub whitelisted_legacy_custom_derives: Vec<Name>,
|
||||
pub found_unresolved_macro: bool,
|
||||
|
||||
// List of crate local macros that we need to warn about as being unused.
|
||||
// Right now this only includes macro_rules! macros, and macros 2.0.
|
||||
/// List of crate local macros that we need to warn about as being unused.
|
||||
/// Right now this only includes macro_rules! macros, and macros 2.0.
|
||||
unused_macros: FxHashSet<DefId>,
|
||||
|
||||
// Maps the `Mark` of an expansion to its containing module or block.
|
||||
/// Maps the `Mark` of an expansion to its containing module or block.
|
||||
invocations: FxHashMap<Mark, &'a InvocationData<'a>>,
|
||||
|
||||
// Avoid duplicated errors for "name already defined".
|
||||
/// Avoid duplicated errors for "name already defined".
|
||||
name_already_seen: FxHashMap<Name, Span>,
|
||||
|
||||
// If `#![feature(proc_macro)]` is set
|
||||
/// If `#![feature(proc_macro)]` is set
|
||||
proc_macro_enabled: bool,
|
||||
|
||||
// A set of procedural macros imported by `#[macro_use]` that have already been warned about
|
||||
/// A set of procedural macros imported by `#[macro_use]` that have already been warned about
|
||||
warned_proc_macros: FxHashSet<Name>,
|
||||
|
||||
potentially_unused_imports: Vec<&'a ImportDirective<'a>>,
|
||||
|
||||
// This table maps struct IDs into struct constructor IDs,
|
||||
// it's not used during normal resolution, only for better error reporting.
|
||||
/// This table maps struct IDs into struct constructor IDs,
|
||||
/// it's not used during normal resolution, only for better error reporting.
|
||||
struct_constructors: DefIdMap<(Def, ty::Visibility)>,
|
||||
|
||||
// Only used for better errors on `fn(): fn()`
|
||||
/// Only used for better errors on `fn(): fn()`
|
||||
current_type_ascription: Vec<Span>,
|
||||
|
||||
injected_crate: Option<Module<'a>>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue