Skip to content

Try to use thin slices in some places in the HIR #109594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
use rustc_ast::{Block, BlockCheckMode, Local, LocalKind, Stmt, StmtKind};
use rustc_hir as hir;
use rustc_data_structures::thin_slice::ThinSlice;

use smallvec::SmallVec;

Expand All @@ -27,7 +28,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_stmts(
&mut self,
mut ast_stmts: &[Stmt],
) -> (&'hir [hir::Stmt<'hir>], Option<&'hir hir::Expr<'hir>>) {
) -> (&'hir ThinSlice<hir::Stmt<'hir>>, Option<&'hir hir::Expr<'hir>>) {
let mut stmts = SmallVec::<[hir::Stmt<'hir>; 8]>::new();
let mut expr = None;
while let [s, tail @ ..] = ast_stmts {
Expand Down Expand Up @@ -78,7 +79,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
ast_stmts = &ast_stmts[1..];
}
(self.arena.alloc_from_iter(stmts), expr)
(self.arena.allocate_thin_from_iter(stmts), expr)
}

fn lower_local(&mut self, l: &Local) -> &'hir hir::Local<'hir> {
Expand Down
129 changes: 66 additions & 63 deletions compiler/rustc_ast_lowering/src/expr.rs

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions compiler/rustc_ast_lowering/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rustc_ast as ast;
use rustc_ast::visit::{self, Visitor};
use rustc_ast::*;
use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::thin_slice::ThinSlice;
use rustc_hir as hir;
use rustc_span::{
sym,
Expand Down Expand Up @@ -212,7 +213,7 @@ fn make_argument<'hir>(
Usize => sym::from_usize,
},
));
ctx.expr_call_mut(sp, new_fn, std::slice::from_ref(arg))
ctx.expr_call_mut(sp, new_fn, arena_thin_vec![ctx; *arg])
}

/// Generate a hir expression for a format_args Count.
Expand Down Expand Up @@ -247,7 +248,7 @@ fn make_count<'hir>(
hir::LangItem::FormatCount,
sym::Is,
));
let value = ctx.arena.alloc_from_iter([ctx.expr_usize(sp, *n)]);
let value = ctx.arena.allocate_thin_from_iter([ctx.expr_usize(sp, *n)]);
ctx.expr_call_mut(sp, count_is, value)
}
Some(FormatCount::Argument(arg)) => {
Expand All @@ -258,7 +259,7 @@ fn make_count<'hir>(
hir::LangItem::FormatCount,
sym::Param,
));
let value = ctx.arena.alloc_from_iter([ctx.expr_usize(sp, i)]);
let value = ctx.arena.allocate_thin_from_iter([ctx.expr_usize(sp, i)]);
ctx.expr_call_mut(sp, count_param, value)
} else {
ctx.expr(
Expand Down Expand Up @@ -340,7 +341,7 @@ fn make_format_spec<'hir>(
hir::LangItem::FormatPlaceholder,
sym::new,
));
let args = ctx.arena.alloc_from_iter([position, fill, align, flags, precision, width]);
let args = ctx.arena.allocate_thin_from_iter([position, fill, align, flags, precision, width]);
ctx.expr_call_mut(sp, format_placeholder_new, args)
}

Expand Down Expand Up @@ -426,7 +427,7 @@ fn expand_format_args<'hir>(
hir::LangItem::FormatArguments,
sym::new_const,
));
let new_args = ctx.arena.alloc_from_iter([lit_pieces]);
let new_args = ctx.arena.allocate_thin_from_iter([lit_pieces]);
return hir::ExprKind::Call(new, new_args);
}

Expand Down Expand Up @@ -530,17 +531,17 @@ fn expand_format_args<'hir>(
hir::LangItem::FormatUnsafeArg,
sym::new,
));
let unsafe_arg_new_call = ctx.expr_call(macsp, unsafe_arg_new, &[]);
let unsafe_arg_new_call = ctx.expr_call(macsp, unsafe_arg_new, ThinSlice::empty());
let hir_id = ctx.next_id();
let unsafe_arg = ctx.expr_block(ctx.arena.alloc(hir::Block {
stmts: &[],
stmts: ThinSlice::empty(),
expr: Some(unsafe_arg_new_call),
hir_id,
rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
span: macsp,
targeted_by_break: false,
}));
let args = ctx.arena.alloc_from_iter([lit_pieces, args, format_options, unsafe_arg]);
let args = ctx.arena.allocate_thin_from_iter([lit_pieces, args, format_options, unsafe_arg]);
hir::ExprKind::Call(new_v1_formatted, args)
} else {
// Generate:
Expand All @@ -553,7 +554,7 @@ fn expand_format_args<'hir>(
hir::LangItem::FormatArguments,
sym::new_v1,
));
let new_args = ctx.arena.alloc_from_iter([lit_pieces, args]);
let new_args = ctx.arena.allocate_thin_from_iter([lit_pieces, args]);
hir::ExprKind::Call(new_v1, new_args)
}
}
Expand Down
13 changes: 7 additions & 6 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_ast::ptr::P;
use rustc_ast::visit::AssocCtxt;
use rustc_ast::*;
use rustc_data_structures::sorted_map::SortedMap;
use rustc_data_structures::thin_slice::ThinSlice;
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
Expand Down Expand Up @@ -1172,7 +1173,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// ```
let body = this.block_all(
desugared_span,
this.arena.alloc_from_iter(statements),
this.arena.allocate_thin_from_iter(statements),
Some(user_body),
);

