Skip to content

Commit

Permalink
Rollup merge of #133779 - BoxyUwU:array_const_arg_infer_hir_id, r=com…
Browse files Browse the repository at this point in the history
…piler-errors

Use correct `hir_id` for array const arg infers

Fixes #133771

`self.next_id()` results in the `DefId` for the const argument, created from the hack introduced by #133468, having no `HirId` associated with it. This then results in an ICE in metadata encoding. Fixing this then results in *another* ICE where `encode_defs` was not skipping encoding `type_of` and other queries for `DefId`s when they correspond to a `ConstArgKind::Infer` node.

This only reproduces with a library crate as metadata is not encoded for binaries, and apparently we had 0 tests for `generic_arg_infer` for array lengths in a library crate so this was not caught :<

cc #133589 `@voidc`

r? `@compiler-errors` `@lcnr`
  • Loading branch information
matthiaskrgr authored Dec 3, 2024
2 parents 6323109 + 2807ba7 commit c80286d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2011,7 +2011,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
ExprKind::Underscore => {
if self.tcx.features().generic_arg_infer() {
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span));
self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind })
self.arena
.alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
} else {
feature_err(
&self.tcx.sess,
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,10 +1389,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
// `ConstArgKind::Path`. We never actually access this `DefId`
// anywhere so we don't need to encode it for other crates.
if def_kind == DefKind::AnonConst
&& matches!(
tcx.hir_node_by_def_id(local_id),
hir::Node::ConstArg(hir::ConstArg { kind: hir::ConstArgKind::Path(..), .. })
)
&& match tcx.hir_node_by_def_id(local_id) {
hir::Node::ConstArg(hir::ConstArg { kind, .. }) => match kind {
// Skip encoding defs for these as they should not have had a `DefId` created
hir::ConstArgKind::Path(..) | hir::ConstArgKind::Infer(..) => true,
hir::ConstArgKind::Anon(..) => false,
},
_ => false,
}
{
continue;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/const-generics/generic_arg_infer/array-repeat-expr-lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ check-pass

#![feature(generic_arg_infer)]
#![crate_type = "lib"]

// Test that encoding the hallucinated `DefId` for the `_` const argument doesn't
// ICE (see #133468). This requires this to be a library crate.

pub fn foo() {
let s: [u8; 10];
s = [0; _];
}

0 comments on commit c80286d

Please sign in to comment.