Skip to content

Commit 138ce66

Browse files
committed
Create defs before lowering NodeId
This ensures the correct DefId <-> HirId mapping is made.
1 parent 73be83c commit 138ce66

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+31-14
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
7070
_ => (),
7171
}
7272

73+
self.create_def_if_needed_for(e);
7374
let hir_id = self.lower_node_id(e.id);
7475
self.lower_attrs(hir_id, &e.attrs);
7576

7677
let kind = match &e.kind {
7778
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
7879
ExprKind::ConstBlock(c) => {
7980
let c = self.with_new_scopes(c.value.span, |this| {
80-
let def_id = this.create_def(
81-
this.current_def_id_parent,
82-
c.id,
83-
kw::Empty,
84-
DefKind::InlineConst,
85-
c.value.span,
86-
);
81+
let def_id = this.local_def_id(c.id);
8782
hir::ConstBlock {
8883
def_id,
8984
hir_id: this.lower_node_id(c.id),
@@ -214,13 +209,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
214209
fn_decl_span,
215210
fn_arg_span,
216211
}) => {
217-
let closure_def = self.create_def(
218-
self.current_def_id_parent,
219-
e.id,
220-
kw::Empty,
221-
DefKind::Closure,
222-
e.span,
223-
);
212+
let closure_def = self.local_def_id(e.id);
224213
self.with_def_id_parent(closure_def, |this| match coroutine_kind {
225214
Some(coroutine_kind) => this.lower_expr_coroutine_closure(
226215
binder,
@@ -371,6 +360,34 @@ impl<'hir> LoweringContext<'_, 'hir> {
371360
})
372361
}
373362

363+
/// HACK(min_generic_const_args): we delay creation of expression defs until ast_lowering
364+
///
365+
/// This only creates a def for the top-level expression. If it has nested expressions that
366+
/// need defs, those are handled by the recursion in the main lowering logic.
367+
fn create_def_if_needed_for(&mut self, e: &Expr) {
368+
match &e.kind {
369+
ExprKind::ConstBlock(c) => {
370+
self.create_def(
371+
self.current_def_id_parent,
372+
c.id,
373+
kw::Empty,
374+
DefKind::InlineConst,
375+
c.value.span,
376+
);
377+
}
378+
ExprKind::Closure(_) => {
379+
self.create_def(
380+
self.current_def_id_parent,
381+
e.id,
382+
kw::Empty,
383+
DefKind::Closure,
384+
e.span,
385+
);
386+
}
387+
_ => {}
388+
}
389+
}
390+
374391
fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
375392
match u {
376393
UnOp::Deref => hir::UnOp::Deref,

tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ help: use parentheses to call this function
1616
LL | check(main());
1717
| ++
1818

19-
error[E0277]: `{closure@$DIR/const_param_ty_bad.rs:8:11: 8:13}` can't be used as a const parameter type
19+
error[E0277]: `{closure@$DIR/const_param_ty_bad.rs:8:11: 8:16}` can't be used as a const parameter type
2020
--> $DIR/const_param_ty_bad.rs:8:11
2121
|
2222
LL | check(|| {});
23-
| ----- ^^^^^ the trait `UnsizedConstParamTy` is not implemented for closure `{closure@$DIR/const_param_ty_bad.rs:8:11: 8:13}`
23+
| ----- ^^^^^ the trait `UnsizedConstParamTy` is not implemented for closure `{closure@$DIR/const_param_ty_bad.rs:8:11: 8:16}`
2424
| |
2525
| required by a bound introduced by this call
2626
|

tests/ui/const-generics/generic_const_exprs/unify-op-with-fn-call.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ LL + #[derive(ConstParamTy)]
1919
LL | struct Foo(u8);
2020
|
2121

22-
error[E0284]: type annotations needed: cannot normalize `foo<N>::{constant#0}`
22+
error[E0284]: type annotations needed: cannot normalize `foo<N>::{constant#1}`
2323
--> $DIR/unify-op-with-fn-call.rs:20:25
2424
|
2525
LL | fn foo<const N: Foo>(a: Evaluatable<{ N + N }>) {
26-
| ^^^^^^^^^^^^^^^^^^^^^^ cannot normalize `foo<N>::{constant#0}`
26+
| ^^^^^^^^^^^^^^^^^^^^^^ cannot normalize `foo<N>::{constant#1}`
2727

2828
error[E0741]: `Foo` must implement `ConstParamTy` to be used as the type of a const generic parameter
2929
--> $DIR/unify-op-with-fn-call.rs:20:17
@@ -49,23 +49,23 @@ LL + #[derive(ConstParamTy)]
4949
LL | struct Foo(u8);
5050
|
5151

52-
error[E0284]: type annotations needed: cannot normalize `foo2<N>::{constant#0}`
52+
error[E0284]: type annotations needed: cannot normalize `foo2<N>::{constant#1}`
5353
--> $DIR/unify-op-with-fn-call.rs:29:28
5454
|
5555
LL | fn foo2<const N: usize>(a: Evaluatable2<{ N + N }>) {
56-
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot normalize `foo2<N>::{constant#0}`
56+
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot normalize `foo2<N>::{constant#1}`
5757

58-
error[E0284]: type annotations needed: cannot normalize `foo<N>::{constant#0}`
58+
error[E0284]: type annotations needed: cannot normalize `foo<N>::{constant#1}`
5959
--> $DIR/unify-op-with-fn-call.rs:21:11
6060
|
6161
LL | bar::<{ std::ops::Add::add(N, N) }>();
62-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot normalize `foo<N>::{constant#0}`
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot normalize `foo<N>::{constant#1}`
6363

64-
error[E0284]: type annotations needed: cannot normalize `foo2<N>::{constant#0}`
64+
error[E0284]: type annotations needed: cannot normalize `foo2<N>::{constant#1}`
6565
--> $DIR/unify-op-with-fn-call.rs:30:12
6666
|
6767
LL | bar2::<{ std::ops::Add::add(N, N) }>();
68-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot normalize `foo2<N>::{constant#0}`
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot normalize `foo2<N>::{constant#1}`
6969

7070
error: aborting due to 8 previous errors
7171

tests/ui/const-generics/nested-type.full.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0015]: cannot call non-const fn `Foo::{constant#0}::Foo::<17>::value` in constants
1+
error[E0015]: cannot call non-const fn `Foo::Foo::<17>::value` in constants
22
--> $DIR/nested-type.rs:15:5
33
|
44
LL | Foo::<17>::value()

tests/ui/const-generics/nested-type.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0015]: cannot call non-const fn `Foo::{constant#0}::Foo::<17>::value` in constants
1+
error[E0015]: cannot call non-const fn `Foo::Foo::<17>::value` in constants
22
--> $DIR/nested-type.rs:15:5
33
|
44
LL | Foo::<17>::value()

0 commit comments

Comments
 (0)