Skip to content

Commit

Permalink
Create defs before lowering NodeId
Browse files Browse the repository at this point in the history
This ensures the correct DefId <-> HirId mapping is made.
  • Loading branch information
camelid committed Aug 8, 2024
1 parent 73be83c commit 138ce66
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
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(
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ help: use parentheses to call this function
LL | check(main());
| ++

error[E0277]: `{closure@$DIR/const_param_ty_bad.rs:8:11: 8:13}` can't be used as a const parameter type
error[E0277]: `{closure@$DIR/const_param_ty_bad.rs:8:11: 8:16}` can't be used as a const parameter type
--> $DIR/const_param_ty_bad.rs:8:11
|
LL | check(|| {});
| ----- ^^^^^ the trait `UnsizedConstParamTy` is not implemented for closure `{closure@$DIR/const_param_ty_bad.rs:8:11: 8:13}`
| ----- ^^^^^ the trait `UnsizedConstParamTy` is not implemented for closure `{closure@$DIR/const_param_ty_bad.rs:8:11: 8:16}`
| |
| required by a bound introduced by this call
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ LL + #[derive(ConstParamTy)]
LL | struct Foo(u8);
|

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

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

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

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

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

error: aborting due to 8 previous errors

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/nested-type.full.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0015]: cannot call non-const fn `Foo::{constant#0}::Foo::<17>::value` in constants
error[E0015]: cannot call non-const fn `Foo::Foo::<17>::value` in constants
--> $DIR/nested-type.rs:15:5
|
LL | Foo::<17>::value()
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/nested-type.min.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0015]: cannot call non-const fn `Foo::{constant#0}::Foo::<17>::value` in constants
error[E0015]: cannot call non-const fn `Foo::Foo::<17>::value` in constants
--> $DIR/nested-type.rs:15:5
|
LL | Foo::<17>::value()
Expand Down

0 comments on commit 138ce66

Please sign in to comment.