Expand Down Expand Up @@ -1366,8 +1367,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
predicates.extend(impl_trait_bounds.into_iter());

let lowered_generics = self.arena.alloc(hir::Generics {
params: self.arena.alloc_from_iter(params),
predicates: self.arena.alloc_from_iter(predicates),
params: self.arena.allocate_thin_from_iter(params),
predicates: self.arena.allocate_thin_from_iter(predicates),
has_where_clause_predicates,
where_clause_span,
span,
Expand Down Expand Up @@ -1418,7 +1419,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
res,
segments: self
.arena
.alloc_from_iter([hir::PathSegment::new(ident, hir_id, res)]),
.allocate_thin_from_iter([hir::PathSegment::new(ident, hir_id, res)]),
});
let ty_id = self.next_id();
let bounded_ty =
Expand All @@ -1428,7 +1429,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
bounded_ty: self.arena.alloc(bounded_ty),
bounds,
span,
bound_generic_params: &[],
bound_generic_params: ThinSlice::empty(),
origin,
}))
}
Expand Down Expand Up @@ -1459,7 +1460,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
bounded_ty: self
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {
bounds: self.arena.allocate_thin_from_iter(bounds.iter().map(|bound| {
self.lower_param_bound(
bound,
&ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
Expand Down
51 changes: 29 additions & 22 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,20 @@ use rustc_span::{Span, DUMMY_SP};
use smallvec::SmallVec;
use std::collections::hash_map::Entry;
use thin_vec::ThinVec;
use rustc_data_structures::thin_slice::ThinSlice;

macro_rules! arena_vec {
($this:expr; $($x:expr),*) => (
$this.arena.alloc_from_iter([$($x),*])
);
}

macro_rules! arena_thin_vec {
($this:expr; $($x:expr),*) => (
$this.arena.allocate_thin_from_iter([$($x),*])
);
}

mod asm;
mod block;
mod errors;
Expand Down Expand Up @@ -848,7 +855,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
&mut self,
binder: NodeId,
generic_params: &[GenericParam],
) -> &'hir [hir::GenericParam<'hir>] {
) -> &'hir ThinSlice<hir::GenericParam<'hir>> {
let mut generic_params: Vec<_> = self
.lower_generic_params_mut(generic_params, hir::GenericParamSource::Binder)
.collect();
Expand All @@ -857,7 +864,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
generic_params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
self.lifetime_res_to_generic_param(ident, node_id, res, hir::GenericParamSource::Binder)
}));
let generic_params = self.arena.alloc_from_iter(generic_params);
let generic_params = self.arena.allocate_thin_from_iter(generic_params);
debug!(?generic_params);

generic_params
Expand Down Expand Up @@ -1304,7 +1311,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
None,
self.arena.alloc(hir::Path {
res,
segments: arena_vec![self; hir::PathSegment::new(
segments: arena_thin_vec![self; hir::PathSegment::new(
Ident::with_dummy_span(kw::SelfUpper),
hir_id,
res
Expand Down Expand Up @@ -1511,7 +1518,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// This creates HIR lifetime definitions as `hir::GenericParam`, in the given
// example `type TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection
// containing `&['x]`.
let lifetime_defs = lctx.arena.alloc_from_iter(collected_lifetimes.iter().map(
let lifetime_defs = lctx.arena.allocate_thin_from_iter(collected_lifetimes.iter().map(
|&(new_node_id, lifetime)| {
let hir_id = lctx.lower_node_id(new_node_id);
debug_assert_ne!(lctx.opt_local_def_id(new_node_id), None);
Expand Down Expand Up @@ -1547,7 +1554,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let opaque_ty_item = hir::OpaqueTy {
generics: self.arena.alloc(hir::Generics {
params: lifetime_defs,
predicates: &[],
predicates: ThinSlice::empty(),
has_where_clause_predicates: false,
where_clause_span: lctx.lower_span(span),
span: lctx.lower_span(span),
Expand Down Expand Up @@ -1969,7 +1976,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
},
);

let generic_params = this.arena.alloc_from_iter(collected_lifetimes.iter().map(
let generic_params = this.arena.allocate_thin_from_iter(collected_lifetimes.iter().map(
|&(new_node_id, lifetime, _)| {
let hir_id = this.lower_node_id(new_node_id);
debug_assert_ne!(this.opt_local_def_id(new_node_id), None);
Expand Down Expand Up @@ -2000,12 +2007,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let opaque_ty_item = hir::OpaqueTy {
generics: this.arena.alloc(hir::Generics {
params: generic_params,
predicates: &[],
predicates: ThinSlice::empty(),
has_where_clause_predicates: false,
where_clause_span: this.lower_span(span),
span: this.lower_span(span),
}),
bounds: arena_vec![this; future_bound],
bounds: arena_thin_vec![this; future_bound],
origin: hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
in_trait,
};
Expand Down Expand Up @@ -2073,8 +2080,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

// "<Output = T>"
let future_args = self.arena.alloc(hir::GenericArgs {
args: &[],
bindings: arena_vec![self; self.output_ty_binding(span, output_ty)],
args: ThinSlice::empty(),
bindings: arena_thin_vec![self; self.output_ty_binding(span, output_ty)],
parenthesized: false,
span_ext: DUMMY_SP,
});
Expand Down Expand Up @@ -2166,8 +2173,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
&mut self,
params: &[GenericParam],
source: hir::GenericParamSource,
) -> &'hir [hir::GenericParam<'hir>] {
self.arena.alloc_from_iter(self.lower_generic_params_mut(params, source))
) -> &'hir ThinSlice<hir::GenericParam<'hir>> {
self.arena.allocate_thin_from_iter(self.lower_generic_params_mut(params, source))
}

#[instrument(level = "trace", skip(self))]
Expand Down Expand Up @@ -2269,7 +2276,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
bounds: &[GenericBound],
itctx: &ImplTraitContext,
) -> hir::GenericBounds<'hir> {
self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, itctx))
self.arena.allocate_thin_from_iter(self.lower_param_bounds_mut(bounds, itctx))
}

fn lower_param_bounds_mut<'s>(
Expand Down Expand Up @@ -2323,7 +2330,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
span,
res,
segments:
arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
arena_thin_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
}),
));

Expand Down Expand Up @@ -2421,13 +2428,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}

fn block_expr(&mut self, expr: &'hir hir::Expr<'hir>) -> &'hir hir::Block<'hir> {
self.block_all(expr.span, &[], Some(expr))
self.block_all(expr.span, ThinSlice::empty(), Some(expr))
}

fn block_all(
&mut self,
span: Span,
stmts: &'hir [hir::Stmt<'hir>],
stmts: &'hir ThinSlice<hir::Stmt<'hir>>,
expr: Option<&'hir hir::Expr<'hir>>,
) -> &'hir hir::Block<'hir> {
let blk = hir::Block {
Expand Down Expand Up @@ -2457,29 +2464,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}

fn pat_none(&mut self, span: Span) -> &'hir hir::Pat<'hir> {
self.pat_lang_item_variant(span, hir::LangItem::OptionNone, &[], None)
self.pat_lang_item_variant(span, hir::LangItem::OptionNone, ThinSlice::empty(), None)
}

fn single_pat_field(
&mut self,
span: Span,
pat: &'hir hir::Pat<'hir>,
) -> &'hir [hir::PatField<'hir>] {
) -> &'hir ThinSlice<hir::PatField<'hir>> {
let field = hir::PatField {
hir_id: self.next_id(),
ident: Ident::new(sym::integer(0), self.lower_span(span)),
is_shorthand: false,
pat,
span: self.lower_span(span),
};
arena_vec![self; field]
arena_thin_vec![self; field]
}

fn pat_lang_item_variant(
&mut self,
span: Span,
lang_item: hir::LangItem,
fields: &'hir [hir::PatField<'hir>],
fields: &'hir ThinSlice<hir::PatField<'hir>>,
hir_id: Option<hir::HirId>,
) -> &'hir hir::Pat<'hir> {
let qpath = hir::QPath::LangItem(lang_item, self.lower_span(span), hir_id);
Expand Down Expand Up @@ -2594,7 +2601,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
/// Helper struct for delayed construction of GenericArgs.
struct GenericArgsCtor<'hir> {
args: SmallVec<[hir::GenericArg<'hir>; 4]>,
bindings: &'hir [hir::TypeBinding<'hir>],
bindings: &'hir ThinSlice<hir::TypeBinding<'hir>>,
parenthesized: bool,
span: Span,
}
Expand All @@ -2606,7 +2613,7 @@ impl<'hir> GenericArgsCtor<'hir> {

fn into_generic_args(self, this: &LoweringContext<'_, 'hir>) -> &'hir hir::GenericArgs<'hir> {
let ga = hir::GenericArgs {
args: this.arena.alloc_from_iter(self.args),
args: this.arena.allocate_thin_from_iter(self.args),
bindings: self.bindings,
parenthesized: self.parenthesized,
span_ext: this.lower_span(self.span),
Expand Down
Loading