Skip to content

Commit 85d7d9b

Browse files
committed
add a test case for generic_const_exprs in trait items
1 parent cb596e3 commit 85d7d9b

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+1-1
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.param_to(param_def_idx as usize, tcx).to_owned();
82+
let params = generics.params_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

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,12 @@ impl<'tcx> Generics {
220220
}
221221
}
222222

223-
pub fn param_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
223+
pub fn params_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
224224
if let Some(index) = param_index.checked_sub(self.parent_count) {
225225
&self.params[..index]
226226
} else {
227227
tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
228-
.param_to(param_index, tcx)
228+
.params_to(param_index, tcx)
229229
}
230230
}
231231

src/test/ui/const-generics/generic_const_exprs/issue-105257.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
trait Trait<T> {
55
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
67
}
78

89
fn main() {}

src/test/ui/const-generics/generic_const_exprs/issue-105257.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@ error: defaults for const parameters are only allowed in `struct`, `enum`, `type
44
LL | fn fnc<const N: usize = "">(&self) {}
55
| ^^^^^^^^^^^^^^^^^^^
66

7-
error: aborting due to previous error
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
814

0 commit comments

Comments
 (0)