Skip to content

Commit 9549c3a

Browse files
committed
Add explanation
1 parent 61c1724 commit 9549c3a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

compiler/rustc_resolve/src/def_collector.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ pub(crate) fn collect_definitions(
3535
struct DefCollector<'a, 'b, 'tcx> {
3636
resolver: &'a mut Resolver<'b, 'tcx>,
3737
parent_def: LocalDefId,
38+
/// If we have an anon const that consists of a macro invocation, e.g. `Foo<{ m!() }>`,
39+
/// we need to wait until we know what the macro expands to before we create the def for
40+
/// the anon const. That's because we lower some anon consts into `hir::ConstArgKind::Path`,
41+
/// which don't have defs.
42+
///
43+
/// See [`Self::visit_anon_const`].
3844
pending_anon_const_info: Option<PendingAnonConstInfo>,
3945
impl_trait_context: ImplTraitContext,
4046
in_attr: bool,
@@ -335,7 +341,9 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
335341
// def. we'll then create a def later in ast lowering in this case. the parent of nested
336342
// items will be messed up, but that's ok because there can't be any if we're just looking
337343
// for bare idents.
344+
338345
if matches!(constant.value.maybe_unwrap_block().kind, ExprKind::MacCall(..)) {
346+
// See self.pending_anon_const_info for explanation
339347
self.pending_anon_const_info =
340348
Some(PendingAnonConstInfo { id: constant.id, span: constant.value.span });
341349
visit::walk_anon_const(self, constant)
@@ -353,8 +361,8 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
353361
return self.visit_macro_invoc(expr.id);
354362
}
355363

356-
let grandparent_def = if let Some(pending_anon) = self.pending_anon_const_info {
357-
self.pending_anon_const_info = None;
364+
let grandparent_def = if let Some(pending_anon) = self.pending_anon_const_info.take() {
365+
// See self.pending_anon_const_info for explanation
358366
if !expr.is_potential_trivial_const_arg() {
359367
self.create_def(pending_anon.id, kw::Empty, DefKind::AnonConst, pending_anon.span)
360368
} else {

0 commit comments

Comments
 (0)