Auto merge of #17695 - Veykril:typeref-size, r=Veykril

internal: Reduce size of TypeRef by 8 bytes
This commit is contained in:
bors 2024-07-25 09:52:30 +00:00
commit cddce62237
3 changed files with 18 additions and 17 deletions

View file

@ -40,7 +40,7 @@ pub struct FunctionData {
pub attrs: Attrs, pub attrs: Attrs,
pub visibility: RawVisibility, pub visibility: RawVisibility,
pub abi: Option<Symbol>, pub abi: Option<Symbol>,
pub legacy_const_generics_indices: Box<[u32]>, pub legacy_const_generics_indices: Option<Box<Box<[u32]>>>,
pub rustc_allow_incoherent_impl: bool, pub rustc_allow_incoherent_impl: bool,
flags: FnFlags, flags: FnFlags,
} }
@ -91,7 +91,8 @@ impl FunctionData {
.tt_values() .tt_values()
.next() .next()
.map(parse_rustc_legacy_const_generics) .map(parse_rustc_legacy_const_generics)
.unwrap_or_default(); .filter(|it| !it.is_empty())
.map(Box::new);
let rustc_allow_incoherent_impl = attrs.by_key(&sym::rustc_allow_incoherent_impl).exists(); let rustc_allow_incoherent_impl = attrs.by_key(&sym::rustc_allow_incoherent_impl).exists();
Arc::new(FunctionData { Arc::new(FunctionData {

View file

@ -121,7 +121,7 @@ pub enum TypeRef {
Slice(Box<TypeRef>), Slice(Box<TypeRef>),
/// A fn pointer. Last element of the vector is the return type. /// A fn pointer. Last element of the vector is the return type.
Fn( Fn(
Vec<(Option<Name>, TypeRef)>, Box<[(Option<Name>, TypeRef)]>,
bool, /*varargs*/ bool, /*varargs*/
bool, /*is_unsafe*/ bool, /*is_unsafe*/
Option<Symbol>, /* abi */ Option<Symbol>, /* abi */
@ -228,7 +228,7 @@ impl TypeRef {
}) })
.collect() .collect()
} else { } else {
Vec::new() Vec::with_capacity(1)
}; };
fn lower_abi(abi: ast::Abi) -> Symbol { fn lower_abi(abi: ast::Abi) -> Symbol {
match abi.abi_string() { match abi.abi_string() {
@ -240,7 +240,7 @@ impl TypeRef {
let abi = inner.abi().map(lower_abi); let abi = inner.abi().map(lower_abi);
params.push((None, ret_ty)); params.push((None, ret_ty));
TypeRef::Fn(params, is_varargs, inner.unsafe_token().is_some(), abi) TypeRef::Fn(params.into(), is_varargs, inner.unsafe_token().is_some(), abi)
} }
// for types are close enough for our purposes to the inner type for now... // for types are close enough for our purposes to the inner type for now...
ast::Type::ForType(inner) => TypeRef::from_ast_opt(ctx, inner.ty()), ast::Type::ForType(inner) => TypeRef::from_ast_opt(ctx, inner.ty()),
@ -396,7 +396,7 @@ impl TypeBound {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConstRef { pub enum ConstRef {
Scalar(LiteralConstRef), Scalar(Box<LiteralConstRef>),
Path(Name), Path(Name),
Complex(AstId<ast::ConstArg>), Complex(AstId<ast::ConstArg>),
} }
@ -408,7 +408,7 @@ impl ConstRef {
return Self::from_expr(expr, Some(lower_ctx.ast_id(&arg))); return Self::from_expr(expr, Some(lower_ctx.ast_id(&arg)));
} }
} }
Self::Scalar(LiteralConstRef::Unknown) Self::Scalar(Box::new(LiteralConstRef::Unknown))
} }
pub(crate) fn from_const_param( pub(crate) fn from_const_param(
@ -452,10 +452,10 @@ impl ConstRef {
ast::Expr::PathExpr(p) if is_path_ident(&p) => { ast::Expr::PathExpr(p) if is_path_ident(&p) => {
match p.path().and_then(|it| it.segment()).and_then(|it| it.name_ref()) { match p.path().and_then(|it| it.segment()).and_then(|it| it.name_ref()) {
Some(it) => Self::Path(it.as_name()), Some(it) => Self::Path(it.as_name()),
None => Self::Scalar(LiteralConstRef::Unknown), None => Self::Scalar(Box::new(LiteralConstRef::Unknown)),
} }
} }
ast::Expr::Literal(literal) => Self::Scalar(match literal.kind() { ast::Expr::Literal(literal) => Self::Scalar(Box::new(match literal.kind() {
ast::LiteralKind::IntNumber(num) => { ast::LiteralKind::IntNumber(num) => {
num.value().map(LiteralConstRef::UInt).unwrap_or(LiteralConstRef::Unknown) num.value().map(LiteralConstRef::UInt).unwrap_or(LiteralConstRef::Unknown)
} }
@ -464,12 +464,12 @@ impl ConstRef {
} }
ast::LiteralKind::Bool(f) => LiteralConstRef::Bool(f), ast::LiteralKind::Bool(f) => LiteralConstRef::Bool(f),
_ => LiteralConstRef::Unknown, _ => LiteralConstRef::Unknown,
}), })),
_ => { _ => {
if let Some(ast_id) = ast_id { if let Some(ast_id) = ast_id {
Self::Complex(ast_id) Self::Complex(ast_id)
} else { } else {
Self::Scalar(LiteralConstRef::Unknown) Self::Scalar(Box::new(LiteralConstRef::Unknown))
} }
} }
} }

View file

@ -1950,25 +1950,25 @@ impl InferenceContext<'_> {
}; };
let data = self.db.function_data(func); let data = self.db.function_data(func);
if data.legacy_const_generics_indices.is_empty() { let Some(legacy_const_generics_indices) = &data.legacy_const_generics_indices else {
return Default::default(); return Default::default();
} };
// only use legacy const generics if the param count matches with them // only use legacy const generics if the param count matches with them
if data.params.len() + data.legacy_const_generics_indices.len() != args.len() { if data.params.len() + legacy_const_generics_indices.len() != args.len() {
if args.len() <= data.params.len() { if args.len() <= data.params.len() {
return Default::default(); return Default::default();
} else { } else {
// there are more parameters than there should be without legacy // there are more parameters than there should be without legacy
// const params; use them // const params; use them
let mut indices = data.legacy_const_generics_indices.clone(); let mut indices = legacy_const_generics_indices.as_ref().clone();
indices.sort(); indices.sort();
return indices; return indices;
} }
} }
// check legacy const parameters // check legacy const parameters
for (subst_idx, arg_idx) in data.legacy_const_generics_indices.iter().copied().enumerate() { for (subst_idx, arg_idx) in legacy_const_generics_indices.iter().copied().enumerate() {
let arg = match subst.at(Interner, subst_idx).constant(Interner) { let arg = match subst.at(Interner, subst_idx).constant(Interner) {
Some(c) => c, Some(c) => c,
None => continue, // not a const parameter? None => continue, // not a const parameter?
@ -1981,7 +1981,7 @@ impl InferenceContext<'_> {
self.infer_expr(args[arg_idx as usize], &expected); self.infer_expr(args[arg_idx as usize], &expected);
// FIXME: evaluate and unify with the const // FIXME: evaluate and unify with the const
} }
let mut indices = data.legacy_const_generics_indices.clone(); let mut indices = legacy_const_generics_indices.as_ref().clone();
indices.sort(); indices.sort();
indices indices
} }