Skip to content

Commit cb596e3

Browse files
committed
consider parent_count for const param defaults
1 parent ed61c13 commit cb596e3

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

compiler/rustc_hir_analysis/src/collect/generics_of.rs

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

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ impl<'tcx> Generics {
220220
}
221221
}
222222

223+
pub fn param_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
224+
if let Some(index) = param_index.checked_sub(self.parent_count) {
225+
&self.params[..index]
226+
} else {
227+
tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
228+
.param_to(param_index, tcx)
229+
}
230+
}
231+
223232
/// Returns the `GenericParamDef` associated with this `EarlyBoundRegion`.
224233
pub fn region_param(
225234
&'tcx self,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}
7+
8+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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: aborting due to previous error
8+

0 commit comments

Comments
 (0)