Skip to content

Commit 3b7667f

Browse files
committed
Make sure only params are lowered to ConstArgKind::Path
1 parent 155e556 commit 3b7667f

File tree

1 file changed

+65
-14
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+65
-14
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,20 +1149,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11491149
ty,
11501150
);
11511151

1152-
let qpath = self.lower_qpath(
1153-
ty.id,
1154-
&None,
1155-
path,
1156-
ParamMode::Optional,
1157-
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1158-
None,
1159-
);
1160-
let const_arg = ConstArg {
1161-
hir_id: self.next_id(),
1162-
kind: ConstArgKind::Path(qpath),
1163-
is_desugared_from_effects: false,
1164-
};
1165-
return GenericArg::Const(self.arena.alloc(const_arg));
1152+
let ct =
1153+
self.lower_const_path_as_const_arg(path, res, ty.id, ty.span);
1154+
return GenericArg::Const(ct);
11661155
}
11671156
}
11681157
}
@@ -1174,6 +1163,68 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11741163
}
11751164
}
11761165

1166+
fn lower_const_path_as_const_arg(
1167+
&mut self,
1168+
path: &Path,
1169+
res: Res<NodeId>,
1170+
ty_id: NodeId,
1171+
span: Span,
1172+
) -> &'hir hir::ConstArg<'hir> {
1173+
let ct_kind = match res {
1174+
Res::Def(DefKind::ConstParam, _) => {
1175+
let qpath = self.lower_qpath(
1176+
ty_id,
1177+
&None,
1178+
path,
1179+
ParamMode::Optional,
1180+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1181+
None,
1182+
);
1183+
ConstArgKind::Path(qpath)
1184+
}
1185+
_ => {
1186+
// Construct an AnonConst where the expr is the "ty"'s path.
1187+
1188+
let parent_def_id = self.current_hir_id_owner;
1189+
let node_id = self.next_node_id();
1190+
let span = self.lower_span(span);
1191+
1192+
// Add a definition for the in-band const def.
1193+
let def_id = self.create_def(
1194+
parent_def_id.def_id,
1195+
node_id,
1196+
kw::Empty,
1197+
DefKind::AnonConst,
1198+
span,
1199+
);
1200+
1201+
let path_expr = Expr {
1202+
id: ty_id,
1203+
kind: ExprKind::Path(None, path.clone()),
1204+
span,
1205+
attrs: AttrVec::new(),
1206+
tokens: None,
1207+
};
1208+
1209+
let ct = self.with_new_scopes(span, |this| {
1210+
self.arena.alloc(hir::AnonConst {
1211+
def_id,
1212+
hir_id: this.lower_node_id(node_id),
1213+
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
1214+
span,
1215+
})
1216+
});
1217+
hir::ConstArgKind::Anon(ct)
1218+
}
1219+
};
1220+
1221+
self.arena.alloc(hir::ConstArg {
1222+
hir_id: self.next_id(),
1223+
kind: ct_kind,
1224+
is_desugared_from_effects: false,
1225+
})
1226+
}
1227+
11771228
fn lower_anon_const_as_const_arg(&mut self, anon: &AnonConst) -> &'hir hir::ConstArg<'hir> {
11781229
self.arena.alloc(self.lower_anon_const_as_const_arg_direct(anon))
11791230
}

0 commit comments

Comments
 (0)