Skip to content

Commit 8771d95

Browse files
committed
some things work
1 parent 5a55dd2 commit 8771d95

File tree

25 files changed

+138
-46
lines changed

25 files changed

+138
-46
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3587,6 +3587,9 @@ pub struct Fn {
35873587
pub contract: Option<P<FnContract>>,
35883588
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
35893589
pub body: Option<P<Block>>,
3590+
3591+
/// This fn implements some EII, pointed to by the `path`
3592+
pub eii_impl: ThinVec<(NodeId, MetaItem)>,
35903593
}
35913594

35923595
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -3936,7 +3939,7 @@ mod size_asserts {
39363939
static_assert_size!(Block, 32);
39373940
static_assert_size!(Expr, 72);
39383941
static_assert_size!(ExprKind, 40);
3939-
static_assert_size!(Fn, 184);
3942+
static_assert_size!(Fn, 192);
39403943
static_assert_size!(ForeignItem, 80);
39413944
static_assert_size!(ForeignItemKind, 16);
39423945
static_assert_size!(GenericArg, 24);

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,11 +939,17 @@ fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
939939
body,
940940
sig: FnSig { header, decl, span },
941941
define_opaque,
942+
eii_impl,
942943
},
943944
) => {
944945
// Visibility is visited as a part of the item.
945946
visit_defaultness(vis, defaultness);
946947
vis.visit_ident(ident);
948+
949+
for (node_id, mi) in eii_impl {
950+
vis.visit_id(node_id);
951+
vis.visit_path(&mut mi.path);
952+
}
947953
vis.visit_fn_header(header);
948954
vis.visit_generics(generics);
949955
vis.visit_fn_decl(decl);

compiler/rustc_ast/src/visit.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,17 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Resu
954954
contract,
955955
body,
956956
define_opaque,
957+
eii_impl,
957958
},
958959
) => {
959960
// Visibility is visited as a part of the item.
960961
try_visit!(visitor.visit_ident(ident));
962+
963+
// Identifier and visibility are visited as a part of the item.
964+
for (node_id, mi) in eii_impl {
965+
try_visit!(visitor.visit_path(&mi.path, *node_id));
966+
}
967+
961968
try_visit!(visitor.visit_fn_header(header));
962969
try_visit!(visitor.visit_generics(generics));
963970
try_visit!(visitor.visit_fn_decl(decl));

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
109109
};
110110
let span = self.lower_span(l.span);
111111
let source = hir::LocalSource::Normal;
112-
self.lower_attrs(hir_id, &l.attrs, l.span);
112+
self.lower_attrs(hir_id, &l.attrs, l.span, &[]);
113113
self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source })
114114
}
115115

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
9999
}
100100

101101
let expr_hir_id = self.lower_node_id(e.id);
102-
self.lower_attrs(expr_hir_id, &e.attrs, e.span);
102+
self.lower_attrs(expr_hir_id, &e.attrs, e.span, &[]);
103103

104104
let kind = match &e.kind {
105105
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
@@ -680,7 +680,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
680680
let guard = arm.guard.as_ref().map(|cond| self.lower_expr(cond));
681681
let hir_id = self.next_id();
682682
let span = self.lower_span(arm.span);
683-
self.lower_attrs(hir_id, &arm.attrs, arm.span);
683+
self.lower_attrs(hir_id, &arm.attrs, arm.span, &[]);
684684
let is_never_pattern = pat.is_never_pattern();
685685
// We need to lower the body even if it's unneeded for never pattern in match,
686686
// ensure that we can get HirId for DefId if need (issue #137708).
@@ -853,6 +853,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
853853
span: unstable_span,
854854
}],
855855
span,
856+
&[],
856857
);
857858
}
858859
}
@@ -1691,7 +1692,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16911692

16921693
fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
16931694
let hir_id = self.lower_node_id(f.id);
1694-
self.lower_attrs(hir_id, &f.attrs, f.span);
1695+
self.lower_attrs(hir_id, &f.attrs, f.span, &[]);
16951696
hir::ExprField {
16961697
hir_id,
16971698
ident: self.lower_ident(f.ident),
@@ -1947,7 +1948,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19471948
//
19481949
// Also, add the attributes to the outer returned expr node.
19491950
let expr = self.expr_drop_temps_mut(for_span, match_expr);
1950-
self.lower_attrs(expr.hir_id, &e.attrs, e.span);
1951+
self.lower_attrs(expr.hir_id, &e.attrs, e.span, &[]);
19511952
expr
19521953
}
19531954

@@ -2004,7 +2005,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20042005
let val_ident = Ident::with_dummy_span(sym::val);
20052006
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
20062007
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
2007-
self.lower_attrs(val_expr.hir_id, &attrs, span);
2008+
self.lower_attrs(val_expr.hir_id, &attrs, span, &[]);
20082009
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
20092010
self.arm(continue_pat, val_expr)
20102011
};
@@ -2035,7 +2036,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20352036
let ret_expr = self.checked_return(Some(from_residual_expr));
20362037
self.arena.alloc(self.expr(try_span, ret_expr))
20372038
};
2038-
self.lower_attrs(ret_expr.hir_id, &attrs, ret_expr.span);
2039+
self.lower_attrs(ret_expr.hir_id, &attrs, ret_expr.span, &[]);
20392040

