Skip to content

Commit 76a8f1d

Browse files
committed
some things work
1 parent 305268a commit 76a8f1d

File tree

25 files changed

+142
-48
lines changed

25 files changed

+142
-48
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3580,6 +3580,9 @@ pub struct Fn {
35803580
pub contract: Option<P<FnContract>>,
35813581
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
35823582
pub body: Option<P<Block>>,
3583+
3584+
/// This fn implements some EII, pointed to by the `path`
3585+
pub eii_impl: ThinVec<(NodeId, MetaItem)>,
35833586
}
35843587

35853588
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -3929,7 +3932,7 @@ mod size_asserts {
39293932
static_assert_size!(Block, 32);
39303933
static_assert_size!(Expr, 72);
39313934
static_assert_size!(ExprKind, 40);
3932-
static_assert_size!(Fn, 184);
3935+
static_assert_size!(Fn, 192);
39333936
static_assert_size!(ForeignItem, 80);
39343937
static_assert_size!(ForeignItemKind, 16);
39353938
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
@@ -839,11 +839,17 @@ fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
839839
body,
840840
sig: FnSig { header, decl, span },
841841
define_opaque,
842+
eii_impl,
842843
},
843844
) => {
844845
// Visibility is visited as a part of the item.
845846
visit_defaultness(vis, defaultness);
846847
vis.visit_ident(ident);
848+
849+
for (node_id, mi) in eii_impl {
850+
vis.visit_id(node_id);
851+
vis.visit_path(&mut mi.path);
852+
}
847853
vis.visit_fn_header(header);
848854
vis.visit_generics(generics);
849855
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
@@ -955,10 +955,17 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Resu
955955
contract,
956956
body,
957957
define_opaque,
958+
eii_impl,
958959
},
959960
) => {
960961
// Visibility is visited as a part of the item.
961962
try_visit!(visitor.visit_ident(ident));
963+
964+
// Identifier and visibility are visited as a part of the item.
965+
for (node_id, mi) in eii_impl {
966+
try_visit!(visitor.visit_path(&mi.path, *node_id));
967+
}
968+
962969
try_visit!(visitor.visit_fn_header(header));
963970
try_visit!(visitor.visit_generics(generics));
964971
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)),
@@ -679,7 +679,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
679679
let guard = arm.guard.as_ref().map(|cond| self.lower_expr(cond));
680680
let hir_id = self.next_id();
681681
let span = self.lower_span(arm.span);
682-
self.lower_attrs(hir_id, &arm.attrs, arm.span);
682+
self.lower_attrs(hir_id, &arm.attrs, arm.span, &[]);
683683
let is_never_pattern = pat.is_never_pattern();
684684
// We need to lower the body even if it's unneeded for never pattern in match,
685685
// ensure that we can get HirId for DefId if need (issue #137708).
@@ -852,6 +852,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
852852
span: unstable_span,
853853
}],
854854
span,
855+
&[],
855856
);
856857
}
857858
}
@@ -1690,7 +1691,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16901691

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

@@ -2003,7 +2004,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20032004
let val_ident = Ident::with_dummy_span(sym::val);
20042005
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
20052006
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
2006-
self.lower_attrs(val_expr.hir_id, &attrs, span);
2007+
self.lower_attrs(val_expr.hir_id, &attrs, span, &[]);
20072008
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
20082009
self.arm(continue_pat, val_expr)
20092010
};
@@ -2034,7 +2035,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20342035
let ret_expr = self.checked_return(Some(from_residual_expr));
20352036
self.arena.alloc(self.expr(try_span, ret_expr))
20362037
};
2037-
self.lower_attrs(ret_expr.hir_id, &attrs, ret_expr.span);
2038+
self.lower_attrs(ret_expr.hir_id, &attrs, ret_expr.span, &[]);
20382039

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

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ 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::{self as hir, HirId, LifetimeSource, PredicateOrigin};
8+
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
9+
use rustc_hir::{self as hir, HirId, LifetimeSource, IsAnonInPath, PredicateOrigin};
910
use rustc_index::{IndexSlice, IndexVec};
1011
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1112
use rustc_span::edit_distance::find_best_match_for_name;
@@ -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),
@@ -1913,7 +1929,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19131929
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
19141930
let hir_id = self.lower_node_id(pred.id);
19151931
let span = self.lower_span(pred.span);
1916-
self.lower_attrs(hir_id, &pred.attrs, span);
1932+
self.lower_attrs(hir_id, &pred.attrs, span, &[]);
19171933
let kind = self.arena.alloc(match &pred.kind {
19181934
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
19191935
bound_generic_params,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,11 +886,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
886886
id: HirId,
887887
attrs: &[Attribute],
888888
target_span: Span,
889+
extra_hir_attributes: &[hir::Attribute],
889890
) -> &'hir [hir::Attribute] {
890891
if attrs.is_empty() {
891892
&[]
892893
} else {
893-
let lowered_attrs = self.lower_attrs_vec(attrs, self.lower_span(target_span));
894+
let mut lowered_attrs = self.lower_attrs_vec(attrs, self.lower_span(target_span));
895+
lowered_attrs.extend(extra_hir_attributes.iter().cloned());
894896

895897
debug_assert_eq!(id.owner, self.current_hir_id_owner);
896898
let ret = self.arena.alloc_from_iter(lowered_attrs);
@@ -1859,7 +1861,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18591861
let (name, kind) = self.lower_generic_param_kind(param, source);
18601862

18611863
let hir_id = self.lower_node_id(param.id);
1862-
self.lower_attrs(hir_id, &param.attrs, param.span());
1864+
self.lower_attrs(hir_id, &param.attrs, param.span(), &[]);
18631865
hir::GenericParam {
18641866
hir_id,
18651867
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,13 +948,18 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
948948
contract: _,
949949
body,
950950
define_opaque: _,
951+
eii_impl,
951952
},
952953
) => {
953954
self.visit_attrs_vis_ident(&item.attrs, &item.vis, ident);
954955
self.check_defaultness(item.span, *defaultness);
955956

957+
for (id, mi) in eii_impl {
958+
self.visit_path(&mi.path, *id);
959+
}
960+
956961
let is_intrinsic = item.attrs.iter().any(|a| a.has_name(sym::rustc_intrinsic));
957-
if body.is_none() && !is_intrinsic && !self.is_sdylib_interface {
962+
if body.is_none() && !is_intrinsic {
958963
self.dcx().emit_err(errors::FnWithoutBody {
959964
span: item.span,
960965
replace_span: self.ending_semi_or_hi(item.span),

compiler/rustc_ast_pretty/src/pprust/state.rs

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

21292129
fn print_meta_item(&mut self, item: &ast::MetaItem) {
21302130
let ib = self.ibox(INDENT_UNIT);
2131+
match item.unsafety {
2132+
ast::Safety::Unsafe(_) => {
2133+
self.word("unsafe");
2134+
self.popen();
2135+
}
2136+
ast::Safety::Default | ast::Safety::Safe(_) => {}
2137+
}
2138+
21312139
match &item.kind {
21322140
ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
21332141
ast::MetaItemKind::NameValue(value) => {
@@ -2143,6 +2151,12 @@ impl<'a> State<'a> {
21432151
self.pclose();
21442152
}
21452153
}
2154+
2155+
match item.unsafety {
2156+
ast::Safety::Unsafe(_) => self.pclose(),
2157+
ast::Safety::Default | ast::Safety::Safe(_) => {}
2158+
}
2159+
21462160
self.end(ib);
21472161
}
21482162

0 commit comments

Comments
 (0)