Skip to content

Commit 4d72800

Browse files
committed
some things work
1 parent 50692d8 commit 4d72800

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
@@ -3500,6 +3500,9 @@ pub struct Fn {
35003500
pub contract: Option<P<FnContract>>,
35013501
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
35023502
pub body: Option<P<Block>>,
3503+
3504+
/// This fn implements some EII, pointed to by the `path`
3505+
pub eii_impl: ThinVec<(NodeId, MetaItem)>,
35033506
}
35043507

35053508
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -3798,7 +3801,7 @@ mod size_asserts {
37983801
static_assert_size!(Block, 32);
37993802
static_assert_size!(Expr, 72);
38003803
static_assert_size!(ExprKind, 40);
3801-
static_assert_size!(Fn, 176);
3804+
static_assert_size!(Fn, 184);
38023805
static_assert_size!(ForeignItem, 88);
38033806
static_assert_size!(ForeignItemKind, 16);
38043807
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
@@ -975,10 +975,16 @@ fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
975975
body,
976976
sig: FnSig { header, decl, span },
977977
define_opaque,
978+
eii_impl,
978979
},
979980
) => {
980981
// Identifier and visibility are visited as a part of the item.
981982
visit_defaultness(vis, defaultness);
983+
984+
for (node_id, mi) in eii_impl {
985+
vis.visit_id(node_id);
986+
vis.visit_path(&mut mi.path);
987+
}
982988
vis.visit_fn_header(header);
983989
vis.visit_generics(generics);
984990
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
@@ -930,9 +930,15 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Resu
930930
contract,
931931
body,
932932
define_opaque,
933+
eii_impl,
933934
},
934935
) => {
935936
// Identifier and visibility are visited as a part of the item.
937+
938+
for (node_id, mi) in eii_impl {
939+
try_visit!(visitor.visit_path(&mi.path, *node_id));
940+
}
941+
936942
try_visit!(visitor.visit_fn_header(header));
937943
try_visit!(visitor.visit_generics(generics));
938944
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, 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
}
@@ -151,7 +152,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
151152
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
152153
let vis_span = self.lower_span(i.vis.span);
153154
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
154-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
155+
156+
let mut extra_hir_attributes = Vec::new();
157+
if let ItemKind::Fn(f) = &i.kind {
158+
extra_hir_attributes.extend(f.eii_impl.iter().map(|(id, mi)| {
159+
let did = self.lower_path_simple_eii(*id, &mi.path);
160+
hir::Attribute::Parsed(AttributeKind::EiiImpl { eii_macro: did })
161+
}));
162+
}
163+
164+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &extra_hir_attributes);
155165
let kind = self.lower_item_kind(i.span, i.id, hir_id, i.ident, attrs, vis_span, &i.kind);
156166
let item = hir::Item {
157167
owner_id: hir_id.expect_owner(),
@@ -219,6 +229,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
219229
body,
220230
contract,
221231
define_opaque,
232+
eii_impl,
222233
..
223234
}) => {
224235
debug_assert_ne!(ident.name, kw::Empty);
@@ -496,23 +507,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
496507
name: ident,
497508
ast_macro_def,
498509
kind: macro_kind,
499-
eii_macro_for: eii_macro_for.as_ref().map(|path| {
500-
let lowered = self.lower_qpath(
501-
id,
502-
&None,
503-
path,
504-
ParamMode::Explicit,
505-
crate::AllowReturnTypeNotation::No,
506-
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
507-
None,
508-
);
509-
510-
let QPath::Resolved(None, path) = lowered else {
511-
panic!("{lowered:?}");
512-
};
513-
514-
path.res.def_id()
515-
}),
510+
eii_macro_for: eii_macro_for
511+
.as_ref()
512+
.map(|path| self.lower_path_simple_eii(id, path)),
516513
}
517514
}
518515
ItemKind::Delegation(box delegation) => {
@@ -533,6 +530,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
533530
}
534531
}
535532

