Skip to content

Commit 60bc83d

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

File tree

227 files changed

+2183
-1531
lines changed

Some content is hidden

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

227 files changed

+2183
-1531
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/anonymous-higher-ranked-lifetime.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0631]: type mismatch in closure arguments
22
--> $DIR/anonymous-higher-ranked-lifetime.rs:2:5
33
|
44
LL | f1(|_: (), _: ()| {});
5-
| ^^^--------------^^^^
5+
| ^^^-----------------^
66
| | |
77
| | found signature defined here
88
| expected due to this
@@ -23,7 +23,7 @@ error[E0631]: type mismatch in closure arguments
2323
--> $DIR/anonymous-higher-ranked-lifetime.rs:3:5
2424
|
2525
LL | f2(|_: (), _: ()| {});
26-
| ^^^--------------^^^^
26+
| ^^^-----------------^
2727
| | |
2828
| | found signature defined here
2929
| expected due to this
@@ -44,7 +44,7 @@ error[E0631]: type mismatch in closure arguments
4444
--> $DIR/anonymous-higher-ranked-lifetime.rs:4:5
4545
|
4646
LL | f3(|_: (), _: ()| {});
47-
| ^^^--------------^^^^
47+
| ^^^-----------------^
4848
| | |
4949
| | found signature defined here
5050
| expected due to this
@@ -65,7 +65,7 @@ error[E0631]: type mismatch in closure arguments
6565
--> $DIR/anonymous-higher-ranked-lifetime.rs:5:5
6666
|
6767
LL | f4(|_: (), _: ()| {});
68-
| ^^^--------------^^^^
68+
| ^^^-----------------^
6969
| | |
7070
| | found signature defined here
7171
| expected due to this
@@ -86,7 +86,7 @@ error[E0631]: type mismatch in closure arguments
8686
--> $DIR/anonymous-higher-ranked-lifetime.rs:6:5
8787
|
8888
LL | f5(|_: (), _: ()| {});
89-
| ^^^--------------^^^^
89+
| ^^^-----------------^
9090
| | |
9191
| | found signature defined here
9292
| expected due to this
@@ -107,7 +107,7 @@ error[E0631]: type mismatch in closure arguments
107107
--> $DIR/anonymous-higher-ranked-lifetime.rs:7:5
108108
|
109109
LL | g1(|_: (), _: ()| {});
110-
| ^^^--------------^^^^
110+
| ^^^-----------------^
111111
| | |
112112
| | found signature defined here
113113
| expected due to this
@@ -128,7 +128,7 @@ error[E0631]: type mismatch in closure arguments
128128
--> $DIR/anonymous-higher-ranked-lifetime.rs:8:5
129129
|
130130
LL | g2(|_: (), _: ()| {});
131-
| ^^^--------------^^^^
131+
| ^^^-----------------^
132132
| | |
133133
| | found signature defined here
134134
| expected due to this
@@ -149,7 +149,7 @@ error[E0631]: type mismatch in closure arguments
149149
--> $DIR/anonymous-higher-ranked-lifetime.rs:9:5
150150
|
151151
LL | g3(|_: (), _: ()| {});
152-
| ^^^--------------^^^^
152+
| ^^^-----------------^
153153
| | |
154154
| | found signature defined here
155155
| expected due to this
@@ -170,7 +170,7 @@ error[E0631]: type mismatch in closure arguments
170170
--> $DIR/anonymous-higher-ranked-lifetime.rs:10:5
171171
|
172172
LL | g4(|_: (), _: ()| {});
173-
| ^^^--------------^^^^
173+
| ^^^-----------------^
174174
| | |
175175
| | found signature defined here
176176
| expected due to this
@@ -191,7 +191,7 @@ error[E0631]: type mismatch in closure arguments
191191
--> $DIR/anonymous-higher-ranked-lifetime.rs:11:5
192192
|
193193
LL | h1(|_: (), _: (), _: (), _: ()| {});
194-
| ^^^----------------------------^^^^
194+
| ^^^-------------------------------^
195195
| | |
196196
| | found signature defined here
197197
| expected due to this
@@ -212,7 +212,7 @@ error[E0631]: type mismatch in closure arguments
212212
--> $DIR/anonymous-higher-ranked-lifetime.rs:12:5
213213
|
214214
LL | h2(|_: (), _: (), _: (), _: ()| {});
215-
| ^^^----------------------------^^^^
215+
| ^^^-------------------------------^
216216
| | |
217217
| | found signature defined here
218218
| expected due to this

