Skip to content

Commit 4cbe0ee

Browse files
veluca93jhpratt
andcommitted
Draft implementation of the unsafe-fields RFC.
Co-Authored-By: Jacob Pratt <jacob@jhpratt.dev>
1 parent 9b18a12 commit 4cbe0ee

File tree

27 files changed

+396
-32
lines changed

27 files changed

+396
-32
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,6 +3028,7 @@ pub struct FieldDef {
30283028
pub id: NodeId,
30293029
pub span: Span,
30303030
pub vis: Visibility,
3031+
pub safety: Safety,
30313032
pub ident: Option<Ident>,
30323033

30333034
pub ty: P<Ty>,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,10 +1044,11 @@ pub fn walk_flat_map_field_def<T: MutVisitor>(
10441044
visitor: &mut T,
10451045
mut fd: FieldDef,
10461046
) -> SmallVec<[FieldDef; 1]> {
1047-
let FieldDef { span, ident, vis, id, ty, attrs, is_placeholder: _ } = &mut fd;
1047+
let FieldDef { span, ident, vis, id, ty, attrs, is_placeholder: _, safety } = &mut fd;
10481048
visitor.visit_id(id);
10491049
visit_attrs(visitor, attrs);
10501050
visitor.visit_vis(vis);
1051+
visit_safety(visitor, safety);
10511052
visit_opt(ident, |ident| visitor.visit_ident(ident));
10521053
visitor.visit_ty(ty);
10531054
visitor.visit_span(span);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ pub fn walk_struct_def<'a, V: Visitor<'a>>(
929929
}
930930

931931
pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) -> V::Result {
932-
let FieldDef { attrs, id: _, span: _, vis, ident, ty, is_placeholder: _ } = field;
932+
let FieldDef { attrs, id: _, span: _, vis, ident, ty, is_placeholder: _, safety: _ } = field;
933933
walk_list!(visitor, visit_attribute, attrs);
934934
try_visit!(visitor.visit_vis(vis));
935935
visit_opt!(visitor, visit_ident, ident);

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
724724
},
725725
vis_span: self.lower_span(f.vis.span),
726726
ty,
727+
safety: self.lower_safety(f.safety, hir::Safety::Safe),
727728
}
728729
}
729730

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
547547
gate_all!(global_registration, "global registration is experimental");
548548
gate_all!(return_type_notation, "return type notation is experimental");
549549
gate_all!(pin_ergonomics, "pinned reference syntax is experimental");
550+
gate_all!(unsafe_fields, "`unsafe` fields are experimental");
550551

551552
if !visitor.features.never_patterns() {
552553
if let Some(spans) = spans.get(&sym::never_patterns) {

compiler/rustc_expand/src/placeholders.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::mut_visit::*;
22
use rustc_ast::ptr::P;
33
use rustc_ast::token::Delimiter;
44
use rustc_ast::visit::AssocCtxt;
5-
use rustc_ast::{self as ast};
5+
use rustc_ast::{self as ast, Safety};
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_span::DUMMY_SP;
88
use rustc_span::symbol::Ident;
@@ -173,6 +173,7 @@ pub(crate) fn placeholder(
173173
ty: ty(),
174174
vis,
175175
is_placeholder: true,
176+
safety: Safety::Default,
176177
}]),
177178
AstFragmentKind::Variants => AstFragment::Variants(smallvec![ast::Variant {
178179
attrs: Default::default(),

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,8 @@ declare_features! (
628628
/// Allows creation of instances of a struct by moving fields that have
629629
/// not changed from prior instances of the same struct (RFC #2528)
630630
(unstable, type_changing_struct_update, "1.58.0", Some(86555)),
631+
/// Allows declaring fields `unsafe`.
632+
(unstable, unsafe_fields, "CURRENT_RUSTC_VERSION", Some(1234567)), // FIXME: proper tracking issue.
631633
/// Allows const generic parameters to be defined with types that
632634
/// are not `Sized`, e.g. `fn foo<const N: [u8]>() {`.
633635
(incomplete, unsized_const_params, "1.82.0", Some(95174)),

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,6 +3192,7 @@ pub struct FieldDef<'hir> {
31923192
pub hir_id: HirId,
31933193
pub def_id: LocalDefId,
31943194
pub ty: &'hir Ty<'hir>,
3195+
pub safety: Safety,
31953196
}
31963197

31973198
impl FieldDef<'_> {

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ fn lower_variant(
10311031
did: f.def_id.to_def_id(),
10321032
name: f.ident.name,
10331033
vis: tcx.visibility(f.def_id),
1034+
safety: f.safety,
10341035
})
10351036
.collect();
10361037
let recovered = match def {

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_data_structures::sync::{Lock, Lrc, OnceLock};
1515
use rustc_data_structures::unhash::UnhashMap;
1616
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
1717
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro};
18+
use rustc_hir::Safety;
1819
use rustc_hir::def::Res;
1920
use rustc_hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE};
2021
use rustc_hir::definitions::{DefPath, DefPathData};
@@ -1101,6 +1102,7 @@ impl<'a> CrateMetadataRef<'a> {
11011102
did,
11021103
name: self.item_name(did.index),
11031104
vis: self.get_visibility(did.index),
1105+
safety: self.get_safety(did.index),
11041106
})
11051107
.collect(),
11061108
adt_kind,
@@ -1162,6 +1164,10 @@ impl<'a> CrateMetadataRef<'a> {
11621164
.map_id(|index| self.local_def_id(index))
11631165
}
11641166

1167+
fn get_safety(self, id: DefIndex) -> Safety {
1168+
self.root.tables.safety.get(self, id).unwrap_or_else(|| self.missing("safety", id))
1169+
}
1170+
11651171
fn get_trait_item_def_id(self, id: DefIndex) -> Option<DefId> {
11661172
self.root.tables.trait_item_def_id.get(self, id).map(|d| d.decode_from_cdata(self))
11671173
}

0 commit comments

Comments
 (0)