Skip to content

Commit ac25a30

Browse files
committed
Implement #[defines] attribute for consts and functions.
statics still need work Implement dummy `defines` attribute that can only avoid adding anything from the signature to the list of opaques that are being defined
1 parent f5729cf commit ac25a30

File tree

625 files changed

+2626
-2089
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

625 files changed

+2626
-2089
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ pub struct Generics {
394394
pub params: ThinVec<GenericParam>,
395395
pub where_clause: WhereClause,
396396
pub span: Span,
397+
pub define_opaques: Option<ThinVec<(NodeId, Path)>>,
397398
}
398399

399400
/// A where-clause in a definition.
@@ -1413,6 +1414,7 @@ pub struct Closure {
14131414
pub fn_decl_span: Span,
14141415
/// The span of the argument block `|...|`
14151416
pub fn_arg_span: Span,
1417+
pub define_opaques: Option<ThinVec<(NodeId, Path)>>,
14161418
}
14171419

14181420
/// Limit types of a range (inclusive or exclusive).
@@ -3233,6 +3235,30 @@ impl Item {
32333235
ItemKind::Impl(i) => Some(&i.generics),
32343236
}
32353237
}
3238+
3239+
pub fn define_opaques(&mut self) -> Option<&mut Option<ThinVec<(NodeId, Path)>>> {
3240+
match &mut self.kind {
3241+
ItemKind::ExternCrate(_)
3242+
| ItemKind::Use(_)
3243+
| ItemKind::Mod(_, _)
3244+
| ItemKind::ForeignMod(_)
3245+
| ItemKind::GlobalAsm(_)
3246+
| ItemKind::MacCall(_)
3247+
| ItemKind::Delegation(_)
3248+
| ItemKind::DelegationMac(_)
3249+
| ItemKind::MacroDef(_) => None,
3250+
ItemKind::Static(s) => Some(&mut s.define_opaques),
3251+
ItemKind::Const(i) => Some(&mut i.generics.define_opaques),
3252+
ItemKind::Fn(i) => Some(&mut i.generics.define_opaques),
3253+
ItemKind::TyAlias(i) => Some(&mut i.generics.define_opaques),
3254+
ItemKind::TraitAlias(generics, _)
3255+
| ItemKind::Enum(_, generics)
3256+
| ItemKind::Struct(_, generics)
3257+
| ItemKind::Union(_, generics) => Some(&mut generics.define_opaques),
3258+
ItemKind::Trait(i) => Some(&mut i.generics.define_opaques),
3259+
ItemKind::Impl(i) => Some(&mut i.generics.define_opaques),
3260+
}
3261+
}
32363262
}
32373263

32383264
/// `extern` qualifier on a function item or function type.
@@ -3411,6 +3437,7 @@ pub struct StaticItem {
34113437
pub safety: Safety,
34123438
pub mutability: Mutability,
34133439
pub expr: Option<P<Expr>>,
3440+
pub define_opaques: Option<ThinVec<(NodeId, Path)>>,
34143441
}
34153442