20402041
let break_pat = self.pat_cf_break(try_span, residual_local);
20412042
self.arm(break_pat, ret_expr)

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use rustc_abi::ExternAbi;
22
use rustc_ast::ptr::P;
33
use rustc_ast::visit::AssocCtxt;
44
use rustc_ast::*;
5+
use rustc_attr_parsing::AttributeKind;
56
use rustc_errors::ErrorGuaranteed;
67
use rustc_hir::def::{DefKind, Res};
7-
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
8+
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
89
use rustc_hir::{self as hir, HirId, IsAnonInPath, PredicateOrigin};
910
use rustc_index::{IndexSlice, IndexVec};
1011
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
@@ -93,7 +94,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
9394
self.with_lctx(CRATE_NODE_ID, |lctx| {
9495
let module = lctx.lower_mod(&c.items, &c.spans);
9596
// FIXME(jdonszelman): is dummy span ever a problem here?
96-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
97+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP, &[]);
9798
hir::OwnerNode::Crate(module)
9899
})
99100
}
@@ -150,7 +151,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
150151
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
151152
let vis_span = self.lower_span(i.vis.span);
152153
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
153-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
154+
155+
let mut extra_hir_attributes = Vec::new();
156+
if let ItemKind::Fn(f) = &i.kind {
157+
extra_hir_attributes.extend(f.eii_impl.iter().map(|(id, mi)| {
158+
let did = self.lower_path_simple_eii(*id, &mi.path);
159+
hir::Attribute::Parsed(AttributeKind::EiiImpl { eii_macro: did })
160+
}));
161+
}
162+
163+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &extra_hir_attributes);
154164
let kind = self.lower_item_kind(i.span, i.id, hir_id, attrs, vis_span, &i.kind);
155165
let item = hir::Item {
156166
owner_id: hir_id.expect_owner(),
@@ -222,6 +232,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
222232
body,
223233
contract,
224234
define_opaque,
235+
eii_impl,
225236
..
226237
}) => {
227238
self.with_new_scopes(*fn_sig_span, |this| {
@@ -485,23 +496,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
485496
name: ident,
486497
ast_macro_def,
487498
kind: macro_kind,
488-
eii_macro_for: eii_macro_for.as_ref().map(|path| {
489-
let lowered = self.lower_qpath(
490-
id,
491-
&None,
492-
path,
493-
ParamMode::Explicit,
494-
crate::AllowReturnTypeNotation::No,
495-
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
496-
None,
497-
);
498-
499-
let QPath::Resolved(None, path) = lowered else {
500-
panic!("{lowered:?}");
501-
};
502-
503-
path.res.def_id()
504-
}),
499+
eii_macro_for: eii_macro_for
500+
.as_ref()
501+
.map(|path| self.lower_path_simple_eii(id, path)),
505502
}
506503
}
507504
ItemKind::Delegation(box delegation) => {
@@ -520,6 +517,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
520517
}
521518
}
522519

