1
Fork 0

Add StaticForeignItem and use it on ForeignItemKind

This commit is contained in:
Santiago Pastorino 2024-04-29 11:27:14 -03:00
parent 90846015cc
commit f06e0f7837
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
11 changed files with 86 additions and 40 deletions

View file

@ -3126,6 +3126,35 @@ pub struct StaticItem {
pub expr: Option<P<Expr>>, pub expr: Option<P<Expr>>,
} }
/// A static item in `extern` block.
// This struct is identical to StaticItem for now but it's going to have a safety attribute.
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct StaticForeignItem {
pub ty: P<Ty>,
pub mutability: Mutability,
pub expr: Option<P<Expr>>,
}
impl From<StaticItem> for StaticForeignItem {
fn from(static_item: StaticItem) -> StaticForeignItem {
StaticForeignItem {
ty: static_item.ty,
mutability: static_item.mutability,
expr: static_item.expr,
}
}
}
impl From<StaticForeignItem> for StaticItem {
fn from(static_item: StaticForeignItem) -> StaticItem {
StaticItem {
ty: static_item.ty,
mutability: static_item.mutability,
expr: static_item.expr,
}
}
}
#[derive(Clone, Encodable, Decodable, Debug)] #[derive(Clone, Encodable, Decodable, Debug)]
pub struct ConstItem { pub struct ConstItem {
pub defaultness: Defaultness, pub defaultness: Defaultness,
@ -3329,7 +3358,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
#[derive(Clone, Encodable, Decodable, Debug)] #[derive(Clone, Encodable, Decodable, Debug)]
pub enum ForeignItemKind { pub enum ForeignItemKind {
/// A foreign static item (`static FOO: u8`). /// A foreign static item (`static FOO: u8`).
Static(P<Ty>, Mutability, Option<P<Expr>>), Static(Box<StaticForeignItem>),
/// An foreign function. /// An foreign function.
Fn(Box<Fn>), Fn(Box<Fn>),
/// An foreign type. /// An foreign type.
@ -3341,8 +3370,8 @@ pub enum ForeignItemKind {
impl From<ForeignItemKind> for ItemKind { impl From<ForeignItemKind> for ItemKind {
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind { fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
match foreign_item_kind { match foreign_item_kind {
ForeignItemKind::Static(a, b, c) => { ForeignItemKind::Static(box static_foreign_item) => {
ItemKind::Static(StaticItem { ty: a, mutability: b, expr: c }.into()) ItemKind::Static(Box::new(static_foreign_item.into()))
} }
ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind), ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind), ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
@ -3356,8 +3385,8 @@ impl TryFrom<ItemKind> for ForeignItemKind {
fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> { fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
Ok(match item_kind { Ok(match item_kind {
ItemKind::Static(box StaticItem { ty: a, mutability: b, expr: c }) => { ItemKind::Static(box static_item) => {
ForeignItemKind::Static(a, b, c) ForeignItemKind::Static(Box::new(static_item.into()))
} }
ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind), ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind), ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
@ -3382,8 +3411,8 @@ mod size_asserts {
static_assert_size!(Expr, 72); static_assert_size!(Expr, 72);
static_assert_size!(ExprKind, 40); static_assert_size!(ExprKind, 40);
static_assert_size!(Fn, 160); static_assert_size!(Fn, 160);
static_assert_size!(ForeignItem, 96); static_assert_size!(ForeignItem, 88);
static_assert_size!(ForeignItemKind, 24); static_assert_size!(ForeignItemKind, 16);
static_assert_size!(GenericArg, 24); static_assert_size!(GenericArg, 24);
static_assert_size!(GenericBound, 88); static_assert_size!(GenericBound, 88);
static_assert_size!(Generics, 40); static_assert_size!(Generics, 40);

View file

@ -1261,7 +1261,7 @@ pub fn noop_flat_map_item<K: NoopVisitItemKind>(
impl NoopVisitItemKind for ForeignItemKind { impl NoopVisitItemKind for ForeignItemKind {
fn noop_visit(&mut self, visitor: &mut impl MutVisitor) { fn noop_visit(&mut self, visitor: &mut impl MutVisitor) {
match self { match self {
ForeignItemKind::Static(ty, _, expr) => { ForeignItemKind::Static(box StaticForeignItem { ty, mutability: _, expr }) => {
visitor.visit_ty(ty); visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr)); visit_opt(expr, |expr| visitor.visit_expr(expr));
} }

View file

@ -642,7 +642,7 @@ impl WalkItemKind for ForeignItemKind {
) -> V::Result { ) -> V::Result {
let &Item { id, span, ident, ref vis, .. } = item; let &Item { id, span, ident, ref vis, .. } = item;
match self { match self {
ForeignItemKind::Static(ty, _, expr) => { ForeignItemKind::Static(box StaticForeignItem { ty, mutability: _, expr }) => {
try_visit!(visitor.visit_ty(ty)); try_visit!(visitor.visit_ty(ty));
visit_opt!(visitor, visit_expr, expr); visit_opt!(visitor, visit_expr, expr);
} }

View file

@ -662,10 +662,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics) hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
} }
ForeignItemKind::Static(t, m, _) => { ForeignItemKind::Static(box StaticForeignItem { ty, mutability, expr: _ }) => {
let ty = let ty = self
self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy)); .lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
hir::ForeignItemKind::Static(ty, *m) hir::ForeignItemKind::Static(ty, *mutability)
} }
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type, ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"), ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),

View file

@ -1185,8 +1185,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.check_foreign_ty_genericless(generics, where_clauses); self.check_foreign_ty_genericless(generics, where_clauses);
self.check_foreign_item_ascii_only(fi.ident); self.check_foreign_item_ascii_only(fi.ident);
} }
ForeignItemKind::Static(_, _, body) => { ForeignItemKind::Static(box StaticForeignItem { ty: _, mutability: _, expr }) => {
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span)); self.check_foreign_kind_bodyless(fi.ident, "static", expr.as_ref().map(|b| b.span));
self.check_foreign_item_ascii_only(fi.ident); self.check_foreign_item_ascii_only(fi.ident);
} }
ForeignItemKind::MacCall(..) => {} ForeignItemKind::MacCall(..) => {}

View file

@ -30,15 +30,17 @@ impl<'a> State<'a> {
ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => { ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs); self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
} }
ast::ForeignItemKind::Static(ty, mutbl, body) => self.print_item_const( ast::ForeignItemKind::Static(box ast::StaticForeignItem { ty, mutability, expr }) => {
ident, self.print_item_const(
Some(*mutbl), ident,
&ast::Generics::default(), Some(*mutability),
ty, &ast::Generics::default(),
body.as_deref(), ty,
vis, expr.as_deref(),
ast::Defaultness::Final, vis,
), ast::Defaultness::Final,
)
}
ast::ForeignItemKind::TyAlias(box ast::TyAlias { ast::ForeignItemKind::TyAlias(box ast::TyAlias {
defaultness, defaultness,
generics, generics,

View file

@ -1191,7 +1191,11 @@ impl<'a> Parser<'a> {
ident_span: ident.span, ident_span: ident.span,
const_span, const_span,
}); });
ForeignItemKind::Static(ty, Mutability::Not, expr) ForeignItemKind::Static(Box::new(StaticForeignItem {
ty,
mutability: Mutability::Not,
expr,
}))
} }
_ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"), _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
}, },

View file

@ -209,7 +209,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) { fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
let def_kind = match fi.kind { let def_kind = match fi.kind {
ForeignItemKind::Static(_, mutability, _) => { ForeignItemKind::Static(box StaticForeignItem { ty: _, mutability, expr: _ }) => {
DefKind::Static { mutability, nested: false } DefKind::Static { mutability, nested: false }
} }
ForeignItemKind::Fn(_) => DefKind::Fn, ForeignItemKind::Fn(_) => DefKind::Fn,

View file

@ -446,7 +446,18 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool { pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
use ForeignItemKind::*; use ForeignItemKind::*;
match (l, r) { match (l, r) {
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), (
Static(box StaticForeignItem {
ty: lt,
mutability: lm,
expr: le,
}),
Static(box StaticForeignItem {
ty: rt,
mutability: rm,
expr: re,
}),
) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
( (
Fn(box ast::Fn { Fn(box ast::Fn {
defaultness: ld, defaultness: ld,

View file

@ -3325,11 +3325,11 @@ impl Rewrite for ast::ForeignItem {
.map(|(s, _, _)| format!("{};", s)) .map(|(s, _, _)| format!("{};", s))
} }
} }
ast::ForeignItemKind::Static(ref ty, mutability, _) => { ast::ForeignItemKind::Static(ref static_foreign_item) => {
// FIXME(#21): we're dropping potential comments in between the // FIXME(#21): we're dropping potential comments in between the
// function kw here. // function kw here.
let vis = format_visibility(context, &self.vis); let vis = format_visibility(context, &self.vis);
let mut_str = format_mutability(mutability); let mut_str = format_mutability(static_foreign_item.mutability);
let prefix = format!( let prefix = format!(
"{}static {}{}:", "{}static {}{}:",
vis, vis,
@ -3340,7 +3340,7 @@ impl Rewrite for ast::ForeignItem {
rewrite_assign_rhs( rewrite_assign_rhs(
context, context,
prefix, prefix,
&**ty, &static_foreign_item.ty,
&RhsAssignKind::Ty, &RhsAssignKind::Ty,
shape.sub_width(1)?, shape.sub_width(1)?,
) )

View file

@ -11,15 +11,15 @@ ast-stats-1 Attribute 64 ( 1.0%) 2 32
ast-stats-1 - Normal 32 ( 0.5%) 1 ast-stats-1 - Normal 32 ( 0.5%) 1
ast-stats-1 - DocComment 32 ( 0.5%) 1 ast-stats-1 - DocComment 32 ( 0.5%) 1
ast-stats-1 Local 80 ( 1.2%) 1 80 ast-stats-1 Local 80 ( 1.2%) 1 80
ast-stats-1 Arm 96 ( 1.4%) 2 48 ast-stats-1 ForeignItem 88 ( 1.3%) 1 88
ast-stats-1 ForeignItem 96 ( 1.4%) 1 96 ast-stats-1 - Fn 88 ( 1.3%) 1
ast-stats-1 - Fn 96 ( 1.4%) 1 ast-stats-1 Arm 96 ( 1.5%) 2 48
ast-stats-1 FnDecl 120 ( 1.8%) 5 24 ast-stats-1 FnDecl 120 ( 1.8%) 5 24
ast-stats-1 FieldDef 160 ( 2.4%) 2 80 ast-stats-1 FieldDef 160 ( 2.4%) 2 80
ast-stats-1 Stmt 160 ( 2.4%) 5 32 ast-stats-1 Stmt 160 ( 2.4%) 5 32
ast-stats-1 - Let 32 ( 0.5%) 1 ast-stats-1 - Let 32 ( 0.5%) 1
ast-stats-1 - MacCall 32 ( 0.5%) 1 ast-stats-1 - MacCall 32 ( 0.5%) 1
ast-stats-1 - Expr 96 ( 1.4%) 3 ast-stats-1 - Expr 96 ( 1.5%) 3
ast-stats-1 Param 160 ( 2.4%) 4 40 ast-stats-1 Param 160 ( 2.4%) 4 40
ast-stats-1 Block 192 ( 2.9%) 6 32 ast-stats-1 Block 192 ( 2.9%) 6 32
ast-stats-1 Variant 208 ( 3.1%) 2 104 ast-stats-1 Variant 208 ( 3.1%) 2 104
@ -28,7 +28,7 @@ ast-stats-1 - Trait 352 ( 5.3%) 4
ast-stats-1 AssocItem 352 ( 5.3%) 4 88 ast-stats-1 AssocItem 352 ( 5.3%) 4 88
ast-stats-1 - Type 176 ( 2.7%) 2 ast-stats-1 - Type 176 ( 2.7%) 2
ast-stats-1 - Fn 176 ( 2.7%) 2 ast-stats-1 - Fn 176 ( 2.7%) 2
ast-stats-1 GenericParam 480 ( 7.2%) 5 96 ast-stats-1 GenericParam 480 ( 7.3%) 5 96
ast-stats-1 Pat 504 ( 7.6%) 7 72 ast-stats-1 Pat 504 ( 7.6%) 7 72
ast-stats-1 - Struct 72 ( 1.1%) 1 ast-stats-1 - Struct 72 ( 1.1%) 1
ast-stats-1 - Wild 72 ( 1.1%) 1 ast-stats-1 - Wild 72 ( 1.1%) 1
@ -53,7 +53,7 @@ ast-stats-1 - Impl 136 ( 2.1%) 1
ast-stats-1 - Fn 272 ( 4.1%) 2 ast-stats-1 - Fn 272 ( 4.1%) 2
ast-stats-1 - Use 408 ( 6.2%) 3 ast-stats-1 - Use 408 ( 6.2%) 3
ast-stats-1 ---------------------------------------------------------------- ast-stats-1 ----------------------------------------------------------------
ast-stats-1 Total 6_624 ast-stats-1 Total 6_616
ast-stats-1 ast-stats-1
ast-stats-2 POST EXPANSION AST STATS ast-stats-2 POST EXPANSION AST STATS
ast-stats-2 Name Accumulated Size Count Item Size ast-stats-2 Name Accumulated Size Count Item Size
@ -65,9 +65,9 @@ ast-stats-2 ExprField 48 ( 0.7%) 1 48
ast-stats-2 WherePredicate 56 ( 0.8%) 1 56 ast-stats-2 WherePredicate 56 ( 0.8%) 1 56
ast-stats-2 - BoundPredicate 56 ( 0.8%) 1 ast-stats-2 - BoundPredicate 56 ( 0.8%) 1
ast-stats-2 Local 80 ( 1.1%) 1 80 ast-stats-2 Local 80 ( 1.1%) 1 80
ast-stats-2 ForeignItem 88 ( 1.2%) 1 88
ast-stats-2 - Fn 88 ( 1.2%) 1
ast-stats-2 Arm 96 ( 1.3%) 2 48 ast-stats-2 Arm 96 ( 1.3%) 2 48
ast-stats-2 ForeignItem 96 ( 1.3%) 1 96
ast-stats-2 - Fn 96 ( 1.3%) 1
ast-stats-2 InlineAsm 120 ( 1.7%) 1 120 ast-stats-2 InlineAsm 120 ( 1.7%) 1 120
ast-stats-2 FnDecl 120 ( 1.7%) 5 24 ast-stats-2 FnDecl 120 ( 1.7%) 5 24
ast-stats-2 Attribute 128 ( 1.8%) 4 32 ast-stats-2 Attribute 128 ( 1.8%) 4 32
@ -86,7 +86,7 @@ ast-stats-2 - Trait 352 ( 4.9%) 4
ast-stats-2 AssocItem 352 ( 4.9%) 4 88 ast-stats-2 AssocItem 352 ( 4.9%) 4 88
ast-stats-2 - Type 176 ( 2.4%) 2 ast-stats-2 - Type 176 ( 2.4%) 2
ast-stats-2 - Fn 176 ( 2.4%) 2 ast-stats-2 - Fn 176 ( 2.4%) 2
ast-stats-2 GenericParam 480 ( 6.6%) 5 96 ast-stats-2 GenericParam 480 ( 6.7%) 5 96
ast-stats-2 Pat 504 ( 7.0%) 7 72 ast-stats-2 Pat 504 ( 7.0%) 7 72
ast-stats-2 - Struct 72 ( 1.0%) 1 ast-stats-2 - Struct 72 ( 1.0%) 1
ast-stats-2 - Wild 72 ( 1.0%) 1 ast-stats-2 - Wild 72 ( 1.0%) 1
@ -113,7 +113,7 @@ ast-stats-2 - Impl 136 ( 1.9%) 1
ast-stats-2 - Fn 272 ( 3.8%) 2 ast-stats-2 - Fn 272 ( 3.8%) 2
ast-stats-2 - Use 544 ( 7.5%) 4 ast-stats-2 - Use 544 ( 7.5%) 4
ast-stats-2 ---------------------------------------------------------------- ast-stats-2 ----------------------------------------------------------------
ast-stats-2 Total 7_224 ast-stats-2 Total 7_216
ast-stats-2 ast-stats-2
hir-stats HIR STATS hir-stats HIR STATS
hir-stats Name Accumulated Size Count Item Size hir-stats Name Accumulated Size Count Item Size