533+
fn lower_path_simple_eii(&mut self, id: NodeId, path: &Path) -> DefId {
534+
let lowered = self.lower_qpath(
535+
id,
536+
&None,
537+
path,
538+
ParamMode::Explicit,
539+
crate::AllowReturnTypeNotation::No,
540+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
541+
None,
542+
);
543+
544+
let QPath::Resolved(None, path) = lowered else {
545+
// TODO
546+
panic!("{lowered:?}");
547+
};
548+
549+
path.res.def_id()
550+
}
551+
536552
fn lower_const_item(
537553
&mut self,
538554
ty: &Ty,
@@ -676,7 +692,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
676692
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
677693
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
678694
let owner_id = hir_id.expect_owner();
679-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
695+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &[]);
680696
let item = hir::ForeignItem {
681697
owner_id,
682698
ident: self.lower_ident(i.ident),
@@ -748,7 +764,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
748764

749765
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
750766
let hir_id = self.lower_node_id(v.id);
751-
self.lower_attrs(hir_id, &v.attrs, v.span);
767+
self.lower_attrs(hir_id, &v.attrs, v.span, &[]);
752768
hir::Variant {
753769
hir_id,
754770
def_id: self.local_def_id(v.id),
@@ -810,7 +826,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
810826
) -> hir::FieldDef<'hir> {
811827
let ty = self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
812828
let hir_id = self.lower_node_id(f.id);
813-
self.lower_attrs(hir_id, &f.attrs, f.span);
829+
self.lower_attrs(hir_id, &f.attrs, f.span, &[]);
814830
hir::FieldDef {
815831
span: self.lower_span(f.span),
816832
hir_id,
@@ -830,7 +846,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
830846
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
831847
debug_assert_ne!(i.ident.name, kw::Empty);
832848
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
833-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
849+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &[]);
834850
let trait_item_def_id = hir_id.expect_owner();
835851

836852
let (generics, kind, has_default) = match &i.kind {
@@ -997,7 +1013,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
9971013
let has_value = true;
9981014
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
9991015
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
1000-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
1016+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, &[]);
10011017

10021018
let (generics, kind) = match &i.kind {
10031019
AssocItemKind::Const(box ConstItem { generics, ty, expr, define_opaque, .. }) => self
@@ -1166,7 +1182,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11661182

11671183
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
11681184
let hir_id = self.lower_node_id(param.id);
1169-
self.lower_attrs(hir_id, &param.attrs, param.span);
1185+
self.lower_attrs(hir_id, &param.attrs, param.span, &[]);
11701186
hir::Param {
11711187
hir_id,
11721188
pat: self.lower_pat(&param.pat),
@@ -1865,7 +1881,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18651881
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
18661882
let hir_id = self.lower_node_id(pred.id);
18671883
let span = self.lower_span(pred.span);
1868-
self.lower_attrs(hir_id, &pred.attrs, span);
1884+
self.lower_attrs(hir_id, &pred.attrs, span, &[]);
18691885
let kind = self.arena.alloc(match &pred.kind {
18701886
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
18711887
bound_generic_params,

compiler/rustc_ast_lowering/src/lib.rs

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

887889
debug_assert_eq!(id.owner, self.current_hir_id_owner);
888890
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
@@ -2066,6 +2066,15 @@ impl<'a> State<'a> {
20662066

20672067
fn print_meta_item(&mut self, item: &ast::MetaItem) {
20682068
self.ibox(INDENT_UNIT);
2069+
2070+
match item.unsafety {
2071+
ast::Safety::Unsafe(_) => {
2072+
self.word("unsafe");
2073+
self.popen();
2074+
}
2075+
ast::Safety::Default | ast::Safety::Safe(_) => {}
2076+
}
2077+
20692078
match &item.kind {
20702079
ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
20712080
ast::MetaItemKind::NameValue(value) => {
@@ -2081,6 +2090,12 @@ impl<'a> State<'a> {
20812090
self.pclose();
20822091
}
20832092
}
2093+
2094+
match item.unsafety {
2095+
ast::Safety::Unsafe(_) => self.pclose(),
2096+
ast::Safety::Default | ast::Safety::Safe(_) => {}
2097+
}
2098+
20842099
self.end();
20852100
}
20862101

0 commit comments

Comments
 (0)