Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,34 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
ty::AnonConstKind::NonTypeSystem => None,
}
}
Node::ConstArg(_) => {
let parent_did = tcx.parent(def_id.to_def_id());
debug!(?parent_did);

if let Node::GenericParam(hir::GenericParam {
def_id: param_id,
kind: hir::GenericParamKind::Const { .. },
..
}) = tcx.parent_hir_node(hir_id)
{
let generics = tcx.generics_of(parent_did);
let param_def_idx = generics.param_def_id_to_index[&param_id.to_def_id()];
let own_params = generics.params_to(param_def_idx as usize, tcx).to_owned();
let param_def_id_to_index =
own_params.iter().map(|param| (param.def_id, param.index)).collect();

return ty::Generics {
parent: generics.parent,
parent_count: generics.parent_count,
own_params,
param_def_id_to_index,
has_self: generics.has_self,
has_late_bound_regions: generics.has_late_bound_regions,
};
}

Some(parent_did)
}
Node::ConstBlock(_)
| Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/const-arg-unresolved.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(min_generic_const_args)]
#![feature(generic_const_exprs)]
#![expect(incomplete_features)]

struct Both<const is_123: u32 = 3, T> { //~ ERROR generic parameters with a default must be trailing
a: A<{ B::<1>::M }>, //~ ERROR cannot find type `A` in this scope
//~| ERROR failed to resolve: use of undeclared type `B`
}

fn main() {}
27 changes: 27 additions & 0 deletions tests/ui/const-generics/const-arg-unresolved.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error: generic parameters with a default must be trailing
--> $DIR/const-arg-unresolved.rs:5:19
|
LL | struct Both<const is_123: u32 = 3, T> {
| ^^^^^^

error[E0412]: cannot find type `A` in this scope
--> $DIR/const-arg-unresolved.rs:6:8
|
LL | struct Both<const is_123: u32 = 3, T> {
| - similarly named type parameter `T` defined here
LL | a: A<{ B::<1>::M }>,
| ^ help: a type parameter with a similar name exists: `T`

error[E0433]: failed to resolve: use of undeclared type `B`
--> $DIR/const-arg-unresolved.rs:6:12
|
LL | a: A<{ B::<1>::M }>,
| ^
| |
| use of undeclared type `B`
| help: a type parameter with a similar name exists: `T`

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0412, E0433.
For more information about an error, try `rustc --explain E0412`.
Loading