Skip to content

Commit 62160cb

Browse files
authored
Rollup merge of #105410 - TaKO8Ki:fix-105257, r=BoxyUwU
Consider `parent_count` for const param defaults Fixes #105257
2 parents 43bee03 + 85d7d9b commit 62160cb

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
7878
let generics = tcx.generics_of(parent_def_id.to_def_id());
7979
let param_def_idx = generics.param_def_id_to_index[&param_id.to_def_id()];
8080
// In the above example this would be .params[..N#0]
81-
let params = generics.params[..param_def_idx as usize].to_owned();
81+
let params = generics.params_to(param_def_idx as usize, tcx).to_owned();
8282
let param_def_id_to_index =
8383
params.iter().map(|param| (param.def_id, param.index)).collect();
8484

compiler/rustc_middle/src/ty/generics.rs

+9
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ impl<'tcx> Generics {
234234
}
235235
}
236236

237+
pub fn params_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
238+
if let Some(index) = param_index.checked_sub(self.parent_count) {
239+
&self.params[..index]
240+
} else {
241+
tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
242+
.params_to(param_index, tcx)
243+
}
244+
}
245+
237246
/// Returns the `GenericParamDef` associated with this `EarlyBoundRegion`.
238247
pub fn region_param(
239248
&'tcx self,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(generic_const_exprs)]
2+
#![allow(incomplete_features)]
3+
4+
trait Trait<T> {
5+
fn fnc<const N: usize = "">(&self) {} //~ERROR defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
6+
fn foo<const N: usize = { std::mem::size_of::<T>() }>(&self) {} //~ERROR defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
7+
}
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
2+
--> $DIR/issue-105257.rs:5:12
3+
|
4+
LL | fn fnc<const N: usize = "">(&self) {}
5+
| ^^^^^^^^^^^^^^^^^^^
6+
7+
error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
8+
--> $DIR/issue-105257.rs:6:12
9+
|
10+
LL | fn foo<const N: usize = { std::mem::size_of::<T>() }>(&self) {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)