34163443
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -3678,15 +3705,15 @@ mod size_asserts {
36783705
static_assert_size!(Block, 32);
36793706
static_assert_size!(Expr, 72);
36803707
static_assert_size!(ExprKind, 40);
3681-
static_assert_size!(Fn, 168);
3708+
static_assert_size!(Fn, 176);
36823709
static_assert_size!(ForeignItem, 88);
36833710
static_assert_size!(ForeignItemKind, 16);
36843711
static_assert_size!(GenericArg, 24);
36853712
static_assert_size!(GenericBound, 88);
3686-
static_assert_size!(Generics, 40);
3687-
static_assert_size!(Impl, 136);
3688-
static_assert_size!(Item, 136);
3689-
static_assert_size!(ItemKind, 64);
3713+
static_assert_size!(Generics, 48);
3714+
static_assert_size!(Impl, 144);
3715+
static_assert_size!(Item, 144);
3716+
static_assert_size!(ItemKind, 72);
36903717
static_assert_size!(LitKind, 24);
36913718
static_assert_size!(Local, 80);
36923719
static_assert_size!(MetaItemLit, 40);

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,10 +1089,15 @@ fn walk_lifetime<T: MutVisitor>(vis: &mut T, Lifetime { id, ident }: &mut Lifeti
10891089
}
10901090

10911091
fn walk_generics<T: MutVisitor>(vis: &mut T, generics: &mut Generics) {
1092-
let Generics { params, where_clause, span } = generics;
1092+
let Generics { params, where_clause, span, define_opaques } = generics;
10931093
params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
10941094
vis.visit_where_clause(where_clause);
10951095
vis.visit_span(span);
1096+
1097+
for (id, path) in define_opaques.iter_mut().flatten() {
1098+
vis.visit_id(id);
1099+
vis.visit_path(path)
1100+
}
10961101
}
10971102

10981103
fn walk_ty_alias_where_clauses<T: MutVisitor>(vis: &mut T, tawcs: &mut TyAliasWhereClauses) {
@@ -1237,9 +1242,20 @@ impl WalkItemKind for ItemKind {
12371242
match self {
12381243
ItemKind::ExternCrate(_orig_name) => {}
12391244
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
1240-
ItemKind::Static(box StaticItem { ty, safety: _, mutability: _, expr }) => {
1245+
ItemKind::Static(box StaticItem {
1246+
ty,
1247+
safety: _,
1248+
mutability: _,
1249+
expr,
1250+
define_opaques,
1251+
}) => {
12411252
vis.visit_ty(ty);
12421253
visit_opt(expr, |expr| vis.visit_expr(expr));
1254+
1255+
for (id, path) in define_opaques.iter_mut().flatten() {
1256+
vis.visit_id(id);
1257+
vis.visit_path(path)
1258+
}
12431259
}
12441260
ItemKind::Const(item) => {
12451261
visit_const_item(item, vis);
@@ -1505,7 +1521,14 @@ impl WalkItemKind for ForeignItemKind {
15051521
visitor: &mut impl MutVisitor,
15061522
) {
15071523
match self {
1508-
ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => {
1524+
ForeignItemKind::Static(box StaticItem {
1525+
ty,
1526+
mutability: _,
1527+
expr,
1528+
safety: _,
1529+
define_opaques,
1530+
}) => {
1531+
assert!(define_opaques.is_none(), "{define_opaques:#?}");
15091532
visitor.visit_ty(ty);
15101533
visit_opt(expr, |expr| visitor.visit_expr(expr));
15111534
}
@@ -1726,12 +1749,17 @@ pub fn walk_expr<T: MutVisitor>(vis: &mut T, Expr { kind, id, span, attrs, token
17261749
body,
17271750
fn_decl_span,
17281751
fn_arg_span,
1752+
define_opaques,
17291753
}) => {
17301754
visit_constness(vis, constness);
17311755
vis.visit_capture_by(capture_clause);
17321756
vis.visit_fn(FnKind::Closure(binder, coroutine_kind, fn_decl, body), *span, *id);
17331757
vis.visit_span(fn_decl_span);
17341758
vis.visit_span(fn_arg_span);
1759+
for (id, path) in define_opaques.iter_mut().flatten() {
1760+
vis.visit_id(id);
1761+
vis.visit_path(path)
1762+
}
17351763
}
17361764
ExprKind::Block(blk, label) => {
17371765
visit_opt(label, |label| vis.visit_label(label));

compiler/rustc_ast/src/visit.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,19 @@ impl WalkItemKind for ItemKind {
371371
match self {
372372
ItemKind::ExternCrate(_rename) => {}
373373
ItemKind::Use(use_tree) => try_visit!(visitor.visit_use_tree(use_tree, id, false)),
374-
ItemKind::Static(box StaticItem { ty, safety: _, mutability: _, expr }) => {
374+
ItemKind::Static(box StaticItem {
375+
ty,
376+
safety: _,
377+
mutability: _,
378+
expr,
379+
define_opaques,
380+
}) => {
375381
try_visit!(visitor.visit_ty(ty));
376382
visit_opt!(visitor, visit_expr, expr);
383+
384+
for (id, path) in define_opaques.iter().flatten() {
385+
try_visit!(visitor.visit_path(path, *id))
386+
}
377387
}
378388
ItemKind::Const(box ConstItem { defaultness: _, generics, ty, expr }) => {
379389
try_visit!(visitor.visit_generics(generics));
@@ -729,9 +739,18 @@ impl WalkItemKind for ForeignItemKind {
729739
visitor: &mut V,
730740
) -> V::Result {
731741
match self {
732-
ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => {
742+
ForeignItemKind::Static(box StaticItem {
743+
ty,
744+
mutability: _,
745+
expr,
746+
safety: _,
747+
define_opaques,
748+
}) => {
733749
try_visit!(visitor.visit_ty(ty));
734750
visit_opt!(visitor, visit_expr, expr);
751+
for (id, path) in define_opaques.iter().flatten() {
752+
try_visit!(visitor.visit_path(path, *id))
753+
}
735754
}
736755
ForeignItemKind::Fn(func) => {
737756
let kind = FnKind::Fn(FnCtxt::Foreign, ident, vis, &*func);
@@ -798,10 +817,14 @@ pub fn walk_generic_param<'a, V: Visitor<'a>>(
798817
}
799818

800819
pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) -> V::Result {
801-
let Generics { params, where_clause, span: _ } = generics;
820+
let Generics { params, where_clause, span: _, define_opaques } = generics;
802821
let WhereClause { has_where_token: _, predicates, span: _ } = where_clause;
803822
walk_list!(visitor, visit_generic_param, params);
804823
walk_list!(visitor, visit_where_predicate, predicates);
824+
825+
for (id, path) in define_opaques.iter().flatten() {
826+
try_visit!(visitor.visit_path(path, *id))
827+
}
805828
V::Result::output()
806829
}
807830

@@ -1196,13 +1219,17 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
11961219
body,
11971220
fn_decl_span: _,
11981221
fn_arg_span: _,
1222+
define_opaques,
11991223
}) => {
12001224
try_visit!(visitor.visit_capture_by(capture_clause));
12011225
try_visit!(visitor.visit_fn(
12021226
FnKind::Closure(binder, coroutine_kind, fn_decl, body),
12031227
*span,
12041228
*id
1205-
))
1229+
));
1230+
for (id, path) in define_opaques.iter().flatten() {
1231+
try_visit!(visitor.visit_path(path, *id))
1232+
}
12061233
}
12071234
ExprKind::Block(block, opt_label) => {
12081235
visit_opt!(visitor, visit_label, opt_label);

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
131131
has_where_clause_predicates: false,
132132
where_clause_span: span,
133133
span,
134+
define_opaques: None,
134135
})
135136
}
136137

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
217217
body,
218218
fn_decl_span,
219219
fn_arg_span,
220+
define_opaques,
220221
}) => match coroutine_kind {
221222
Some(coroutine_kind) => self.lower_expr_coroutine_closure(
222223
binder,
@@ -228,6 +229,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
228229
body,
229230
*fn_decl_span,
230231
*fn_arg_span,
232+
define_opaques,
231233
),
232234
None => self.lower_expr_closure(
233235
binder,
@@ -240,6 +242,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
240242
body,
241243
*fn_decl_span,
242244
*fn_arg_span,
245+
define_opaques,
243246
),
244247
},
245248
ExprKind::Gen(capture_clause, block, genblock_kind, decl_span) => {
@@ -807,6 +810,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
807810
fn_arg_span: None,
808811
kind: hir::ClosureKind::Coroutine(coroutine_kind),
809812
constness: hir::Constness::NotConst,
813+
// FIXME(type_alias_impl_trait): generators should also be able to have the attribute
814+
define_opaques: None,
810815
}))
811816
}
812817

@@ -1076,6 +1081,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10761081
body: &Expr,
10771082
fn_decl_span: Span,
10781083
fn_arg_span: Span,
1084+
define_opaques: &Option<ThinVec<(NodeId, Path)>>,
10791085
) -> hir::ExprKind<'hir> {
10801086
let closure_def_id = self.local_def_id(closure_id);
10811087
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
@@ -1105,8 +1111,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
11051111
let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
11061112
// Lower outside new scope to preserve `is_in_loop_condition`.
11071113
let fn_decl = self.lower_fn_decl(decl, closure_id, fn_decl_span, FnDeclKind::Closure, None);
1114+
let define_opaques = self.lower_define_opaques(define_opaques);
11081115

11091116
let c = self.arena.alloc(hir::Closure {
1117+
define_opaques,
11101118
def_id: closure_def_id,
11111119
binder: binder_clause,
11121120
capture_clause,
@@ -1178,6 +1186,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11781186
body: &Expr,
11791187
fn_decl_span: Span,
11801188
fn_arg_span: Span,
1189+
define_opaques: &Option<ThinVec<(NodeId, Path)>>,
11811190
) -> hir::ExprKind<'hir> {
11821191
let closure_def_id = self.local_def_id(closure_id);
11831192
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
@@ -1217,8 +1226,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
12171226
// closure argument types.
12181227
let fn_decl =
12191228
self.lower_fn_decl(&decl, closure_id, fn_decl_span, FnDeclKind::Closure, None);
1229+
let define_opaques = self.lower_define_opaques(define_opaques);
12201230

12211231
let c = self.arena.alloc(hir::Closure {
1232+
define_opaques,
12221233
def_id: closure_def_id,
12231234
binder: binder_clause,
12241235
capture_clause,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
188188

189189
self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
190190
}
191-
ItemKind::Static(box ast::StaticItem { ty: t, safety: _, mutability: m, expr: e }) => {
191+
ItemKind::Static(box ast::StaticItem {
192+
ty: t,
193+
safety: _,
194+
mutability: m,
195+
expr: e,
196+
define_opaques,
197+
}) => {
192198
let (ty, body_id) =
193199
self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
194-
hir::ItemKind::Static(ty, *m, body_id)
200+
201+
let define_opaques = self.lower_define_opaques(define_opaques);
202+
hir::ItemKind::Static(ty, *m, body_id, define_opaques)
195203
}
196204
ItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => {
197205
let (generics, (ty, body_id)) = self.lower_generics(
@@ -209,7 +217,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
209217
generics,
210218
body,
211219
contract,
212-
..
220+
defaultness: _,
213221
}) => {
214222
self.with_new_scopes(*fn_sig_span, |this| {
215223
// Note: we don't need to change the return type from `T` to
@@ -653,10 +661,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
653661
generics,
654662
)
655663
}
656-
ForeignItemKind::Static(box StaticItem { ty, mutability, expr: _, safety }) => {
664+
ForeignItemKind::Static(box StaticItem {
665+
ty,
666+
mutability,
667+
expr: _,
668+
safety,
669+
define_opaques,
670+
}) => {
657671
let ty = self
658672
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
659673
let safety = self.lower_safety(*safety, hir::Safety::Unsafe);
674+
assert!(define_opaques.is_none());
660675

661676
hir::ForeignItemKind::Static(ty, *mutability, safety)
662677
}
@@ -1646,17 +1661,40 @@ impl<'hir> LoweringContext<'_, 'hir> {
16461661
let impl_trait_bounds = std::mem::take(&mut self.impl_trait_bounds);
16471662
predicates.extend(impl_trait_bounds.into_iter());
16481663

1664+
let define_opaques = self.lower_define_opaques(&generics.define_opaques);
1665+
16491666
let lowered_generics = self.arena.alloc(hir::Generics {
16501667
params: self.arena.alloc_from_iter(params),
16511668
predicates: self.arena.alloc_from_iter(predicates),
16521669
has_where_clause_predicates,
16531670
where_clause_span,
16541671
span,
1672+
define_opaques,
16551673
});
16561674

16571675
(lowered_generics, res)
16581676
}
16591677

1678+
pub(super) fn lower_define_opaques(
1679+
&mut self,
1680+
define_opaques: &Option<ThinVec<(NodeId, Path)>>,
1681+
) -> Option<&'hir [LocalDefId]> {
1682+
define_opaques.as_ref().map(|d| {
1683+
&*self.arena.alloc_from_iter(
1684+
d.iter()
1685+
// TODO: error reporting for non-local items being mentioned and tests that go through these code paths
1686+
.map(|(id, _path)| {
1687+
self.resolver
1688+
.get_partial_res(*id)
1689+
.unwrap()
1690+
.expect_full_res()
1691+
.def_id()
1692+
.expect_local()
1693+
}),
1694+
)
1695+
})
1696+
}
1697+
16601698
pub(super) fn lower_generic_bound_predicate(
16611699
&mut self,
16621700
ident: Ident,

0 commit comments

Comments
 (0)