520+
fn lower_path_simple_eii(&mut self, id: NodeId, path: &Path) -> DefId {
521+
let lowered = self.lower_qpath(
522+
id,
523+
&None,
524+
path,
525+
ParamMode::Explicit,
526+
crate::AllowReturnTypeNotation::No,
527+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
528+
None,
529+
);
530+
531+
let QPath::Resolved(None, path) = lowered else {
532+
// TODO
533+
panic!("{lowered:?}");
534+
};
535+
536+
path.res.def_id()
537+
}
538+
523539
fn lower_const_item(
524540
&mut self,
525541
ty: &Ty,
@@ -663,7 +679,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
663679
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
664680
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
665681
let owner_id = hir_id.expect_owner();
666-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
682+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &[]);
667683
let (ident, kind) = match &i.kind {
668684
ForeignItemKind::Fn(box Fn { sig, ident, generics, define_opaque, .. }) => {
669685
let fdec = &sig.decl;
@@ -672,11 +688,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
672688
self.lower_generics(generics, i.id, itctx, |this| {
673689
(
674690
// Disallow `impl Trait` in foreign items.
675-
this.lower_fn_decl(fdec, i.id, sig.span, FnDeclKind::ExternFn, None),
676691
this.lower_fn_params_to_idents(fdec),
677692
)
678693
});
679694

695+
680696
// Unmarked safety in unsafe block defaults to unsafe.
681697
let header = self.lower_fn_header(sig.header, hir::Safety::Unsafe, attrs);
682698

@@ -737,7 +753,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
737753

738754
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
739755
let hir_id = self.lower_node_id(v.id);
740-
self.lower_attrs(hir_id, &v.attrs, v.span);
756+
self.lower_attrs(hir_id, &v.attrs, v.span, &[]);
741757
hir::Variant {
742758
hir_id,
743759
def_id: self.local_def_id(v.id),
@@ -799,7 +815,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
799815
) -> hir::FieldDef<'hir> {
800816
let ty = self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
801817
let hir_id = self.lower_node_id(f.id);
802-
self.lower_attrs(hir_id, &f.attrs, f.span);
818+
self.lower_attrs(hir_id, &f.attrs, f.span, &[]);
803819
hir::FieldDef {
804820
span: self.lower_span(f.span),
805821
hir_id,
@@ -818,7 +834,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
818834

819835
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
820836
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
821-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
837+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &[]);
822838
let trait_item_def_id = hir_id.expect_owner();
823839

824840
let (ident, generics, kind, has_default) = match &i.kind {
@@ -1011,7 +1027,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10111027
let has_value = true;
10121028
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
10131029
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
1014-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
1030+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &[]);
10151031

10161032
let (ident, (generics, kind)) = match &i.kind {
10171033
AssocItemKind::Const(box ConstItem {
@@ -1204,7 +1220,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12041220

12051221
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
12061222
let hir_id = self.lower_node_id(param.id);
1207-
self.lower_attrs(hir_id, &param.attrs, param.span);
1223+
self.lower_attrs(hir_id, &param.attrs, param.span, &[]);
12081224
hir::Param {
12091225
hir_id,
12101226
pat: self.lower_pat(&param.pat),
@@ -1910,7 +1926,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19101926
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
19111927
let hir_id = self.lower_node_id(pred.id);
19121928
let span = self.lower_span(pred.span);
1913-
self.lower_attrs(hir_id, &pred.attrs, span);
1929+
self.lower_attrs(hir_id, &pred.attrs, span, &[]);
19141930
let kind = self.arena.alloc(match &pred.kind {
19151931
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
19161932
bound_generic_params,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,11 +877,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
877877
id: HirId,
878878
attrs: &[Attribute],
879879
target_span: Span,
880+
extra_hir_attributes: &[hir::Attribute],
880881
) -> &'hir [hir::Attribute] {
881882
if attrs.is_empty() {
882883
&[]
883884
} else {
884-
let lowered_attrs = self.lower_attrs_vec(attrs, self.lower_span(target_span));
885+
let mut lowered_attrs = self.lower_attrs_vec(attrs, self.lower_span(target_span));
886+
lowered_attrs.extend(extra_hir_attributes.iter().cloned());
885887

886888
debug_assert_eq!(id.owner, self.current_hir_id_owner);
887889
let ret = self.arena.alloc_from_iter(lowered_attrs);
@@ -1826,7 +1828,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18261828
let (name, kind) = self.lower_generic_param_kind(param, source);
18271829

18281830
let hir_id = self.lower_node_id(param.id);
1829-
self.lower_attrs(hir_id, &param.attrs, param.span());
1831+
self.lower_attrs(hir_id, &param.attrs, param.span(), &[]);
18301832
hir::GenericParam {
18311833
hir_id,
18321834
def_id: self.local_def_id(param.id),

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9494

9595
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| {
9696
let hir_id = self.lower_node_id(f.id);
97-
self.lower_attrs(hir_id, &f.attrs, f.span);
97+
self.lower_attrs(hir_id, &f.attrs, f.span, &[]);
9898

9999
hir::PatField {
100100
hir_id,

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,11 +942,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
942942
contract: _,
943943
body,
944944
define_opaque: _,
945+
eii_impl,
945946
},
946947
) => {
947948
self.visit_attrs_vis_ident(&item.attrs, &item.vis, ident);
948949
self.check_defaultness(item.span, *defaultness);
949950

951+
for (id, mi) in eii_impl {
952+
self.visit_path(&mi.path, *id);
953+
}
954+
950955
let is_intrinsic =
951956
item.attrs.iter().any(|a| a.name_or_empty() == sym::rustc_intrinsic);
952957
if body.is_none() && !is_intrinsic {

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,6 +2054,15 @@ impl<'a> State<'a> {
20542054

20552055
fn print_meta_item(&mut self, item: &ast::MetaItem) {
20562056
self.ibox(INDENT_UNIT);
2057+
2058+
match item.unsafety {
2059+
ast::Safety::Unsafe(_) => {
2060+
self.word("unsafe");
2061+
self.popen();
2062+
}
2063+
ast::Safety::Default | ast::Safety::Safe(_) => {}
2064+
}
2065+
20572066
match &item.kind {
20582067
ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
20592068
ast::MetaItemKind::NameValue(value) => {
@@ -2069,6 +2078,12 @@ impl<'a> State<'a> {
20692078
self.pclose();
20702079
}
20712080
}
2081+
2082+
match item.unsafety {
2083+
ast::Safety::Unsafe(_) => self.pclose(),
2084+
ast::Safety::Default | ast::Safety::Safe(_) => {}
2085+
}
2086+
20722087
self.end();
20732088
}
20742089

0 commit comments

Comments
 (0)