Skip to content

Commit 8de3067

Browse files
committed
some things work
1 parent 9e19e97 commit 8de3067

File tree

25 files changed

+144
-46
lines changed

25 files changed

+144
-46
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3470,6 +3470,9 @@ pub struct Fn {
34703470
pub contract: Option<P<FnContract>>,
34713471
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
34723472
pub body: Option<P<Block>>,
3473+
3474+
/// This fn implements some EII, pointed to by the `path`
3475+
pub eii_impl: ThinVec<(NodeId, MetaItem)>,
34733476
}
34743477

34753478
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -3766,7 +3769,7 @@ mod size_asserts {
37663769
static_assert_size!(Block, 32);
37673770
static_assert_size!(Expr, 72);
37683771
static_assert_size!(ExprKind, 40);
3769-
static_assert_size!(Fn, 176);
3772+
static_assert_size!(Fn, 184);
37703773
static_assert_size!(ForeignItem, 88);
37713774
static_assert_size!(ForeignItemKind, 16);
37723775
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
@@ -971,10 +971,16 @@ fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
971971
body,
972972
sig: FnSig { header, decl, span },
973973
define_opaque,
974+
eii_impl,
974975
},
975976
) => {
976977
// Identifier and visibility are visited as a part of the item.
977978
visit_defaultness(vis, defaultness);
979+
980+
for (node_id, mi) in eii_impl {
981+
vis.visit_id(node_id);
982+
vis.visit_path(&mut mi.path);
983+
}
978984
vis.visit_fn_header(header);
979985
vis.visit_generics(generics);
980986
vis.visit_fn_decl(decl);

compiler/rustc_ast/src/visit.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,15 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Resu
903903
contract,
904904
body,
905905
define_opaque,
906+
eii_impl,
906907
},
907908
) => {
908909
// Identifier and visibility are visited as a part of the item.
910+
911+
for (node_id, mi) in eii_impl {
912+
try_visit!(visitor.visit_path(&mi.path, *node_id));
913+
}
914+
909915
try_visit!(visitor.visit_fn_header(header));
910916
try_visit!(visitor.visit_generics(generics));
911917
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
@@ -108,7 +108,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
108108
};
109109
let span = self.lower_span(l.span);
110110
let source = hir::LocalSource::Normal;
111-
self.lower_attrs(hir_id, &l.attrs, l.span);
111+
self.lower_attrs(hir_id, &l.attrs, l.span, &[]);
112112
self.arena.alloc(hir::LetStmt { hir_id, ty, pat, init, els, span, source })
113113
}
114114

compiler/rustc_ast_lowering/src/expr.rs

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

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

102102
let kind = match &e.kind {
103103
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
@@ -670,7 +670,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
670670
let guard = arm.guard.as_ref().map(|cond| self.lower_expr(cond));
671671
let hir_id = self.next_id();
672672
let span = self.lower_span(arm.span);
673-
self.lower_attrs(hir_id, &arm.attrs, arm.span);
673+
self.lower_attrs(hir_id, &arm.attrs, arm.span, &[]);
674674
let is_never_pattern = pat.is_never_pattern();
675675
// We need to lower the body even if it's unneeded for never pattern in match,
676676
// ensure that we can get HirId for DefId if need (issue #137708).
@@ -843,6 +843,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
843843
span: unstable_span,
844844
}],
845845
span,
846+
&[],
846847
);
847848
}
848849
}
@@ -1681,7 +1682,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16811682

16821683
fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
16831684
let hir_id = self.lower_node_id(f.id);
1684-
self.lower_attrs(hir_id, &f.attrs, f.span);
1685+
self.lower_attrs(hir_id, &f.attrs, f.span, &[]);
16851686
hir::ExprField {
16861687
hir_id,
16871688
ident: self.lower_ident(f.ident),
@@ -1937,7 +1938,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19371938
//
19381939
// Also, add the attributes to the outer returned expr node.
19391940
let expr = self.expr_drop_temps_mut(for_span, match_expr);
1940-
self.lower_attrs(expr.hir_id, &e.attrs, e.span);
1941+
self.lower_attrs(expr.hir_id, &e.attrs, e.span, &[]);
19411942
expr
19421943
}
19431944