tests/ui/argument-suggestions/basic.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ note: closure defined here
9292
--> $DIR/basic.rs:26:19
9393
|
9494
LL | let closure = |x| x;
95-
| ^^^
95+
| ^^^^^
9696
help: provide the argument
9797
|
9898
LL | closure(/* x */);

tests/ui/argument-suggestions/exotic-calls.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ note: closure defined here
5959
--> $DIR/exotic-calls.rs:21:13
6060
|
6161
LL | let x = || {};
62-
| ^^
62+
| ^^^^^
6363
help: remove the extra argument
6464
|
6565
LL - x(1i32);

tests/ui/argument-suggestions/issue-98894.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ note: closure defined here
88
--> $DIR/issue-98894.rs:2:6
99
|
1010
LL | (|_, ()| ())(if true {} else {return;});
11-
| ^^^^^^^
11+
| ^^^^^^^^^^
1212
help: provide the argument
1313
|
1414
LL | (|_, ()| ())(if true {} else {return;}, ());

tests/ui/argument-suggestions/issue-98897.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ note: closure defined here
88
--> $DIR/issue-98897.rs:2:6
99
|
1010
LL | (|_, ()| ())([return, ()]);
11-
| ^^^^^^^
11+
| ^^^^^^^^^^
1212
help: provide the argument
1313
|
1414
LL | (|_, ()| ())([return, ()], ());

tests/ui/argument-suggestions/issue-99482.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ note: closure defined here
88
--> $DIR/issue-99482.rs:2:13
99
|
1010
LL | let f = |_: (), f: fn()| f;
11-
| ^^^^^^^^^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^
1212
help: provide the argument
1313
|
1414
LL | let _f = f((), main);

tests/ui/asm/x86_64/type-check-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | asm!("{}", in(xmm_reg) SimdNonCopy(0.0, 0.0, 0.0, 0.0));
66
|
77
= note: `SimdNonCopy` does not implement the Copy trait
88

9-
error: cannot use value of type `{closure@$DIR/type-check-2.rs:44:28: 44:36}` for inline assembly
9+
error: cannot use value of type `{closure@$DIR/type-check-2.rs:44:28: 44:38}` for inline assembly
1010
--> $DIR/type-check-2.rs:44:28
1111
|
1212
LL | asm!("{}", in(reg) |x: i32| x);

