Skip to content

Commit f06e0f7

Browse files
committed
Add StaticForeignItem and use it on ForeignItemKind
1 parent 9084601 commit f06e0f7

File tree

11 files changed

+86
-40
lines changed

11 files changed

+86
-40
lines changed

compiler/rustc_ast/src/ast.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,35 @@ pub struct StaticItem {
31263126
pub expr: Option<P<Expr>>,
31273127
}
31283128

3129+
/// A static item in `extern` block.
3130+
// This struct is identical to StaticItem for now but it's going to have a safety attribute.
3131+
#[derive(Clone, Encodable, Decodable, Debug)]
3132+
pub struct StaticForeignItem {
3133+
pub ty: P<Ty>,
3134+
pub mutability: Mutability,
3135+
pub expr: Option<P<Expr>>,
3136+
}
3137+
3138+
impl From<StaticItem> for StaticForeignItem {
3139+
fn from(static_item: StaticItem) -> StaticForeignItem {
3140+
StaticForeignItem {
3141+
ty: static_item.ty,
3142+
mutability: static_item.mutability,
3143+
expr: static_item.expr,
3144+
}
3145+
}
3146+
}
3147+
3148+
impl From<StaticForeignItem> for StaticItem {
3149+
fn from(static_item: StaticForeignItem) -> StaticItem {
3150+
StaticItem {
3151+
ty: static_item.ty,
3152+
mutability: static_item.mutability,
3153+
expr: static_item.expr,
3154+
}
3155+
}
3156+
}
3157+
31293158
#[derive(Clone, Encodable, Decodable, Debug)]
31303159
pub struct ConstItem {
31313160
pub defaultness: Defaultness,
@@ -3329,7 +3358,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
33293358
#[derive(Clone, Encodable, Decodable, Debug)]
33303359
pub enum ForeignItemKind {
33313360
/// A foreign static item (`static FOO: u8`).
3332-
Static(P<Ty>, Mutability, Option<P<Expr>>),
3361+
Static(Box<StaticForeignItem>),
33333362
/// An foreign function.
33343363
Fn(Box<Fn>),
33353364
/// An foreign type.
@@ -3341,8 +3370,8 @@ pub enum ForeignItemKind {
33413370
impl From<ForeignItemKind> for ItemKind {
33423371
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
33433372
match foreign_item_kind {
3344-
ForeignItemKind::Static(a, b, c) => {
3345-
ItemKind::Static(StaticItem { ty: a, mutability: b, expr: c }.into())
3373+
ForeignItemKind::Static(box static_foreign_item) => {
3374+
ItemKind::Static(Box::new(static_foreign_item.into()))
33463375
}
33473376
ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
33483377
ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
@@ -3356,8 +3385,8 @@ impl TryFrom<ItemKind> for ForeignItemKind {
33563385

33573386
fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
33583387
Ok(match item_kind {
3359-
ItemKind::Static(box StaticItem { ty: a, mutability: b, expr: c }) => {
3360-
ForeignItemKind::Static(a, b, c)
3388+
ItemKind::Static(box static_item) => {
3389+
ForeignItemKind::Static(Box::new(static_item.into()))
33613390
}
33623391
ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
33633392
ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
@@ -3382,8 +3411,8 @@ mod size_asserts {
33823411
static_assert_size!(Expr, 72);
33833412
static_assert_size!(ExprKind, 40);
33843413
static_assert_size!(Fn, 160);
3385-
static_assert_size!(ForeignItem, 96);
3386-
static_assert_size!(ForeignItemKind, 24);
3414+
static_assert_size!(ForeignItem, 88);
3415+
static_assert_size!(ForeignItemKind, 16);
33873416
static_assert_size!(GenericArg, 24);
33883417
static_assert_size!(GenericBound, 88);
33893418
static_assert_size!(Generics, 40);

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ pub fn noop_flat_map_item<K: NoopVisitItemKind>(
12611261
impl NoopVisitItemKind for ForeignItemKind {
12621262
fn noop_visit(&mut self, visitor: &mut impl MutVisitor) {
12631263
match self {
1264-
ForeignItemKind::Static(ty, _, expr) => {
1264+
ForeignItemKind::Static(box StaticForeignItem { ty, mutability: _, expr }) => {
12651265
visitor.visit_ty(ty);
12661266
visit_opt(expr, |expr| visitor.visit_expr(expr));
12671267
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ impl WalkItemKind for ForeignItemKind {
642642
) -> V::Result {
643643
let &Item { id, span, ident, ref vis, .. } = item;
644644
match self {
645-
ForeignItemKind::Static(ty, _, expr) => {
645+
ForeignItemKind::Static(box StaticForeignItem { ty, mutability: _, expr }) => {
646646
try_visit!(visitor.visit_ty(ty));
647647
visit_opt!(visitor, visit_expr, expr);
648648
}

compiler/rustc_ast_lowering/src/item.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
662662

663663
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
664664
}
665-
ForeignItemKind::Static(t, m, _) => {
666-
let ty =
667-
self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
668-
hir::ForeignItemKind::Static(ty, *m)
665+
ForeignItemKind::Static(box StaticForeignItem { ty, mutability, expr: _ }) => {
666+
let ty = self
667+
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
668+
hir::ForeignItemKind::Static(ty, *mutability)
669669
}
670670
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
671671
ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),

compiler/rustc_ast_passes/src/ast_validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1185,8 +1185,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11851185
self.check_foreign_ty_genericless(generics, where_clauses);
11861186
self.check_foreign_item_ascii_only(fi.ident);
11871187
}
1188-
ForeignItemKind::Static(_, _, body) => {
1189-
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
1188+
ForeignItemKind::Static(box StaticForeignItem { ty: _, mutability: _, expr }) => {
1189+
self.check_foreign_kind_bodyless(fi.ident, "static", expr.as_ref().map(|b| b.span));
11901190
self.check_foreign_item_ascii_only(fi.ident);
11911191
}
11921192
ForeignItemKind::MacCall(..) => {}

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ impl<'a> State<'a> {
3030
ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
3131
self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
3232
}
33-
ast::ForeignItemKind::Static(ty, mutbl, body) => self.print_item_const(
34-
ident,
35-
Some(*mutbl),
36-
&ast::Generics::default(),
37-
ty,
38-
body.as_deref(),
39-
vis,
40-
ast::Defaultness::Final,
41-
),
33+
ast::ForeignItemKind::Static(box ast::StaticForeignItem { ty, mutability, expr }) => {
34+
self.print_item_const(
35+
ident,
36+
Some(*mutability),
37+
&ast::Generics::default(),
38+
ty,
39+
expr.as_deref(),
40+
vis,
41+
ast::Defaultness::Final,
42+
)
43+
}
4244
ast::ForeignItemKind::TyAlias(box ast::TyAlias {
4345
defaultness,
4446
generics,

compiler/rustc_parse/src/parser/item.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,11 @@ impl<'a> Parser<'a> {
11911191
ident_span: ident.span,
11921192
const_span,
11931193
});
1194-
ForeignItemKind::Static(ty, Mutability::Not, expr)
1194+
ForeignItemKind::Static(Box::new(StaticForeignItem {
1195+
ty,
1196+
mutability: Mutability::Not,
1197+
expr,
1198+
}))
11951199
}
11961200
_ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
11971201
},

compiler/rustc_resolve/src/def_collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
209209

210210
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
211211
let def_kind = match fi.kind {
212-
ForeignItemKind::Static(_, mutability, _) => {
212+
ForeignItemKind::Static(box StaticForeignItem { ty: _, mutability, expr: _ }) => {
213213
DefKind::Static { mutability, nested: false }
214214
}
215215
ForeignItemKind::Fn(_) => DefKind::Fn,

src/tools/clippy/clippy_utils/src/ast_utils.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,18 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
446446
pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
447447
use ForeignItemKind::*;
448448
match (l, r) {
449-
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
449+
(
450+
Static(box StaticForeignItem {
451+
ty: lt,
452+
mutability: lm,
453+
expr: le,
454+
}),
455+
Static(box StaticForeignItem {
456+
ty: rt,
457+
mutability: rm,
458+
expr: re,
459+
}),
460+
) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
450461
(
451462
Fn(box ast::Fn {
452463
defaultness: ld,

src/tools/rustfmt/src/items.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3325,11 +3325,11 @@ impl Rewrite for ast::ForeignItem {
33253325
.map(|(s, _, _)| format!("{};", s))
33263326
}
33273327
}
3328-
ast::ForeignItemKind::Static(ref ty, mutability, _) => {
3328+
ast::ForeignItemKind::Static(ref static_foreign_item) => {
33293329
// FIXME(#21): we're dropping potential comments in between the
33303330
// function kw here.
33313331
let vis = format_visibility(context, &self.vis);
3332-
let mut_str = format_mutability(mutability);
3332+
let mut_str = format_mutability(static_foreign_item.mutability);
33333333
let prefix = format!(
33343334
"{}static {}{}:",
33353335
vis,
@@ -3340,7 +3340,7 @@ impl Rewrite for ast::ForeignItem {
33403340
rewrite_assign_rhs(
33413341
context,
33423342
prefix,
3343-
&**ty,
3343+
&static_foreign_item.ty,
33443344
&RhsAssignKind::Ty,
33453345
shape.sub_width(1)?,
33463346
)

tests/ui/stats/hir-stats.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ ast-stats-1 Attribute 64 ( 1.0%) 2 32
1111
ast-stats-1 - Normal 32 ( 0.5%) 1
1212
ast-stats-1 - DocComment 32 ( 0.5%) 1
1313
ast-stats-1 Local 80 ( 1.2%) 1 80
14-
ast-stats-1 Arm 96 ( 1.4%) 2 48
15-
ast-stats-1 ForeignItem 96 ( 1.4%) 1 96
16-
ast-stats-1 - Fn 96 ( 1.4%) 1
14+
ast-stats-1 ForeignItem 88 ( 1.3%) 1 88
15+
ast-stats-1 - Fn 88 ( 1.3%) 1
16+
ast-stats-1 Arm 96 ( 1.5%) 2 48
1717
ast-stats-1 FnDecl 120 ( 1.8%) 5 24
1818
ast-stats-1 FieldDef 160 ( 2.4%) 2 80
1919
ast-stats-1 Stmt 160 ( 2.4%) 5 32
2020
ast-stats-1 - Let 32 ( 0.5%) 1
2121
ast-stats-1 - MacCall 32 ( 0.5%) 1
22-
ast-stats-1 - Expr 96 ( 1.4%) 3
22+
ast-stats-1 - Expr 96 ( 1.5%) 3
2323
ast-stats-1 Param 160 ( 2.4%) 4 40
2424
ast-stats-1 Block 192 ( 2.9%) 6 32
2525
ast-stats-1 Variant 208 ( 3.1%) 2 104
@@ -28,7 +28,7 @@ ast-stats-1 - Trait 352 ( 5.3%) 4
2828
ast-stats-1 AssocItem 352 ( 5.3%) 4 88
2929
ast-stats-1 - Type 176 ( 2.7%) 2
3030
ast-stats-1 - Fn 176 ( 2.7%) 2
31-
ast-stats-1 GenericParam 480 ( 7.2%) 5 96
31+
ast-stats-1 GenericParam 480 ( 7.3%) 5 96
3232
ast-stats-1 Pat 504 ( 7.6%) 7 72
3333
ast-stats-1 - Struct 72 ( 1.1%) 1
3434
ast-stats-1 - Wild 72 ( 1.1%) 1
@@ -53,7 +53,7 @@ ast-stats-1 - Impl 136 ( 2.1%) 1
5353
ast-stats-1 - Fn 272 ( 4.1%) 2
5454
ast-stats-1 - Use 408 ( 6.2%) 3
5555
ast-stats-1 ----------------------------------------------------------------
56-
ast-stats-1 Total 6_624
56+
ast-stats-1 Total 6_616
5757
ast-stats-1
5858
ast-stats-2 POST EXPANSION AST STATS
5959
ast-stats-2 Name Accumulated Size Count Item Size
@@ -65,9 +65,9 @@ ast-stats-2 ExprField 48 ( 0.7%) 1 48
6565
ast-stats-2 WherePredicate 56 ( 0.8%) 1 56
6666
ast-stats-2 - BoundPredicate 56 ( 0.8%) 1
6767
ast-stats-2 Local 80 ( 1.1%) 1 80
68+
ast-stats-2 ForeignItem 88 ( 1.2%) 1 88
69+
ast-stats-2 - Fn 88 ( 1.2%) 1
6870
ast-stats-2 Arm 96 ( 1.3%) 2 48
69-
ast-stats-2 ForeignItem 96 ( 1.3%) 1 96
70-
ast-stats-2 - Fn 96 ( 1.3%) 1
7171
ast-stats-2 InlineAsm 120 ( 1.7%) 1 120
7272
ast-stats-2 FnDecl 120 ( 1.7%) 5 24
7373
ast-stats-2 Attribute 128 ( 1.8%) 4 32
@@ -86,7 +86,7 @@ ast-stats-2 - Trait 352 ( 4.9%) 4
8686
ast-stats-2 AssocItem 352 ( 4.9%) 4 88
8787
ast-stats-2 - Type 176 ( 2.4%) 2
8888
ast-stats-2 - Fn 176 ( 2.4%) 2
89-
ast-stats-2 GenericParam 480 ( 6.6%) 5 96
89+
ast-stats-2 GenericParam 480 ( 6.7%) 5 96
9090
ast-stats-2 Pat 504 ( 7.0%) 7 72
9191
ast-stats-2 - Struct 72 ( 1.0%) 1
9292
ast-stats-2 - Wild 72 ( 1.0%) 1
@@ -113,7 +113,7 @@ ast-stats-2 - Impl 136 ( 1.9%) 1
113113
ast-stats-2 - Fn 272 ( 3.8%) 2
114114
ast-stats-2 - Use 544 ( 7.5%) 4
115115
ast-stats-2 ----------------------------------------------------------------
116-
ast-stats-2 Total 7_224
116+
ast-stats-2 Total 7_216
117117
ast-stats-2
118118
hir-stats HIR STATS
119119
hir-stats Name Accumulated Size Count Item Size

0 commit comments

Comments
 (0)