@@ -1994,7 +1995,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19941995
let val_ident = Ident::with_dummy_span(sym::val);
19951996
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
19961997
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
1997-
self.lower_attrs(val_expr.hir_id, &attrs, span);
1998+
self.lower_attrs(val_expr.hir_id, &attrs, span, &[]);
19981999
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
19992000
self.arm(continue_pat, val_expr)
20002001
};
@@ -2025,7 +2026,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20252026
let ret_expr = self.checked_return(Some(from_residual_expr));
20262027
self.arena.alloc(self.expr(try_span, ret_expr))
20272028
};
2028-
self.lower_attrs(ret_expr.hir_id, &attrs, ret_expr.span);
2029+
self.lower_attrs(ret_expr.hir_id, &attrs, ret_expr.span, &[]);
20292030

20302031
let break_pat = self.pat_cf_break(try_span, residual_local);
20312032
self.arm(break_pat, ret_expr)

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 43 additions & 27 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, PredicateOrigin};
910
use rustc_index::{IndexSlice, IndexVec};
1011
use rustc_middle::span_bug;
@@ -94,7 +95,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
9495
self.with_lctx(CRATE_NODE_ID, |lctx| {
9596
let module = lctx.lower_mod(&c.items, &c.spans);
9697
// FIXME(jdonszelman): is dummy span ever a problem here?
97-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
98+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP, &[]);
9899
hir::OwnerNode::Crate(module)
99100
})
100101
}
@@ -158,7 +159,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
158159
let mut ident = i.ident;
159160
let vis_span = self.lower_span(i.vis.span);
160161
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
161-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
162+
163+
let mut extra_hir_attributes = Vec::new();
164+
if let ItemKind::Fn(f) = &i.kind {
165+
extra_hir_attributes.extend(f.eii_impl.iter().map(|(id, mi)| {
166+
let did = self.lower_path_simple_eii(*id, &mi.path);
167+
hir::Attribute::Parsed(AttributeKind::EiiImpl { eii_macro: did })
168+
}));
169+
}
170+
171+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &extra_hir_attributes);
162172
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind);
163173
let item = hir::Item {
164174
owner_id: hir_id.expect_owner(),
@@ -210,6 +220,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
210220
body,
211221
contract,
212222
define_opaque,
223+
eii_impl,
213224
..
214225
}) => {
215226
self.with_new_scopes(*fn_sig_span, |this| {
@@ -453,23 +464,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
453464
hir::ItemKind::Macro {
454465
ast_macro_def,
455466
kind: macro_kind,
456-
eii_macro_for: eii_macro_for.as_ref().map(|path| {
457-
let lowered = self.lower_qpath(
458-
id,
459-
&None,
460-
path,
461-
ParamMode::Explicit,
462-
crate::AllowReturnTypeNotation::No,
463-
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
464-
None,
465-
);
466-
467-
let QPath::Resolved(None, path) = lowered else {
468-
panic!("{lowered:?}");
469-
};
470-
471-
path.res.def_id()
472-
}),
467+
eii_macro_for: eii_macro_for
468+
.as_ref()
469+
.map(|path| self.lower_path_simple_eii(id, path)),
473470
}
474471
}
475472
ItemKind::Delegation(box delegation) => {
@@ -487,6 +484,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
487484
}
488485
}
489486

487+
fn lower_path_simple_eii(&mut self, id: NodeId, path: &Path) -> DefId {
488+
let lowered = self.lower_qpath(
489+
id,
490+
&None,
491+
path,
492+
ParamMode::Explicit,
493+
crate::AllowReturnTypeNotation::No,
494+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
495+
None,
496+
);
497+
498+
let QPath::Resolved(None, path) = lowered else {
499+
// TODO
500+
panic!("{lowered:?}");
501+
};
502+
503+
path.res.def_id()
504+
}
505+
490506
fn lower_const_item(
491507
&mut self,
492508
ty: &Ty,
@@ -648,7 +664,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
648664
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
649665
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
650666
let owner_id = hir_id.expect_owner();
651-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
667+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &[]);
652668
let item = hir::ForeignItem {
653669
owner_id,
654670
ident: self.lower_ident(i.ident),
@@ -706,7 +722,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
706722

707723
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
708724
let hir_id = self.lower_node_id(v.id);
709-
self.lower_attrs(hir_id, &v.attrs, v.span);
725+
self.lower_attrs(hir_id, &v.attrs, v.span, &[]);
710726
hir::Variant {
711727
hir_id,
712728
def_id: self.local_def_id(v.id),
@@ -768,7 +784,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
768784
) -> hir::FieldDef<'hir> {
769785
let ty = self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
770786
let hir_id = self.lower_node_id(f.id);
771-
self.lower_attrs(hir_id, &f.attrs, f.span);
787+
self.lower_attrs(hir_id, &f.attrs, f.span, &[]);
772788
hir::FieldDef {
773789
span: self.lower_span(f.span),
774790
hir_id,
@@ -787,7 +803,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
787803

788804
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
789805
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
790-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
806+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &[]);
791807
let trait_item_def_id = hir_id.expect_owner();
792808

793809
let (generics, kind, has_default) = match &i.kind {
@@ -937,7 +953,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
937953
let has_value = true;
938954
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
939955
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
940-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
956+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &[]);
941957

942958
let (generics, kind) = match &i.kind {
943959
AssocItemKind::Const(box ConstItem { generics, ty, expr, .. }) => self.lower_generics(
@@ -1099,7 +1115,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10991115

11001116
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
11011117
let hir_id = self.lower_node_id(param.id);
1102-
self.lower_attrs(hir_id, &param.attrs, param.span);
1118+
self.lower_attrs(hir_id, &param.attrs, param.span, &[]);
11031119
hir::Param {
11041120
hir_id,
11051121
pat: self.lower_pat(&param.pat),
@@ -1799,7 +1815,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17991815
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
18001816
let hir_id = self.lower_node_id(pred.id);
18011817
let span = self.lower_span(pred.span);
1802-
self.lower_attrs(hir_id, &pred.attrs, span);
1818+
self.lower_attrs(hir_id, &pred.attrs, span, &[]);
18031819
let kind = self.arena.alloc(match &pred.kind {
18041820
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
18051821
bound_generic_params,

compiler/rustc_ast_lowering/src/lib.rs

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

885887
debug_assert_eq!(id.owner, self.current_hir_id_owner);
886888
let ret = self.arena.alloc_from_iter(lowered_attrs);
@@ -1833,7 +1835,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18331835
let (name, kind) = self.lower_generic_param_kind(param, source);
18341836

18351837
let hir_id = self.lower_node_id(param.id);
1836-
self.lower_attrs(hir_id, &param.attrs, param.span());
1838+
self.lower_attrs(hir_id, &param.attrs, param.span(), &[]);
18371839
hir::GenericParam {
18381840
hir_id,
18391841
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
@@ -93,7 +93,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9393

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

9898
hir::PatField {
9999
hir_id,

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,11 +918,22 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
918918
return; // Avoid visiting again.
919919
}
920920
ItemKind::Fn(
921-
func
922-
@ box Fn { defaultness, generics: _, sig, contract: _, body, define_opaque: _ },
921+
func @ box Fn {
922+
defaultness,
923+
generics: _,
924+
sig,
925+
contract: _,
926+
body,
927+
define_opaque: _,
928+
eii_impl,
929+
},
923930
) => {
924931
self.check_defaultness(item.span, *defaultness);
925932

933+
for (id, mi) in eii_impl {
934+
self.visit_path(&mi.path, *id);
935+
}
936+
926937
let is_intrinsic =
927938
item.attrs.iter().any(|a| a.name_or_empty() == sym::rustc_intrinsic);
928939
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
@@ -2059,6 +2059,15 @@ impl<'a> State<'a> {
20592059

20602060
fn print_meta_item(&mut self, item: &ast::MetaItem) {
20612061
self.ibox(INDENT_UNIT);
2062+
2063+
match item.unsafety {
2064+
ast::Safety::Unsafe(_) => {
2065+
self.word("unsafe");
2066+
self.popen();
2067+
}
2068+
ast::Safety::Default | ast::Safety::Safe(_) => {}
2069+
}
2070+
20622071
match &item.kind {
20632072
ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
20642073
ast::MetaItemKind::NameValue(value) => {
@@ -2074,6 +2083,12 @@ impl<'a> State<'a> {
20742083
self.pclose();
20752084
}
20762085
}
2086+
2087+
match item.unsafety {
2088+
ast::Safety::Unsafe(_) => self.pclose(),
2089+
ast::Safety::Default | ast::Safety::Safe(_) => {}
2090+
}
2091+
20772092
self.end();
20782093
}
20792094

0 commit comments

Comments
 (0)