tests/ui/async-await/async-closures/not-clone-closure.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
error[E0277]: the trait bound `NotClonableUpvar: Clone` is not satisfied in `{async closure@$DIR/not-clone-closure.rs:29:21: 29:34}`
1+
error[E0277]: the trait bound `NotClonableUpvar: Clone` is not satisfied in `{async closure@$DIR/not-clone-closure.rs:29:21: 31:6}`
22
--> $DIR/not-clone-closure.rs:32:15
33
|
44
LL | not_clone.clone();
5-
| ^^^^^ within `{async closure@$DIR/not-clone-closure.rs:29:21: 29:34}`, the trait `Clone` is not implemented for `NotClonableUpvar`, which is required by `{async closure@$DIR/not-clone-closure.rs:29:21: 29:34}: Clone`
5+
| ^^^^^ within `{async closure@$DIR/not-clone-closure.rs:29:21: 31:6}`, the trait `Clone` is not implemented for `NotClonableUpvar`, which is required by `{async closure@$DIR/not-clone-closure.rs:29:21: 31:6}: Clone`
66
|
77
note: required because it's used within this closure
88
--> $DIR/not-clone-closure.rs:29:21
99
|
10-
LL | let not_clone = async move || {
11-
| ^^^^^^^^^^^^^
10+
LL | let not_clone = async move || {
11+
| _____________________^
12+
LL | | println!("{z:?}");
13+
LL | | };
14+
| |_____^
1215
help: consider annotating `NotClonableUpvar` with `#[derive(Clone)]`
1316
|
1417
LL + #[derive(Clone)]

tests/ui/async-await/async-closures/not-fn.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
error: async closure does not implement `FnMut` because it captures state from its environment
22
--> $DIR/not-fn.rs:11:14
33
|
4-
LL | needs_fn(async || {
5-
| -------- ^^^^^^^^
6-
| |
7-
| required by a bound introduced by this call
4+
LL | needs_fn(async || {
5+
| _____--------_^
6+
| | |
7+
| | required by a bound introduced by this call
8+
LL | |
9+
LL | | x += 1;
10+
LL | | });
11+
| |_____^
812
|
913
note: required by a bound in `needs_fn`
1014
--> $DIR/not-fn.rs:8:28

tests/ui/attributes/dump_def_parents.stderr

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
error: rustc_dump_def_parents: DefId(..)
22
--> $DIR/dump_def_parents.rs:8:13
33
|
4-
LL | || {
5-
| ^^
4+
LL | / || {
5+
LL | |
6+
LL | | qux::<
7+
LL | | {
8+
... |
9+
LL | | >();
10+
LL | | };
11+
| |_____________^
612
|
713
note: DefId(..)
814
--> $DIR/dump_def_parents.rs:6:9
@@ -46,8 +52,14 @@ LL | | },
4652
note: DefId(..)
4753
--> $DIR/dump_def_parents.rs:8:13
4854
|
49-
LL | || {
50-
| ^^
55+
LL | / || {
56+
LL | |
57+
LL | | qux::<
58+
LL | | {
59+
... |
60+
LL | | >();
61+
LL | | };
62+
| |_____________^
5163
note: DefId(..)
5264
--> $DIR/dump_def_parents.rs:6:9
5365
|
@@ -95,8 +107,14 @@ LL | | },
95107
note: DefId(..)
96108
--> $DIR/dump_def_parents.rs:8:13
97109
|
98-
LL | || {
99-
| ^^
110+
LL | / || {
111+
LL | |
112+
LL | | qux::<
113+
LL | | {
114+
... |
115+
LL | | >();
116+
LL | | };
117+
| |_____________^
100118
note: DefId(..)
101119
--> $DIR/dump_def_parents.rs:6:9
102120
|

tests/ui/block-result/issue-20862.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | |y| x + y
77
| ^^^^^^^^^ expected `()`, found closure
88
|
99
= note: expected unit type `()`
10-
found closure `{closure@$DIR/issue-20862.rs:2:5: 2:8}`
10+
found closure `{closure@$DIR/issue-20862.rs:2:5: 2:14}`
1111

1212
error[E0618]: expected function, found `()`
1313
--> $DIR/issue-20862.rs:7:13

tests/ui/borrowck/borrow-immutable-upvar-mutation-impl-trait.stderr

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
22
--> $DIR/borrow-immutable-upvar-mutation-impl-trait.rs:11:9
33
|
4-
LL | fn bar() -> impl Fn() -> usize {
5-
| --- ------------------ change this to return `FnMut` instead of `Fn`
6-
LL | let mut x = 0;
7-
LL | move || {
8-
| ------- in this closure
9-
LL | x += 1;
10-
| ^^^^^^ cannot assign
4+
LL | fn bar() -> impl Fn() -> usize {
5+
| --- ------------------ change this to return `FnMut` instead of `Fn`
6+
LL | let mut x = 0;
7+
LL | / move || {
8+
LL | | x += 1;
9+
| | ^^^^^^ cannot assign
10+
LL | | x
11+
LL | | }
12+
| |_____- in this closure
1113

1214
error: aborting due to 1 previous error
1315

0 commit comments

Comments
 (0)