Point at inner item when using outer const param

```
error[E0401]: can't use generic parameters from outer item
  --> $DIR/const-param-from-outer-fn.rs:3:9
   |
LL | fn foo<const X: u32>() {
   |              - const parameter from outer item
LL |     fn bar() -> u32 {
   |        --- generic parameter used in this inner function
LL |         X
   |         ^ use of generic parameter from outer item
   |
help: try introducing a local generic parameter here
   |
LL |     fn bar<X>() -> u32 {
   |           +++
```
This commit is contained in:
Esteban Küber 2025-11-01 19:21:27 +00:00
parent 8fdd34713d
commit 44ece2e9ce
3 changed files with 28 additions and 8 deletions

View file

@ -1484,13 +1484,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// This was an attempt to use a const parameter outside its scope.
if let Some(span) = finalize {
let item = if let Some(diag_metadata) = diag_metadata
&& let Some(current_item) = diag_metadata.current_item
{
let span = current_item
.kind
.ident()
.map(|i| i.span)
.unwrap_or(current_item.span);
Some((span, current_item.kind.descr().to_string()))
} else {
None
};
self.report_error(
span,
ResolutionError::GenericParamsFromOuterItem(
res,
has_generic_params,
def_kind,
None,
item,
),
);
}

View file

@ -4,6 +4,7 @@ error[E0401]: can't use generic parameters from outer item
LL | fn foo<const X: u32>() {
| - const parameter from outer item
LL | fn bar() -> u32 {
| --- generic parameter used in this inner function
LL | X
| ^ use of generic parameter from outer item
|

View file

@ -27,11 +27,14 @@ LL | static a: *const T = Default::default();
error[E0401]: can't use generic parameters from outer item
--> $DIR/issue-65035-static-with-parent-generics.rs:15:24
|
LL | fn h<const N: usize>() {
| - const parameter from outer item
LL | extern "C" {
LL | static a: [u8; N];
| ^ use of generic parameter from outer item
LL | fn h<const N: usize>() {
| - const parameter from outer item
LL | / extern "C" {
LL | | static a: [u8; N];
| | ^ use of generic parameter from outer item
LL | |
LL | | }
| |_____- generic parameter used in this inner extern block
|
= note: a `static` is a separate item from the item that contains it
@ -41,7 +44,9 @@ error[E0401]: can't use generic parameters from outer item
LL | fn i<const N: usize>() {
| - const parameter from outer item
LL | static a: [u8; N] = [0; N];
| ^ use of generic parameter from outer item
| - ^ use of generic parameter from outer item
| |
| generic parameter used in this inner static item
|
= note: a `static` is a separate item from the item that contains it
@ -51,7 +56,9 @@ error[E0401]: can't use generic parameters from outer item
LL | fn i<const N: usize>() {
| - const parameter from outer item
LL | static a: [u8; N] = [0; N];
| ^ use of generic parameter from outer item
| - ^ use of generic parameter from outer item
| |
| generic parameter used in this inner static item
|
= note: a `static` is a separate item from the item that contains it