Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Delay def collection for expression-like nodes #128844

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
_ => (),
}

self.create_def_if_needed_for(e);
let hir_id = self.lower_node_id(e.id);
self.lower_attrs(hir_id, &e.attrs);

let kind = match &e.kind {
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
ExprKind::ConstBlock(c) => {
let c = self.with_new_scopes(c.value.span, |this| {
let def_id = this.create_def(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the NodeId has already been lowered, create_def should either ICE or register it correctly.

Copy link
Member

@BoxyUwU BoxyUwU Aug 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you want to see changed here? I'm a little confused by the purpose of this comment haha
edit: oh this is on an old commit I see 😅 nevermind

this.current_def_id_parent,
c.id,
kw::Empty,
DefKind::InlineConst,
c.value.span,
);
let def_id = this.local_def_id(c.id);
hir::ConstBlock {
def_id,
hir_id: this.lower_node_id(c.id),
Expand Down Expand Up @@ -214,13 +209,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn_decl_span,
fn_arg_span,
}) => {
let closure_def = self.create_def(
self.current_def_id_parent,
e.id,
kw::Empty,
DefKind::Closure,
e.span,
);
let closure_def = self.local_def_id(e.id);
self.with_def_id_parent(closure_def, |this| match coroutine_kind {
Some(coroutine_kind) => this.lower_expr_coroutine_closure(
binder,
Expand Down Expand Up @@ -371,6 +360,34 @@ impl<'hir> LoweringContext<'_, 'hir> {
})
}

/// HACK(min_generic_const_args): we delay creation of expression defs until ast_lowering
///
/// This only creates a def for the top-level expression. If it has nested expressions that
/// need defs, those are handled by the recursion in the main lowering logic.
fn create_def_if_needed_for(&mut self, e: &Expr) {
match &e.kind {
ExprKind::ConstBlock(c) => {
self.create_def(
self.current_def_id_parent,
c.id,
kw::Empty,
DefKind::InlineConst,
c.value.span,
);
}
ExprKind::Closure(_) => {
self.create_def(
self.current_def_id_parent,
e.id,
kw::Empty,
DefKind::Closure,
e.span,
);
}
_ => {}
}
}

fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
match u {
UnOp::Deref => hir::UnOp::Deref,
Expand Down
22 changes: 11 additions & 11 deletions tests/ui/anonymous-higher-ranked-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:2:5
|
LL | f1(|_: (), _: ()| {});
| ^^^--------------^^^^
| ^^^-----------------^
| | |
| | found signature defined here
| expected due to this
Expand All @@ -23,7 +23,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:3:5
|
LL | f2(|_: (), _: ()| {});
| ^^^--------------^^^^
| ^^^-----------------^
| | |
| | found signature defined here
| expected due to this
Expand All @@ -44,7 +44,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:4:5
|
LL | f3(|_: (), _: ()| {});
| ^^^--------------^^^^
| ^^^-----------------^
| | |
| | found signature defined here
| expected due to this
Expand All @@ -65,7 +65,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:5:5
|
LL | f4(|_: (), _: ()| {});
| ^^^--------------^^^^
| ^^^-----------------^
| | |
| | found signature defined here
| expected due to this
Expand All @@ -86,7 +86,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:6:5
|
LL | f5(|_: (), _: ()| {});
| ^^^--------------^^^^
| ^^^-----------------^
| | |
| | found signature defined here
| expected due to this
Expand All @@ -107,7 +107,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:7:5
|
LL | g1(|_: (), _: ()| {});
| ^^^--------------^^^^
| ^^^-----------------^
| | |
| | found signature defined here
| expected due to this
Expand All @@ -128,7 +128,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:8:5
|
LL | g2(|_: (), _: ()| {});
| ^^^--------------^^^^
| ^^^-----------------^
| | |
| | found signature defined here
| expected due to this
Expand All @@ -149,7 +149,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:9:5
|
LL | g3(|_: (), _: ()| {});
| ^^^--------------^^^^
| ^^^-----------------^
| | |
| | found signature defined here
| expected due to this
Expand All @@ -170,7 +170,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:10:5
|
LL | g4(|_: (), _: ()| {});
| ^^^--------------^^^^
| ^^^-----------------^
| | |
| | found signature defined here
| expected due to this
Expand All @@ -191,7 +191,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:11:5
|
LL | h1(|_: (), _: (), _: (), _: ()| {});
| ^^^----------------------------^^^^
| ^^^-------------------------------^
| | |
| | found signature defined here
| expected due to this
Expand All @@ -212,7 +212,7 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:12:5
|
LL | h2(|_: (), _: (), _: (), _: ()| {});
| ^^^----------------------------^^^^
| ^^^-------------------------------^
| | |
| | found signature defined here
| expected due to this
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/basic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ note: closure defined here
--> $DIR/basic.rs:26:19
|
LL | let closure = |x| x;
| ^^^
| ^^^^^
help: provide the argument
|
LL | closure(/* x */);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/exotic-calls.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ note: closure defined here
--> $DIR/exotic-calls.rs:21:13
|
LL | let x = || {};
| ^^
| ^^^^^
help: remove the extra argument
|
LL - x(1i32);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/issue-98894.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ note: closure defined here
--> $DIR/issue-98894.rs:2:6
|
LL | (|_, ()| ())(if true {} else {return;});
| ^^^^^^^
| ^^^^^^^^^^
help: provide the argument
|
LL | (|_, ()| ())(if true {} else {return;}, ());
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/issue-98897.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ note: closure defined here
--> $DIR/issue-98897.rs:2:6
|
LL | (|_, ()| ())([return, ()]);
| ^^^^^^^
| ^^^^^^^^^^
help: provide the argument
|
LL | (|_, ()| ())([return, ()], ());
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/issue-99482.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ note: closure defined here
--> $DIR/issue-99482.rs:2:13
|
LL | let f = |_: (), f: fn()| f;
| ^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^
help: provide the argument
|
LL | let _f = f((), main);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/asm/x86_64/type-check-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | asm!("{}", in(xmm_reg) SimdNonCopy(0.0, 0.0, 0.0, 0.0));
|
= note: `SimdNonCopy` does not implement the Copy trait

error: cannot use value of type `{closure@$DIR/type-check-2.rs:44:28: 44:36}` for inline assembly
error: cannot use value of type `{closure@$DIR/type-check-2.rs:44:28: 44:38}` for inline assembly
--> $DIR/type-check-2.rs:44:28
|
LL | asm!("{}", in(reg) |x: i32| x);
Expand Down
11 changes: 7 additions & 4 deletions tests/ui/async-await/async-closures/not-clone-closure.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
error[E0277]: the trait bound `NotClonableUpvar: Clone` is not satisfied in `{async closure@$DIR/not-clone-closure.rs:29:21: 29:34}`
error[E0277]: the trait bound `NotClonableUpvar: Clone` is not satisfied in `{async closure@$DIR/not-clone-closure.rs:29:21: 31:6}`
--> $DIR/not-clone-closure.rs:32:15
|
LL | not_clone.clone();
| ^^^^^ 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`
| ^^^^^ 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`
|
note: required because it's used within this closure
--> $DIR/not-clone-closure.rs:29:21
|
LL | let not_clone = async move || {
| ^^^^^^^^^^^^^
LL | let not_clone = async move || {
| _____________________^
LL | | println!("{z:?}");
LL | | };
| |_____^
help: consider annotating `NotClonableUpvar` with `#[derive(Clone)]`
|
LL + #[derive(Clone)]
Expand Down
12 changes: 8 additions & 4 deletions tests/ui/async-await/async-closures/not-fn.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
error: async closure does not implement `FnMut` because it captures state from its environment
--> $DIR/not-fn.rs:11:14
|
LL | needs_fn(async || {
| -------- ^^^^^^^^
| |
| required by a bound introduced by this call
LL | needs_fn(async || {
| _____--------_^
| | |
| | required by a bound introduced by this call
LL | |
LL | | x += 1;
LL | | });
| |_____^
|
note: required by a bound in `needs_fn`
--> $DIR/not-fn.rs:8:28
Expand Down
30 changes: 24 additions & 6 deletions tests/ui/attributes/dump_def_parents.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
error: rustc_dump_def_parents: DefId(..)
--> $DIR/dump_def_parents.rs:8:13
|
LL | || {
| ^^
LL | / || {
LL | |
LL | | qux::<
LL | | {
... |
LL | | >();
LL | | };
| |_____________^
|
note: DefId(..)
--> $DIR/dump_def_parents.rs:6:9
Expand Down Expand Up @@ -46,8 +52,14 @@ LL | | },
note: DefId(..)
--> $DIR/dump_def_parents.rs:8:13
|
LL | || {
| ^^
LL | / || {
LL | |
LL | | qux::<
LL | | {
... |
LL | | >();
LL | | };
| |_____________^
note: DefId(..)
--> $DIR/dump_def_parents.rs:6:9
|
Expand Down Expand Up @@ -95,8 +107,14 @@ LL | | },
note: DefId(..)
--> $DIR/dump_def_parents.rs:8:13
|
LL | || {
| ^^
LL | / || {
LL | |
LL | | qux::<
LL | | {
... |
LL | | >();
LL | | };
| |_____________^
note: DefId(..)
--> $DIR/dump_def_parents.rs:6:9
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/block-result/issue-20862.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | |y| x + y
| ^^^^^^^^^ expected `()`, found closure
|
= note: expected unit type `()`
found closure `{closure@$DIR/issue-20862.rs:2:5: 2:8}`
found closure `{closure@$DIR/issue-20862.rs:2:5: 2:14}`

error[E0618]: expected function, found `()`
--> $DIR/issue-20862.rs:7:13
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation-impl-trait.rs:11:9
|
LL | fn bar() -> impl Fn() -> usize {
| --- ------------------ change this to return `FnMut` instead of `Fn`
LL | let mut x = 0;
LL | move || {
| ------- in this closure
LL | x += 1;
| ^^^^^^ cannot assign
LL | fn bar() -> impl Fn() -> usize {
| --- ------------------ change this to return `FnMut` instead of `Fn`
LL | let mut x = 0;
LL | / move || {
LL | | x += 1;
| | ^^^^^^ cannot assign
LL | | x
LL | | }
| |_____- in this closure

error: aborting due to 1 previous error

Expand Down
Loading
Loading